DEV Community

Cover image for Fork() System Call
Ajisafe Oluwapelumi
Ajisafe Oluwapelumi

Posted on

Fork() System Call

Please read about processes in unix to understand the fork() system call.

Fork system call is used for creating a new process or processes, this new process becomes the child process.

Think of it as leaving home to pick your child from school and returning home with your child, you are the parent, embarking on the journey to pick your child is the process, picking your child is creating a new process, your child goes where ever you go and does whatever you do, the parent and the child then return home from school.

Dad Meme

Return Value

  • If fork() returns a negative value (-1), the creation of a child process was unsuccessful. Using the analogy above, if you return home without your child, it means you did not pick up your child from school.

  • if fork() returns zero (0), the creation of a child process was successful however, zero (0) is returned to the newly created child process. Basically, we give your child zero (0) to acknowledge that he was picked from school.

  • After returning a zero (0) value to the child process, the ID of the child process (a non-zero value) is returned to the parent. Going by our parent-child analogy, we get to give you the identification card of your child.

Syntax

  • <unistd.h> must be included inorder to call fork()

  • fork() takes no parameters and returns an integer value.

Example

#include <stdio.h>
#include <unistd.h>

/**
 * main - fork example 1
 *
 * Return: Always 0.
 */

int main(void) 
{
    printf("Before fork\n");

    fork();
    printf("After fork\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

Before fork
After fork
After fork
Enter fullscreen mode Exit fullscreen mode

In the above code, a child process is created when fork() is called and both the child and parent process execute the rest of the program. Let's see another example:

#include <stdio.h>
#include <unistd.h>

/**
 * main - fork example 2
 *
 * Return: Always 0.
 */

int main(void)
{
    pid_t pid; /* pid_t is an integer form so pid returns an integer */

    pid = fork();
    if (pid == -1)
    {
        return (1);
    }
    if (pid == 0)
    {
        /* child process is created */
        printf("Hello from the Child\n");
    }
    else
    {
        /* parent process executes */
        printf("Hello from the Parent\n");
    }
    return (0);
}
Enter fullscreen mode Exit fullscreen mode

Output

Hello from the Parent
Hello from the Child
Enter fullscreen mode Exit fullscreen mode

In the above code, a child process is created and two (2) values are returned:

  1. zero (0) value to the child process which causes Hello from the Child to be printed.
  2. non-zero value (process ID) to the parent process which causes Hello from the Parent to be printed.

Note: The parent is printed first because the parent process and child process are running concurrently so the operating system gives first control to the parent process over the child process, it could be different on other OS. If the child must be printed first, the wait() system call is introduced. This is beyond the scope of this article but you could read about the wait() system call.

Conclusion

The fork() system call is combined with other system calls to carry out commands and execute programs concurrently however this article is to introduce you to the fork() system call and how it works. Read more on the fork() system call here:

I would love to know your thoughts on the fork() system call. Please leave a comment in the comment box below and remember to hit the follow button.

Top comments (0)