#include #include #include #include #include #include #include #include #include #define SIZE 20 //syncro6.c int main(int argc, char **argv) { int fd, i, val, pid,*ptr; sem_t mutex; sem_t* pmutex; ptr = mmap(0, sizeof(int)*SIZE+sizeof(sem_t*), PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_SHARED, -1, 0); pmutex=(sem_t*)(ptr+sizeof(int)*SIZE); sem_init(pmutex,1,1); pid=fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { sem_wait(pmutex); val=(*ptr)++; printf("figlio: %d\n", val); sem_post(pmutex); } exit(0); } else{ for (i = 0; i < SIZE; i++) { sem_wait(pmutex); val=(*ptr)++; usleep(1e2); printf("padre: %d\n", val); sem_post(pmutex); } } wait(); return(0); }