Thread: Processes - wait()

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Processes - wait()

    How is WEXITSTATUS(stat_val) obtaining the value 37? I know that WEXITSTATUS is used to obtain the child's exit code and the exit_code variable is assigned 37 in this program but i don't see how this WEXITSTATUS is working. Is the WEXITSTATUS function written to look through the child process code for an exit_code variable?

    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    int main()
    {
    
      pid_t pid;
      char *message;
      int n;
      int exit_code;
      printf(“fork program starting\n”);
      pid = fork();
      switch(pid)
      {
        case -1:
          perror(“fork failed”);
          exit(1);
        case 0:
          message = “This is the child”;
          n = 5;
          exit_code = 37;
          break;
        default:
          message = “This is the parent”;
          n = 3;
          exit_code = 0;
          break;
      }
      for(; n > 0; n--) {
        puts(message);
        sleep(1);
      }
      //This section of the program waits for the child process to finish.
      if (pid != 0) {
        int stat_val;
        pid_t child_pid;
        child_pid = wait(&stat_val);
        printf(“Child has finished: PID = %d\n”, child_pid);
        if(WIFEXITED(stat_val))
          printf(“Child exited with code %d\n”, WEXITSTATUS(stat_val));
        else
          printf(“Child terminated abnormally\n”);
        }
      exit(exit_code);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The value given in exit/return is returned to the operating system. It's not looking for specific variable names, because quite frankly your executable doesn't have any variable names in it.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The exit_code variable is a member of "struct process_state" and when the child process returns this field is populated if the variable is set.
    But it is baffling that ps->exit_code is being filled in with a variable of the same name w/o any explicit assignments ie totally out of context.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    The exit_code variable is a member of "struct process_state" and when the child process returns this field is populated if the variable is set.
    But it is baffling that ps->exit_code is being filled in with a variable of the same name w/o any explicit assignments ie totally out of context.
    What about "int exit_code" and "exit(exit_code)"?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by tabstop View Post
    What about "int exit_code" and "exit(exit_code)"?
    That's precisely my dilemma!
    "exit_code" is just another variable. It is not part of "struct process_state" a struct that has not been defined anywhere. So when the child returns it sets "exit_code" to 37 and the macro WEXITSTATUS prints it out as the return status of the child. The exit(exit_code) is being used in the parent unrelated to the same name variable in the child.

    [Update] I think my compiler is driving me nuts
    Just a few minutes ago it was capturing the lower 8 bits of the exit_code variable in the "child" case label and now it seems to have a mind of its own and is consistently printing 120. Go figure!

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The exit(exit_code) is being used in the parent unrelated to the same name variable in the child.
    Not sure what you're getting at, but exit(exit_code) is called in the child. That's why the parents reads this as the child's exit code. Unless I am missing something very obvious, this appears to be a non-issue.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    That's precisely my dilemma!
    "exit_code" is just another variable. It is not part of "struct process_state" a struct that has not been defined anywhere. So when the child returns it sets "exit_code" to 37 and the macro WEXITSTATUS prints it out as the return status of the child. The exit(exit_code) is being used in the parent unrelated to the same name variable in the child.

    [Update] I think my compiler is driving me nuts
    Just a few minutes ago it was capturing the lower 8 bits of the exit_code variable in the "child" case label and now it seems to have a mind of its own and is consistently printing 120. Go figure!
    Both the parent and child do exit(exit_code). And I was (and still am) unaware that WEXITSTATUS has anything to do with struct process_state. It might, I suppose, but this is what I've found on t'internet:
    Quote Originally Posted by man wait
    WEXITSTATUS(status)
    returns the exit status of the child. This consists of the least significant 16-8 bits of the status argument that the child specified in a call to exit() or _exit() or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    $ ./wait
    fork program starting
    This is the child
    This is the parent
    This is the parent
    This is the child
    This is the parent
    This is the child
    This is the child
    This is the child
    Child has finished: PID = 1582
    Child exited with code 37
    $

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Never mind I mistook the terminating right brace at the end of the program thinking that it'll only be called in the parent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. anybody with experience using the wait() function?
    By flawildcat in forum C Programming
    Replies: 7
    Last Post: 04-22-2002, 02:43 PM