{"id":3764,"date":"2024-02-04T09:56:31","date_gmt":"2024-02-04T04:26:31","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3764"},"modified":"2024-02-19T11:15:38","modified_gmt":"2024-02-19T05:45:38","slug":"fork-it","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3764","title":{"rendered":"FORK IT!"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-1 wp-block-paragraph\" style=\"font-size:17px\">The <strong>fork()<\/strong> system call allows one process, the current running parent process to create a new child process. A duplicate is made of the parent process. The child obtains the exact copies of its parent\u2019s memory segment.When the fork() system call is invoked, a new process is created with an identical memory layout to that of its parent.<br><br>Syntax of fork():<br><br><strong>pid_t fork(void);<\/strong><br><br>In parent, it returns the child\u2019s process ID is returned to the parent and 0 is returned to the child on success or returns -1 on error.<br><br>&nbsp;The execution continues from the point where the fork () is returned. But, it is important to remember that, upon the successful call of the fork function, two processes will be under execution at any given point in time. Note, once the fork is called, both the parent and child a race condition will start where whichever process gets the time slice, gets the CPU. We need to use synchronization techniques if we want to the fork in desired manner.<br><br>The code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"197\" data-attachment-id=\"3768\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3768\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-4.png?fit=975%2C197&amp;ssl=1\" data-orig-size=\"975,197\" 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-4\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-4.png?fit=975%2C197&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-4.png?resize=975%2C197&#038;ssl=1\" alt=\"\" class=\"wp-image-3768\" style=\"width:688px;height:auto\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-2 wp-block-paragraph\" 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=\"975\" height=\"124\" data-attachment-id=\"3769\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3769\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-5.png?fit=975%2C124&amp;ssl=1\" data-orig-size=\"975,124\" 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-5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-5.png?fit=975%2C124&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-5.png?resize=975%2C124&#038;ssl=1\" alt=\"\" class=\"wp-image-3769\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-3 wp-block-paragraph\" style=\"font-size:17px\"><strong>As you can see in the below image, the 3402 and 3401 are run by a.out concurrently. This process tree is obtained by running the command ps axjf.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"210\" data-attachment-id=\"3770\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3770\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-6.png?fit=975%2C210&amp;ssl=1\" data-orig-size=\"975,210\" 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-6\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-6.png?fit=975%2C210&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-6.png?resize=975%2C210&#038;ssl=1\" alt=\"\" class=\"wp-image-3770\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-4 wp-block-paragraph\" style=\"font-size:17px\">In Parent&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n #include&lt;stdio.h&gt;\n\n void main()\n {\n     printf(&quot;Printing in Parent, only run once\\n&quot;);\n     printf(&quot;Process ID and parent process ID in parent: %d , %d\\n&quot;,getpid(),getppid());\n     fork();\n     printf(&quot;Process ID and parent process ID in (runs in parent and child): %d , %d\\n&quot;,getpid(),getppid());\n     while(1);\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-5 wp-block-paragraph\" style=\"font-size:17px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In Child<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [8,9,10,11]; title: ; notranslate\" title=\"\">\n #include&lt;stdio.h&gt;\n\n void main()\n {\n     printf(&quot;Printing in Parent, only run once\\n&quot;);\n     printf(&quot;Process ID and parent process ID in parent: %d , %d\\n&quot;,getpid(),getppid());\n     fork();\n     printf(&quot;Process ID and parent process ID in (runs in parent and child): %d , %d\\n&quot;,getpid(),getppid());\n     while(1);\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-6 wp-block-paragraph\" style=\"font-size:17px\"><strong>Let us understand how the fork works if called multiple time. In this section of code, we will call the fork line by line.<\/strong><\/p>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-7 wp-block-paragraph\" style=\"font-size:17px\"><strong>The code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include&lt;stdio.h&gt;\nvoid main()\n{\n    printf(&quot; Started from main, Calling forks\\n&quot;);\n    fork(); \/\/fork1\n    printf(&quot; Fork 1 was called.\\n&quot;);\n    fork(); \/\/fork2\n    printf(&quot; Fork 2 was called.\\n&quot;);\n    fork(); \/\/fork3\n    printf(&quot; Fork 3 was called.\\n&quot;);\n    fork(); \/\/fork4\n    printf(&quot; Fork 4 was called.\\n&quot;);\n    while(1);\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-8 wp-block-paragraph\" 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=\"356\" height=\"688\" data-attachment-id=\"3774\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3774\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-7.png?fit=356%2C688&amp;ssl=1\" data-orig-size=\"356,688\" 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-7\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-7.png?fit=356%2C688&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-7.png?resize=356%2C688&#038;ssl=1\" alt=\"\" class=\"wp-image-3774\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-9 wp-block-paragraph\" style=\"font-size:17px\">It is indeed confusing. Let\u2019s make a tree and see how it works.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"446\" data-attachment-id=\"3775\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3775\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-8.png?fit=1125%2C502&amp;ssl=1\" data-orig-size=\"1125,502\" 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-8\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-8.png?fit=1000%2C446&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-8.png?resize=1000%2C446&#038;ssl=1\" alt=\"\" class=\"wp-image-3775\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-10 wp-block-paragraph\" style=\"font-size:17px\">The above tree was made by using the command <strong>ps axjf.<\/strong><br><br>Upon running the program, we see that a total of 16 processes have been created. Check the below image for a detailed analysis of how the fork works for that particular code snippet.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"867\" data-attachment-id=\"3785\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3785\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/fork4_calls.png?fit=4415%2C3830&amp;ssl=1\" data-orig-size=\"4415,3830\" 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=\"fork4_calls\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/fork4_calls.png?fit=1000%2C867&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/fork4_calls.png?resize=1000%2C867&#038;ssl=1\" alt=\"\" class=\"wp-image-3785\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-11 wp-block-paragraph\" style=\"font-size:17px\"><br><br>Let us look at how the fork function call works when called iteratively.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid main()\n{\n        int i;\n\n        for(i=1;i&lt;=3;i++)\n        {\n                if(fork()==0)\n                {\n                        printf(&quot;Loop %d - PID:%d\\tPPID:%d\\n&quot;,i,getpid(),getppid()  ) ;\n                }\n\n        }\n        while(1);\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-12 wp-block-paragraph\" style=\"font-size:17px\">The output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"256\" data-attachment-id=\"3777\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3777\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-10.png?fit=620%2C256&amp;ssl=1\" data-orig-size=\"620,256\" 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-10\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-10.png?fit=620%2C256&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-10.png?resize=620%2C256&#038;ssl=1\" alt=\"\" class=\"wp-image-3777\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-13 wp-block-paragraph\" style=\"font-size:17px\">Using 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=\"453\" height=\"328\" data-attachment-id=\"3779\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3779\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-11.png?fit=453%2C328&amp;ssl=1\" data-orig-size=\"453,328\" 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-11\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-11.png?fit=453%2C328&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-11.png?resize=453%2C328&#038;ssl=1\" alt=\"\" class=\"wp-image-3779\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-14 wp-block-paragraph\" style=\"font-size:17px\">It can be visualized as the following diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"749\" data-attachment-id=\"3843\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3843\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-13.webp?fit=1024%2C767&amp;ssl=1\" data-orig-size=\"1024,767\" 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-13\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-13.webp?fit=1000%2C749&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/image-13.webp?resize=1000%2C749&#038;ssl=1\" alt=\"\" class=\"wp-image-3843\" \/><\/figure>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-15 wp-block-paragraph\" style=\"font-size:17px\">When the fork call is made, the kernel creates a text segment for the child process by setting up a set of per-process page table entries. These entries refer to the same virtual memory page frames that are already in use by the parent process. The text segment is designated as a read-only code segment.<br><br>In the case of data, heap, and stack segments, the kernel employs a copy-on-write mechanism. After the fork call, if either the parent or the child process attempts to modify one of these pages that are initially created by the kernel, the kernel intercepts the access and creates a duplicate copy of the page that can be modified. This involves assigning a new page and updating the corresponding page table entry for the child process.<br><br>At this point, the parent and child processes can independently modify their respective private copies of the page without affecting each other. The parent&#8217;s process page is not copied for the child process; instead, it is shared between the parent and the child upon forking. When a process attempts to modify a shared page, a separate copy of the page is created exclusively for that process. This copy-on-write approach ensures that pages are only duplicated when a process actually writes to them, optimizing memory usage.<br><br>By utilizing the fork system call and implementing copy-on-write, the operating system efficiently manages memory resources and allows parent and child processes to safely and independently modify shared and private memory pages as needed.<br><\/p>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-16 wp-block-paragraph\" 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\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-17 wp-block-paragraph\"><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child&#8217;s process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3764\" class=\"more-link\">Read More <span class=\"screen-reader-text\">FORK IT!<\/span><\/a>","protected":false},"author":120055267,"featured_media":3841,"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,"_wpcom_ai_launchpad_first_post":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":false,"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,30181,35264203,5495],"tags":[273,772321181,772321193,142396,264,34922710,4525386,34920936],"class_list":["post-3764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","category-embedded-systems","category-linux-3","category-operating-system","tag-blog","tag-education","tag-embedded-systems","tag-fork","tag-learning","tag-linux-2","tag-system-call","tag-ubuntu-2","fallback-thumbnail"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child&#039;s process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yashwanth Naidu Tikkisetty\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/cthecosmos.com\/?p=3764\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Yashwanth Naidu Tikkisetty - Embedded Systems | Space Blogger\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"FORK IT! - Yashwanth Naidu Tikkisetty\" \/>\n\t\t<meta property=\"og:description\" content=\"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child&#039;s process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/cthecosmos.com\/?p=3764\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2024-02-04T04:26:31+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2024-02-19T05:45:38+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"FORK IT! - Yashwanth Naidu Tikkisetty\" \/>\n\t\t<meta name=\"twitter:description\" content=\"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child&#039;s process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#blogposting\",\"name\":\"FORK IT! - Yashwanth Naidu Tikkisetty\",\"headline\":\"FORK IT!\",\"author\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?author=120055267#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/cthecosmos.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/openacc2-2981521003-e1707023695567.png?fit=454%2C174&ssl=1\",\"width\":454,\"height\":174},\"datePublished\":\"2024-02-04T09:56:31+05:30\",\"dateModified\":\"2024-02-19T11:15:38+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#webpage\"},\"articleSection\":\"c programming, Embedded Systems, linux, Operating System, blog, Education, Embedded Systems, fork, learning, linux, system call, ubuntu\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/cthecosmos.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?cat=5495#listItem\",\"name\":\"Operating System\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?cat=5495#listItem\",\"position\":2,\"name\":\"Operating System\",\"item\":\"https:\\\/\\\/cthecosmos.com\\\/?cat=5495\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#listItem\",\"name\":\"FORK IT!\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#listItem\",\"position\":3,\"name\":\"FORK IT!\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?cat=5495#listItem\",\"name\":\"Operating System\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/#person\",\"name\":\"Yashwanth Naidu Tikkisetty\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a02d9f38e4c924587b544c74f6d5de0c8469b6a5c684622f1aa4ea2ba65b17d8?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Yashwanth Naidu Tikkisetty\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?author=120055267#author\",\"url\":\"https:\\\/\\\/cthecosmos.com\\\/?author=120055267\",\"name\":\"Yashwanth Naidu Tikkisetty\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a02d9f38e4c924587b544c74f6d5de0c8469b6a5c684622f1aa4ea2ba65b17d8?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Yashwanth Naidu Tikkisetty\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#webpage\",\"url\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764\",\"name\":\"FORK IT! - Yashwanth Naidu Tikkisetty\",\"description\":\"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child's process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?author=120055267#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?author=120055267#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/cthecosmos.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/openacc2-2981521003-e1707023695567.png?fit=454%2C174&ssl=1\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764\\\/#mainImage\",\"width\":454,\"height\":174},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/?p=3764#mainImage\"},\"datePublished\":\"2024-02-04T09:56:31+05:30\",\"dateModified\":\"2024-02-19T11:15:38+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/#website\",\"url\":\"https:\\\/\\\/cthecosmos.com\\\/\",\"name\":\"Yashwanth Naidu Tikkisetty\",\"description\":\"Embedded Systems | Space Blogger\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/cthecosmos.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"FORK IT! - Yashwanth Naidu Tikkisetty","description":"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child's process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.","canonical_url":"https:\/\/cthecosmos.com\/?p=3764","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/cthecosmos.com\/?p=3764#blogposting","name":"FORK IT! - Yashwanth Naidu Tikkisetty","headline":"FORK IT!","author":{"@id":"https:\/\/cthecosmos.com\/?author=120055267#author"},"publisher":{"@id":"https:\/\/cthecosmos.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/openacc2-2981521003-e1707023695567.png?fit=454%2C174&ssl=1","width":454,"height":174},"datePublished":"2024-02-04T09:56:31+05:30","dateModified":"2024-02-19T11:15:38+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/cthecosmos.com\/?p=3764#webpage"},"isPartOf":{"@id":"https:\/\/cthecosmos.com\/?p=3764#webpage"},"articleSection":"c programming, Embedded Systems, linux, Operating System, blog, Education, Embedded Systems, fork, learning, linux, system call, ubuntu"},{"@type":"BreadcrumbList","@id":"https:\/\/cthecosmos.com\/?p=3764#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/cthecosmos.com#listItem","position":1,"name":"Home","item":"https:\/\/cthecosmos.com","nextItem":{"@type":"ListItem","@id":"https:\/\/cthecosmos.com\/?cat=5495#listItem","name":"Operating System"}},{"@type":"ListItem","@id":"https:\/\/cthecosmos.com\/?cat=5495#listItem","position":2,"name":"Operating System","item":"https:\/\/cthecosmos.com\/?cat=5495","nextItem":{"@type":"ListItem","@id":"https:\/\/cthecosmos.com\/?p=3764#listItem","name":"FORK IT!"},"previousItem":{"@type":"ListItem","@id":"https:\/\/cthecosmos.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/cthecosmos.com\/?p=3764#listItem","position":3,"name":"FORK IT!","previousItem":{"@type":"ListItem","@id":"https:\/\/cthecosmos.com\/?cat=5495#listItem","name":"Operating System"}}]},{"@type":"Person","@id":"https:\/\/cthecosmos.com\/#person","name":"Yashwanth Naidu Tikkisetty","image":{"@type":"ImageObject","@id":"https:\/\/cthecosmos.com\/?p=3764#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/a02d9f38e4c924587b544c74f6d5de0c8469b6a5c684622f1aa4ea2ba65b17d8?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Yashwanth Naidu Tikkisetty"}},{"@type":"Person","@id":"https:\/\/cthecosmos.com\/?author=120055267#author","url":"https:\/\/cthecosmos.com\/?author=120055267","name":"Yashwanth Naidu Tikkisetty","image":{"@type":"ImageObject","@id":"https:\/\/cthecosmos.com\/?p=3764#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a02d9f38e4c924587b544c74f6d5de0c8469b6a5c684622f1aa4ea2ba65b17d8?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Yashwanth Naidu Tikkisetty"}},{"@type":"WebPage","@id":"https:\/\/cthecosmos.com\/?p=3764#webpage","url":"https:\/\/cthecosmos.com\/?p=3764","name":"FORK IT! - Yashwanth Naidu Tikkisetty","description":"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child's process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/cthecosmos.com\/#website"},"breadcrumb":{"@id":"https:\/\/cthecosmos.com\/?p=3764#breadcrumblist"},"author":{"@id":"https:\/\/cthecosmos.com\/?author=120055267#author"},"creator":{"@id":"https:\/\/cthecosmos.com\/?author=120055267#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/openacc2-2981521003-e1707023695567.png?fit=454%2C174&ssl=1","@id":"https:\/\/cthecosmos.com\/?p=3764\/#mainImage","width":454,"height":174},"primaryImageOfPage":{"@id":"https:\/\/cthecosmos.com\/?p=3764#mainImage"},"datePublished":"2024-02-04T09:56:31+05:30","dateModified":"2024-02-19T11:15:38+05:30"},{"@type":"WebSite","@id":"https:\/\/cthecosmos.com\/#website","url":"https:\/\/cthecosmos.com\/","name":"Yashwanth Naidu Tikkisetty","description":"Embedded Systems | Space Blogger","inLanguage":"en-US","publisher":{"@id":"https:\/\/cthecosmos.com\/#person"}}]},"og:locale":"en_US","og:site_name":"Yashwanth Naidu Tikkisetty - Embedded Systems | Space Blogger","og:type":"article","og:title":"FORK IT! - Yashwanth Naidu Tikkisetty","og:description":"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child's process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.","og:url":"https:\/\/cthecosmos.com\/?p=3764","og:image":"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png","og:image:secure_url":"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png","og:image:width":1200,"og:image:height":1200,"article:published_time":"2024-02-04T04:26:31+00:00","article:modified_time":"2024-02-19T05:45:38+00:00","twitter:card":"summary_large_image","twitter:title":"FORK IT! - Yashwanth Naidu Tikkisetty","twitter:description":"The fork() system call allows a parent process to create a child process with an identical memory layout. The parent receives the child's process ID, while the child receives 0. Synchronization techniques are needed for desired forking. Multiple calls to fork() create a complex process tree, managed efficiently through copy-on-write to optimize memory usage.","twitter:image":"https:\/\/cthecosmos.com\/wp-content\/uploads\/2018\/08\/logo1.png"},"aioseo_meta_data":{"post_id":"3764","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2025-08-31 20:16:09","updated":"2025-08-31 20:16:09","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/cthecosmos.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/cthecosmos.com\/?cat=5495\" title=\"Operating System\">Operating System<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tFORK IT!\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/cthecosmos.com"},{"label":"Operating System","link":"https:\/\/cthecosmos.com\/?cat=5495"},{"label":"FORK IT!","link":"https:\/\/cthecosmos.com\/?p=3764"}],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-YI","jetpack-related-posts":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/openacc2-2981521003-e1707023695567.png?fit=454%2C174&ssl=1","_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3764","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=3764"}],"version-history":[{"count":14,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3764\/revisions"}],"predecessor-version":[{"id":3844,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3764\/revisions\/3844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3841"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}