I do not get why Unix has fork()
for creating a new process. In Win32 API we have CreateProcess()
which creates a new process and loads an executable into its address space, then starts executing from the entry point. However Unix offers fork for creating a new process, and I don't get why would I duplicate my process if I'd like to run another process.
So let me ask these two questions:
fork()
and then exec()
is more efficient, why isn't there a function forkexec(const char *newProc)
since we will call exec()
after fork()
almost in every case?fork()
exist at all?The fork()
call is sufficient. It is also more flexible; it allows you to things like adjust the I/O redirection in the child process, rather than complicating the system call to create the process. With SUID or SGID programs, it allows the child to lose its elevated privileges before executing the other process.
If you want a complex way to create a process, lookup the posix_spawn()
function.
#include <spawn.h> int posix_spawn(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]); int posix_spawnp(pid_t *restrict pid, const char *restrict file, const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]);
The difference is the posix_spawnp()
does a search on PATH for the executable.
There is a whole set of other functions for handling posix_spawn_file_actions_t
and posix_spawnattr_t
types (follow the 'See Also' links at the bottom of the referenced man page).
This is quite a bit more like CreateProcess()
on Windows. For the most part, though, using fork()
followed shortly by exec()
is simpler.
I don't understand what you mean. The child process code will be written by me, so what is the difference between writing
if (fork() == 0)
and putting this code in the beginning of child'smain()
?
Very often, the code you execute is not written by you, so you can't modify what happens in the beginning of the child's process. Think of a shell; if the only programs you run from the shell are those you've written, life is going to be very impoverished.
Quite often, the code you execute will be called from many different places. In particular, think of a shell and a program that will sometimes be executed in a pipeline and sometimes executed without pipes. The called program cannot tell what I/O redirections and fixups it should do; the calling program knows.
If the calling program is running with elevated privileges (SUID or SGID privileges), it is normal to want to turn those 'off' before running another program. Relying on the other program to know what to do is ... foolish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With