I'm having a problem trying to implement a solution to the producer-consumer problem in linux.
I'm supposed to use a shared semaphore to synchronize across different processes.
I understand the concept of signalling alternate semaphores like this (I'm also using sem_open()
to make the semaphores shared across the two processes:
process 1
----------------------------------------------
semaphore s1 = sem_open("/s1", O_CREAT, 0666, 0);
semaphore s2 = sem_open("/s2", O_CREAT, 0666, BUFFSIZE);
sem_wait(s2);
/* do stuff */
printf("This is process 1!\n");
sem_post(s1);
process 2
----------------------------------------------
semaphore s1 = sem_open("/s1", 0);
semaphore s2 = sem_open("/s2", 0);
sem_wait(s1);
/* do stuff */
printf("This is process 2!\n");
sem_post(s2);
The problem I'm having is that both processes are deadlocked. From what I understand, process one should enter the critical section first (since the semaphore has an initial value of BUFFSIZE), then signal s1 so process 2 can proceed.
This isn't the case; both processes just sit there blankly, with no output to the screen.