When killing a process, can I use its' `eax` to save exit status?

509 Views Asked by At

I'm taking OS class and we need to extend XV6's exit() to support exit status, thus we're writing exit2(int).

I thought of two candidates for the purpose of saving the exit status of the killed process.

  1. The first option was to add a variable to struct proc for exit status. My problem with this solution is that it involves changing a fundamental structure of the OS for a very little cause, and moreover it means I'm gonna have a garbage integer for every process, which is not the best idea.

  2. Second idea was to save the exit status in a trap frame register of the killed process, but then I have something inside me saying that I should not trust the value of eax in the killed process.

What's a better idea? (or less worse)

2

There are 2 best solutions below

0
On BEST ANSWER

The definite answer is no, the purpose of eax is to be used as a return value from procedures. In some kernel implementations (I know it works on rev 6 of xv6) this trick MIGHT work but one can not rely on eax after a kill() as you never know when the kernel will actually kill the process.

1
On

Option 1 is the easiest and safest.

Option 2, strange as it might sound, can work in xv6. No memory is freed when a process exit()s. Only when the parent invokes wait() the child process memory is freed, including the child kernel stack.

Since no one is going to use the kernel stack after the child goes ZOMBIE, the exit status can be stored anywhere on the kernel stack.

So you can store the exist status at proc->kstack-4 and fetch it in the wait routine from p->kstack-4. Just make sure to fetch it BEFORE the kfree(p->kstack)!