Fork() System Call
Tags: tutorial, programming
Published: 2022-11-07 15:52:28

Please read about [processes in unix](https://www.geeksforgeeks.org/processes-in-linuxunix/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttce7qc7fwbe14p5wzo3.png)

### 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
```c
#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;
}
```
### Output

```
Before fork
After fork
After fork
```
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:

```c
#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);
}
```
### Output

```
Hello from the Parent
Hello from the Child
```
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](https://www.geeksforgeeks.org/wait-system-call-c/).

### 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:

- [GeeksforGeeks](https://www.geeksforgeeks.org/fork-system-call/)
- [Fork() System Call](https://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html)

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.