您好,欢迎来到年旅网。
搜索
您的当前位置:首页linux非阻塞读终端示例

linux非阻塞读终端示例

来源:年旅网
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#define MSG_AGAIN "try again\n"
int main()
{
        int fd,n;
        char buf[10];
        fd=open("/dev/tty",O_RDONLY|O_NONBLOCK);
        if(fd<0)
        {
                perror("open dev/tty");
                exit(1);
        }

try_again:
        n=read(fd,buf,10);
        if(n<0)
        {
                if(errno==EAGAIN)
                {
                        sleep(1);
                        write(STDOUT_FILENO,MSG_AGAIN,strlen(MSG_AGAIN));
                        goto try_again;
                }
                perror("read dev/tty");
                exit(1);
        }

        write(STDOUT_FILENO,buf,n);

        close(fd);
        return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务