Understanding how processes are created is crucial for navigating the Linux operating system. A foundational understanding of process generation is essential for software engineers and system administrators operating within Linux environments. This exposition will dissect the functionality of these system calls, elucidating their distinct mechanisms for initiating child processes from parent processes. Through illustrative code examples, we will examine the resulting process hierarchies and the critical distinctions in their memory management and execution characteristics. This exploration aims to provide a clear and concise comprehension of these core Linux system functionalities.
A new process can be created by the fork() system call. The new process consists of a copy of the address space of the original process. fork() creates new process from existing process. Existing process is called the parent process and the process is created newly is called child process. The function is called from parent process. Both the parent and the child processes continue execution at the instruction after the fork(), the return code for the fork() is zero for the new process, whereas the process identifier of the child is returned to the parent.
Fork() system call is situated in <sys/types.h> library.
System call getpid() returns the Process ID of the current process and getppid() returns the process ID of the current process’s parent process.
Name:-fork()-Creating a chilled process
Synopsis:
#include<sys/types.h>
#include<unistd.h>
int main()
{
Pid_t p;
p=fork();
return 0;
}
Let’s take an example how to create child process using fork() system call.
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> // <– Important
#include <stdio.h>
int main() {
pid_t child_pid;
child_pid = fork(); // Create a new child process
if (child_pid < 0) {
printf(“fork failed\n”);
return 1;
}
else if (child_pid == 0) {
// Child process
printf(“Child process successfully created!\n”);
printf(“Child_PID = %d, Parent_PID = %d\n”, getpid(), getppid());
}
else {
// Parent process
wait(NULL); // Wait for child to finish
printf(“Parent process resumed after child terminated!\n”);
printf(“Parent_PID = %d, (Original Parent_PID) = %d\n”, getpid(), getppid());
}
return 0;
}
Here, getppid() in the child process returns the same value as getpid() in the parent process. pid_t is a data type which represents the process ID. It is created for process identification. Each process has a unique ID number. Next, we call the system call fork() which will create a new process from calling process. Parent process is the calling function and a new process is a child process. The system call fork() is returns zero or positive value if the process is successfully created.
Name:-vfork()-create a child process and block parent
Synopsis:
#include<sys/types.h>
#include<unistd.h>
int main()
{
Pid_t p;
p=vfork();
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main() {
pid_t pid;
pid = vfork(); // create child using vfork
if (pid < 0) {
printf(“vfork failed\n”);
exit(1);
}
else if (pid == 0) {
// Child process
printf(“Child process!\n”);
printf(“Child PID: %d, Parent PID: %d\n”, getpid(), getppid());
_exit(0); // Important: use _exit() after vfork
}
else {
// Parent process
printf(“Parent process!\n”);
printf(“Parent PID: %d\n”, getpid());
}
return 0;
}
Here, in the program, we use the system call vfork() to create a child process.
pid_t is the data type used to represent a process ID. Every process has a unique ID number assigned by the operating system for identification.
When vfork() is called, it creates a child process from the parent (calling) process.
In vfork(), the child process shares the address space with the parent process until it calls _exit() or an exec() family function.
The vfork() system call returns:
An important point in vfork() is that the parent process is suspended until the child process either exits or loads a new program.
Because of shared memory, the child should not modify variables in the parent.
In the child process, we use getpid() to get its own process ID and getppid() to get its parent process ID.
Here, getppid() in the child returns the same value as getpid() in the parent.
After printing its process details, the child must immediately call _exit(0), so that the parent process can continue safely.
Then the parent process prints its own process ID after the child terminates.
Thus, vfork() is a faster alternative to fork(), but should be used carefully because of shared memory.
Indian Institute of Embedded Systems – IIES