{"id":5358,"date":"2022-05-21T00:01:17","date_gmt":"2022-05-21T07:01:17","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5358"},"modified":"2022-05-28T10:14:47","modified_gmt":"2022-05-28T17:14:47","slug":"forking-the-grandchildren","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5358","title":{"rendered":"Forking the Grandchildren"},"content":{"rendered":"<p>I wrote about forking a while back, in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1395\">June 2015<\/a>. The fork splits a program into two processes, each capable of handling different tasks simultaneously. The power behind this trick is the <em>fork()<\/em> function.<br \/>\n<!--more--><br \/>\nBefore you get all excited, be aware that the <em>fork()<\/em> function isn&#8217;t available in Windows. It shouldn&#8217;t be available in the Unix environment either, but it is and so everyone deals with it.<\/p>\n<p>The typical <em>fork()<\/em> example has a program split into two parts: The fork duplicates the program and the code uses the program&#8217;s process ID (PID) to determine which version is the original, or parent, and which is the duplicate, or child. The <em>getpid()<\/em> function returns the process ID, which is how the code knows which version of itself is running.<\/p>\n<p>It&#8217;s possible to fork a program more than once, though things get hairy quickly. The code must track all of its various clones lest the program continue to spawn children like over-fed Tribbles.<\/p>\n<p>The following code forks a program into a parent and child process. In the code, when the child process is identified, it forks into a grandchild process.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_05_21-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_05_21-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    pid_t parent,a,b;\r\n\r\n    parent = getpid();\r\n    fork();\r\n    a = getpid();\r\n    if( a==parent )\r\n    {\r\n        printf(\"I am the parent: %d\\n\",parent);\r\n    }\r\n    else\r\n    {\r\n        fork();\r\n        b = getpid();\r\n        if( a==b )\r\n            printf(\"I am the child: %d\\n\",a);\r\n        else\r\n            printf(\"I am the grandchild: %d\\n\",b);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The parent process ID is obtained at Line 8, before the <em>fork()<\/em> call.<\/p>\n<p>After the <em>fork()<\/em> call, at Line 10, variable <code>a<\/code> holds the current process&#8217; ID. If it&#8217;s the parent (Line 11), the process ID is output. Otherwise the <em>else<\/em> block is executed.<\/p>\n<p>In the <em>else<\/em> block, a second fork is issued at Line 17. This operation takes place only in the child process. Variable <code>b<\/code> is assigned the current process ID, then an <em>if-else<\/em> block outputs text based on whether the child or grandchild is running.<\/p>\n<p>Whew!<\/p>\n<p>This code can be complex, not because of the simple decision tree but because it runs three programs (or processes) not just one. Here&#8217;s sample output:<\/p>\n<p><code>I am the parent: 24630<br \/>\nI am the child: 24631<br \/>\nI am the grandchild: 24632<\/code><\/p>\n<p>Forking isn&#8217;t the best way to run two separate processes, as I wrote earlier. A better approach for a program to do two (or more) things at once is to create a new thread. A thread is like a process, but more focused. I cover creating threads in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5365\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to fork a program into several child processes, or as I call them, grandchild processes. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5358\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5358","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5358"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5358\/revisions"}],"predecessor-version":[{"id":5385,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5358\/revisions\/5385"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}