Exit and _ exit, Fock and vfock

Source: Internet
Author: User
Tags terminates

This article was transferred from a netizen who only merged the two articles. The author of each article is unknown. Reject piracy.

_ Exit: terminates the calling process, but does not close the file, clear the output cache, or call the exit function.
The exit function terminates the calling process. Before exiting the program, all files are closed. The buffered output content refreshes the definition and calls all refreshed export functions (defined by atexit ).
There are many differences between 'exit () 'and' _ exit () 'when using 'fork ()', especially 'vfork.

The basic difference between 'exit () 'and' _ exit () 'is that the user-mode constructs) clean-up and user-defined purge programs are called. The user-defined purge program is defined by the atexit function and can be defined multiple times, and executed in reverse order). Correspondingly, the last function only performs kernel cleanup for the process.

In the sub-process Branch created by 'fork () ', it is incorrect to use 'exit ()' normally, this is because the buffer of the standard input output (Translator's note: stdio: Standard Input Output) is cleared twice, and the temporary file is deleted unexpectedly: the temporary file is created by the tmpfile function in the temporary directory of the system, and the file name is randomly generated by the system ). In the C ++ program, the situation is worse, because the Destructors of the static object can be mistakenly executed. (In some special cases, such as Daemon, their * parent process * needs to call '_ exit ()' instead of the child process. The basic rule for most cases is, 'exit () 'It is called only once after each entry to the 'main' function .)

In the sub-process Branch created by 'vfork () ', the use of 'exit ()' is more dangerous because it affects the status of the parent process.

Before terminating the process that calls exit (), perform the following steps:
1. Cleanup ();
2. Functions registered in atexit;
Finally, call the _ exit () function...

Before terminating the process that calls exit (), perform the following steps:
1. Cleanup ();
2. Functions registered in atexit;
Finally, call the _ exit () function...

After fork, the child process and the parent process will continue to execute the commands after the fork call. A child process is a copy of the parent process. It will obtain the data space, heap and stack copies of the parent process. These are copies and the Parent and Child processes do not share the memory. That is to say, modifying the variable with the same name in the parent process does not affect the value of the variable in the parent process. However, the Parent and Child processes share something, which is simply the body segment of the program. The body segment stores machine commands executed by the CPU, usually read-only.

Since we often follow exec after fork, to improve efficiency, many implementations do not completely copy data segments and stacks, but adopt write-time replication, it is a bit similar to some cache and Memory Data Synchronization Methods.

Another way to improve efficiency is to use vfork. vfork originated from 2.9bsd at the earliest. The difference between it and fork is that it does not completely copy the address space of the parent process to the child process, because the sub-process will immediately call exec. The child process from vfork runs in the space of the parent process. It exists for exec calling, so it does not need to copy these things, because the replication is useless. If the sub-process modifies a variable, this will affect the parent process.
Another difference between vfork and fork is that vfork ensures that the sub-process runs first. Only after it calls exec or exit can the parent process be scheduled to run. The fork Parent and Child processes run in an indefinite order, which depends on the scheduling algorithm of the kernel.

Therefore, when fork is used, the program code is reused-I mean the program code is the machine instruction part executed by the CPU, which has nothing to do with how many processes are running, even a program with frequent execution requires only one copy in the memory, and it may be read-only during the execution period. Of course, if you execute, it's another thing.
In addition, the data space and heap and stack in the parent process may generate copies. The actual situation depends on whether you are using vfork or fork. Fork generates copies, while vfork shares the memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.