Thread: Using POSIX Semaphores to divide work in Parent and Child Processes

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    1

    Using POSIX Semaphores to divide work in Parent and Child Processes

    Hi, I'm learning about semaphores and I'm trying to implement a programme where parent and child processes each have to perform an action N number of times. Here's what I did so far:

    1. Init two shared semaphores, fill, and unfill
    Init one semaphore, mutex, that is not shared
    2. Initial values are, fill=S(1), unfill=S(0), mutex=S(1)
    3. Created a shared array, where the processes will perform actions (this 'action' is meant to be the critical section, which has to be protected)

    And I've arranged the semaphores in the following way so that
    1. Each process will wait for the other until they can do the action with the sharedArray (the outer sem_wait and sem_post), and
    2. Within each process, only one process can do the action at a time (hence the mutex)

    But I am not getting an equal share of N actions for both the Parent and the Child. Can someone please help me out?

    Code:
    result = fork();    if (result){  
            for(i = 0; i < N; i ++){
                
                sem_wait(fill);
                sem_wait(mutex);
                // do something to sharedArray;
                sem_post(mutex);
                sem_post(unfill);
    
    
            }
    
    
         } else {            //Child
            for(i = 0; i < N; i ++){
    
    
                sem_wait(unfill);
                sem_wait(mutex);
                // do something to sharedArray;
                sem_post(mutex);
                sem_post(fill);
    
    
            }
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Any chance you could post the whole program?
    I mean if we want to actually try and run the code and debug it, it would save a hell of a lot of time and guesswork (most likely wrong guesses) on our part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parent and Child Processes
    By rogeroneilyoung in forum C Programming
    Replies: 1
    Last Post: 02-25-2016, 01:47 PM
  2. Replies: 6
    Last Post: 05-01-2011, 11:06 AM
  3. Parent and child processes program variables
    By tfarmer4 in forum Linux Programming
    Replies: 1
    Last Post: 03-01-2011, 05:36 PM
  4. Trouble understanding parent and child processes
    By cardinals03 in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2009, 05:51 PM
  5. Sighandler questions with child/parent processes
    By ninjacookies in forum C Programming
    Replies: 2
    Last Post: 07-05-2005, 06:38 AM

Tags for this Thread