Python fork() – How to create child processes using the fork() method?

Fork() System Call In Python

In this article, we’ll talk about the Python fork() method which allows us to create child processes using the processes through which it was called. Let’s look

What is a system call?

A system call is used to get access to the kernel mode. It is a way with which a program can interact with the operating system and request its services to perform specified tasks.

Hence all the system calls are executed in the kernel (privileged) mode when the execution completes the control is returned to the user mode. Sometimes a system call is also called syscall in abbreviated form.

In modern computer systems, there are generally two modes of operations – User mode and Kernel mode.

All the user programs and processes run in User mode and they cannot access the kernel-mode directly. This is done to protect the operating system from the modifications or changes caused by any user program.

If any user program needs to perform privileged work, it requires the support of the operating system which it can get only through the system calls. And it the system calls which provide the various services of the operating system to the user programs and processes via API (Application Program Interface).

What is Python fork()?

There are multiple system calls for managing different types of services provided by the operating system. Also, these are different for different operating systems.

System calls are broadly classified into five main categories:

  1. Files related system calls
  2. Device related system calls
  3. Process related system calls
  4. Information related system calls
  5. Communication-related system calls

So, Python fork() is an example of Process related or Process control system call. It is a method used to create a Child process of the process which calls it just like the clone of the original (calling) process. The process which makes a system call using the fork() statement is called the Parent process.

Importing the OS module

In order to use and implement the fork() system call in Python, we require the os module of Python. This os module in Python allows us to use the various operating system-dependent functionality. It allows the user program to access the operating system functionality on which the Python is running.

We need not install the os module as it comes under Python’s standard utility modules and gets installed when Python is installed in our system.

It is imported inside the program as follows:

import os

Working of Python fork()

Following are the key properties of fork() method/statement:

  • It takes no argument/parameter when it is being called inside any program.
  • If the child process is created successfully then both the parent process and the child process will execute the next statement/instruction followed by the fork() statement.
  • The number of child processes = 2N – 1; where N = Number of fork() statements used inside the main program.

On execution of the Python fork() statement it returns three types of integer values:

  1. Zero (0) is returned to the child process if the child process is created successfully.
  2. A positive (+ve) value is returned to the parent process if the child process is created successfully. This positive value is usually the PID that is the **process ID of the newly created child process.
  3. A negative (-ve) value is returned to the parent process if some error occurs in the creation of the child process due to any reason.

Process ID usually referred to as PID is a unique identifier associated with every process present inside the computer system. The process ID of any process can be accessed using another system call getpid() which is an example of Information related system calls. The getpid() statement returns the process ID of the process which calls it.

It is called inside the program as follows:

os.fork()

Creating child processes with the fork() method in Python

Example 1:

# Importing os module
import os

# Creating child processes using fork() method
os.fork()
os.fork()

# This will be executed by both parent & child processes
print("AskPython")

Output:

Python fork()

The above python program has produced the correct output as there are two fork() statements used inside the program. Hence the print() statement executed four times (3 + 1) three (22 -1 = 3) times by the three child processes and one time by the parent process.

Example 2:

# Importing os module
import os

# Creating a child process using fork() method
val = os.fork()

# Testing the values returned by fork() method
if val == 0:
    pid = os.getpid()
    print(f"Hi I am Child Process and my PID is {pid}.")
elif val > 0:
    pid = os.getpid()
    print(f"Hi I am Parent Process and my PID is {pid} and PID {val} is my Child Process.")
else:
    print("Sorry!! Child Process creation has failed...")

Output:

Python fork()

The above python program has produced the correct output and clearly demonstrated the values returned by the fork() statement.

Conclusion

In this tutorial, we have learned what is a system call, what a Python fork() method is, its properties, and how to create child processes using the fork() method in Python.

Note: fork() method/system call is available only for Linux/Unix operating systems. If you try to run any Python program that calls the fork() method on a Windows system then the following error will be produced:

Fork Windows Error