0

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.

docaholic
  • 613
  • 9
  • 29
  • Check whether your semaphore is initialized correctly or not.. `if (s1 = sem_open("/s1", O_CREAT, 0666, 0)) == SEM_FAILED)`.. – brokenfoot Mar 12 '14 at 07:10
  • yeah i just tried that, no errors on any of the `sem_open()` statements. – docaholic Mar 12 '14 at 07:11
  • There is nothing obvious wrong with what you have posted so maybe post a relevant snippet of the real code. – Duck Mar 12 '14 at 19:03

0 Answers0