Linux File Description Descriptor set to non-clogging method

Source: Internet
Author: User

PassFcntlSet File Description Descriptor PropertiesFcntl is the use of F_SETFL,F_GETFL, set the file of flags, clogging is set to non-clogging, non-clogging is set to plug (this can be encapsulated as a basic function in server development)1. Get the flags of the file, which is the second parameter of the Open function:
    Flags = FCNTL (fd,f_getfl,0);


2, set the file flags:
     Fcntl (Fd,f_setfl,flags);


3. Add a flags for the file. For example, the file is blocked and you want to set it to non-clogging:
      Flags = FCNTL (fd,f_getfl,0);      Flags |= O_nonblock;      Fcntl (Fd,f_setfl,flags);


Or one step the way:

     Fcntl (Socket,f_setfl,fcntl (SOCKET,F_GETFL) | O_nonblock);

4, cancel a file of a flags, for example, the file is non-clogging, want to set up a blockage: 
    Flags = FCNTL (fd,f_getfl,0);    Flags &= ~o_nonblock;    Fcntl (Fd,f_setfl,flags);

PassSocket APIcreate a non-cloggingSocket

</pre><pre name= "code" class= "CPP" > #include <sys/types.h> #include <sys/socket.h>int socket ( int domain,int type,int protocal);


TypeSpecify the type of service, the type of service is mainlySock_streamServices,Sock_dgreamservice, it is worth noting that theLinuxKernel version number2.6.17up,typeParticipants can accept the following two logos at the same time as these services:Sock_nonblock,sock_cloexec,each of them represents the newly createdsockset to non-clogging, as well as inForkcall when creating a child process is closed in a child processsocket.However, the previous version number is not supported.

1. Creating blockagesSocket
    int fd_sock = socket (af_inet,sock_stream,0);


2. create non-clogging sockets
    int fd_sock = socket (af_inet,sock_stream| sock_nonblock,0);


The above code indicates that a new socket created is non-clogging

Linux File Description Descriptor set to non-clogging method

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.