Exam 1, Question 3 Solution

Process Control (20 pts.)

If fork is called once and fails, how many times does it return? Explain your answer.

Once. It returns in the calling process(returning a negative value :-1).



If fork is called once and succeeds, how many times does it return? Explain your answer.

Twice. It returns once in the calling process(with a return value of the pid of the child created) and once in the child(with a return value of 0).



If exit is called once, how many times does it return? Explain your answer.

Never. When exit has completed, the process is terminated and cannot be returned to.

(Partial credit would also be given for an answer of "once" if you explained that the exit value is saved and given (in encoded form) to the parent process when it does a wait or waitpid. This is, strictly speaking, not a return, since it is not executing the next bit of the same code.)



If an exec family function is called once and fails, how many times does it return? Explain your answer.

Once. It returns in the calling process(returning a negative value: -1)



If an exec family function is called once and succeeds, how many times does it return? Explain your answer.

Never. When an exec succeeds, the code of the calling process is replaced by the code referenced in the exec . The new code is executed from the beginning.

(Partial credit would also be given for an answer of "once" if you explained that the new code is negun with the parameters from the exec passed in through argv and envp. This is, strictly speaking, not a return, since it is not executing the next bit of the same code.)