Published on 2020-10-24, by Javed Shaikh
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.
Visit my Github page for all the demo code snippets https://github.com/jaqsparow/fork-demos
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.
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 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.
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
1import os
2os.fork()
3
Now lets do some demos to understand whats going on
In below example we are just checking how getpid() can be used to get the PID of current process.
1import os
2
3def demo1():
4 print('I am the only process, My PID:',os.getpid())
5demo1()
6
Here is the output:
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
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: -
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.
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
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
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
In below example, we are calling fork() twice.
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:
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
If we call fork 3 times, guess the number of processes that will be created in the comment section 🙂
Below example will show that after fork call both parent and child will have different copies of variable num
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 🙂
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
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
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
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
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
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