Artemisia
Artemisia

Reputation: 45

exit and wait function in UNIX (or LINUX)

I’m writing a program that simulates an unix-based operating system and I have some questions:

  1. From unix.org

    The wait() function will suspend execution of the calling thread until status information for one of its terminated child processes is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process

Let's imagine there is process A with two child processes B and C. If B and C call the exit function, and then A calls the wait function, which exit status will be retrieved? The one from B or the one from C? Which first and why?

2.When the process is in the waiting state, it doesn't execute its code until for example the status information for one of the terminated child processes is available, is that right? So it can't for example call a fork fuction while waiting, is that correct?

3.Are there any restriction on when a process can normally be killed in UNIX?

3.a. Are users authorized to kill root processes? (all of the root processes at will?)

Upvotes: 2

Views: 2628

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

  1. It is indeterminate whether B or C will be reported first.

  2. While the process is in wait(), it can do nothing else (in a single-threaded process).

  3. No restrictions for the most part. There are non-interruptible system calls, but the system tries to avoid getting processes hung in them.

  4. No; a user can kill their own processes. User root can kill other people's process (in general); but no-one else can kill root's processes.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249123

  1. wait() returns the PID of whatever child process exited. If two have exited, you must call wait() twice and check the returned PIDs. You shouldn't rely on the order.
  2. Correct, the entire purpose wait() (without the WNOHANG option) is to block. So you cannot do anything else, apart from handling signals, in the waiting process.
  3. I'm not sure exactly what you mean here, but I suspect the answer is mostly "no."
  4. Users cannot kill root processes (at least, not without special configuration). Users also cannot kill processes owned by other users.

Upvotes: 2

Related Questions