Fork and exec in Linux

Source: Internet
Author: User

Anyone who has learned C language knows that the memory of a process in UNIX is divided into three parts:CodeSegment, stack segment, and data segment. Code segments are used to storeProgramThe Running code, the stack segment is used to store the local variables of the subroutine, and the data segment is used to store global variables. This is also true in Perl.
The Perl fork call is the same as that of C. When a fork call occurs, the following actually happens:
The parent process copies the code segment, stack segment, and data segment to the child process. That is to say, at the beginning of a child process, it has all the variables and handles of the parent process. For example, if the parent process declares a hash table, the hash table will also be owned by the process.
However, once a child process starts running, its data segment and stack segment are completely separated from the parent process in the memory. That is to say, the two processes no longer share any data. For example, in the hash table mentioned above, although the child process inherits the data structure from the parent process, the data written to the hash by the child process is not accessed by the parent process. Using the ps command in shell, we can see two independent processes. You usually kill one task without affecting the operation of the other task.
How can a parent process communicate with a fork child process? There are multiple methods for communication between the parent process and the child process. The most common method is the signal. In addition, there are pipelines, sockets, message queues, and so on, which are not described here. I am not familiar with the data sharing method between two processes, which can be thread or shared memory.
If you understand fork, exec is easy to understand. Once a process calls the exec function, it is "dead". The system replaces the code segment with the code of the new program and discards the original data segment and stack segment, and allocate new data segments and stack segments for the new program. The only difference is the process number. That is to say, for the system, it is the same process, but it is already another program.
In Perl, after exec is called, the original process disappears completely. As it disappears, it will not receive any returned values from the new process unless the new process is terminated unexpectedly, the original process will receive the error value.

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.