Linux blocking and non blocking

1. The concept of block: refers to that when a process or thread performs device operation or pipeline, or network, it cannot obtain resources and is suspended,
Until the operational conditions are met, the suspended process enters the sleep state and moves away from the running queue until
Wait for the conditions to be met before proceeding. That is, when some functions are executed, you must wait for an event to occur before returning.
2. Non block: even if the process does not get the resource or does not wait for the event to happen and does not hang, it usually gives up or queries continuously,
Up to where it can be done. In other words, the execution of a function does not need to wait for an event to occur. Once the execution is positive, the return value is used to reflect the execution of the function.
3. For example: read read I / O device / dev/tty

4. Block in read device file

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* argv[])
{
    int fd = open("/dev/tty",O_RDONLY); //Open the standard I / O file, which is blocked.
    if(fd == -1){
        perror("open /dev/tty");
        exit(1);
    }
    int ret = 0;
    char buf[1024] = {0};
    while(1){
        ret = read(fd, buf, sizeof(buf));
        if(ret == -1){
            perror("read");
            exit(1);
        }
        else if(ret == 0)
            printf("buf is null\n");
        else if(ret > 0)
            printf("buf is %s\n",buf);
        printf("test\n");
    } 
       sleep(1);
    }
    close(fd);

    return 0;
}

5. Directly set non blocking with O ﹣ Nonblock

int main(int argc, char* argv[])
{
    int fd = open("/dev/tty", O_RDONLY | O_NONBLOCK); // O ﹣ Nonblock set file input and output to non blocking
    if(fd == -1){
        perror("open /dev/tty");
        exit(1);
    }
    int ret = 0;
    char buf[1024] = {0};
    while(1){
        ret = read(fd, buf, sizeof(buf));
        if(ret == -1){
            perror("read /dev/tty");
            printf("no input,buf is null\n");
        }
        else {
            printf("ret = %d, buf is %s\n",ret, buf);
        }
        sleep(1);
    }
    close(fd);

    return 0;
}

6. Set non blocking with fcntl() function and O ﹣ Nonblock

int main(int argc, char* argv[])
{
    int fd = open("/dev/tty", O_RDONLY);

    //The fcntl() function sets the standard I / O file to non blocking
    int flag = fcntl(fd, F_GETFL);  //Step 1: get file operation permission
    flag |= O_NONBLOCK;       //Step 2: get the file property that is the same as o ﹣ Nonblock or
    fcntl(fd, F_SETFL, flag); //Step 3: set file operation permission again
    int ret = 0;
    char buf[1024] = {0};
    
    while(1){
        ret = read(fd, buf, sizeof(buf));
        if(ret == -1){
            perror("read /dev/tty");
            printf("no input,buf is null\n");
        }
        else {
            printf("ret = %d, buf is %s\n",ret, buf);
        }
        sleep(1);
    }
    close(fd);

    return 0;
}

Keywords: network

Added by roopurt18 on Mon, 11 Nov 2019 22:41:08 +0200