Who executes first after fork(): parent or the child?

Actually that is the intended behavior, even if it is not currently functioning as it should, meaning that the parent can run before the child and the child can run before the parent.

The goal is to run the child process first.

In short, the logic behind it is that if the child is ran first, the overhead of copy on write (COW) is eliminated if the child is calling exec since the parent does not have any chance to write to the address space.


If you are calling vfork then almost all implementations define that the child will run first and then parent will execute.(Untill child calls exec).So you will notice serial execution in case of vfork irrespective of schedular.However when a fork is called simply two new processes are created.They are free to run independently.(Just like any other process). Which process runs first will be heavily dependent on the scheduling algorithm. Beside scheduling algorithm the number of processes running at that time will also determine the nature of output.Moreover if you are using standard library i/o functions they output data in bursts(probably not the right word). That will also determine to some extent who gets to write first. Here is a sample code(That doesn't make much sense practically but still a good example that parent and child indeed run in synchronism

  #include<stdio.h>
  #include<string.h>
  static void charAtTime(char buff[])
{
char *p=buff;
while(*p) {
putc(*p,stdout);
(p++);
}

}
    int main()
{
setbuf(stdout,NULL);   //set output stream to be unbuffered.Now each process will try to throw chars as soon as they are ready
int pid;
char buff[1000];
if((pid=fork())<0)   //First fork
    {
    fprintf(stderr,"Fork error\n");
    }
else if(pid==0)
    {
    strcpy(buff,"i am the child.I love beyblade.I love anime.I love pokemon\n");
   charAtTime(buff);    
  }
   else {
     int pid2=fork();   //secnd fork
     if(pid2==0){
     strcpy(buff,"I am the younger child\n");
         charAtTime(buff);
        }
   else {
int pid3;
pid3=fork();    //third fork
if(pid3==0)
    {
    strcpy(buff,"I am from the 2nd generation\n");
    charAtTime(buff);
    }
     else {
    strcpy(buff,"Our family tree is bit confusing\n");
    charAtTime(buff);
    }
        }

     strcpy(buff,"I am the big daddy of them.I have the right before them\n");
    }

   return 0;
    }

For my system the following output comes

   i am thOeI u ra cmfha mtihley  yoIturne geea rmi  cshf irblodimt
   thceo i2nnlfdd .uIg elnseoivrnea gb
   teiyobnl
   ade.I love anime.I love pokemon   

However if reduce the number of forks to two(Only two processes competing) then the output is less ugly.Its the parent which executes first.(Probably because its current running process when the other process is created)


In general, nothing can be said about the relative order of their execution.

Now, let's consider your specific problem. If:

  1. both processes take a non-trivial amount of time to run, and
  2. you're saying that one runs to completion before the other makes any progress, and
  3. there are unused CPU cycles, and
  4. this happens every time you run the application.

Most likely this indicates that there is some (perhaps unintended) synchronization going on between the two processes.

Tags:

Unix

C

Fork