{"id":3790,"date":"2024-02-04T10:22:16","date_gmt":"2024-02-04T04:52:16","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3790"},"modified":"2024-02-04T10:43:31","modified_gmt":"2024-02-04T05:13:31","slug":"%f0%9d%90%84%f0%9d%90%a2%f0%9d%90%ad%f0%9d%90%a1%f0%9d%90%9e%f0%9d%90%ab-%f0%9d%90%8e%f0%9d%90%ab%f0%9d%90%a9%f0%9d%90%a1%f0%9d%90%9a%f0%9d%90%a7%f0%9d%90%9e%f0%9d%90%9d-%f0%9d%90%a8%f0%9d%90%ab","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3790","title":{"rendered":"\ud835\udc04\ud835\udc22\ud835\udc2d\ud835\udc21\ud835\udc1e\ud835\udc2b \ud835\udc0e\ud835\udc2b\ud835\udc29\ud835\udc21\ud835\udc1a\ud835\udc27\ud835\udc1e\ud835\udc1d \ud835\udc28\ud835\udc2b \ud835\udc19\ud835\udc28\ud835\udc26\ud835\udc1b\ud835\udc22\ud835\udc1e\ud835\udc1d"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-5d4346972582fff3e8f73255eb405e25\" style=\"font-size:17px\">When using the fork() function call, it is important to handle the possible outcomes of the process creation properly to prevent orphaned or zombie processes. The fork() function creates a child process, and the concepts of orphan and zombie processes are closely related to this child process.<br><br>In this short write-up, you can observe how and child process becomes orphaned or becomes a zombie process.<br><br>If it is left running, it might be orphaned or zombified. I am talking about the outcomes of the fork() function call. When fork() is called, a child process would be created. The concepts of the orphan process and the zombie process are related to the child process. The child and the parent processes are initially not timed, so one of the following situations might happen.<br><br>The Parent process completes its execution while the Child process is still executing; this would lead to the child process becoming an orphan process. Once the parent completes its execution and exits, the PPID of the child is handed over to init.<br><br>The second scenario would be, the child completed its execution but the parent is still executing. The child process enters a state where it is no longer able to execute instructions, becoming a dead or zombie process. This occurs because the parent process needs to gather the exit status of its child. Once the exit status is acquired, the process manager removes the child from the zombie state. In cases where the parent process terminates before collecting the child&#8217;s exit status, the kernel considers it an orphan process that is then adopted by the &#8220;init&#8221; process.<br><br>There is not much problem in the occurrence of the first scenario, but in the second scenario, If a large number of zombie child processes are created, their entry would be maintained in the kernel\u2019s process table, and many occurrences would fill up the process table that would prevent the creation of new processes. Zombies cannot be killed by a signal, so the only way to remove them is by killing the parent process of the zombies. As the zombies have ppid as the init pid(1), the entire system needs to be shut in order to kill the zombies.\u00a0<br><br>To avoid the child from becoming orphan, the parent should wait until the child process is completed. To avoid the child from becoming zombie, the child should send an exit status to the parent and the parent should wait until the exit status is received from the child.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/ Illustration of Orphan Child Process                                                                                  \n   #include&lt;stdio.h&gt;\n   #include&lt;sys\/types.h&gt;\n   #include&lt;unistd.h&gt;\n  \n   void main()\n   {\n       if(fork()==0)\n      {\n          \/\/ Child process Execution\n          printf(&quot;In Child Process\\n&quot;);\n          printf(&quot;PID: %d\\tPPID: %d\\n&quot;,getpid(),getppid());\n          sleep(7);   \/\/ Child process turns into orphan process as the parent will be exiting after 3 seconds\n          printf(&quot;Child Process terminated\\n&quot;);\n \n      }\n      else\n      {\n          \/\/Parent Process Execution\n \n          printf(&quot;In Parent Process\\n&quot;);\n          printf(&quot;PID: %d\\tPPID: %d\\n&quot;,getpid(),getppid());\n          sleep(3);   \/\/ Exit after 3 seconds\n          printf(&quot;Parent Process Terminated\\n&quot;);\n \n      }\n  }\n\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-c7a17a673a12323bf6150e9bb0de5910\" style=\"font-size:17px\">Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"182\" data-attachment-id=\"3793\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3793\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-14.png?fit=1125%2C205&amp;ssl=1\" data-orig-size=\"1125,205\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-14\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-14.png?fit=1000%2C182&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-14.png?resize=1000%2C182&#038;ssl=1\" alt=\"\" class=\"wp-image-3793\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-2151e60b50995dc29536bbfc602fc6e9\" style=\"font-size:17px\">With the ps command:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"104\" data-attachment-id=\"3794\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3794\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-15.png?fit=1125%2C117&amp;ssl=1\" data-orig-size=\"1125,117\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-15\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-15.png?fit=1000%2C104&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-15.png?resize=1000%2C104&#038;ssl=1\" alt=\"\" class=\"wp-image-3794\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-e2a3e0beeac08234598fc323da2f447a\" style=\"font-size:17px\">After the parent exits:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"76\" data-attachment-id=\"3795\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3795\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-16.png?fit=1125%2C86&amp;ssl=1\" data-orig-size=\"1125,86\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-16\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-16.png?fit=1000%2C76&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-16.png?resize=1000%2C76&#038;ssl=1\" alt=\"\" class=\"wp-image-3795\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-2f3e6b43732ea5e6ff3e08614ba3f6e3\" style=\"font-size:17px\">The condition if(fork()==0) is executed both in parent and child process. In the parent process, the else condition is executed as fork() returns PID of the child upon successful creation and returns 0 to the child.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"556\" data-attachment-id=\"3796\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3796\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-17.png?fit=1126%2C626&amp;ssl=1\" data-orig-size=\"1126,626\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-17\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-17.png?fit=1000%2C556&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-17.png?resize=1000%2C556&#038;ssl=1\" alt=\"\" class=\"wp-image-3796\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-a6a4ee6a86fbf4592ba5029f287d879b\" style=\"font-size:17px\">Zombie Process:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ Illustration of transformation of Child process Zombie Process\n#include&amp;lt;stdio.h&amp;gt;\n#include&amp;lt;unistd.h&amp;gt;\n#include&amp;lt;sys\/types.h&amp;gt;\n\nvoid main()\n{\n        printf(&quot; Current process PID: %d\\tPPID:%d\\n&quot;,getpid(),getppid());\n\n        if(fork()==0)\n        {\n                printf(&quot;Enetered Child Process\\n&quot;);\n                \/\/Child execution\n                printf(&quot;Executing in Child PID:%d\\tPPID:%d\\n&quot;,getpid(),getppid());\n                sleep(2);\n                printf(&quot;Exited Child Process\\n&quot;);\n\n        }\n        else{\n                \/\/Parent execution\n\n                printf(&quot;Entered Parent Process\\n&quot;);\n                printf(&quot;Executing in Parent PID:%d\\tPPID:%d\\n&quot;,getpid(),getppid());\n                sleep(7);\n                printf(&quot;Exited Parent Process\\n&quot;);\n        }\n}\n\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-cfc3b47c1698b1f9d55f1dfe1e3f74de\" style=\"font-size:17px\">Output: The child is exited while the parent is still executing.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"263\" data-attachment-id=\"3798\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3798\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-18.png?fit=838%2C263&amp;ssl=1\" data-orig-size=\"838,263\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-18\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-18.png?fit=838%2C263&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-18.png?resize=838%2C263&#038;ssl=1\" alt=\"\" class=\"wp-image-3798\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-3c4ce1a85d6352db4262e65cf9709540\" style=\"font-size:17px\">Ps:<br><br>Child Created<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"64\" data-attachment-id=\"3799\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3799\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-19.png?fit=1125%2C72&amp;ssl=1\" data-orig-size=\"1125,72\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-19\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-19.png?fit=1000%2C64&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-19.png?resize=1000%2C64&#038;ssl=1\" alt=\"\" class=\"wp-image-3799\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-5630aaf84608234a45d4b21c0933554c\" style=\"font-size:17px\">Child Defunctional<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"36\" data-attachment-id=\"3800\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3800\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-20.png?fit=1125%2C41&amp;ssl=1\" data-orig-size=\"1125,41\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-20\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-20.png?fit=1000%2C36&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-20.png?resize=1000%2C36&#038;ssl=1\" alt=\"\" class=\"wp-image-3800\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the fork() function, it&#8217;s crucial to handle the process creation outcomes to avoid orphaned or zombie processes. Orphaned processes result when the parent completes before the child, and zombies occur when the child finishes first. This creates resource management challenges and may require system shutdown to address. Proper handling prevents these issues.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3790\" class=\"more-link\">Read More <span class=\"screen-reader-text\">\ud835\udc04\ud835\udc22\ud835\udc2d\ud835\udc21\ud835\udc1e\ud835\udc2b \ud835\udc0e\ud835\udc2b\ud835\udc29\ud835\udc21\ud835\udc1a\ud835\udc27\ud835\udc1e\ud835\udc1d \ud835\udc28\ud835\udc2b \ud835\udc19\ud835\udc28\ud835\udc26\ud835\udc1b\ud835\udc22\ud835\udc1e\ud835\udc1d<\/span><\/a>","protected":false},"author":120055267,"featured_media":3840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[36985,1342,28627,30181,35264203,769114260,5495],"tags":[142396,34922710,189046278,9719,43912,34920936,290712],"class_list":{"0":"post-3790","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-c-programming","8":"category-education","9":"category-embedded","10":"category-embedded-systems","11":"category-linux-3","12":"category-linuxlearnings","13":"category-operating-system","14":"tag-fork","15":"tag-linux-2","16":"tag-orphan-process","17":"tag-process","18":"tag-shell","19":"tag-ubuntu-2","20":"tag-zombie","22":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/funwithc-1.webp?fit=1792%2C1024&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-Z8","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3790"}],"version-history":[{"count":7,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3790\/revisions"}],"predecessor-version":[{"id":3804,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3790\/revisions\/3804"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3840"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}