Open In App

Code for Fork System Call in OS

Last Updated : 13 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Fork is a system call in the Operating System used to create a copy of the process, the copied process created is known as the child process. The main process or the process from which the child process is created is known as the parent process. The parent and child processes are in separate address spaces in fork(). A Child process will have its Process ID different from the parent's ID.

Primary Terminologies related to Fork System Call in OS

Process: The task or job performed by the computer. Example: running a program.

  • System call: A system call is a way to shift between user mode and kernel node to access the Functionalities of Operating Systems.
  • Child process: The child process is the new process that is created by the parent process. It is essentially a copy of the parent process, and A system inherits many of its attributes, such as code, data, and open files.
  • Parent process: The parent process is the original process that initiates the creation of a new process. It serves as the starting point for the new process
  • Process ID (PID): Process ID is a special identification that OS assigns to every process. It returns an integer value.

We use fork() one time in a parent (P) then a child process is created (C1) and there is also a parent process (P) present. Similarly, if we write fork again, here C1 becomes the parent and creates a new child process C2 and there is a parent process (C1) available. P also creates a child process (C3) and P is also present (P).

fork-system-call-in-os-1
Example 1: Fork system call in OS



Fork-system-call-in-OS-2drawio
Example 2: Fork system call in OS


By observing, we can conclude:

  • Total no. of child process generated will be: 2^n - 1.
  • Total no of processes executed: 2^n.

Here n is the number of times forks called.

Remember that child processes return 0, while parent processes return a positive value (probably +1) at the fork call.

Examples of Fork System call in OS

1. Simple Fork:

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

int main() {
    pid_t child_pid;

    // Create a child process
    child_pid = fork();

    if (child_pid == 0) {
        printf("Child process\n");
    } else if (child_pid > 0) {
        printf("Parent process\n");
    } else {
        perror("Fork failed");
    }

    return 0;
}

Output:

Screenshot-2023-09-28-065829
Fork example in linux OS

2. Parent and Child Processes Printing with PIDs:

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

int main() {
    pid_t child_pid;

    // Create a child process
    child_pid = fork();

    if (child_pid == 0) {
        printf("Child process: PID=%d\n", getpid());
    } else if (child_pid > 0) {
        printf("Parent process: PID=%d, Child PID=%d\n", getpid(), child_pid);
    } else {
        perror("Fork failed");
    }

    return 0;
}

Output:

Screenshot-2023-09-28-071314
Fork example in linux OS

3. Forking Multiple Child Processes:

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

int main() {
    int i;

    for (i = 0; i < 3; i++) {
        pid_t child_pid = fork();

        if (child_pid == 0) {
            printf("Child process %d: PID=%d\n", i + 1, getpid());
            break;  // Each child process should exit the loop
        } else if (child_pid < 0) {
            perror("Fork failed");
        }
    }

    if (getpid() != 1) {
        // This code is executed only by the parent process
        printf("Parent process: PID=%d\n", getpid());
    }

    return 0;
}

Output:

fork3
Fork example in linux OS

Conclusion

fork() system call is the fundamental tool that allows processes to duplicate themselves. It makes a copy of process in a separate memory space, it provides both child and parents a different process ID (PIDs) for identification. The uses of fork are multitasking, parallel processing and complex process structure.


Next Article

Similar Reads