{"id":18185,"date":"2021-06-17T16:51:41","date_gmt":"2021-06-17T16:51:41","guid":{"rendered":"https:\/\/www.askpython.com\/?p=18185"},"modified":"2021-06-17T17:02:04","modified_gmt":"2021-06-17T17:02:04","slug":"python-fork","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-fork","title":{"rendered":"Python fork() &#8211; How to create child processes using the fork() method?"},"content":{"rendered":"\n<p>In this article, we&#8217;ll talk about the Python fork() method which allows us to create child processes using the processes through which it was called. Let&#8217;s look <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a system call?<\/strong><\/h2>\n\n\n\n<p>A <strong><em>system call<\/em><\/strong> is used to get access to the kernel mode. It is a way with which a program can interact with the operating system and request its services to perform specified tasks.<\/p>\n\n\n\n<p>Hence all the system calls are executed in the kernel (privileged) mode when the execution completes the control is returned to the user mode. Sometimes a system call is also called <strong><em>syscall<\/em><\/strong> in abbreviated form.<\/p>\n\n\n\n<p>In modern computer systems, there are generally two modes of operations &#8211;  <em>User mode<\/em> and <em>Kernel mode<\/em>. <\/p>\n\n\n\n<p>All the user programs and processes run in User mode and they cannot access the kernel-mode directly. This is done to protect the operating system from the modifications or changes caused by any user program.<\/p>\n\n\n\n<p>If any user program needs to perform privileged work, it requires the support of the operating system which it can get only through the <strong><em>system calls<\/em><\/strong>. And it the system calls which provide the various services of the operating system to the user programs and processes via API (Application Program Interface).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Python fork()?<\/strong><\/h2>\n\n\n\n<p>There are multiple system calls for managing different types of services provided by the operating system. Also, these are different for different operating systems.<\/p>\n\n\n\n<p>System calls are broadly classified into five main categories:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Files related system calls<\/li><li>Device related system calls<\/li><li><strong>Process related system calls<\/strong><\/li><li><strong>Information related system calls<\/strong><\/li><li>Communication-related system calls<\/li><\/ol>\n\n\n\n<p>So, Python <strong><code>fork()<\/code><\/strong> is an example of Process related or Process control system call. It is a method used to create a <em>Child process<\/em> of the process which calls it just like the clone of the original (calling) process. The process which makes a system call using the <code>fork()<\/code> statement is called the <em>Parent process<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Importing the OS module <\/strong><\/h2>\n\n\n\n<p>In order to use and implement the <code>fork()<\/code> system call in Python, we require the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-os-module-10-must-know-functions\" data-type=\"post\" data-id=\"1345\"><code>os<\/code> module of Python<\/a>. This os module in Python allows us to use the various operating system-dependent functionality. It allows the user program to access the operating system functionality on which the Python is running. <\/p>\n\n\n\n<p>We need not install the <code>os<\/code> module as it comes under Python\u2019s standard utility modules and gets installed when Python is installed in our system.<\/p>\n\n\n\n<p>It is imported inside the program as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Working of Python fork()<\/strong><\/h2>\n\n\n\n<p>Following are the key properties of <code>fork()<\/code> method\/statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It takes no argument\/parameter when it is being called inside any program.<\/li><li>If the child process is created successfully then both the parent process and the child process will execute the next statement\/instruction followed by the <code>fork()<\/code> statement.<\/li><li>The number of child processes = <strong><em>2<sup>N<\/sup> &#8211; 1<\/em><\/strong>; where <em>N<\/em> = Number of <code>fork()<\/code> statements used inside the main program.<\/li><\/ul>\n\n\n\n<p><strong>On execution of the Python fork() statement it returns three types of integer values:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Zero<\/strong> (<strong>0<\/strong>) is returned to the child process if the child process is created successfully.<\/li><li>A<strong> positive <\/strong>(<strong>+ve<\/strong>) value is returned to the parent process if the child process is created successfully. This positive value is usually the <strong>PID<\/strong> that is the <sup><strong>**<\/strong><\/sup><em>process ID<\/em> of the newly created child process.<\/li><li>A<strong> negative <\/strong>(<strong>-ve<\/strong>) value is returned to the parent process if some error occurs in the creation of the child process due to any reason.<\/li><\/ol>\n\n\n\n<p><strong>Process ID<\/strong> usually referred to as <strong>PID<\/strong> is a unique identifier associated with every process present inside the computer system. The process ID of any process can be accessed using another system call <strong><em><code>getpid()<\/code><\/em><\/strong> which is an example of Information related system calls. The <code>getpid()<\/code> statement returns the process ID of the process which calls it.<\/p>\n\n\n\n<p>It is called inside the program as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nos.fork()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating child processes with the fork() method in Python<\/strong><\/h2>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing os module\nimport os\n\n# Creating child processes using fork() method\nos.fork()\nos.fork()\n\n# This will be executed by both parent &amp; child processes\nprint(&quot;AskPython&quot;)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"275\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example1.png\" alt=\"Python fork()\" class=\"wp-image-18192\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example1.png 498w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example1-300x166.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/figure><\/div>\n\n\n\n<p>The above python program has produced the correct output as there are two <code>fork()<\/code> statements used inside the program. Hence the <code>print()<\/code> statement executed four times (3 + 1) three (<strong>2<sup>2<\/sup> -1 = 3<\/strong>) times by the three child processes and one time by the parent process.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing os module\nimport os\n\n# Creating a child process using fork() method\nval = os.fork()\n\n# Testing the values returned by fork() method\nif val == 0:\n    pid = os.getpid()\n    print(f&quot;Hi I am Child Process and my PID is {pid}.&quot;)\nelif val &gt; 0:\n    pid = os.getpid()\n    print(f&quot;Hi I am Parent Process and my PID is {pid} and PID {val} is my Child Process.&quot;)\nelse:\n    print(&quot;Sorry!! Child Process creation has failed...&quot;)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"214\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example2.png\" alt=\"Python fork()\" class=\"wp-image-18193\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example2.png 1025w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example2-300x63.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_example2-768x160.png 768w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/figure><\/div>\n\n\n\n<p>The above python program has produced the correct output and clearly demonstrated the values returned by the <code>fork()<\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this tutorial, we have learned what is a system call, what a Python <code>fork()<\/code> method is, its properties, and how to create child processes using the <code>fork()<\/code> method in Python.<\/p>\n\n\n\n<p><strong>Note: <\/strong><code>fork()<\/code> method\/system call is available only for Linux\/Unix operating systems. If you try to run any Python program that calls the <code>fork()<\/code> method on a Windows system then the following error will be produced:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"212\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_windows_error-1.png\" alt=\"Fork Windows Error\" class=\"wp-image-18196\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_windows_error-1.png 734w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/fork_windows_error-1-300x87.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll talk about the Python fork() method which allows us to create child processes using the processes through which it was called. Let&#8217;s look What is a system call? A system call is used to get access to the kernel mode. It is a way with which a program can interact with [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":18186,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-18185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=18185"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18185\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/18186"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=18185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=18185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=18185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}