Fork () Relationship of the parent-child process file descriptor

Source: Internet
Author: User

Parent-Child process shared file descriptor: This is actually a parent-child process to share a file table entry (parent process and child process share the same Single file table entry)

Because the child process is a copy of the parent process, the child process copies the file descriptor in the process descriptor of the parent process, which can be said to inherit the file descriptor of the parent process (a copy of the struct file *fd_array[nr_open_default] in files_struct)

If we do not call the EXEC function, then our parent-child process's code snippet, stack, data segment are identical (because it is a copy), so at this point our child process can use the FD value before fork (), although the FD is the data segment belonging to the child process (he is a copy of the previous FD)

Once a process calls the Exec class function, it is itself "dead", the system replaces the code snippet with the new program's code, discards the original data segment and stack segment, and assigns new data segment and stack segment to the new program, the only one left is the process number, that is, for the system, or the same process, But it's already another program. (But some of the Exec class functions also allow the inheritance of information such as environment variables.) )

So the process's storage image is replaced by the new program (that is, the socket address structure with the other address is lost), but since the open file descriptor persists even after exec, we can call Getpeername (FD ...). ) Get the IP and port number of the peer

Generally speaking: Before calling exec to open a description in the process after exec or remain open, we can use the FCNTL function to set the FD_CLOEXEC description Word flag to close. This is also referred to as the file descriptor label, which is cleared by default

Since exec, the original CONNFD description Word must not have, so we have to restore this CONNFD

1, the CONNFD as a string, as the EXEC command line parameters to the new program

2. Before calling exec, set a descriptor to CONNFD (via Close (FD) and then invoke DUP (CONNFD), use the least unused FD according to the rules, and FD, so that FD points to the same file table entry as CONNFD), Usually we use 0,1,2 to set the CONNFD.

Fork () Relationship of the parent-child process file descriptor

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.