Extra Credit: Returning a status code from main()

20 points

Here is an extra credit assignment for those seeking a further challenge. Only attempt this if you are done with Parts 1 and 2.

If you run the following xv6 program :

#include "types.h"
#include "user.h"

int main() {
printf(1, "hello world!");
return 0;

It will actually crash with a message like this:

pid 3 mytest: trap 14 err 5 on cpu 1 eip 0xffffffff addr 0xffffffff--kill proc

xv6 programs are supposed to call the exit() syscall before finishing naturally.  That's kind of silly and it's different than standard C.  Your extra credit task is to fix this. 

Your Tasks

  1. Allow the program above to run to completion without printing that crash message.
  2. Change the exit() syscall to take an optional integer parameter (a return code), similar to the number being returned by main().  Zero means success and anything else signifies an error.  If exit is called without a parameter, then assume that the user meant exit(0).  So, you shouldn't have to change all the existing user code that just calls exit().
  3. Whenever a process terminates (normally or even if it's killed by the kernel due to a segfault), print to the terminal: "PROCESS [pid] EXITED WITH STATUS CODE [status_code].  The status_code should be either:

Note that a real unix system would make the return status available to parent process.  When the program is run from a shell like bash or tcsh, the $? variable stores the return code of the last process.  However, xv6's primitive shell does not have any notion of variables, and that's why we're just printing out the return status.