Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why fork and exec are kept 2 seperate calls

I understand the differences between fork, vfork, exec, execv, execp. So pls dont rant about it. My question is about the design of the unix process creation. Why did the designers think of creating 2 seperate calls ( fork and exec ) instead of keeping it one tight call ( spawn ). Was good API design a reason so that developers had more control over process creation? Is it because of performance reason, so that we could delay allocating process table and other kernel structures to the child till either copy-on-write or copy-on-access?

like image 835
loneranger Avatar asked Feb 23 '11 12:02

loneranger


People also ask

How is fork () and exec () calls different from each other?

fork vs execfork starts a new process which is a copy of the one that calls it, while exec replaces the current process image with another (different) one. Both parent and child processes are executed simultaneously in case of fork() while Control never returns to the original program unless there is an exec() error.

Is it possible to call exec () without fork ()?

A program that calls exec() without fork() is chain loading, overlaying its process with a different program image. There is a whole subculture of chain loading utilities that do particular things to process state and then execute another program to run with that revised process state.

Why are fork and exec used together?

fork() creates a new process, which is a copy of the parent process. So if you did only fork() , you would have two identical processes running. Therefore in order to replace the forked process with another code, you need to perform exec() which replaces the currently running process with the specified executable file.

How are fork () and exec () system calls different from normal system function calls in terms of their call return behavior?

This leads to more efficient memory usage. Question 7: [6 points] In terms of call-return behavior, how are the fork() and exec() system calls different from other system/function calls? Ans: fork(), if successful, returns twice – once in the parent and once in the child. Exec() never returns, if successful.


2 Answers

The main reason is likely that the separation of the fork() and exec() steps allows arbitrary setup of the child environment to be done using other system calls. For example, you can:

  • Set up an arbitrary set of open file descriptors;
  • Alter the signal mask;
  • Set the current working directory;
  • Set the process group and/or session;
  • Set the user, group and supplementary groups;
  • Set hard and soft resource limits;

...and many more besides. If you were to combine these calls into a single spawn() call, it would have to have a very complex interface, to be able to encode all of these possible changes to the child's environment - and if you ever added a new setting, the interface would need to be changed. On the other hand, separate fork() and exec() steps allow you to use the ordinary system calls (open(), close(), dup(), fcntl(), ...) to manipulate the child's environment prior to the exec(). New functionality (eg. capset()) is easily supported.

like image 189
caf Avatar answered Nov 25 '22 10:11

caf


fork and exec do completely different things.

  • fork() - duplicates a process
  • exec() - replaces a process

There's plenty of reasons to use one without the other. You can fork off child processes that perform tasks on behalf of your controlling parent app e.g., pretty common in the unix world. And you can e.g. setup the preconditions for some other quirky application and then exec it from your launcher application without ever using fork.

like image 22
Erik Avatar answered Nov 25 '22 10:11

Erik