In my code always the child gets scheduled first after fork

I have been taught by my professor that its unpredictable that which process gets scheduled first after fork.
However, my professor gave an assignment to fork and observe that each time always child gets scheduled firsta and asked us why is it so?
To my surprise, child always gets executed…i dont know why:
(i code on intel core2 duo, linux OS(suse))

and one more issue…

i learnt that child process is the exact copy of its parent code.
so if
int main()
{
{
/this part must be common for both parent n child process/

printf("before fork
");
while(some condition)
{do some work}
printf(“work result is %d”,something);

/this part must be common for both parent n child process/
}
/after this call fork/
ret=fork();
if(ret==0)
{
/*
child process
/
}
if(ret>0)
{
/

parent process after fork
*/
}

my doubt is as child process is the exact copy of its parent it must do the work before entering the (ret==0) block code.
but it doesnt work like dat in my code? wats wrong?

I think your professor is hinting that you should read the kernel code instead of asking us to do your homework for you. :stuck_out_tongue:

hey ken,
am a beginner, never read source code
for me kernel code is monster…dont know where it starts n ends…
so can u give me a hint which part of kernel do i read…
i guess its scheduler algos?
am i correct

There’s always a first time. You could do worse than google for “Linux fork process scheduling” or something like that. :wink:

thanx ken
i got pdf called Process scheduling in linux…am reading its i hope all doubts get cleared…
thanx again:)

Hey harrynath123, your question was not very clear and the responders did not understand what you really asked. I am rephrasing your question as follows:

int main() {
    printf("Test started..
");
    if(fork()) {
        printf("Inside the parent block
");
    } else {
        printf("Inside the child block
");
    }
}

Output of the above code can either be:

Test started..
Inside the child block
Inside the parent block

or

Test started..
Inside the parent block
Inside the child block

There is no guarantee to this.

Your doubt is that why are we not getting “Test started…” message from the child process?
Right?
And the answer is …

Also, study the following code:

int main() {
    printf("Test started..
");
    int ret = fork();
    printf("Both parent and child will print this
");
    if(ret) {
        printf("Only parent will print this
");
    } else {
        printf("Only child will print this
");
    }
}

A C programming course for the forum lol!

hi syampillai,
i got my second doubt clear, i.e why the child doesnt execute statements before fork-reason fork call makes the exact copy of the calling process not the calling program. and the state of the parent process is also copied so that register holding next instruction will be the same for both parent n child therefore child executes from the point after fork call returns. this is wat i learnt if any mistake plz correct me.

but surprisingly…in all my codes child always executes first.

Code:

int main() {
printf("Test started…
");
if(fork()) {
printf("Inside the parent block
");
} else {
printf("Inside the child block
");
}
}

Output of the above code in my system is always
Code:

Test started…
Inside the child block
Inside the parent block

Test started…
Inside the child block
Inside the parent block

I wonder why this happens with my system.

OK, you understood fork() now :slight_smile:

Now, try to answer the question: “Why is that your machine always running the child of the fork first?”

See if you can answer these too: How many CPUs does your machine have? How does the kernel schedule processes in a SMP environment? Also, try out your program in a multi-core CPU machine (Your university lab may have one if you don’t have it).

thank u:)
now i got the hint i exactly need…
i work on intel core2 duo on my lapy, n i suspect this is the reason…multiprocessor environment.
now i think i have to refer kernel scheduler code…
before that i will experiment by recompiling kernel for
uniprocessor n observe any difference.

This thread should be exposed as an example of how to deal with students asking questions !! Nice to see a question not being answered directly. Instead you made the poster discover the answer him- or herself !! Poster will never forget this !!!