UNIX & GNU/Linux - System calls - fork()

The fork() function is a Linux system call function.
It is used to make a new identical process from another one.

The original process becoming the parent and the new process becoming its child. The both are alive at the same time.

But when the fork() function is called, both process will terminate with the same end.

Let us see an example of the fork() function with 3 fonctions:

/*
** Made by BadproG.com
*/

#include        <stdio.h>

void    begin()
{
  puts("#############");
  puts("### BEGIN ###");
  puts("#############");
}

void    ending()
{
  puts("##############");
  puts("### ENDING ###");
  puts("##############");
}

int     main()
{
  begin();
  puts("Here the neutral code");
  fork();
  puts("Here the code after fork()");
  ending();
}

We compile:

cc main.c

We execute:

./a.out

Result:

#############
### BEGIN ###
#############
Here the neutral code
Here the code after fork()
##############
### ENDING ###
##############
Here the code after fork()
##############
### ENDING ###
##############

We can see with this example of fork() that the ending() function is called twice. Uh ? Why ?
Because the fork() function split the program in two process. And thus, we have 2 programs alive in 2 distinct process.
The main program is executed and when fork() is called, this program finishes until the end and the second one executes also the end of the program.
So the beginning is displayed once and the end twice.

Let's see another example of the fork() function with getpid() and getppid(). We will see that the fork() function returns an integer (int).
If this int is less than 0, the fork() function did not work.
If this int is equal to 0, we are in the child process.
If this int is greater than 0, we are in the parent process.
OK, let's go:

/*                                                                                    
** Made by BadproG.com                                                                
*/

#include        <stdio.h>
#include        <stdlib.h>
#include        <sys/types.h>
#include        <unistd.h>

void    begin()
{
  puts("\n#################################");
  puts("### BEGIN ###");
  puts("#############");
}

void    ending()
{
  puts("/************/");
  puts("/** ENDING **/");
  puts("/*******************************/");
}

int     main()
{
  int   c;
  char  *str;

  str = malloc(sizeof(*str)+1);
  begin();
  puts("\n1 - Before fork()\n");
  if ((c = fork()) < 0)
    {
      printf("fork() < 0 indeed fork() = %d\n", c);
    }
  else if (c > 0)
    {
      printf("fork() = %d\n", c);
      str = "We are inside the PARENT process because fork() > 0.";
      printf("getpid() = %d\n", getpid());
    }
  else
    {
      printf("fork() = %d\n", c);
      str = "We are inside the CHILD process because fork() == 0.";
      printf("getpid() = %d\n", getpid());
    }
  printf("%s\n", str);
  puts("\n2 - After fork()\n");
  ending();
}

Let's compile this code:

gcc main.c

Let's execute it:

./a.out

Result:

#################################
### BEGIN ###
#############

1 - Before fork()

fork() = 20326
getpid() = 20325
We are inside the PARENT process because fork() > 0.

2 - After fork()

/************/
/** ENDING **/
/*******************************/
fork() = 0
getpid() = 20326
We are inside the CHILD process because fork() == 0.

2 - After fork()

/************/
/** ENDING **/
/*******************************/

Now you know that a fork is not just a cover.
You are able to use it.
Well done. cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.