#include #include #include #include #include #include /* mnemonici in mode */ #include /* mnemonici in oflag */ #include void main () { int fd, i, j, pid, size=sizeof(int); int* buffer; sem_t* mutex1; sem_t* barbiere; sem_t* cliente; int* nr_clienti; mutex1=sem_open("mutex1", O_CREAT, S_IRUSR | S_IWUSR, 1); /* creo i semafori */ barbiere=sem_open("barb", O_CREAT, S_IRUSR | S_IWUSR, 0); cliente=sem_open("clien", O_CREAT, S_IRUSR | S_IWUSR, 0); fd = shm_open("mymem1", O_CREAT | O_EXCL |O_RDWR, S_IRUSR | S_IWUSR); /*creo la SHM */ ftruncate(fd,size); buffer = mmap(NULL ,size,PROT_READ | PROT_WRITE, MAP_SHARED ,fd, 0); nr_clienti=buffer; //*nr_clienti=0; while(1){ sem_wait(cliente); sem_post(barbiere); sleep(1); printf("barbiere: ho servito il cliente %d\n",*nr_clienti); sem_wait(mutex1); *nr_clienti=*nr_clienti-1; sem_post(mutex1); } }