{"id":5195,"date":"2022-02-12T00:01:54","date_gmt":"2022-02-12T08:01:54","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5195"},"modified":"2022-02-05T10:32:39","modified_gmt":"2022-02-05T18:32:39","slug":"capturing-a-programs-return-value-in-linux","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5195","title":{"rendered":"Capturing a Program&#8217;s Return Value in Linux"},"content":{"rendered":"<p>As I wrote in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5179\">last week&#8217;s Lesson<\/a>, the <em>system()<\/em> function can&#8217;t be used in Linux to obtain a program&#8217;s return value. Instead, you must use one of the <em>execl()<\/em> family of functions. Further, this function must be spawned as a child process. This task involves using the <em>fork()<\/em> and <em>wait()<\/em> functions.<br \/>\n<!--more--><br \/>\nThe <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1395\"><em>fork()<\/em> function<\/a> and the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1410\">the <em>execl()<\/em> function<\/a> have already been covered on this blog. The <em>wait()<\/em> function is defined in the <code>sys\/wait.h<\/code> header file. Here is the <em>man<\/em> page format:<\/p>\n<p><code>pid_t wait(int *stat_loc);<\/code><\/p>\n<p>The function consumes the address of an integer value <code>*stat_loc<\/code> and returns a <em>pid_t<\/em> value, a process ID.<\/p>\n<p>What the <em>wait()<\/em> function does is to wait (duh) for a child process to complete. Information about the child process is obtained from the integer variable referenced as the function&#8217;s argument. Various macros, defined in <code>sys\/wait.h<\/code>, assist you in divining the wisdom packed into the referenced integer.<\/p>\n<p>To obtain the return value from another program run as a child process your code must fork. The child fork (pid zero) contains the <em>execl()<\/em> function to &#8220;execute and leave&#8221; another program. The parent fork contains the <em>wait()<\/em> function, which suspends execution until the child process completes its task.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_12-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_02_12-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;sys\/wait.h&gt;\r\n\r\nint main()\r\n{\r\n    pid_t p;\r\n    int stat;\r\n\r\n    <span class=\"comments\">\/* split this program into two processes *\/<\/span>\r\n    p = fork();\r\n    \r\n    if( p==0 )        <span class=\"comments\">\/* child process is running *\/<\/span>\r\n    {\r\n        execl(\"twelve\",NULL);\r\n    }\r\n    else            <span class=\"comments\">\/* parent process is running *\/<\/span>\r\n    {\r\n        wait(&amp;stat);                <span class=\"comments\">\/* wait for the child *\/<\/span>\r\n        if( WIFEXITED(stat))        <span class=\"comments\">\/* examine exit status *\/<\/span>\r\n            printf(\"Child process exit status = %d\\n\",WEXITSTATUS(stat));\r\n        else\r\n            printf(\"Unable to locate exit status\\n\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>fork()<\/em> function at Line 11 spawns a second version of the program. The process ID is returned in variable <code>p<\/code>.<\/p>\n<p>The <em>if<\/em> decision at Line 13 uses variable <code>p<\/code> to determine whether the child or parent is running. For <code>p==0<\/code>, the child is running and the <em>twelve<\/em> program is executed. It&#8217;s assumed that this program is in the same directory as the current program. Refer to last week&#8217;s Lesson for the <em>twelve<\/em> program&#8217;s code (<a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">Github<\/a>).<\/p>\n<p>The parent process is handled by the <em>else<\/em> part of the decision. The <em>wait()<\/em> function pauses execution until the child exits. The <em>wait()<\/em> function&#8217;s argument, <code>stat<\/code>, stores details about the child process&#8217;s termination.<\/p>\n<p>The <code>WIFEXITED<\/code> macro examines the <code>stat<\/code> variable and returns TRUE when the child process has an exit status. Macro <code>WEXITSTATUS<\/code> extracts the chid program&#8217;s exit status from the <code>stat<\/code> variable, which is output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>I return the value 12 to the operating system<br \/>\nChild process exit status = 12<\/code><\/p>\n<p>And it works.<\/p>\n<p>This method is the only way I&#8217;ve found (so far) to fetch and identify a return value from another program run in Linux: You must spawn a child process, wait for it to finish, then examine the <em>wait()<\/em> function&#8217;s argument. The setup is clunkier than using the <em>system()<\/em> function in Windows, but it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows users have it easy. For Linux, you must code special magic to obtain a child program&#8217;s return value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5195\">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":[32,33,30,29,34,27,28,31],"class_list":["post-5195","post","type-post","status-publish","format-standard","hentry","category-main","tag-execl","tag-fork","tag-linux","tag-operating-system-return-value","tag-process","tag-return-value","tag-system-call","tag-wait-function"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5195","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=5195"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5195\/revisions"}],"predecessor-version":[{"id":5206,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5195\/revisions\/5206"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}