Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about fork and execve system call

Tags:

linux

unix

It is said that fork system call creates a clone of the calling process, and then (usually) the child process issues execve system call to change its image and running a new process. Why this two-step?

BTW, what does execve stand for?

like image 913
Pwn Avatar asked May 29 '09 14:05

Pwn


People also ask

What is execve system call?

execve() executes the program referred to by pathname. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

What is fork system call?

Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What is difference between fork and exec () system call?

So the main difference between fork() and exec() is that fork starts new process which is a copy of the main process. the exec() replaces the current process image with new one, Both parent and child processes are executed simultaneously.

Does Execve fork?

exec() replaces the current process with a new one. It has nothing to do with fork(), except that an exec() often follows fork() when what's wanted is to launch a different child process, rather than replace the current one.


2 Answers

The reason for the two-step is flexibility. Between the two steps you can modify the context of the child process that the newly exec'ed program will inherit.

Some things you may want to change are:

  • File descriptors
  • User/group ID
  • Process group and session IDs
  • Current directory
  • Resource limits
  • Scheduling priority and affinity
  • File creation mask (umask)

If you did not split up fork and exec and instead had a single spawn-like system call, it would need to take arguments for each of these process attributes if you wanted them set differently in a child process. For example, see the argument list to CreateProcess in the Windows API.

With fork/exec, you change whatever inheritable process attributes you want to in the child before you exec the new program.

Setting up file descriptors is one of the more common things to change in a child's process context. If you want to capture the output of a program, you will typically create a pipe in the parent with the pipe(2) system call, and after fork(2)ing, you will close the write end in the parent process and close the read end in the child process before calling execve(2). (You'll also use dup(2) to set the child end of the pipe to be file descriptor 1 (stdout)). This would either be impossible or restrictive in a single system call.

like image 167
camh Avatar answered Oct 01 '22 17:10

camh


  • exec: Execute new process
  • v : use array of arguments
  • e: Specify as well the environment

Other variations of exec abound:

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
  • l: list arg on function
  • p: use $PATH to locate executable file
like image 15
0x6adb015 Avatar answered Oct 01 '22 18:10

0x6adb015