免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 892 | 回复: 2
打印 上一主题 下一主题

[系统管理] 关于apue 第三版p435.....相应的WAIT_XXX函数调用read读一个字符,没有读到字符时 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-02 17:10 |只看该作者 |倒序浏览
关于apue 第三版p435页.....相应的WAIT_XXX函数调用read读一个字符,没有读到字符时阻塞(睡眠等待)。

void  
WAIT_PARENT(void)  
{  
        char    c;  
        if (read(pfd1[0], &c, 1) != 1)  
                err_sys("read error");  
        if (c != 'p')  
                err_quit("WAIT_PARENT: incorrect data");  
}  
请问这里面是如何等待的,我看不出哪个 是等待的语句,gdb调试的时候也是看不出来,好奇怪,求大神指导?
下面是全部代码

static int      pfd1[2], pfd2[2];  
void  
TELL_WAIT()  
{  
        if (pipe(pfd1) < 0 || pipe(pfd2) < 0)  
                err_sys("pipe error");  
}  
void  
TELL_PARENT(pid_t pid)  
{  
        if (write(pfd2[1], "c", 1) != 1)  
                err_sys("write error");  
}  
void  
WAIT_PARENT(void)  
{  
        char    c;  
        if (read(pfd1[0], &c, 1) != 1)  
                err_sys("read error");  
        if (c != 'p')  
                err_quit("WAIT_PARENT: incorrect data");  
}  
void  
TELL_CHILD(pid_t pid)  
{  
        if (write(pfd1[1], "p", 1) != 1)  
                err_sys("write error");  
}  
void  
WAIT_CHILD(void)  
{  
        char    c;  
        if (read(pfd2[0], &c, 1) != 1)  
                err_sys("read error");  
        if (c != 'c')  
                err_quit("WAIT_CHILD: incorrect data");  
}  

论坛徽章:
0
2 [报告]
发表于 2014-10-02 17:12 |只看该作者
int
main(void)
{
        pid_t   pid;

        TELL_WAIT();

        if ((pid = fork()) < 0) {
                err_sys("fork error");
        } else if (pid == 0) {
                WAIT_PARENT();          /* parent goes first */
                charatatime("output from child\n");
        } else {
                sleep(5);
                charatatime("output from parent\n");
                TELL_CHILD(pid);
        }
        exit(0);
}

static void
charatatime(char *str)
{
        char    *ptr;
        int             c;

        setbuf(stdout, NULL);                   /* set unbuffered */
        for (ptr = str; (c = *ptr++) != 0; )
                putc(c, stdout);
}

论坛徽章:
0
3 [报告]
发表于 2014-10-02 17:13 |只看该作者
上面是我用的测试主函数,证实确实可以等待的,即使我用了sleep(5),依旧还是等待的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP