Question

I had implemented the Bounded buffer(Buffer size 5) problem using three semaphores, two counting (with count MAX 5) and one binary semaphore for critical section. The producer and consumer processes were separate and were sharing the buffer.

Then I have moved on to try the same problem, this time with One Parent process that sets up the shared memory( Buffer) and two child processes which act like Producer and consumer.

I almost copied whatever Implemented in the earlier code into the new one (Producer goes into the ret ==0 and i==0 block and Consumer goes into ret ==0 and i==1 block..Here i is the count of the child processes);

However my process blocks. The pseudo implementation of the code is as follows: Please suggest if the steps are correct. I think i may be going wrong with the sharing of semaphores and their values. The shared memory anyways gets shared implicitly between the parent and both child processes.

struct shma{readindex,writeindex,buf_max,char buf[5],used_count};

main()
{
struct shma* shm;
shmid = shmget();
shm = shmat(shmid);

init_shma(0,0,5,0);
while(i++<2)
{
   ret = fork();
   if(ret > 0 )
      continue;
   if(ret ==0 )
   {
       if(i==0)
       {
         char value;
         sembuf[3]; semun u1; values[] = {5,1,0}; // Sem Numbers 1,2,3
         semid = semget(3 semaphores);
         semctl(SETALL,values);
         while(1)
         {
           getValuefromuser(&value);
           decrement(1);
           decrement(2); // Critical section
           *copy value to shared memory*
           increment(2);
           increment(3); // used count
         }
       }
       if(i==1)
       {
         char value;
         sembuf[3]; semun u1; values[] = {5,1,0}; // Sem Numbers 1,2,3
         semid = semget(3 semaphores);
         semctl(SETALL,values);
         while(1)
         {
           decrement(3); // Used Count
           decrement(2); // Critical Section
           read and print(&value); // From Shared Memory
           increment(2);
           increment(1); // free slots
         }
       }
     } 
 }//while
Cleanup Code.
 }//main

Should I get semaphore ids in both child processes..or is there something else missing.

Was it helpful?

Solution

The pseudo code implementation would be something like this. Get the semaphore ID in the child process using same key, either using ftok or hardcoded key, then obtain the current value of semaphore then perform appropriate operations.

struct shma{readindex,writeindex,buf_max,char buf[5],used_count};

main()
{
struct shma* shm;
shmid = shmget();
shm = shmat(shmid);

init_shma(0,0,5,0);
 sembuf[3]; semun u1; values[] = {5,1,0}; // Sem Numbers 1,2,3
 semid = semget(3 semaphores);
 semctl(SETALL,values);

while(i++<2)
{
   ret = fork();
   if(ret > 0 )
      continue;
   if(ret ==0 )
   {
       if(i==0)
       {
         char value;
         sembuf[3]; semun u1; values[];
         semid = semget(3 semaphores);

         while(1)
         {
           getValuefromuser(&value);
           decrement(1);
           decrement(2); // Critical section
           *copy value to shared memory*
           increment(2);
           increment(3); // used count
         }
       }
       if(i==1)
       {
         char value;
         sembuf[3]; semun u1; values[];
         while(1)
         {
           getValuefromuser(&value);
           decrement(3); // Used Count
           decrement(2); // Critical Section
           read and print(&value); // From Shared Memory
           increment(2);
           increment(1); // free slots
         }
       }
     } 
 }//while
Cleanup Code.
 }//main

OTHER TIPS

I ultimately found a problem with my understanding.

I have to create and set up a semaphore or three semaphores in the parent process and then obtain their value in the respective producer and consumer child processes then use them accordingly.

I was earlier, creating semaphores in both producer and consumer. silly me.!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top