![]() |
VOOZH | about |
Every application(program) comes into execution through means of process, process is a running instance of a program. Processes are created through different system calls, most popular are fork() and exec()
pid_t pid = fork();
fork() creates a new process by duplicating the calling process, The new process, referred to as child, is an exact duplicate of the calling process, referred to as parent, except for the following :
Return value of fork() On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
exec()
The exec() family of functions replaces the current process image with a new process image. It loads the program into the current process space and runs it from the entry point. The exec() family consists of following functions, I have implemented execv() in following C program, you can try rest as an exercise
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[]); int execvpe(const char *file, char *const argv[], char *const envp[]);
fork vs exec
Output:
parent process, pid = 11523 child process, pid = 14188 Program execution successful
Let us see the differences in a tabular form -:
| fork() | exec() | |
| 1. | It is a system call in the C programming language | It is a system call of operating system |
| 2. | It is used to create a new process | exec() runs an executable file |
| 3. | Its return value is an integer type | It does not creates new process |
| 4. | It does not takes any parameters. | Here the Process identifier does not changes |
| 5. | It can return three types of integer values | In exec() the machine code, data, heap, and stack of the process are replaced by the new program. |