{"id":1363,"date":"2013-10-08T21:22:59","date_gmt":"2013-10-08T21:22:59","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1363"},"modified":"2020-07-24T14:56:43","modified_gmt":"2020-07-24T14:56:43","slug":"forking-a-new-thread","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/threads\/forking-a-new-thread","title":{"rendered":"Forking A New Thread"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Call On A Background Thread <\/h4>\n\n\n\n<p>fork creates a clone of the current process on a new thread. Use with execlp to trigger other processes, e.g. open another application <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tint pid;\n\tpid = fork();\n\tif (pid == 0)\n\t{\n\t\t\/\/Child thread\n\t\tprintf(\"I am the child\\n\");\n\n\t\t\/\/Do something\n\t\texeclp(\"\/usr\/bin\/omxplayer\", \" \", \"\/home\/pi\/projects\/game\/audio\/alpha.wav\", NULL);\t\t\/\/Execute file: file, arguments (1 or more strings followed by NULL - omxplayer has &#91;OPTIONS] &#91;FILE] hence the blank string)\n\n\t\t\/\/Exit fork\n\t\t_exit(0);\t\t\/\/Don't forget to exit!\n\t}\n\telse\n\t{\n\t\t\/\/Parent thread\n\t\tprintf(\"I am the parent\\n\");\n\t\twait();\t\t\/\/Optional if you want to wait on the child thread, remove if not\n\t} <\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Do something that might take a while without waiting example<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpid = fork();\n\tif (pid == 0)\n\t{\n\t\t\/\/This is the child thread\n\t\tsystem(\"sudo ifdown wlan1; sudo ifup wlan1 &amp;\");\t\t\t\/\/'&amp;' means do in background\n\t\t_exit(0);\n\t}<\/code><\/pre>\n\n\n\n<p><strong>IMPORTANT NOTE:<\/strong> <\/p>\n\n\n\n<p>The child process once it dies remains in the Linux process table marked as&nbsp;&lt;defunct&gt;. You&#8217;ll get one added every time you use this fork technique. They remain so the OS can give the parent process a handle to the child exit status&nbsp;and will only be removed when the main process dies. &nbsp;Whilst sometimes considered annoying they consume no resources and are not typically an issue unless there are a lots of them <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Call On A Background Thread fork creates a clone of the current process on a new thread. Use with execlp to trigger other processes, e.g. open another application Do something that might take a while without waiting example IMPORTANT NOTE: The child process once it dies remains in the Linux process table marked as&nbsp;&lt;defunct&gt;. You&#8217;ll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-1363","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1363"}],"version-history":[{"count":8,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1363\/revisions"}],"predecessor-version":[{"id":3354,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1363\/revisions\/3354"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}