Search code examples
coperating-systemforkwait

What happens to status variable in various situations of wait(&status) system call?


(1) Assume, that main process calls fork(), and it returns -1, so process have no children. After this call in main process called wait(&status), and according to manuals it will return -1. Will 'status' variable stay unchanged, as before call?

(2) Having this situation from (1), if right after wait(&status) line, there is a line status = WEXITSTATUS(status), will 'status' variable change then?

int status = 0;
  if (fork()!=0) {
  wait(&status);
  status = WEXITSTATUS(status);
}

Solution

  • 1: undefined behaviour. There is no guarantee that status is unchanged if waitpid failes

    2: yes, status = WEXITSTATUS(status); will replace the value in status. (Specifically, WEXITSTATUS should mask away all upper bits of the value)