【Linux系统编程】进程_04_I/O多路复用_01_select

select

  1. 功能:可以将多个阻塞点变成一个阻塞点
  2. 原型
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  1. 参数
    1)nfds,监听的最大的文件描述符 maxfd + 1
    2)readfds
    3)writefds
    4)exceptfds
    5)timeout
  2. 返回值
    成功:返回就绪事件的数目,若超时,返回0
    失败:返回-1,并会设置errno
  3. 同步的 I/O 多路复用
  4. 缺陷
    a.监听的文件描述符的个数是有限的
    b.当 select 返回时,还得遍历fd_set,找到就绪的文件描述符
  5. 补充
    strtok:
    char *strtok(char *str, const char *delim);
    str:
    a.没加const,传入传出参数
    b.设NULL:从上次停止的地方继续分割原字符串
    delim:分隔符,要扔掉的
  6. DO
// 先创建两个有名管道
// mkfifo pipe1 pipe2
// select_p1.c
#include <func.h>

#define MAXLINE 256

int main(int argc, char* argv[]) {
    int fd1 = open("pipe1", O_WRONLY);
    if (fd1 == -1) {
        error(1, errno, "open pipe1");
    }
    int fd2 = open("pipe2", O_RDONLY);
    if (fd2 == -1) {
        error(1, errno, "open pipe2");
    }

    printf("Established\n");

    char recvline[MAXLINE];
    char sendline[MAXLINE];

    fd_set mainfds; // 局部变量
    FD_ZERO(&mainfds);  // 将所有位置为0
    FD_SET(STDIN_FILENO, &mainfds);
    int maxfds = STDIN_FILENO;

    FD_SET(fd2, &mainfds);
    if (fd2 > maxfds) {
        maxfds = fd2;
    }

    while (1) {
        fd_set readfds = mainfds;   // 结构体的复制

        // struct timeval timeout = {5, 0}; // 5秒
        // int events = select(maxfds + 1, &readfds, NULL, NULL, &timeout);
        int events = select(maxfds + 1, &readfds, NULL, NULL, NULL);
        switch (events) {
        case -1:
            error(1, errno, "select");
        case 0:
            // 超时
            printf("TIMEOUT\n");
            continue;
        default:
            // 打印timeout的值
            /*printf("timeout: tv_sec = %ld, tv_usec = %ld\n,
             *          timeout.tv_sec, timeout.tv_usec");*/
            // STDIN_FILENO 就绪,写
            if (FD_ISSET(STDIN_FILENO, &readfds)) {
                // 一定不会阻塞
                fgets(sendline, MAXLINE, stdin);
                write(fd1, sendline, strlen(sendline) + 1); // +1: for '\0'
            }
            // pipe2 就绪,读
            if (FD_ISSET(fd2, &readfds)) {
                // 一定不会阻塞
                int nbytes = read(fd2, recvline, MAXLINE);
                if (!nbytes) {
                    // 管道的写端关闭了
                    goto end;
                } else if (nbytes == -1) {
                    error(1, errno, "read pipe2");
                }
                printf("from p2: %s", recvline);
            }
        }
    }

end: 
    close(fd1);
    close(fd2);

    return 0;
}
// select_p2.c
#include <func.h>

#define MAXLINE 256

int main(int argc, char* argv[]) {
    int fd1 = open("pipe1", O_RDONLY);
    if (fd1 == -1) {
        error(1, errno, "open pipe1");
    }
    int fd2 = open("pipe2", O_WRONLY);
    if (fd2 == -1) {
        error(1, errno, "open pipe2");
    }

    printf("Established\n");

    char recvline[MAXLINE];
    char sendline[MAXLINE];

    fd_set mainfds; // 局部变量
    FD_ZERO(&mainfds);  // 将所有位 置为0
    FD_SET(STDIN_FILENO, &mainfds);
    int maxfds = STDIN_FILENO;

    FD_SET(fd1, &mainfds);
    if (fd1 > maxfds) {
        maxfds = fd1;
    }

    while (1) {
        fd_set readfds = mainfds;   // 结构体的复制

        // struct timeval timeout = {5, 0};
        // int events = select(maxfds + 1, &readfds, NULL, NULL, &timeout);
        int events = select(maxfds + 1, &readfds, NULL, NULL, NULL);
        switch (events) {
        case -1:
            error(1, errno, "select");
        case 0:
            // 超时
            printf("TIMEOUT\n");
            continue;
        default:
            /*printf("timeout: tv_sec = %ld, tv_usec = %ld\n,
             *          timeout.tv_sec, timeout.tv_usec");*/
            // STDIN_FILENO 就绪
            if (FD_ISSET(STDIN_FILENO, &readfds)) {
                // 一定不会阻塞
                fgets(sendline, MAXLINE, stdin);
                // memset(sendline, 0, MAXLINE);
                write(fd2, sendline, strlen(sendline) + 1); // +1: for '\0'
            }
            // pipe1 就绪
            if (FD_ISSET(fd1, &readfds)) {
                // 一定不会阻塞
                int nbytes = read(fd1, recvline, MAXLINE);
                if (!nbytes) {
                    // 管道的写端关闭了
                    goto end;
                } else if (nbytes == -1) {
                    error(1, errno, "read pipe1");
                }
                printf("from p1: %s", recvline);
            }
        }
    }

end: 
    close(fd1);
    close(fd2);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值