-1

I have recently started a course on Operating Systems. I came across this question where I need to figure out what the output is. Here is the code -

int main() {
  int val = 5;
  if(fork()) {
    wait(&val);
  }
  val++;
  printf("%d ", val);
  return val;
}

This has been answered on SO before on this post - Explain this code's working; how the child process returns values and where?
I understand how the return value is returned to val from the child process, which is then incremented and printed in the parent process, and the output according to that logic should in fact be 6 7

But when I tried executing the code myself, I seem to be getting something completely different. I get an output 6 1537. If I remove the increment, I get the output - 6 1280
What this implies is that the value being returned is val*256 and not just val(which is not in accordance with that answer I linked). I have tried looking on the internet for an explanation but I can't seem to find an answer. Would really help if anyone can explain what's happening here.

Community
  • 1
  • 1
Zeokav
  • 1,653
  • 4
  • 14
  • 29
  • 1
    Your code indentation does not resemble what's actually going on. – Jonny Henly Aug 26 '16 at 17:04
  • 1
    You seem to be missing a closing brace (or have one-too-many opening braces). – WhozCraig Aug 26 '16 at 17:07
  • Yes, I forgot to include a closing brace there. – Zeokav Aug 26 '16 at 18:39
  • See also: [Exit codes bigger than 255 — possible?](http://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible), and for POSIX systems, [`exit()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html) and [`waitpid()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html), and my comments [1](http://stackoverflow.com/questions/39171352/why-does-my-output-differ-for-this-code-snippet#comment65690331_39171518) and [2](http://stackoverflow.com/questions/39171352/why-does-my-output-differ-for-this-code-snippet#comment65690729_39171518) below. – Jonathan Leffler Aug 26 '16 at 20:49

1 Answers1

1

The wait() system call puts more than just the return value from the child process in the returned value from wait(). In fact just the lower 8 bits are the returned value, the higher bits are status flags, indicating among other things how the child exited (by a signal for example). To get the return value from the child use:

val = WEXITSTATUS(val);

immediately after the wait().

Steve Baker
  • 4,323
  • 1
  • 20
  • 15
  • I did come across this. So basically the value returned will without using WEXITSTATUS will always be shifted by 8 bits? – Zeokav Aug 26 '16 at 18:42
  • Classically, the high-order 8 bits of the 16-bit status are the exit status, and the low-order 8 bits are an indication of whether the process exited normally (in which case the high-order bits are meaningful) or exited as a result of a signal (in which case you can determine which signal caused the death, and whether a core dump was created, but the high-order bits contain no useful information). This is the reverse of what you seem to be describing. – Jonathan Leffler Aug 26 '16 at 20:15
  • Having said that, maybe the confusion is between what is passed to [`exit()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html) and what is returned by [`waitpid()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/waitpid.html) or `wait()`. The low-order 8 bits of the value passed to `exit()` are made available to the parent process. However, the value returned by `waitpid()` or `wait()` normally has those exit bits in the bits 15-8 while bits 7-0 contain the signal information (though POSIX does _not_ mandate that layout — but it is normally observed even so). – Jonathan Leffler Aug 26 '16 at 20:31