What is fork() system call and how to fork using Python

Published on 2020-10-24, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

Before going into fork, lets understand what is process. A process in computer term is a program being executed currently by a computer. Each process is unique and can be identified by its PID or process ID.

  • Note: All the examples and demo codes shown below were tried on Ubuntu 20.04 LTS and Python v3.8.5.

Visit my Github page for all the demo code snippets

What we will learn in this post?

  • What is fork 💡
  • How to call fork in python 📗
  • How to get process id or PID in Python 📙
  • How to identify parent and child process 📕
  • Examples with code snippets 💻

Introduction : What is fork system call?

Fork is one of the most important concept in Unix and Linux operating system. In short note, fork is nothing but cloning a process. That means fork will create a new process with exact copy of calling process. So when a program encounters a fork() system call, it will create another process with same copy of memory. So here comes the concept of parent and child process.

The main or first process which calls fork and creates a new process is called parent process. The new process created by fork is known as child process.

How to identify parent and child process?

Since both processes child and parent have exact same copy of memory, then comes the question how can we identify which of them is parent and which one is child. As I mentioned above, each process has unique ID known as process ID or PID which can be used to differentiate among processes.

To identify parent and child process, we need to check the return code of fork system call.

Return code of fork()

Return code of fork system call determines the parent or child process. When the parent process calls fork, fork returns PID of child process just created to parent process and 0 to child process. So basically if return code from fork call is zero, then its child process and if its a positive value, then it must be the parent process.

  • ZERO If return code is 0, then it must be the child process
  • A positive value , If return code is positive value (or child's PID), then its parent process
  • Negative , If return code is negative, then child process creation is failed and unsuccessful

post image IDJMNLLyf_iflu6v

How to fork using Python?

Python's os module provides a function fork() to create a child process. To know the PID of any process, use function getpid() from os module

import os

Now lets do some demos to understand whats going on

DEMO 1: To check process ID of any process

In below example we are just checking how getpid() can be used to get the PID of current process.

import os

def demo1():
    print('I am the only process, My PID:',os.getpid())

Here is the output:

post image 6lUSzeXhS_mklgct

DEMO 2: Create one child process using fork()

In below example we are printing process ID before and after fork() call. That means before fork, we have one process and after call we got another new process with total of 2 processes.

Lets check the output of below snippets

import os

def demo2():
    print('Before calling fork(),PID: ', os.getpid())
    print('After calling fork(), PID: ', os.getpid())


Here goes output: -

shaikh@ubuntu:~/Jupyter/fork demos$ python3
Before calling fork(),PID:  6837
After calling fork(), PID:  6837
After calling fork(), PID:  6838
shaikh@ubuntu:~/Jupyter/fork demos$

As displayed above, before fork() we only had one process with PID 6837 and after fork we have a new process with PID 6838.

Demo 3: To identify parent and child

Lets see, how we can identify parent and child programmatically. As mentioned in the last section, that if return code from fork is zero, then its child process and if its a positive value then its parent process. Lets check the same here

import os

def demo3():
    print('Before calling fork(),PID: ', os.getpid())
    rc = os.fork()
    if rc == 0:
        print('I am child, PID: ', os.getpid())
    elif rc > 0:
        print('I am parent,PID:',os.getpid())
        print('Child process creation failed!!')



shaikh@ubuntu:~/Jupyter/fork demos$ python3
Before calling fork(),PID:  7316
I am parent,PID: 7316
I am child, PID:  7317

Lets understand what happened above. Before fork we had only one process with PID 7316 and the moment it called fork(), we got another process. Each of those processes have different copy of return code rc. The parent has rc with positive value (PID of child process) and child has the rc equals to zero

Demo 4: Lets create two child process

In below example, we are calling fork() twice.

import os

def demo4():
    #No fork, only one process
    print('Before any fork,  PID:',os.getpid())
    #First fork
    print('After first fork, PID:',os.getpid())
    #Second fork
    print('After second fork,PID:',os.getpid())


Here goes the output:

shaikh@ubuntu:~/Jupyter/fork demos$ python3
Before any fork,  PID: 7471
After first fork, PID: 7471
After first fork, PID: 7472
After second fork,PID: 7471
After second fork,PID: 7473
After second fork,PID: 7472
After second fork,PID: 7474
  • Before first fork there was only one process
  • After first fork, total processes are two
  • After second call, total processes are four
If we call fork 3 times, guess the number of processes that will be created in the comment section 🙂

Demo 5: Lets do it for fun

Below example will show that after fork call both parent and child will have different copies of variable num

import os

def demo5():
    num = 0
    rc = os.fork()
    if rc > 0:
        num = os.getpid()
    print ('num: ',num)    

And guess the output 🙂

shaikh@ubuntu:~/Jupyter/fork demos$ python3
num:  7825
num:  0

So in above code, only parent process can go inside if statement because it has the positive response code which is PID of child process. Since rc for child is ZERO, it will still have the original copy of num


Hope this was fun and interesting learning 🙂. fork is very common way to create a child process in any Linux operating system. It is being used to create multiple processes and the most common use case is web server that forks a new process on each http request.

Be careful when you use fork and make sure you have exited the process successfully after completion of a task else there will be high memory and cpu usage and may create memory leak situation which is called fork bomb

All the demos are in my GitHub page. Click here to visit



Similar posts

About the Author

I am a backend system engineer working for a credit card issuer. I mostly work on C/C++ and assembler programs on IBM's TPF OS. However I love web development and keep trying my hands on Nodejs and Python when offwork

Connect with author

Related articles ...

How to auto change desktop wallpaper every minute using Python

We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.


How to get email alert when your website is down using shell and Python?

There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show how we can write and setup our own alert tool with just few lines of code


Files, Folders and Python

In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files.


How to setup alarm for CPU usage using IFTTT ?

Monitoring CPU and memory usage are one of the top todo checklist for a backend engineer. Sometimes you wont even notice when your server is down due to high CPU usage unless you login and manually check the system.