Documente Academic
Documente Profesional
Documente Cultură
Ejemplo 1
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main (int argc, char *argv[])
{
int fd, i;
struct stat st;
char *addr;
/* Control de argumentos */
if (argc != 2)
{
fprintf(stderr, "Uso: %s nombre_de_archivo\n", argv[0]);
exit(1);
}
/* Obtencin del tamao del archivo */
if (stat(argv[1], &st) == -1)
{
perror("stat");
exit(2);
}
/* Apertura del archivo */
fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
perror("open");
exit(3);
}
/* Proyeccin del archivo en memoria */
addr = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0);
if (addr == NULL)
{
perror("mmap");
(void) close(fd);
exit(4);
}
/* Cierre del archivo */
close(fd);
/* Bucle de visualizacin del contenido del archivo */
for (i = 0; (i < st.st_size); i++)
putchar(addr[i]);
/* Liberacin del archivo (de la proyeccin en memoria del archivo) */
if (munmap(addr, st.st_size) == -1)
{
perror("munmap");
(void) close(fd);
exit(5);
}
exit(0);
Ingeniera en Telecomunicaciones
}
COMPILANDO EL ARCHIVO MIAN.CPP
Ingeniera en Telecomunicaciones
CREANDO EL ARCVHIVO VI
Ingeniera en Telecomunicaciones
ejemplo 2
Ingeniera en Telecomunicaciones
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
close(fd);
exit(5);
}
exit(0);
}
COMPILAR EL ARCHIVO
Ingeniera en Telecomunicaciones
CREANDO EL ARCHIVO VI
Ingeniera en Telecomunicaciones
Ingeniera en Telecomunicaciones