#include #include #include #include #include #include /* mnemonici in mode */ #include /* mnemonici in oflag */ #include #define BUF_SIZE 5 void main () { int fd, i, j, pid, size=sizeof(int); int* buffer; sem_t* mutex; sem_t* barbiere; sem_t* cliente; int* nr_clienti; mutex=sem_open("mutex", 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 memoria condivisa */ 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(mutex); *nr_clienti=*nr_clienti-1; sem_post(mutex); } }