shaikhu-blog-v1626930827/image_flqfjy.jpg

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 https://github.com/jaqsparow/fork-demos

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

python
1import os 2os.fork() 3

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.

python
1import os 2 3def demo1(): 4 print('I am the only process, My PID:',os.getpid()) 5demo1() 6

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

python
1import os 2 3def demo2(): 4 print('Before calling fork(),PID: ', os.getpid()) 5 os.fork() 6 print('After calling fork(), PID: ', os.getpid()) 7 8demo2() 9

Here goes output: -

python
1shaikh@ubuntu:~/Jupyter/fork demos$ python3 demo2.py 2Before calling fork(),PID: 6837 3After calling fork(), PID: 6837 4After calling fork(), PID: 6838 5shaikh@ubuntu:~/Jupyter/fork demos$ 6

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

python
1import os 2 3def demo3(): 4 print('Before calling fork(),PID: ', os.getpid()) 5 rc = os.fork() 6 if rc == 0: 7 print('I am child, PID: ', os.getpid()) 8 os._exit(0) 9 elif rc > 0: 10 print('I am parent,PID:',os.getpid()) 11 else: 12 print('Child process creation failed!!') 13 14demo3() 15

Output

python
1shaikh@ubuntu:~/Jupyter/fork demos$ python3 demo3.py 2Before calling fork(),PID: 7316 3I am parent,PID: 7316 4I am child, PID: 7317 5

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.

python
1import os 2 3def demo4(): 4 #No fork, only one process 5 print('Before any fork, PID:',os.getpid()) 6 #First fork 7 os.fork() 8 print('After first fork, PID:',os.getpid()) 9 #Second fork 10 os.fork() 11 print('After second fork,PID:',os.getpid()) 12 13demo4() 14

Here goes the output:

python
1shaikh@ubuntu:~/Jupyter/fork demos$ python3 demo4.py 2Before any fork, PID: 7471 3After first fork, PID: 7471 4After first fork, PID: 7472 5After second fork,PID: 7471 6After second fork,PID: 7473 7After second fork,PID: 7472 8After second fork,PID: 7474 9
  • 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

python
1import os 2 3def demo5(): 4 num = 0 5 rc = os.fork() 6 if rc > 0: 7 num = os.getpid() 8 print ('num: ',num) 9demo5() 10

And guess the output 🙂

python
1shaikh@ubuntu:~/Jupyter/fork demos$ python3 demo5.py 2num: 7825 3num: 0 4

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

Conclusion

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

References

  1. http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html
  2. http://people.cs.pitt.edu/~aus/cs449/ts-lecture14.pdf

Similar posts

About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

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.

2020-10-27

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

2020-10-29

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.

2020-10-26

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.

2020-11-04