简体   繁体   English

如何检测其他程序关闭的eventfd文件描述符

[英]how to detect eventfd file descriptor close by other program

I have a client/server communicate through eventfd. 我有一个客户端/服务器通过eventfd进行通信。 If either client or server call close(fd) I would like the other end to find out (like file descriptor is closed now). 如果客户端或服务器调用close(fd)我希望另一端查明(就像文件描述符现在已关闭)。 I tried to use select with non-zero timeout, it always return 0 which is timeout. 我尝试将select与非零超时一起使用,它始终返回0,即超时。 I saw people suggesting use fcntl it doesn't seems to be working either. 我看到有人建议使用fcntl似乎也不起作用。 Any suggestions? 有什么建议么?

Addtion Details (omitted non important part code, you can see here for how to exchange file descriptor detail code : It is multi processes application. Server process created eventfd by calling 添加详细信息(省略的非重要部分代码,您可以在此处查看如何交换文件描述符详细代码 :这是多进程应用程序。服务器进程通过调用创建eventfd

 struct msghdr control_message;
 int fd = eventfd(0,0);
 *CMSG_DATA(control_message) = fd;
 message.msg_control = &control_message;
 sendmsg(socket_fd, & message,0); //send this to client

From client side: 从客户端:

 recvmsg(socket_fd, & message,0);
 //loop using CMSG_NXTHDR(&message, control_message)
 int fd = *(int *) CMSG_DATA(contro_message);

Then on server side: 然后在服务器端:

 close(fd);

On Client side: int rc; 在客户端:int rc; rc = dup2(fd,fd); rc = dup2(fd,fd);

rc is never invalid. rc永远不会无效。

Checking for a closed file descriptor? 检查关闭的文件描述符? How about this? 这个怎么样?

#include <errno.h>
#include <stdio.h>
#include <string.h>

static void checkit ()
{
    int     rc;

    rc = dup2(2, 2);

    if ( rc == -1 )
            printf("error %d on dup2(2, 2): %s\n", errno, strerror(errno));
    else
            printf("dup2 successful\n");

    write(2, "still working\n", 14);
}

int main (int argc, char **argv)
{
    int     rc;

    checkit();

    close(2);

    checkit();

    return  0;
}

Running it generates this output: 运行它会生成以下输出:

dup2 successful
still working
error 9 on dup2(2, 2): Bad file descriptor

If this is a multi-threaded application using poll and you want poll to return when the file descriptor is closed by another thread, POLLERR, POLLHUP, or POLLNVAL might help. 如果这是一个使用poll的多线程应用程序,并且您希望在文件描述符被另一个线程关闭时返回poll,则POLLERR,POLLHUP或POLLNVAL可能会有所帮助。

Multi-Threaded Version using Poll 使用轮询的多线程版本

And here's a sample that shows how to detect a closed fd with poll (POLLNVAL is the event) in a multi-threaded program: 这是一个示例,该示例显示如何在多线程程序中使用轮询(POLLNVAL是事件)检测封闭的fd:

#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static void *run_poll (void *arg)
{
    struct pollfd   fds[1];
    int             rc;

    fds[0].fd = 2;
    fds[0].events = POLLERR | POLLHUP | POLLNVAL;

            //
            // Poll indefinitely
            //

    printf("starting poll\n");
    fflush(stdout);
    rc = poll((struct pollfd *) &fds, 1, -1);

    if ( rc == -1 )
    {
            printf("POLL returned error %d: %s\n", errno, strerror(errno));
    }
    else
    {
            printf("POLL returned %d (revents = 0x%08x): %s %s %s\n",
                   rc,
                   fds[0].revents,
                   ( ( fds[0].revents & POLLERR  ) ? "pollerr" : "noerr" ),
                   ( ( fds[0].revents & POLLHUP  ) ? "pollhup" : "nohup" ),
                   ( ( fds[0].revents & POLLNVAL ) ? "pollnval" : "nonval" ));
    }

    return  NULL;
}

int main (int argc, char **argv)
{
    pthread_t       thread;
    int             rc;

    rc = pthread_create(&thread, NULL, run_poll, NULL);

    usleep(100);
    printf("closing stderr\n");
    close(2);
    usleep(100);

    return  0;
}

This generates the output 这产生输出

starting poll
closing stderr
POLL returned 1 (revents = 0x00000020): noerr nohup pollnval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM