Home >Operation and Maintenance >Linux Operation and Maintenance >What is the difference between fork and exec in linux

What is the difference between fork and exec in linux

WBOY
WBOYOriginal
2022-05-17 15:52:352674browse

The difference between fork and exec in Linux: 1. Fork is used to create a new process, called a child process, while exec replaces all the contents of the current process with a specified program; 2. Before and after exec The process id has not changed, but the id has changed after fork re-created the child process.

What is the difference between fork and exec in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the difference between fork and exec in Linux

Fork and exec are both process-related calls.

fork

Calling fork can create a new process called a child process, and the process calling the fork function is called a parent process. All the contents of the child process are the same as the parent process, except for pcd (process control module). If neither process has If the memory is being written, then the two processes share the memory page of the process that calls the fork function. In this way, it seems that the process created by fork is faster than the process created by exec. But as long as one of the two processes modifies the memory, then before the modification, , will copy the memory page to the child process.

exec

Calling exec to create a process is not actually creating a process, but to be more precise, loading Executable file, after calling exec, the executable file specified in exec will be loaded into the space of the process calling exec, and the memory of the process calling exec will be updated to the content of the executable file specified in exec.

Difference

fork is mainly designed to create new processes (threads) in Linux. The exec() series of functions are used to replace all of the current process with a specified program. content. Therefore, the exec() series of functions are often called after the first three functions are used to establish a new program running environment. The process of Linux using the init process to start other processes is usually like this

fork is the simplest call, which does not require any parameters. It just creates a child process and creates a space for it that is independent of the parent process. .

After we use the fork function to create a new process, we often call the exec function in the new process to execute another program (for example, when we write a program, we need to use the function of a program we have written before, Directly calling an executable program in this way can greatly simplify our program). When a process calls the exec function, the process is completely replaced with a new program. Because calling the exec function does not create a new process, the IDs of the preceding and following processes have not changed.

The parent process generates an identical child process through fork(). The created child process then uses the exec function to execute the actual process, and finally becomes a process that can execute certain specific tasks. Functional child process.

Once a process calls the exec class function, it itself "dies". The system replaces the code segment with the code of the new program, discards the original data segment and stack segment, and allocates new code for the new program. The only thing left behind in the data segment and stack segment is the process number. In other words, to the system, it is still the same process, but it is already another program. That is, after calling the exec family function, the subsequent programs will not be executed.

Recommended learning: Linux video tutorial

The above is the detailed content of What is the difference between fork and exec in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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