#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* out; float value; 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); fd = shm_open("mymem2", O_EXCL |O_RDWR, S_IRUSR | S_IWUSR); /* mi collego alla shmem */ buffer = mmap(NULL ,size,PROT_READ | PROT_WRITE, MAP_SHARED ,fd, 0); out = (int*) (buffer + (BUF_SIZE+1)*sizeof(float)); while(1) { sem_wait(pieno); sem_wait(mutex2); value = buffer[*out]; /* leggo il buffer */ *out = (*out + 1) % BUF_SIZE; sem_post(mutex2); sem_post(vuoto); printf ("\tho letto %f\n", value); } }