Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between fork and exec in linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the differences between fork and exec in linux". In daily operation, I believe many people have doubts about the differences between fork and exec in linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the differences between fork and exec in linux?" Next, please follow the editor to study!

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

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

What is the difference between fork and exec in linux

Both fork and exec are process-related calls.

Fork

Calling fork can create a new process called the child process, and the process that calls the fork function is called the parent process, and all the contents of the child process are the same as the parent process, except pcd (process control module). If the two processes do not write to memory, then the two processes share the memory page of the process that calls the fork function, so on the surface, the fork creation process is faster than the exec creation process. However, as long as one of the two processes makes changes to the memory, a copy of the memory page will be copied to the child process before the modification.

Exec

Calling exec to create a process is not actually creating a process, but rather loading an 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 by Linux to create new processes (threads), while the exec () series functions are used to replace the entire contents of the current process with the specified program. Therefore, the exec () series functions are often called after the first three functions are used to establish a new program running environment. Linux usually uses the init process to start the rest of the process like this

Fork is the simplest call without any arguments, just creating a child process and creating a space for it independent of the parent process.

After we create a new process with the fork function, 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 functions of a program we have written before, and calling an executable program directly in this way can greatly simplify our program). When a process calls the exec function, the process is completely replaced with the new program. Because calling the exec function does not create a new process, the ID of the front and back processes has not changed.

The parent process produces an identical child process through fork (), and the created child process executes the process it actually wants to execute in the way of exec function, and finally becomes a child process that can perform some specific functions.

Once a process calls an exec class function, it itself is "dead". The system replaces the code segment with the new program code, discards the original data segment and stack segment, and assigns new data segment and stack segment to the new program. The only thing left is the process number, that is to say, for the system, it is still the same process, but it is already another program. That is, after calling the exc family function, the following programs will no longer be executed.

At this point, the study on "what is the difference between fork and exec in linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report