{"id":26129,"date":"2021-12-14T08:46:07","date_gmt":"2021-12-14T03:16:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=26129"},"modified":"2021-12-14T08:46:07","modified_gmt":"2021-12-14T03:16:07","slug":"python-fork-creating-child-processes-using-the-fork-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-fork-creating-child-processes-using-the-fork-method\/","title":{"rendered":"Python fork() – Creating child processes Using the fork() Method"},"content":{"rendered":"
Python’s fork() method allows us to create child processes from the processes through which it was\u00a0called.<\/p>\n
System Call:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n There are two modes of operation in modern computer systems: user mode and kernel mode.<\/p>\n 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.<\/p>\n 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).<\/p>\n 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.<\/p>\n System calls are divided into five major categories:<\/p>\n Build a child process and show the process ids of both the parent and child processes.<\/p>\n 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.<\/p>\n The OS module in Python allows you to use operating system dependent functionality.\u00a0 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.<\/p>\n It is imported as shown below:<\/p>\n The following are the main characteristics of the fork() method\/statement:<\/p>\n When the Python fork() instruction is executed, it returns three types of integer values:<\/p>\n Zero(0):<\/strong> If the child process is successfully created, a value of zero (0) is returned to it. 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.<\/p>\n Inside the program, it can be called as shown below:<\/p>\n System Calls:<\/strong><\/p>\n fork():<\/strong> 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. Child processes creation using the fork():<\/strong><\/p>\n Example1:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python’s fork() method allows us to create child processes from the processes through which it was\u00a0called. 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 …<\/p>\nfork() Method in Python:<\/strong><\/h4>\n
\n
OS Module<\/strong><\/h4>\n
import os<\/pre>\n
Working of fork() Method<\/h4>\n
\n
\npositive(+ve):<\/strong> 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.
\nnegative(-ve):<\/strong> If an error occurs during the creation of the child process for any reason, a negative (-ve) value is returned to the parent process.<\/p>\nos.fork()<\/pre>\n
\ngetpid():<\/strong> The process ID (PID) of the calling process is returned by getpid().<\/p>\n\n
# Import os module using the import keyword.\r\nimport os\r\n\r\n# Call the fork() method to create the child processes\r\nos.fork()\r\nos.fork()\r\nos.fork()\r\n\r\n# Print some random text.\r\n# It is executed by both the parent and child processes.\r\n# Since the fork() method is called 3 times it is executed 8 times in total(7+1)\r\n# 2^3 -1 = 7 times by child processes\r\n# 1 time by parent process\r\nprint(\"welcome to Python-programs\")\r\n<\/pre>\n
welcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs \r\nwelcome to Python-programs<\/pre>\n
\n
# Import os module using the import keyword.\r\nimport os\r\n# Call the fork() method to create the child processes\r\nrslt_valu = os.fork()\r\n# Check if the value returned by the fork() method is greater than 0 using the\r\n# if conditional statement.\r\nif rslt_valu > 0:\r\n # If it is true, then print \"It is a parent process\" with the pid.\r\n # Get the PID using the getpid() method\r\n p_id = os.getpid()\r\n print(\"This is a Parent process with the pid = \", p_id)\r\nelse:\r\n # Else, print \"It is a child process\" with the pid.\r\n # Get the PID of the child process using the getpid() method\r\n p_id = os.getpid()\r\n print(\"This is a child process with the pid = \", p_id)<\/pre>\n
This is a Parent process with the pid = 60\r\nThis is a child process with the pid = 264<\/pre>\n