Linux Network Programming-file attributes fcntl Function

Source: Internet
Author: User

Linux Network Programming-file attributes fcntl Function

/* Use the fcntl control file operator */# include
  
   
# Include
   
    
# Include
    
     
Int main (void) {int flags =-1; int accmode =-1;/* Get the status of the standard input */flags = fcntl (0, F_GETFL, 0 ); if (flags <0) {/* error occurs */printf ("failure to use fcntl \ n"); return-1 ;} /* obtain access mode */accmode = flags & O_ACCMODE; if (accmode = O_RDONLY)/* READ-ONLY */printf ("stdin read only \ n "); else if (accmode = O_WRONLY)/* write only */printf ("stdin write only \ n"); else if (accmode = O_RDWR) /* readable/printf ("stdin read write \ n"); else/* Other MODE */printf ("stdin unknown mode"); if (flags & O_APPEND) printf ("stdin append \ n"); if (flags & O_NONBLOCK) printf ("stdin nonblock \ n"); return 0 ;}
    
   
  

Example 1: Use the int fcntl (int fd, int cmd) function. The returned value is the new file descriptor/* use fcntl to modify the File status value */# include  # Include  # Include  # Include  /* Strlen function */# define NEWFD 8int main (void) {char buf [] = "FCNTL"; int fd = open ("test.txt", O_RDWR ); printf ("the file test.txt ID is % d \ n", fd);/* get the file status */fd = fcntl (fd, F_GETFD ); printf ("the file test.txt ID is % d \ n", fd); fd = NEWFD;/* write status to */fcntl (NEWFD, F_SETFL, & fd ); /* write the string */write (NEWFD, buf, strlen (buf); close (NEWFD); return 0 ;}example 2: use the int fcntl (int fd, int cmd, long arg) function; return value is the obtained response flag/* use fcntl to modify the File status value */# include  # Include  # Include  # Include  /* Strlen function */int main (void) {int flags =-1; char buf [] = "FCNTL"; int fd = open ("test.txt", O_RDWR ); /* get the file status */flags = fcntl (fd, F_GETFL, 0);/* increase the status to append */flags | = O_APPEND; /* write status to */flags = fcntl (fd, F_SETFL, & flags); if (flags <0) {/* error occurred */printf ("failure to use fcntl \ n"); return-1;}/* write the string */write (fd, buf, strlen (buf); close (fd); return 0 ;}        

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> /* Use fcntl to obtain the process ID for receiving signals */# include # Include # Include # Define NEWFD 8int main (void) {int uid;/* open the file test.txt */int fd = open ("test.txt", O_RDWR ); /* process ID for receiving signals */uid = fcntl (fd, F_GETOWN); printf ("the SIG recv ID is % d \ n", uid ); close (fd); return 0 ;}

/* Use fcntl to set the process ID for receiving signals: 1000 */# include  # Include  # Include  # Define NEWFD 8int main (void) {int uid;/* open the file test.txt */int fd = open ("test.txt", O_RDWR ); /* process ID for receiving signals */uid = fcntl (fd, F_SETOWN, 1000); close (fd); return 0 ;}   

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.