#include #include #include #include #include #include /* mnemonici in mode */ #include /* mnemonici in oflag */ #include #define BUF_SIZE 5 int main (void) { int fd, i, pid, size=(BUF_SIZE+2)*sizeof(float); float* buffer; sem_t* mutex2; sem_t* vuoto; sem_t* pieno; int* in; int* out; float value, elto=0; mutex2=sem_open("/mutex2", O_CREAT, S_IRUSR | S_IWUSR, 1); /* creo i semafori */ vuoto=sem_open("/vuoto", O_CREAT, S_IRUSR | S_IWUSR, BUF_SIZE); pieno=sem_open("/pieno", O_CREAT, S_IRUSR | S_IWUSR, 0); shm_unlink("mymem2"); fd = shm_open("mymem2", O_CREAT | O_EXCL |O_RDWR, S_IRUSR | S_IWUSR); /* creo la shmem */ ftruncate(fd,size); buffer = mmap(NULL ,size,PROT_READ | PROT_WRITE, MAP_SHARED ,fd, 0); in = (int*) (buffer + BUF_SIZE*sizeof(float)); out = (int*) (buffer + (BUF_SIZE+1)*sizeof(float)); *in = *out = 0; for(i=0;i<100;i++) { sem_wait(vuoto); sem_wait(mutex2); usleep(500000); /*aspetta 1/2 secondo */ value=elto++; buffer[*in] = value; /* scrivo il buffer */ *in = (*in + 1) % BUF_SIZE; sem_post(mutex2); sem_post(pieno); printf ("ho scritto %f \n", value); } shm_unlink("mymem2"); }