I have written code in C as follows
#include<stdio.h> #include<unistd.h> int main() { int id; id = fork(); if(id == 0) printf("Child Processn"); else if(id > 0) printf("Parent Processn"); else perror("Unable to forkn"); return 0; }
Output as shown in ubuntu 20 OS
Parent Process Child Process
I am expecting that child process should be written first as after fork() is called child process’s printf() will be printed on screen and after that parent’s printf() will be printed but reverse is printed actually. Please help me why child process’s printf() is not printed first as id is 0 in child process.
Advertisement
Answer
The processes may run in either order as they are independently scheduled (as said by Jonathan Leffler in the comments), and usually also run in different CPU cores. Think of them like two different browser windows that can play video/audio at the same time.
Maybe sleep(1);
the parent process after fork();
if you have any data that the child process must insert before the parent process reads it. (or any other reason you have).
In theory, it is still not guaranteed that the child process will run before the parent process even with the parent process waiting for 1 second.
Also, why would you want the child process to run before the parent process?
Isn’t it like when we call fork() control goes to child process and after child process is done executing it’s instructions, control goes back to parent process. The reason for this behaviour is like c executes its instructions one by one from top to bottom. Am I missing something?
It is a different process!!! Once the child process starts up, it has a totally different execution thread, and fork() returns back to the parent process and the child process runs on its own. And this is why when you call exit(1);
on the parent process, the child process won’t exit.
As siride suggested: you can use wait()
, use this if you want to make the parent process only run after the child process:
#include <stdio.h> #include <unistd.h> int main() { int id; id = fork(); if(id == 0) { printf("Child Processn"); } else if(id > 0) { wait(id); // waits until child process finishes doing its work printf("Parent Processn"); } else { perror("Unable to forkn"); } return 0; }