{"id":7150,"date":"2020-05-28T15:42:26","date_gmt":"2020-05-28T15:42:26","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=7150"},"modified":"2024-02-20T11:48:12","modified_gmt":"2024-02-20T11:48:12","slug":"child-process-linux-vs-windows","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=7150","title":{"rendered":"Child process (Linux vs. Windows)"},"content":{"rendered":"<p><strong>Windows<\/strong><br \/>\nYou always have to create a totally new process via a CreateProcess(&#8230;) call.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: child.cpp; notranslate\" title=\"child.cpp\">\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  std::cout &lt;&lt; &quot;I&#039;m a child&quot; &lt;&lt; std::endl;\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: parent.cpp; notranslate\" title=\"parent.cpp\">\r\n#include &lt;windows.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  char path[] = &quot;.\\\\child.exe&quot;;\r\n  char args[] = &quot;child.exe&quot;;\r\n  STARTUPINFO startupInfo = { sizeof(startupInfo) };\r\n  PROCESS_INFORMATION processInfo;\r\n\r\n  if (!CreateProcess(path, args, NULL, NULL, TRUE, 0, NULL, NULL,\r\n                     &amp;startupInfo, &amp;processInfo))\r\n  {\r\n    \/* Couldn&#039;t create process *\/\r\n    std::cout &lt;&lt; &quot;Couldn&#039;t create process&quot; &lt;&lt; std::endl;\r\n  }\r\n  std::cout &lt;&lt; &quot;I&#039;m the parent&quot; &lt;&lt; std::endl;\r\n\r\n  WaitForSingleObject(processInfo.hProcess, INFINITE);\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Linux<\/strong><br \/>\nYou can either just copy the process (1) or copy it and replace the program (2).<\/p>\n<p>(1) Just copy parent process<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  pid_t pid = fork();\r\n\r\n  if (pid == -1) {\r\n    \/* Couldn&#039;t fork *\/\r\n    std::cout &lt;&lt; &quot;Couldn&#039;t fork&quot; &lt;&lt; std::endl;\r\n  } else if (pid == 0) {\r\n    \/* Child process *\/\r\n    std::cout &lt;&lt; &quot;I&#039;m the child&quot; &lt;&lt; std::endl;\r\n  } else {\r\n    \/* Parent process *\/\r\n    std::cout &lt;&lt; &quot;I&#039;m the parent&quot; &lt;&lt; std::endl;\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<p>(2) Copy parent process and replace program<\/p>\n<pre class=\"brush: cpp; gutter: false; title: parent.cpp; notranslate\" title=\"parent.cpp\">\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/wait.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n  pid_t pid = fork();\r\n\r\n  if (pid == -1) {\r\n    \/* Couldn&#039;t fork *\/\r\n    std::cout &lt;&lt; &quot;Couldn&#039;t fork&quot; &lt;&lt; std::endl;\r\n  } else if (pid == 0) {\r\n    \/* Child process *\/\r\n    execlp(&quot;.\/child.out&quot;, &quot;child.out&quot;, NULL);\r\n  } else {\r\n    \/* Parent process *\/\r\n    std::cout &lt;&lt; &quot;I&#039;m the parent&quot; &lt;&lt; std::endl;\r\n    waitpid(pid, NULL, 0);\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n<p>In the past fork() would do straight away a copy of the memory of the parent process. That makes no sence at all if you call exec(&#8230;) afterwards. These days it only starts to copy that memory as soon as it gets modified.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows You always have to create a totally new process via a CreateProcess(&#8230;) call. Linux You can either just copy<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[65,66],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7150"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7150"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7150\/revisions"}],"predecessor-version":[{"id":16960,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7150\/revisions\/16960"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7150"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}