-2

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?

Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42
  • Did you read *several times* the documentation of [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html)? Did you read a good Linux or POSIX programming book? See references [here](http://stackoverflow.com/a/11681845/841108) – Basile Starynkevitch Sep 25 '15 at 08:30
  • You should use [fflush(3)](http://man7.org/linux/man-pages/man3/fflush.3.html) because I/O is buffered and you should compile with `gcc -Wall -Wextra -g` – Basile Starynkevitch Sep 25 '15 at 08:33
  • @BasileStarynkevitch-Is there a need of buffering,if I typed in the wrong code first and the right later?Because I first tried the wrong code. – Frosted Cupcake Sep 25 '15 at 08:42
  • You should read much more... I can't spend hours to explain what is written in several good books. Notice that `fork` is difficult to understand for newbies (so it is normal to spend hours and days understanding it). See also wikipage on [fork](https://en.wikipedia.org/wiki/Fork_%28system_call%29) – Basile Starynkevitch Sep 25 '15 at 08:43
  • BTW, don't suspect the compiler or the implementation first. Question your own code. – Basile Starynkevitch Sep 25 '15 at 08:46
  • I crossed the street without looking both ways and nothing bad happened, so that must be something that's safe to do. – David Schwartz Mar 18 '17 at 09:36

2 Answers2

4

Using pid_t means that the source code is portable to e.g. systems that use a 64-bit PID.

The processes execute in that order because that is how the scheduler has decided to execute them.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Here pid_t is the 64-bit unsigned int, You can find it out in header files. This basically used to make the program portable.

Why parent process first?

Ans: 1 . After forking a process child had to copy the memory layout of parent process ( copying the head, stack, initialized data, uninitialized data ), So that time parent has nothing to do, So in most of the cases, parent has to execute first.

  1. But in a few cases when child executes first, only when parent scheduling time expires.

  2. In UNIX system /proc/sys/kernel/sched_runs_first, make this value 1 to make sure that the child process runs first.

In Conclusion, this behavior is not defined and undetminstic, Better to use any syncing methods.

Sandeep_black
  • 1,352
  • 17
  • 18