如何与民意调查封闭管道协议 [英] How poll deal with closed pipe

查看:142
本文介绍了如何与民意调查封闭管道协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要konw:

管道关闭,什么样的地位调查将用于管道的文件描述符设置?

When a pipe close , what status poll will set for the pipe's file descriptor?

我尝试以下code,子进程关闭所有文件描述符后,民调只是觉得所有的文件描述符可以读!是对的吗?或者只是我做一些错误,在这个code?

I try the code below, After child process close all file descriptor , the poll just think all file descriptor can read! Is that right? Or just I make some mistake in this code?

我用SUSE和gcc。

I use SUSE and gcc .

#include <stdio.h>
#include <unistd.h>
#include "../../../myInclude/apue.h"// this is ok

#include <sys/poll.h>

int main(int argc, char **argv)
{
    int fd1[2]; int fd2[2]; int fd3[2];
    pid_t pid;
    if(pipe(fd1)<0 ||pipe(fd2)<0 ||pipe(fd3) <0)
        err_sys("pipe error");//this is a error deal function .it will exit the program and print error message.
    if((pid = fork()) <0)
        err_sys("fork() error");
    else if(pid == 0)
    {
        close(fd1[0]);
        close(fd2[0]);
        close(fd3[0]);

        if(write(fd1[1],"hello fd1 write!",17)!= 17)
            err_sys("write 1error ");
        sleep(2);
        if(write(fd2[1],"hello fd2 write!",17)!=17)
            err_sys("write 2error");
        sleep(2);
        if(write(fd3[1],"hello fd3 write!",17)!= 17)
            err_sys("write 3error");
        sleep(2);
        close(fd1[1]);
        close(fd2[1]);
        close(fd3[1]);
    }
    else
    {
        close(fd1[1]);
        close(fd2[1]);
        close(fd3[1]);
        struct pollfd fd[3];
        fd[0].fd = fd1[0];
        fd[1].fd = fd2[0];
        fd[2].fd = fd3[0];
        fd[0].events = POLLIN;
        fd[1].events = POLLIN;
        fd[2].events = POLLIN;
        while(poll(fd,3,3000) >0)
        {
            printf("now I come \n");
            int i = 0,n;
            char line[MAXLINE];
            for(; i< 3; i++)
            {
                if(fd[i].revents = POLLIN)
                        if ((n =read(fd[i].fd,line,MAXLINE))< 0)
                            err_sys("read error : %d",i);
                        else
                        {
                            line[n] = 0;
                            printf("read from pipe %d : %s\n",i,line);
                        }
            }
        }
        close(fd1[0]);
        close(fd2[0]);
        close(fd3[0]);
    }

    return 0;
}

我觉得子进程关闭所有写文件描述符后,投票将设置的revents POLLHUP 。但它只是设置的 POLLIN

我读的书。我知道这是老书。所以,我想知道民意调查是如何工作的呢?它是将 POLLIN 近距离管?或者仅仅因为Linux呢?还是我的code是错的?

I am reading the book .I know this an old book. So I want to know how poll works now? Is it set POLLIN for close pipe ? Or just because Linux? Or my code is wrong?

推荐答案

您应该始终与 -Wall 选项编译程序(至少)。这会告诉你关于这个问题:

You should always compile your programs with the -Wall option (at least). That would have told you about this problem:

if(fd[i].revents = POLLIN)

的条件总是为真,因为这是一个赋值,不进行比较,并且 POLLIN 是非零。以下就是不正确的,但其中它的更好:

The condition will always be true because that is an assignment, not a comparison, and POLLIN is non-zero. The following would not be correct either, although it's better:

if(fd[i].revents == POLLIN)

如果 POLLIN 的revents 设置的唯一标志,这将是真实的。也许,这就是你想什么,你要检查,但正常的考验将是:

That will be true if POLLIN is the only flag set in revents. Perhaps that's what you thought you wanted to check, but the normal test would be:

if(fd[i].revents & POLLIN)

这将检查 POLLIN 位,这表明一个不会阻止。

which will check if the POLLIN bit is set, indicating that a read won't block.

错误的情况下,可以检测读取失败后,所以它不是真的有必要检查,例如,设置 POLLHUP 。这不是一个好主意,以测试 POLLHUP 输入插座,因为即使数据可用来读取该标志可以设置,而且它通常需要读取数据。

The error cases can be detected after the read fails, so it's not really necessary to check if, for example, POLLHUP is set. It's not a good idea to test POLLHUP on input sockets because the flag may be set even if data is available to read, and it's usually desirable to read the data.

这篇关于如何与民意调查封闭管道协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆