Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a child process WITHOUT fork()

Is there a way to start a child process without fork(), using execvp() exclusively?

like image 693
gonidelis Avatar asked Jan 11 '19 19:01

gonidelis


2 Answers

The pedantic answer to your question is no. The only system call that creates a new process is fork. The system call underlying execvp (called execve) loads a new program into an existing process, which is a different thing.

Some species of Unix have additional system calls besides fork (e.g. vfork, rfork, clone) that create a new process, but they are only small variations on fork itself, and none of them are part of the POSIX standard that specifies the functionality you can count on on anything that calls itself a Unix.

The slightly more helpful answer is that you might be looking for posix_spawn, which is a library routine wrapping fork and exec into a single operation, but I find it more troublesome to use that correctly than to write my own fork+exec subroutine. YMMV.

like image 99
zwol Avatar answered Sep 21 '22 01:09

zwol


Unlike Windows systems, where creating a new process and executing a new process image happen in a single step, Linux and other UNIX-like systems do them as two distinct steps.

The fork function makes an exact duplicate of the calling process and actually returns twice, once to the parent process and once to the child process. The execvp function (and other functions in the exec family) executes a new process image in the same process, overwriting the existing process image.

You can call execvp without calling fork first. If so, that just means the currently running program goes away and is replaced with the given program. However, fork is the way to create a new process.

like image 27
dbush Avatar answered Sep 21 '22 01:09

dbush