Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在什么情况下,关闭/关闭在前台或后台完成?_Linux_Sockets_Epoll - Fatal编程技术网

Linux 在什么情况下,关闭/关闭在前台或后台完成?

Linux 在什么情况下,关闭/关闭在前台或后台完成?,linux,sockets,epoll,Linux,Sockets,Epoll,我正在服务器端对套接字执行终止关闭操作,使用: struct linger so_linger; so_linger.l_onoff = 1; so_linger.l_linger = 0; setsockopt(s, SOL_SOCKET, SO_LINGER, &so_linger, socklen_t)sizeof(so_linger)); shutdown(s, SHUT_WR); close(s); 其中s是我要关闭的插座 它可以工作,但我有时会遇到问题,它似乎会影响某些服

我正在服务器端对套接字执行终止关闭操作,使用:

struct linger so_linger;
so_linger.l_onoff = 1;
so_linger.l_linger = 0;
setsockopt(s, SOL_SOCKET, SO_LINGER, &so_linger, socklen_t)sizeof(so_linger));

shutdown(s, SHUT_WR);
close(s);
其中
s
是我要关闭的插座

它可以工作,但我有时会遇到问题,它似乎会影响某些服务器,而不会影响其他服务器。有些在Ubuntu上运行,有些在CoreOS上运行。它在CoreOS上运行良好

在Ubuntu上,我从epoll_wait收到一个与套接字相关的事件,尽管调用了close

我以为这会马上发生。但是如果您使用的是非阻塞I/O,我想情况可能不是这样

它本质上意味着我在epoll_wait中获取事件,该事件的ev.data.ptr值设置指向已销毁的内容

所以,问题是,这是真的吗?半关闭不会从epoll中删除事件描述符,关闭不会与非阻塞i/o同步


因此,如果我不再需要这些事件,我实际上应该使用EPOLL\u CTL\u DEL手动删除描述符。

关闭套接字会将其从EPOLL集合中清除。但有一个警告。指 :


因此,有可能在调用了
close
的套接字上报告事件。不确定您的测试是否可以在特定平台上正常运行。当然,我认为这与异步I/O无关。

您先发送FIN,然后再发送RST。为什么?@EJP-你是说这不是一次失败的关闭?但它似乎奏效了。
> Q6  Will closing a file descriptor cause it to be removed from all
           epoll sets automatically?

   A6  Yes, but be aware of the following point.  A file descriptor is a
       reference to an open file description (see open(2)).  Whenever a
       file descriptor is duplicated via dup(2), dup2(2), fcntl(2)
       F_DUPFD, or fork(2), a new file descriptor referring to the same
       open file description is created.  An open file description
       continues to exist until all file descriptors referring to it
       have been closed.  A file descriptor is removed from an epoll set
       only after all the file descriptors referring to the underlying
       open file description have been closed (or before if the file
       descriptor is explicitly removed using epoll_ctl(2)
       EPOLL_CTL_DEL).This means that even after a file descriptor
       that is part of an epoll set has been closed, events may be
       reported for that file descriptor if other file descriptors
       referring to the same underlying file description remain open.