Python’s fork() method allows us to create child processes from the processes through which it was called.
System Call:
To gain access to the kernel mode, a system call is used. It is a method for a program to communicate with the operating system and request its services to do specific tasks.
As a result, all system calls are executed in the kernel (privileged) mode, and after the execution is finished, control is returned to the user mode. In shortened form, a system call is also known as syscall.
There are two modes of operation in modern computer systems: user mode and kernel mode.
All user programs and processes run in User mode and have no direct access to the kernel mode. This is done to protect the operating system against alterations or modifications induced by any user program.
If a user program needs to execute privileged tasks, it must rely on the operating system for assistance, which it can only obtain through system calls. And it is the system calls that, via API, deliver the different services of the operating system to user programs and processes (Application Program Interface).
fork() Method in Python:
There are numerous system calls for managing the various sorts of services offered by the operating system. Furthermore, these varies depending on the operating system.
System calls are divided into five major categories:
- System calls relating to files
- System calls relating to devices
- System calls related to the process
- System calls related to information
- System calls related to communication
Build a child process and show the process ids of both the parent and child processes.
Fork system call uses for to establish a new process called child process that runs concurrently with process (whose process called system call fork) and is named parent process. Following the creation of a new child process, both processes will execute the next instruction following the fork() system call.
OS Module
The OS module in Python allows you to use operating system dependent functionality. The OS module’s functions allow users to interface with the underlying operating system that Python is running on, whether it’s Windows, Mac, or Linux.
It is imported as shown below:
import os
Working of fork() Method
The following are the main characteristics of the fork() method/statement:
- When called from within a program, it accepts no arguments.
- If the child process is successfully created, both the parent and child processes will execute the next statement/instruction, which will be followed by the fork() statement.
- The number of child processes equals 2N – 1, where N is the number of fork() statements used within the main program.
When the Python fork() instruction is executed, it returns three types of integer values:
Zero(0): If the child process is successfully created, a value of zero (0) is returned to it.
positive(+ve): If the child process is successfully created, a positive (+ve) value is returned to the parent process. This positive value is usually the PID of the newly formed child process, which is the **process ID.
negative(-ve): If an error occurs during the creation of the child process for any reason, a negative (-ve) value is returned to the parent process.
Process ID, often known as PID, is a unique identifier assigned with each process present inside the computer. The process ID of any process can be retrieved by another system call called getpid(), which is an example of an information-related system call. The getpid() command returns the process ID of the calling process.
Inside the program, it can be called as shown below:
os.fork()
System Calls:
fork(): fork() is an operation that allows a process to generate a replica(copy) of itself. It is typically a system call that is implemented in the kernel.
getpid(): The process ID (PID) of the calling process is returned by getpid().
Child processes creation using the fork():
Example1:
Approach:
- Import os module using the import keyword.
- Call the fork() method 3 times to create the child processes.
- Print some random text.
- Â It is executed by both the parent and child processes.
- Since the fork() method is called 3 times it is executed 8 times in total (7+1).
- 2^3 -1 = 7 times by child processes
- 1 time by the parent process.
- The Exit of the Program.
Below is the implementation:
# Import os module using the import keyword. import os # Call the fork() method to create the child processes os.fork() os.fork() os.fork() # Print some random text. # It is executed by both the parent and child processes. # Since the fork() method is called 3 times it is executed 8 times in total(7+1) # 2^3 -1 = 7 times by child processes # 1 time by parent process print("welcome to Python-programs")
Output:
welcome to Python-programs welcome to Python-programs welcome to Python-programs welcome to Python-programs welcome to Python-programs welcome to Python-programs welcome to Python-programs welcome to Python-programs
Example2:
Approach:
- Import os module using the import keyword.
- Call the fork() method to create the child processes.
- Check if the value returned by the fork() method is greater than 0 using the if conditional statement.
- If it is true, then print “It is a parent process” with the PID.
- Get the PID using the getpid() method.
- Else, print “It is a child process” with the PID.
- Get the PID of the child process using the getpid() method.
- The Exit of the Program.
Below is the implementation:
# Import os module using the import keyword. import os # Call the fork() method to create the child processes rslt_valu = os.fork() # Check if the value returned by the fork() method is greater than 0 using the # if conditional statement. if rslt_valu > 0: # If it is true, then print "It is a parent process" with the pid. # Get the PID using the getpid() method p_id = os.getpid() print("This is a Parent process with the pid = ", p_id) else: # Else, print "It is a child process" with the pid. # Get the PID of the child process using the getpid() method p_id = os.getpid() print("This is a child process with the pid = ", p_id)
Output:
This is a Parent process with the pid = 60 This is a child process with the pid = 264