1

I am teaching an Operating Systems course and trying to wrap my mind around the fork/execve technique for creating new processes.

My current understanding is that a fork make a complete copy of the old process, establishes a new PID and parent/child relationship, but otherwise does very little else.

On the other hand, after the child process is created, it runs execve to replace most of its memory with the new process. For example, the program code, stack, and heap are completely replaced and started from scratch as a new program.

But not everything is replaced in the new process. The child process inherits file descriptors (which allows pipes to be set up before the execve), the process ID (PID) and user ID (UID) and some permissions (man page).

I imagine the full list of properties that are NOT replaced by an execve call is quite long, but are there any other key properties like the ones I mentioned above that I'm missing?

4

Since we’re discussing Linux specifically (at least, I take it that’s what you want since you used the tag), the fork and execve manpages are the appropriate references; they list all the attributes which aren’t preserved. Most of this behaviour is specified by POSIX, but there are some Linux specificities.

The man pages don’t list attributes which are preserved, focusing instead on those which aren’t:

All process attributes are preserved during an execve(), except the following:

etc.

I won’t try to answer your question by listing all the attributes which are preserved. However I will point out one key property which is preserved, and which you haven’t listed: ignored and default signals are preserved across execve. This means that a parent can ignore a signal (at least, signals that can be ignored) and that behaviour will be propagated to any children. This is what allows nohup to work.

You can find a complete list of process attributes, with an explanation of what happens to them on exec() or fork(), in section 28.4 of The Linux Programming Interface.

  • I like the comment at the end of the execve man page you recommended: 'One sometimes sees execve() ... described as "executing a new process" ... This is a highly misleading description: there is no new process; many attributes of the calling process remain unchanged (in particular, its PID). All that execve(2) does is arrange for an existing process(the calling process) to execute a new program' An example of how the docs can be very newbie-friendly! – Josiah Yoder Sep 24 '18 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.