Home > Backend Development > C#.Net Tutorial > What is the difference between fork() and exec() in C? A simple comparison between fork() and exec()

What is the difference between fork() and exec() in C? A simple comparison between fork() and exec()

青灯夜游
Release: 2019-04-01 15:02:00
Original
6126 people have browsed it

Every application (program) is executed through a process, which is a running program instance. Processes are created through different system calls, the most popular ones are fork() and exec(), so what is the difference between them? The following article will introduce to you the difference between fork() and exec(), I hope it will be helpful to you.

What is the difference between fork() and exec() in C? A simple comparison between fork() and exec()

fork() in C language

pid_t pid = fork();
Copy after login

fork() is created by copying the calling process A new process is spawned with a new PID, and the new process (called the child process) is an exact copy of the calling process (called the parent process). The two processes are almost identical, except for the following: 1. The child process has its own unique process ID, and this PID does not match the ID of any existing process group.

2. The parent process ID of the child process is the same as the ID of the calling process.

3. The child does not inherit the memory lock and semaphore adjustment of its parent.

 4. The child does not inherit unfinished asynchronous I/O operations from its parent, nor does it inherit any asynchronous I/O context from its parent.

The return value of fork()

fork() does not accept any parameters and returns an integer value. Below are the different values ​​returned by fork().

● Negative value: Failed to create the child process.

● Zero: Return to the newly created child process.

●Positive value: Returns the parent or caller. This value contains the process ID of the newly created child process.

Note: Once successful, the PID of the child process will be returned in the parent process, and 0 will be returned in the child process. On failure, -1 is returned in the parent process, the child process is not created, and errno is set appropriately.

Exec() in C languageThe exec() function series 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.

Example: We have implemented execv() in the following C program. The exec() series consists of the following functions,

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[]);
Copy after login

fork() and exec( )#1. fork() creates and starts a new process, which is a copy of the process that called it. The two processes are exactly the same; while exec() uses Another (different) process image replaces the current process image, and the "data segment", "stack segment" and "code segment" of the current process are overwritten by the new program.

2. Fork() creates a new process and generates a new PID, so the child process has its own process ID. exec starts a new program and replaces the original process, so the new program will keep the ID of the process calling exec() unchanged, that is, the PID of the new process executed by exec will not change and is the same as the process calling the exec function.

3. The file descriptors opened by the parent process before fork() are also opened in the child process and point to the same file table entry. However, descriptors opened before calling exec() will continue to be opened.

Recommended related video tutorials: "

C Language Tutorial

" The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between fork() and exec() in C? A simple comparison between fork() and exec(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template