Professional Documents
Culture Documents
Reg no – 17BCE0511
Question – In a fork system call, which order does the processor execute using pid –
Answer –
17BCE0511 Harsh Agarwal
In the above code we are using a int variable x to store the return value of fork() system call. The
return valuse of a fork system call for a child process is always 0. Hence, when we print the value of
x, for child process it is 0.
Therefore the child process is executed first and then the parent process is executed.
17BCE0511 Harsh Agarwal
In the second output, 3 child processes were run first and then all the parent processes. The rest 2
child processes in the last.
17BCE0511 Harsh Agarwal
In the third one, now there is a mix match of the parent and child processes, the ) after the print
statement is run tells that it is a child process.
17BCE0511 Harsh Agarwal
In the fourth one the child processes are executed first and then the parent process.