File Descriptor Flags

Source: Internet
Author: User

File descriptor flags (currently there is only one close-on-exec):

It is only a flag that is used when the EXEC function is called in a child process when the process is fork a subprocess. The meaning is whether to close the file descriptor before executing exec.

Close-on-exec is a bitmap flag for all file descriptors (file handles) of a process, each of which represents a flag bit of an open file descriptor that determines whether a handle to the file needs to be closed when the calling system calls exec (). When a process uses the fork () function to create a child process, it is common to call the Execve () function in that child process to load the execution of another new program. At this point the subroutine is completely replaced by the new program and starts executing the new program in the child process. If a file descriptor is set in the corresponding bit in close-on-exec, the descriptor will be closed when Execve () is executed, otherwise the descriptor will always be in the open state, that is, the default state of Close-on-exec is not set.

The Close-on-exec logo is used for the following purposes:

1. If exec () is called, the specified socket should be closed.

2. It is common to call exec to execute another program, which replaces the child process's body, data, heap, and stack data with a completely new program. The variable that saves the file descriptor does not exist at this point, and we cannot close the useless file descriptor. So usually we will fork the process after executing close in the subprocess to turn off the useless file descriptor and then execute exec.

Example: Parent Process Code

/*Parent Process*/#include<fcntl.h>#include<unistd.h>#include<string.h>#include<stdio.h>#include<sys/wait.h>intMain () {intFD = open ("Test.txt", O_RDWR |o_append); if(FD = =-1) {printf ("The file Test.txt open failed! the FD =%d\n", FD); Execl ("/bin/touch","Touch","Test.txt", (Char*) NULL); return 0; }    Else{printf ("The file Test.txt open success! the FD =%d\n", FD); } printf ("fork...\n"); //write nothing, equivalent to the default Fcntl (FD, F_SETFD, 0), when executing a child process with execl//do not open the "close at execution" flag bit fd_cloexec, at which time the child process can write a string to Test.txt//The following three words control whether the parent process's file descriptor is closed in the child process//fcntl (FD, F_SETFD, 1);    intFlags =fcntl (FD, F_FETFD); Fcntl (FD, F_SETFD, Flags|fd_cloexec); Char*str ="The Parent Process writted!\n"; pid_t PID=Fork (); if(PID = =0) {printf ("***execl child***\n"); Execl (" Child","./child", &FD, NULL); printf ("*****************\n"); }     //wait for the child process to endWait (NULL); ssize_t write_bytes= Write (FD, (void*) str, strlen (str)); if(Write_bytes = =-1) {printf ("The Parent Process Write to FD:%d failed!\n", FD);    } close (FD); return 0;}

Child Process Code:

/*Child Process*/#include<stdio.h>#include<unistd.h>#include<string.h>intMainintargcChar*argv[]) {printf ("argc =%d\n", ARGC); if(argv[1] ==NULL) {printf ("there is no parameter!\n"); return 0; }     intFD = *argv[1]; printf ("Child fd =%d\n", FD); Char*str ="Child Process writted!\n"; ssize_t write_bytes= Write (FD, (void*) str, Strlslen (str)); if(Write_bytes = =-1) {printf ("The child Process write to FD:%d failed\n", FD);    } close (FD); return 0;}

Two sections of code use the FCNTL function to set the file description flag bit of FD to control whether the file descriptor in the parent process is closed in the child process. Also note that here the fd_cloexec identifier, only for the EXEC series of functions valid, if the child process directly in the current routine PID = = 0 of the judgment inside write, rather than through execl a new routine write file, which is Fd_ The cloexec identifier is not functional.

Reprint: http://www.cnblogs.com/stemon/p/5242547.html

File Descriptor Flags

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.