synchronizing 2 processes

10,734

Solution 1

When you do a fork() each process gets a new address space that is private. Parent and children will not share any data. That's where inter-process communication mechanisms enter.

You might use semaphores. See this links:

semaphore equivalent for processes?.

http://www.csc.villanova.edu/~mdamian/threads/posixsem.html

You can prepare semaphores on parent, have each child wait on it after each fork, and then release them in parent. Both child processes will be released and continue execution at same time. Which ones executes first depends on OS execution scheduler,of course.

    #include <stdio.h>
    #include <semaphore.h>
    #include <unistd.h>

    int p1ready=0;
    int p2ready=0;
    int onebil = 1000000000;

    int main(){

    int pid;
    int exit_code;

    // create a semaphore for each child: value=0
    sem_t *sem1 = sem_open("test_semaphore", O_CREAT|O_EXCL);
    sem_t *sem2 = sem_open("test_semaphore", O_CREAT|O_EXCL);

    pid=fork();
    if(pid==0){
        //child1
        // wait on semaphore => blocks if value <= 0
        sem_wait(sem1);
        int count1=0;
        // do work => a function might be better
        while(count1!=onebil){
            count1++;
        }
        exit(0);
    }
    else{
        pid=fork();
        if(pid==0){
            //child2
            // wait on semaphore => blocks if value <= 0 
            sem_wait(sem2);
            // do work
            int count2=0;
            while(count2!=onebil){
                count2++;
                }
            exit(0);
        }
        else{
            //parent
            // signal semaphore1 (increment) releasing child 1
            sem_post(sem1);
            // signal semaphore2 (increment) releasing child 2
            sem_post(sem2);

            // do work

            // wait for child1/child2
            int status;
            wait(&status); // a child has exited
            // do something with status
            wait(&status); // another child has exited
            // do something with status

        }
    return 0;
    }

Solution 2

The way you are trying to synchronize your processes is not going to work as every process you are creating will have its own copy of p1ready and p2ready.

What you seem to be looking for is some kind of inter process communication. You might want to take a look at http://en.wikipedia.org/wiki/Inter-process_communication to see a list of possible options.

In a simple case like the one mentioned in your question most probably sending both of your child processes a signal from the parent process will be enough, so i suggest you best take a look at that.

Share:
10,734
turtlesoup
Author by

turtlesoup

Updated on June 04, 2022

Comments

  • turtlesoup
    turtlesoup almost 2 years

    I'm trying to make 2 processes start on a task at the same time (count a number, for example). I set 2 ready flags, one for each process, and perform a while loop to check if both flags are up. Then the 2 processes will start counting after the check is passed. Here's the not working code, which I don't know why:

    int p1ready=0;
    int p2ready=0;
    int onebil = 1000000000;
    
    int main(){
    
    int pid;
    int exit_code;
    
    pid=fork();
    if(pid==0){
    //child1
        int count1=0;
        p1ready=1;    //signal
        while(!(p1ready&p2ready))'//wait until 2 processes are both ready
    
        while(count1!=onebil){
            count1++;
        }
        exit(0);
    }
    else{
        pid=fork();
        if(pid==0){
        //child2
            int count2=0;
            p2ready=1;    //signal
            while(!(p1ready&p2ready));//wait until 2 processes are both ready
            while(count2!=onebil){
                count2++;
                }
            exit(0);
        }
        else{
        //parent
           //do stuff
        }
    return 0;
    }
    

    The problem with this code is, in child1 and child2, only their own ready flag is set to 1. They cannot see the flag of the other child being set. For example, child1 only sees p1ready=1, but p2ready is always 0. Why is this so? How can I fix this?

    Thanks in advance!