I have these silly doubts relating to fork() system call, Shall be grateful if anyone please answer these questions.
- Does fork() system call returns an integer? If yes,then why while
executing the fork() system call,we are taking its value in pid_t?
Can't we just write int x=fork();
For eg-
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("Child Process");
}
else if(pid>0)
{
printf("Parent Process");
}
else
{
printf("Unable to create");
}
}
- Why we are executing pid=fork() instead of int x=fork()?
- The above program gives an output- Parent ProcessChild Process Why it is first executing the parent process and not the child?
I have tried this code-
#include<stdio.h>
int main()
{
int x;
x=fork();
if(x==0)
{
printf("Child Process");
}
else if(x>0)
{
printf("Parent Process");
}
else
{
printf("Unable to create");
}
}
- I have tried to collect the value of fork() in an integer variable in gcc compiler of ubuntu 15.04 and its working fine,not showing any error and giving the same result as the above program will give.
- Is it the compiler problem or is this code fine? Even I haven't given the header fies sys/types.h and unistd.h,still not showing any errors.
Can someone please give an answer to my queries?