【问题标题】:Socket blocked by pause()套接字被 pause() 阻塞
【发布时间】:2014-02-11 23:57:00
【问题描述】:

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

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

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

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

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

UPDATE2:我发现了问题所在。我发布的代码和我的问题代码之间存在一个关键区别。以下是区别:

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);
}

【问题讨论】:

  • pause 挂起当前线程。给我们看一个代码示例怎么样?

标签: c linux multithreading sockets signals


【解决方案1】:

来自 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 并找出每个线程在做什么吗?

【讨论】:

  • 我知道在子线程中调用了接受,因为它正在记录状态消息。如果 select() 调用返回错误状态,则会记录下来。目前睡眠线程没有任何目的。正如我已经提到的,我只是对正在发生的事情感到好奇。
  • @jdepth 对于它的价值,当我在 OS X 和 Linux 上编译你的示例代码时,我可以在这两种情况下连接到套接字......也就是说,我不知道我是否可以读写。所以也许手册页不正确,pause() 真的只是暂停当前线程。您能否更具体地了解确切的行为?
  • 由于某种原因,我提供的精简代码版本没有出现问题。我也可以连接。我会继续调查。
【解决方案2】:

下面的程序显示了一个线程在主线程暂停时继续运行。结合Mike's 无法在两个不同的平台上重现您的问题,我认为要求您再次确认您确实看到了上述内容是公平的。

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define exitOnErr(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)

static void handler(int sig)
{
    printf("don't use printf in a signal handler\n");
}

void *athread(void* x)
{
    while (1)
    {
        printf("thread running\n");
        sleep(1);
    }
}

int main(int argc, char *argv[])
{
    printf("%d\n", getpid());

    pthread_t pid;

    if (signal(SIGINT, handler) == SIG_ERR)
        exitOnErr("signal");

    if(pthread_create(&pid, NULL, athread, NULL) != 0)
        exitOnErr("pthread_create");

    while(1)
    {
        pause();
        printf("pause returned\n");
    }
}

使用了kill -SIGINT "pid" 和kill -SIGTERM "pid"。

【讨论】:

  • 我提供的代码实际上并没有表现出我描述的行为。我直接从问题代码中获取代码并删除了非必要部分。我将回到问题代码,看看我是否可以确定为什么它的行为不同。
猜你喜欢
  • 1970-01-01
  • 2010-10-31
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多