fork vs exec System call 10 Main Key Differences

Difference between fork() and exec()

In Unix operating systems, process management often involves two key system calls: fork vs exec. While both are essential for creating and managing processes, they serve different purposes. This article delves into the functions of fork() and exec(), exploring how they operate and their specific uses in process management

fork() Process

Create a Process
The fork() system call is used to create a new process. When a process calls fork(), it creates a copy of itself. This means that after fork() is called, two processes are running: the original process (parent) and the new process (child).
pid_t pid = fork();

Important Keypoints

  • Unique Child Process ID
    The child has its unique process ID, and this PID does not match the ID of any existing process group.
  • Process Duplication
    fork() form call The new process (child process) is a copy of the old process (parent process) when fork() is called. (Parent Process and Child Process use the same ID)
  • Process ID
    Once completed, the PID of the child process will be returned in the parent process and 0 will be returned in the child process. On failure, the parent process returns -1 and the child process is not created.
  • Independent Process
    After fork(), both the parent process and the child process run independently. They all implement the same policy but may take different paths as the value returned by fork().
    The child process does not inherit its parent’s memory locks and semaphore adjustments.
    A child process does not inherit asynchronous I/O operations from its parent process and does not inherit asynchronous I/O operations from its parent process.
    Example
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == -1) {
// Error handling
perror("fork");
return 1;
}
if (pid == 0) {
// Child process
printf("This is the child process with PID %d\n", getpid());
} else {
// Parent process
printf("This is the parent process with PID %d, child PID %d\n", getpid(), pid);
}
return 0;
}

exec() Process

It replaces the current process.
The exec() series of operations replaces the current process image with the new process image. This means that exec() completely transforms the process into a new one that runs the new program.

Important Key Points

Process Replacement
exec() does not create a new process. Instead, it replaces the memory location, code, and data of the calling process with the memory location, code, and data of the new program.
No Return any value:
If exec() is successful, it does not return anything. The new program starts executing immediately, and the previous program is terminated.
Different Types of exec
exec() function execl(), execp(), execle(), etc. There are many varieties such as. provide different ways to pass the command-line arguments and environment variables to the new program.

Example

#include <stdio.h>
#include <unistd.h>
int main() {
printf("This is the original program\n");
char *args[] = {"ls", "-l", NULL};
execvp("ls", args);
// This line will not be executed if execvp() is successful
perror("execvp");
return 1;
}

Combining fork vs exec Process.

Many applications use Fork() vs exec()  together. A common pattern is to fork() a new process and then use exec() on the child process to complete a different task. This allows the parent process to continue executing its code while the child process runs the new program.

Combine Example

#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == -1) {
// Error handling
perror("fork");
return 1;
}
if (pid == 0) {
// Child process
char *args[] = {"ls", "-l", NULL};
execvp("ls", args);
// This line will not be executed if execvp() is successful
perror("execvp");
return 1;
} else {
// Parent process
printf("This is the parent process with PID %d\n", getpid());
// Parent can continue to execute its own code
}
return 0;
}

fork() vs exec

  • While fork starts a new process that is a copy of the process that called it, whereas exec replaces the existing process with another (different) process.
  • In the case of fork(), the parent process and the child process are executed simultaneously, whereas Control never returns to the original program unless there is an exec( ) error.
  • fork() It is used to create a new process whereas exec() runs an executable file.
  • fork() returns an integer type value whereas exec() does not return any value.
  • fork() returns three types of integer values, whereas exec() replaces the machine code, data, heap, and stack of the process with the new program

Conclusion:

fork() and exec()  as powerful tools for managing processes on Unix-like systems. They use fork() to create a new process and exec() to execute different tasks in the existing process. Understanding the difference between these two calls and using them together is important in managing the process in the workplace.

Introduction System Call

Visit Online Video Classes

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *