Linux fcntl function set blocking and non-blocking

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/xuyh/p/3273082.html

Set file flags with command F_GETFL and F_SETFL, such as blocking and non-blocking

F_SETFL set to the ARG descriptor status flag, several flags that can be changed are: O_append, O_nonblock,o_sync, and O_async.

The command Word (cmd) F_GETFL and F_SETFL flags are described below:

O_nonblock non-blocking I/O; if read (2) calls no readable data, or if the write (2) operation is blocked, the read or write call returns-1 and Eagain errors

O_append force each write operation to be added at the end of the file, equivalent to the O_APPEND flag of open (2)

O_direct minimizes or removes the cache impact of reading and writing. The system will attempt to avoid caching your read or write data.

If it is not possible to avoid caching, it will minimize the impact of data that has already been cached. If the logo is not good enough, it will greatly reduce the performance

O_async allows the Sigio signal to be sent to the process group when I/O is available, for example: when there is data to read

Note: You must be careful when modifying the file descriptor flag or file status flag to get the current flag value, then modify it as desired, and then set the new flag value. You cannot just execute the F_SETFD or F_SETFL command, which turns off the previously set flag bit.

#include <stdio.h>#include<sys/types.h>#include<unistd.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>/********************** enable non-blocking i/o*********************int flags;*if (flags = FCNTL (FD, F_GETFL, 0) < 0) *{* perror ("F Cntl "); * return-1;*}*flags |= o_nonblock;*if (Fcntl (FD, F_SETFL, Flags) < 0) *{* perror (" Fcntl "); * return-1;*} *******************************************************//********************** off non-blocking i/o******************flags &= ~o_nonblock;if (Fcntl (FD, F_SETFL, Flags) < 0) {    Perror ("Fcntl"); return-1;} *******************************************************/intMain () {Charbuf[Ten] = {0}; intret; intflags; //using non-blocking IO    if(Flags = FCNTL (Stdin_fileno, F_GETFL,0) <0) {perror ("Fcntl"); return-1; } Flags|=O_nonblock; if(Fcntl (Stdin_fileno, F_SETFL, flags) <0) {perror ("Fcntl"); return-1; }     while(1) {Sleep (2); RET= Read (Stdin_fileno, buf,9); if(ret = =0) {perror ("read--no"); }        Else{printf ("Read =%d\n", ret); } write (Stdout_fileno, buf,Ten); memset (BUF,0,Ten); }    return 0;}

Linux fcntl function set blocking and non-blocking

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.