Linux阻塞和非阻塞读终端

阻塞和非阻塞

  • 读常规文件是不会阻塞的,不管读多少字节,read一定会在有限的时间内返回。从终端设备或网络读则不一定,如果从终端输入的数据没有换行符,调用read读终端设备就会阻塞,如果网络上没有接收到数据包,调用read从网络读就会阻塞,至于会阻塞多长时间也是不确定的,如果一直没有数据到达就一直阻塞在那里。同样,写常规文件是不会阻塞的,而向终端设备或网络写则不一定。


  • 现在明确一下阻塞(Block)这个概念。当进程调用一个阻塞的系统函数时,该进程被置于睡眠(Sleep)状态,这时内核调度其它进程运行,直到该进程等待的事件发生了(比如网络上接收到数据包,或者调用sleep指定的睡眠时间到了)它才有可能继续运行。与睡眠状态相对的是运行(Running)状态,在Linux内核中,处于运行状态的进程分为两种情况:
    正在被调度执行。CPU处于该进程的上下文环境中,程序计数器(eip)里保存着该进程的指令地址,通用寄存器里保存着该进程运算过程的中间结果,正在执行该进程的指令,正在读写该进程的地址空间。


  • 就绪状态。该进程不需要等待什么事件发生,随时都可以执行,但CPU暂时还在执行另一个进程,所以该进程在一个就绪队列中等待被内核调度。系统中可能同时有多个就绪的进程,那么该调度谁执行呢?内核的调度算法是基于优先级和时间片的,而且会根据每个进程的运行情况动态调整它的优先级和时间片,让每个进程都能比较公平地得到机会执行,同时要兼顾用户体验,不能让和用户交互的进程响应太慢。

阻塞读终端

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
        char buf[10] = {0};
        int n = 0;
        n = read(STDIN_FILENO, buf, 10);
        if (n < 0)
        {
                perror("read STDIN_FILENO");
        }
        write(STDOUT_FILENO, buf, n);
        printf("\n");
        return 0;
}

非阻塞读终端

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

#define MSG_AGN "try again\n"

int main(void)
{
        char buf[10] = {0};
        int n = 0;
        int fd = 0;
        fd = open("/dev/tty", O_RDONLY | O_NONBLOCK);
        if (fd < 0)
        {
                perror("open /dev/tty");
                exit(1);
        }
tryagain:
        n = read(fd, buf, 10);
        if (n < 0)
        {
                if (errno == EAGAIN)
                {
                        sleep(5);
                        write(STDOUT_FILENO, MSG_AGN, strlen(MSG_AGN));
                        goto tryagain;
                }
                perror("read /dev/tty");
                exit(1);
        }
        write(STDOUT_FILENO, buf, n);
        printf("\n");
        close(fd);
        return 0;
}

非阻塞读终端和超时等待

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

#define MSG_AGN "try again\n"
#define MSG_TIMEOUT "time out\n"

int main(void)
{
        char buf[10] = {0}; 
        int n = 0,fd = 0;
        int i = 0;
        fd = open("/dev/tty", O_RDONLY | O_NONBLOCK);
        if (fd < 0)
        {
                perror("open /dev/tty");
                exit(1);
        }
        for (i = 0; i<5; i++)
        {

                n = read(fd, buf, 10);
                if (n >= 0)
                {
                        break;
                }

                if (errno != EAGAIN)
                {

                        perror("read /dev/tty");
                        exit(1);
                }

                sleep(1);
            write(STDOUT_FILENO, MSG_AGN, strlen(MSG_AGN));
        }
        if (i == 5)
                write(STDOUT_FILENO, MSG_TIMEOUT, strlen(MSG_TIMEOUT));
                else
                        write(STDOUT_FILENO, buf, n);
        printf("\n");
        close(fd);
        return 0;
}

猜你喜欢

转载自blog.csdn.net/u011135852/article/details/48793203