Home >Backend Development >C++ >In C language, what is the difference between fork() and exec()?

In C language, what is the difference between fork() and exec()?

王林
王林forward
2023-09-13 11:01:021455browse

In C language, what is the difference between fork() and exec()?

Here, we will see the effect of fork() and exec() system calls in C language. fork is used to create a new process by copying the calling process. The new process is a child process. Please refer to the following properties.

  • The child process has its own unique process ID.
  • The parent process ID of the child process is the same as the process ID of the calling process.
  • The child process does not inherit the memory locks and semaphores of the parent process.

fork() returns the PID of the child process. If the value is non-zero, it is the ID of the parent process, if the value is 0, it is the ID of the child process.

The exec() system call is used to replace the current process image with a new process image. It loads the program into the current space and runs it from the entry point.

So, the main difference between fork() and exec() is that fork() starts a new process copy that is identical to the main process. exec() replaces the current process image with a new process image, and the parent process and child process are executed at the same time.

Example

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main() {
   pid_t process_id;
   int return_val = 1;
   int state;
   process_id = fork();
   if (process_id == -1) { //when process id is negative, there is an error, unable to fork
      printf("can&#39;t fork, error occured</p><p>");
         exit(EXIT_FAILURE);
   } else if (process_id == 0) { //the child process is created
      printf("The child process is (%u)</p><p>",getpid());
         char * argv_list[] = {"ls","-lart","/home",NULL};
      execv("ls",argv_list); // the execv() only return if error occured.
      exit(0);
   } else { //for the parent process
      printf("The parent process is (%u)</p><p>",getppid());
      if (waitpid(process_id, &state, 0) > 0) { //wait untill the process change its state
         if (WIFEXITED(state) && !WEXITSTATUS(state))
            printf("program is executed successfully</p><p>");
         else if (WIFEXITED(state) && WEXITSTATUS(state)) {
            if (WEXITSTATUS(state) == 127) {
               printf("Execution failed</p><p>");
            } else
               printf("program terminated with non-zero status</p><p>");
         } else
            printf("program didn&#39;t terminate normally</p><p>");
      }
      else {
         printf("waitpid() function failed</p><p>");
      }
      exit(0);
   }
   return 0;
}

Output

The parent process is (8627)
The child process is (8756)
program is executed successfully

The above is the detailed content of In C language, what is the difference between fork() and exec()?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete