title: process exit status using system calls
body: <p>Ok i want to print the return status of my child process once it finishes its execution.</p>
  
  <p>the code is to be able to basically mimic the <code>system()</code> call using <code>execl()</code>.</p>
  
  <p>My problem is that the line <code>printf("The exit status of the process is : %d",status);</code> in my main function does not even execute, it waits for 5 seconds because of the <code>sleep(5)</code> and always prints <code>"Alarm clock"</code> on the terminal when i run it. instead i want it to return the exit status of the child which is returned form my system function.</p>
  
  <p>am i doing something wrong here ?</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;signal.h&gt;
  using namespace std;
  
  void wakeup() {};
  
  int sleep(int timer)
  {
      struct sigaction action;
      //action.sa_handler = wakeup;
      action.sa_flags=0;
      sigemptyset(&amp;action.sa_mask);
  
      if (sigaction(SIGALRM, &amp;action, 0)==-1)
      {
          perror("sigaction");
          return -1;
      }
      (void)alarm(timer);
      (void)pause();
      return 0;
  }
  
  int system(const char *cmd)
  {
          pid_t pid;
          int status;
          pid = fork();
          if (pid==0) //child
          {
              execl("/bin/sh","sh","-c",cmd,0);
              perror("execl");
              exit(errno);
          }
          /*if(sleep(5)==-1)
          {
              perror("sigaction");
          }*/
          sleep(5);
          if(waitpid(pid,&amp;status,0)==pid &amp;&amp; WIFEXITED(status))
              return WEXITSTATUS(status);
  
  
          return -1;
  }
  int main(int argc,char *argv[])
  {
          int status;
          if(argc!=2)
          {
            cout&lt;&lt;"Usage Error\nCorrect usage:./a.out &lt;cmd&gt;\n";
            exit(0);
          }
          else
          {
              status=system(argv[1]);
              if(status!=0)
              {
                  cout&lt;&lt;"The exit status of the process is : %d"&lt;&lt;status;
              }
          }
          return 0;
  }
  </code></pre>
  
  <p><strong>Solution:</strong>
  Thanks to Dave S for helping me finish this assignment.</p>
  
  <p>the original assignment question was:
  <code>Write a program which should accept one command(like date/time/find..or
  any user created executable file) and run it by its child process, if the child
  process takes more than five seconds to run the command,parent should
  terminate the child process, else if the child terminates before 5 seconds-print
  exit status of the child.</code></p>
  
  <p>Finished code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;signal.h&gt;
  using namespace std;
  bool timeup=false;
  void wakeup(int signum) {
      if(signum==SIGALRM)
      {
          timeup=true;
      }
  };
  
  int sleeper(int timer)
  {
      struct sigaction action;
      action.sa_handler = wakeup;
      action.sa_flags=0;
      sigemptyset(&amp;action.sa_mask);
  
      if (sigaction(SIGALRM, &amp;action, 0)==-1)
      {
          perror("sigaction");
          return -1;
      }
      (void)alarm(timer);
      //(void)pause();
      return 0;
  }
  
  int system(pid_t *pid,const char *cmd)
  {
  
          int status;
          *pid = fork();
          if (*pid==0)    //child
          {
          sleep(6); // remove or modify value to change how long the process will minimally take to execute
              execl("/bin/sh","sh","-c",cmd,0);
              perror("execl");
              exit(errno);
          }
  
          return 0;
  }
  int main(int argc,char *argv[])
  {
          int status=-999;
      pid_t pid;
          if(argc!=2)
          {
            cout&lt;&lt;"Usage Error\nCorrect usage:./a.out &lt;cmd&gt;\n";
            exit(0);
          }
          else
          {
  
          system(&amp;pid,argv[1]);
  
          sleeper(5);// the timer for 5 seconds
  
          if(waitpid(pid,&amp;status,0)==pid &amp;&amp; WIFEXITED(status))
                  status = WEXITSTATUS(status);
          if(!timeup)
                  cout&lt;&lt;"The exit status of the process is :"&lt;&lt;status&lt;&lt;"\n";
          else
          {
          cout&lt;&lt;"Took more that 5 seconds..Stopping\n";
          kill(pid, SIGTERM);
          //exit(0);
          }
  
          }
          return 0;
  }
  </code></pre>