b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

sigpipe圖片_sigpipe_sigpipe信号

电脑杂谈  发布时间:2017-02-23 20:28:10  来源:网络整理

一、当没有数据可读时

O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止。

O_NONBLOCK enable:read调用返回-1,errno值为EAGAIN。sigpipe

示例程序如下:

#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

int main(int argc, char *argv[])
{
    int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

    pid_t pid;
    pid = fork();
    if (pid == -1)
        ERR_EXIT("fork error");

    if (pid == 0)
    {
        sleep(3);
        close(pipefd[0]);
        write(pipefd[1], "hello", 5);
        close(pipefd[1]);
        exit(EXIT_SUCCESS);
    }
//  sleep(3);
    close(pipefd[1]);
    char buf[10] = {0};
    int flags = fcntl(pipefd[0], F_GETFL);
    fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK); //enable fd的O_NONBLOCK
    int ret = read(pipefd[0], buf, 10); //默认是disable fd的O_NONBLOCK
    if (ret == -1) // 父进程不会阻塞,出错返回
        ERR_EXIT("read error");
    printf("buf=%s\n", buf);

    return 0;
}
特意在子进程中sleep了3s,让父进程先被调度运行,而且读端文件描述符标志设置为非阻塞,即立刻出错返回,如下:
huangcheng@ubuntu:~$ ./a.out
read error: Resource temporarily unavailable

假设开启35行,注释29行,让父进程先sleep,子进程先运行,则运行结果:

huangcheng@ubuntu:~$ ./a.out
buf=hello

二、当管道满的时候

O_NONBLOCK disable: write调用阻塞,直到有进程读走数据


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34100-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...