so I think that wait() waits until a child terminates (either through signal or exit). So let's say the father makes a fork() and then just wait(). The child uses exec() to execute another program. Well, and now what's happening? The exec call never comes back (in case it was succesfull). So how long does the father waits now?

In this particular case I think that the father remains until cat is forced to exit, but I'm not sure.

Any suggestions are welcome :)

Thanks
TurboToJo

03-18-2008

xuftugulus

I think the father waits indefinitely....

03-18-2008

matsp

wait() will wait until the process terminates [unless you abort the process itself] - whether that happens in milliseconds or years later doesn't make any difference. wait() doesn't stop waiting.

You should really specify an integer pointer (address of an integer) to store the status for wait() in.

--
Mats

03-18-2008

DJTurboToJo

Ok, so the father waits with wait() until its son is done with the exec thing? Isn't that weird as exec doesnt return anything. So how come that the father knows when its son is terminated (that is the exec process is done).

Thank you guys
TurboToJo

03-18-2008

matsp

The way that wait() works it that there is a mechanism in the kernel that knows when this process finishes, and that's when the status variable in wait() gets set, and the wait() finishes.

exec() can not return [assuming it's a success] since it loads a different piece of code, which overwrites where exec() would come back to. So there is "nowhere to go back to" from exec().

In other OS's the mechanism of creating a new process is slightly different, the fork() and exec() are often combined into a "CreateProcess()" or similar function, that creates another process and loads the executable. In this case, the return value from CreateProcess() is one of three things:
1. Failure to create a new process (matches fork()).
2. Failure to load the executable (matches exec()).
3. The result of the execution itself (matches wait()).

Often you can also issue a flag to CreateProcess() to say "Don't wait for the process to finish".

There are some good reasons to have the "create a new process" split into multiple separate function. There are other reasons to make it one function. Which is preferred depends on the implementor of the OS.

--
Mats

03-18-2008

DJTurboToJo

Solution

Ok, so I checked the exit status and that's what I found (please correct me if I'm wrong):

the father waits with this function wait() until one child terminates. If this child is doing an exec, the father waits until this exec procedure is done.