Jump to content

For an assignment, we are required to make two child processes. I have the code working fine, but I have a question with regards to the order in which the processes finish. Based on the code below, I would think that the first child process should finished before the second, but every time I run the code, the second child process finished before the first. The output I get is as follows (the PID is always different for each run through, but the PID which is larger always finished before the PID one below it, which doesn't make sense.

 

before: mypid is 10258

waiting for 10259 and 10260
child 10260 here, will sleep for 2 seconds
child 10259 here, will sleep for 2 seconds
child done. about to exit
child done. about to exit
Wait returned: 10260
Wait returned: 10259

 

 

#include <stdio.h>
#include <stdlib.h>
#define DELAY 2

main(){
    int  newpid1, newpid2;
    void child_code(), parent_code();

    printf("before: mypid is %d\n", getpid());

    newpid1 = fork();
    if ( newpid1 == 0 ){
        child_code(DELAY);
    }
	else{
        // create second child 
        newpid2 = fork()
        if ( newpid2 == 0 ){
            child_code(DELAY);
        }
        else{
            parent_code(newpid1, newpid2);
        }
    }
    return 0;
}

void child_code(int delay){
    printf( "child %d here, will sleep for %d seconds\n", getpid(), delay );
    sleep( delay );
    printf( "child done. about to exit\n" );
    exit(17);
}

void parent_code(int child1, int child2){
    int wait_rv1, wait_rv2;
    printf("waiting for %d and %d \n", child1, child2);
    wait_rv1 = wait(NULL);
    wait_rv2 = wait(NULL);
    printf("Wait returned: %d\n", wait_rv1);
    printf("Wait returned: %d\n", wait_rv2);
}

 

Link to comment
https://linustechtips.com/topic/569980-processes-in-c/
Share on other sites

Link to post
Share on other sites

Welcome to world of multiprocessing (similar to multithreading), where nothing ever works as expected and you can't figure out why. Also on the second run, the result was completely different. That's the reason why it's so difficult to utilize multiple cpu cores and why a lot of games don't even bother. 

 

First of all, doesn't your compiler throw any warnings about getpid, fork and sleep? You should use #include <unistd.h>, to avoid implicit function declarations. Also, you're missing a semicolon at second fork, not sure if that's a typo here on the forum or in your actual code, but with that mistake, the code should not compile. Also, it should be int main, not just main. Again, that something that the compiler is smart enough to know, but it's still a good practice to use it and avoid unnecessary headaches.

 

But I'm sorry to say that I can't reproduce your issue. Running on OS X 10.11 (El Capitan), using gcc w/ Apple LLVM version 7.3.0 (clang-703.0.29), I get this output:

 

before: mypid is 11761
waiting for 11762 and 11763
child 11762 here, will sleep for 2 seconds
child 11763 here, will sleep for 2 seconds
child done. about to exit
child done. about to exit
Wait returned: 11763
Wait returned: 11762

 

Can you elaborate a bit more on your setup (OS, hardware) and I'll try to reproduce your environment. 

 

Also, I've been doing C about 3 years (although nothing hardcore), never saw something like void child_code(), parent_code(); in main. Also, explicitly define each function in it's own line, don't group their return types. You should also define what arguments are expected in function, at least the types. So for the child_code, you should define it as void child_code(int); That is not a problem, but it's a nice coding convention to follow and could save you some headaches in the future. Don't try to write code in as few lines as possible, it won't run any faster.

Btw,  you don't have to explain how pids work, whoever understands what you're trying to do, should know how pids work.

Link to comment
https://linustechtips.com/topic/569980-processes-in-c/#findComment-7478744
Share on other sites

Link to post
Share on other sites

I have a bit more code than that but I just wanted to get across the main point of the program so I just cut stuff out and thats why I may be missing semi-colons, etc. We are given "templates" by our prof as too function prototypes, definitions, etc. so I am just following them.

 

We are using ssh to do our work on the school server using gcc, so which ever version they are using, I am not sure (the compiler gives no warnings for the implicit declarations). I have tried running the cleaned up code on OSX as a test and I do get the same output which you got (the lower PID starts before the higher PID, but the higher PID finishes before the lower PID). 

 

I am just more concerned on whether I am doing something wrong, or if this is just one of the issues like race conditions while using multi-threading.

Link to comment
https://linustechtips.com/topic/569980-processes-in-c/#findComment-7478814
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×