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()
fork()
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 :
- The child has its own unique process ID, and this PID does not match the ID of any existing process group.
- The child’s parent process ID is the same as the parent’s process ID.
- The child does not inherit its parent’s memory locks and semaphore adjustments.
- The child does not inherit outstanding asynchronous I/O operations from its parent nor does it inherit any asynchronous I/O contexts from its parent.
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
- fork 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.
C
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1){
printf("can't fork, error occurred\n");
exit(EXIT_FAILURE);
}
else if (pid == 0){
printf("child process, pid = %u\n",getpid());
printf("parent of child process, pid = %u\n",getppid());
char * argv_list[] = {"ls","-lart","/home",NULL};
execv("ls",argv_list);
exit(0);
}
else{
printf("Parent Of parent process, pid = %u\n",getppid());
printf("parent process, pid = %u\n",getpid());
if (waitpid(pid, &status;, 0) > 0) {
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successful\n");
else if (WIFEXITED(status) && WEXITSTATUS(status)) {
if (WEXITSTATUS(status) == 127) {
printf("execv failed\n");
}
else
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else {
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
|
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. |
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
08 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...