Upon completion of this lesson, you will be able to:
In Unix-like operating systems, including Linux and MacOS, processes are created using a system call called fork()
followed by the exec()
family of functions or other related functions. Here’s an overview of how processes are created in Unix:
fork()
system call is used to create a new process that is a copy of the current (parent) process. The new process is often referred to as the child process.fork()
is called, a nearly identical copy of the parent process is created. This includes copying the program code, data, stack, and heap memory. Both the parent and child processes start executing at the same point in the code, typically just after the fork()
call.fork()
system call returns twice, once in the parent process and once in the child process. In the parent process, it returns the child’s process ID (PID), while in the child process, it returns 0. This allows the parent and child processes to distinguish themselves from each other.fork()
, it’s common to replace the child’s process image with a new program using the exec()
family of functions (e.g., execv()
, execvp()
, execve()
).exec()
functions load a new program into the child process’s memory space, replacing the original program inherited from the parent process. The new program can be specified by providing its file path as an argument to the exec()
function.exec()
function is called successfully, the child process becomes a different program entirely and continues execution from the beginning of the new program. The child process can pass data to the new program using command-line arguments or other inter-process communication mechanisms.Here’s a simplified example in C code illustrating the process creation in Unix using fork()
and exec()
:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t child_pid;
// Create a new process (child)
child_pid = fork();
if (child_pid == -1) {
perror("fork() failed");
return 1;
}
if (child_pid == 0) {
// This code is executed by the child process
// Replace the child process image with a new program
execl("/bin/ls", "ls", "-l", NULL); // Example: List files in the current directory
perror("execl() failed"); // Executing this line only if execl() fails
return 1;
} else {
// This code is executed by the parent process
// Wait for the child process to complete
wait(NULL);
printf("Child process completed.\n");
}
return 0;
}
In this example, the parent process forks a child process and then waits for the child process to finish executing the ls
command using execl()
. The exec()
function replaces the child’s process image with the ls
program, and the child starts executing ls
.
In Unix-like operating systems, processes are created using the fork()
system call to duplicate the current process, followed by the exec()
family of functions to replace the child process’s program image with a new program, allowing for the execution of different tasks in separate processes.
None.
None collected yet. Let us know.