gpt4 book ai didi

c - 套接字被 pause() 阻塞

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:49 26 4
gpt4 key购买 nike

我正在开发接受传入 TCP 连接的软件,但遇到了一些我不理解的问题。首先,我将解释该软件的基本功能。请记住,有些部分是临时的,我知道这很可能是一种糟糕的做事方式,但在原型(prototype)设计过程中我遇到了这个问题。

我让主进程为 SIGINT 建立一个信号处理程序。主进程然后启动一个新线程,将其称为“监听器”,使用 pthread_create() 默认值。监听器首先打开一个套接字,绑定(bind)、监听并设置套接字为非阻塞。然后监听器将使用 select() 轮询套接字,等待传入连接。

现在,如果我在主线程中有一个愚蠢的 while(1) 循环,我可以毫无问题地连接到套接字。问题是:如果我用 pause() 替换 while(1) 循环,我将无法再连接到套接字。我知道监听器线程通过日志消息仍然处于事件状态。同样,我不打算使用 pause(),但我只想知道发生了什么。

pause() 是否阻止某个信号到达子线程?

更新:我提供的精简代码似乎没有表现出相同的行为。如果我能确定原因,我会再次更新。

更新 2: 我发现了问题所在。我发布的代码和我的问题代码之间存在一个关键区别。以下是不同之处:

static void* listener_thread(void* arg)
{
int listen_port = *(int *)arg;
int listen_fd;
fd_set readSet;
int fdsMax, status;
struct timeval timeout;

if(open_listen_port(listen_port, &listen_fd) == -1)

...

int start_listener_thread(int port)
{
int status = 0;

if(0 > pthread_create(&thread_id, NULL, listener_thread, (void *)&port))

在 main.c 中:

    if(0 == status && -1 == start_listener_thread(3000))

所以你可以看到我将端口号作为指向堆栈位置的指针传递给线程。这不是个好主意。奇怪的是,如果我将 pause() 更改为 while(1) 循环,它就会起作用。使用 pause() 时,端口号恰好是一个有效端口。

在start_listener_thread 中为端口号分配空间解决了这个问题。感谢一路上的帮助!

代码示例(精简):

#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/select.h>
#include <unistd.h>

pthread_t thread_id;

void sighandler(int signum)
{
}

int open_listen_port(int listenPort, int* listenFd)
{
struct sockaddr_in listenAddr;
int flags;

memset(&listenAddr, 0, sizeof(listenAddr));
listenAddr.sin_family = AF_INET;
listenAddr.sin_port = htons(listenPort);
listenAddr.sin_addr.s_addr = INADDR_ANY;

if( (*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
return(-1);
}

if( bind(*listenFd, (struct sockaddr*) &listenAddr,
sizeof(listenAddr)) == -1 )
{
return(-1);
}

if( listen(*listenFd, 16) == -1 )
{
return(-1);
}

// change listener to be non-blocking
flags = fcntl(*listenFd, F_GETFL);
if(fcntl(*listenFd, F_SETFL, flags | O_NONBLOCK) == -1)
{
return(-1);
}

return (0);
}

static void* listener_thread(void* arg)
{
int listen_fd;
fd_set readSet;
int fdsMax, status;
struct timeval timeout;

if(open_listen_port(3000, &listen_fd) == -1)
{
pthread_exit(NULL);
}

while(1)
{
FD_ZERO(&readSet);
fdsMax = 0;
timeout.tv_sec = 0;
timeout.tv_usec = 500000;

FD_SET(listen_fd, &readSet);
if(listen_fd > fdsMax)
{
fdsMax = listen_fd;
}

status = select(fdsMax + 1, &readSet, NULL, NULL, &timeout);
}
return NULL;
}

int start_listener_thread()
{
int status = 0;

if(0 > pthread_create(&thread_id, NULL, listener_thread, NULL))
{
status = -1;
}

return(status);
}

int main(int argc, char *argv[])
{
struct sigaction sigopt;
int status = 0;

memset(&sigopt, 0, sizeof(struct sigaction));
sigopt.sa_handler = sighandler;
if(0 != sigaction(SIGINT, &sigopt, NULL))
{
status = -1;
}

if(0 == status && -1 == start_listener_thread())
{
status = -1;
}

pause();

return(0);
}

最佳答案

来自 OS X 上的 man pause:

DESCRIPTION
Pause is made obsolete by sigsuspend(2).

The pause() function forces a process to pause until a signal is received
from either the kill(2) function or an interval timer. (See
setitimer(2).) Upon termination of a signal handler started during a
pause(), the pause() call will return.

来自 Linux 上的 man pause:

DESCRIPTION
pause() causes the calling process (or thread) to sleep until a signal
is delivered that either terminates the process or causes the invoca‐
tion of a signal-catching function.

两个手册页都暗示调用进程 将休眠。这解释了无法调用 accept()

你能确定 accept() 是否被调用了吗?您是否在适当的地方检查所有返回状态和 errno

我不确定休眠线程的目的是什么。如果您必须保持主线程处于事件状态,为什么不使用类似 sleep() 调用的 while 循环呢? (也许你打算稍后在此处添加代码以轮询某些内容?在这种情况下,使用 usleep() 和你想要检查的任何间隔,或者 sleep(1) 如果一次 -每秒的粒度就足够了吗?)或者只是在主线程上运行你的 select()

编辑: 看起来程序正在向我运行。我修改如下:

--- /tmp/foo.c  2014-02-11 16:43:04.000000000 -0800
+++ /tmp/foo.c 2014-02-11 16:46:17.000000000 -0800
@@ -7,6 +7,7 @@
#include <signal.h>
#include <sys/select.h>
#include <unistd.h>
+#include <stdio.h>

pthread_t thread_id;

@@ -76,6 +77,7 @@
}

status = select(fdsMax + 1, &readSet, NULL, NULL, &timeout);
+ printf("select() woke up\n");
}
return NULL;
}

当我进行上述更改时,它会每半秒打印一次 select() woke up,直到我连接到套接字。然后重复打印它。

你能更好地描述你看到的行为吗?是否有阻塞调用,例如对套接字的读取或写入?

你能附加(或在里面运行)gdb 并找出每个线程在做什么吗?

关于c - 套接字被 pause() 阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com