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 :
- 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.
Detailed article on fork system call
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.
parent process, pid = 11523 child process, pid = 14188 Program execution successful
Linux man pages
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- exec family of functions in C
- fork() in C
- Fork() Bomb
- searching in fork()
- sorting in fork()
- C vs BASH Fork bomb
- Fork() - Practice questions
- fork() and Binary Tree
- Creating multiple process using fork()
- C program to demonstrate fork() and pipe()
- Factorial calculation using fork() in C for Linux
- fork() to execute processes from bottom to up using wait()
- Creating child process using fork() in Python
- Calculation in parent and child process using fork()
- fork() and memory shared b/w processes created using it
- Create n-child process from same parent process using fork() in C
- Difference between PCI and PCI-X
- Difference between GSM and LTE
- Difference between PIP and PCP
- Difference between URL and URI