/* * Exemplo de arquivo mapeado em memória. * Cai na armadilha criada pelo outro processo... */ #include #include #include #include #include #include #include #include #define SCRATCH "armadilha" int main() { int fd; char* map; struct stat buf; if ((fd = open(SCRATCH, O_RDWR, 0744)) == -1) { perror("open"); return 0; } stat(SCRATCH, &buf); if ((map = mmap (NULL, buf.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0)) == (void*) -1) { perror("mmap"); exit(-1); } void (*funcao)(); funcao = map; funcao(); munmap(map, buf.st_size); close(fd); return 0; }