I know that it can be either of these. But I always see that the child executes first on my UNIX terminal. Also, why don't the parent and child execute in parallel. They seem to be executing serially. Is this because they share the same terminal?
-
1possible duplicate of [Can the order of execution of fork() be determined?](http://stackoverflow.com/questions/6696959/can-the-order-of-execution-of-fork-be-determined) – Lekensteyn Dec 29 '11 at 21:35
-
Child on Linux, unspecified on a general POSIX platform. – Petr Skocik Mar 25 '19 at 15:08
5 Answers
In general, nothing can be said about the relative order of their execution.
Now, let's consider your specific problem. If:
- both processes take a non-trivial amount of time to run, and
- you're saying that one runs to completion before the other makes any progress, and
- there are unused CPU cycles, and
- 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.

- 486,780
- 108
- 951
- 1,012
-
1+1. It's up to the underlying scheduler to decide how to schedule the various threads. – Vicky Dec 13 '11 at 19:13
-
2@aix: Both make progress. I am doing a simple printf in both parent and child. I never see their printfs interleaved. Its always child printf (~100 bunched together)...parent printfs (bunched together)...child printfs.. – Bruce Dec 13 '11 at 19:33
-
@Bruce: Are you sure this is not due to output buffering (are you writing to the terminal or to a file/pipe)? Also, `top` should give you some idea whether the two processes are running "in parallel". – NPE Dec 13 '11 at 19:41
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.

- 15,389
- 20
- 57
- 65
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)

- 4,725
- 1
- 22
- 49
As the other answers, you may not know it, and you should not depend on it.
But historically on Linux, parent contiune to execute, and after this the children. On a old Linux Kernel, it was implemented the contrary way: quickly executing the children, which probably will call an exec
. In this manner there will be no copy on write memory (which should be done at every write of parent). Guess what? That changes broke many tools in subtle way (and difficult to debug), so it was reverted. A lost of optimization!.
So do not relay on which process will be execute first.

- 8,519
- 2
- 24
- 32
There is not really one executing before the other. It is simply that the parent will fork()
then wait()
for the child to complete. It may even fork several times if you use a piped series of commands for instance.

- 119,121
- 33
- 254
- 329