pthread_kill()在从第二个线程调用时给出分段错误

时间:2015-01-05 22:31:41

标签: c pthreads

我试图在read()系统调用阻止时手动中断程序的主线程。我在调用pthread_kill()的第二个线程中执行此操作,但会发生分段错误。但是,如果我在第二个线程中调用read(),即不是主线程,并从主线程调用pthread_kill(),那么所有都按预期工作。

例如,以下代码导致分段错误,我在第二个线程中调用pthread_kill(),大约在启动后2秒。它使用通过调用(在主线程中)到pthread_t获得的主线程的pthread_self()

示例1

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>


static int fd = 0;
unsigned char buf[255];
static pthread_t s;
void sigHandler(int sig){
    printf("Signal handler called.\n");
}

void * closeFD(void *arg){
    printf("Second thread started.\n");
    sleep(2);
    int r = pthread_kill(s, SIGUSR1);
}

int main(char *argv[], int argc){
    struct termios newtio;
    pthread_t t1;
    unsigned char buf[255];
    void *res;
    struct sigaction int_handler = {.sa_handler=sigHandler};
    sigaction(SIGUSR1,&int_handler,0);
    s = pthread_self();
    printf("Process id is: %d.\n", getpid());
    fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
    if (fd != -1){
        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = B2400 | CS7 | CLOCAL | CREAD ;
        newtio.c_iflag = ICRNL;
        newtio.c_oflag = 0;
        newtio.c_lflag = ~ICANON;
        newtio.c_cc[VMIN]     = 14;
        tcsetattr(fd,TCSANOW,&newtio);
        pthread_create(&t1, NULL, closeFD, NULL);
        printf("Reading ..\n");
        read(fd,buf,255);
        close(fd);
    }
    return 0;
}

以下代码是相同的,除了我在第二个线程(read())中调用closeFD()并按预期工作。第二个线程解除阻塞并终止,而主线程等待它退出然后退出。

示例2:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>


static int fd = 0;
unsigned char buf[255];
static pthread_t s;
void sigHandler(int sig){
    printf("Signal handler called.\n");
}

void * closeFD(void *arg){
    printf("Second thread started.\n");
    read(fd,buf,255);
    printf("Read interrupted.\n");
}

int main(char *argv[], int argc){
    struct termios newtio;
    pthread_t t1;
    unsigned char buf[255];
    void *res;
        struct sigaction int_handler = {.sa_handler=sigHandler};
        sigaction(SIGUSR1,&int_handler,0);
        s = pthread_self();
        printf("Process id is: %d.\n", getpid());
        fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
        if (fd != -1){
            bzero(&newtio, sizeof(newtio));
            newtio.c_cflag = B2400 | CS7 | CLOCAL | CREAD ;
            newtio.c_iflag = ICRNL;
            newtio.c_oflag = 0;
            newtio.c_lflag = ~ICANON;
            newtio.c_cc[VMIN]     = 14;
            tcsetattr(fd,TCSANOW,&newtio);
            pthread_create(&t1, NULL, closeFD, NULL);
            sleep(2);
            int r = pthread_kill(t1, SIGUSR1);
            pthread_join(t1, &res);
            close(fd);
        }
    return 0;
}

到目前为止,我还没有找到一个特定的引用,说明从一秒钟(在同一个进程中)终止主线程是非法操作,所以我做错了吗?

更新#1
感谢所有回复的人,不过我应该清楚几点:

  1. 我知道在信号处理程序中使用printf是不安全的,但这是一个例子,它不是分段错误的原因,尽管它是一个有效点。将printf()从信号处理程序中取出仍会导致分段错误。示例2使用printf()进出信号处理程序。
  2. 我知道发送SIGUSR不会终止程序。但是,通过使用pthread_kill(pthread_t thread, int signal),它将向线程thread发送一个信号,它将解锁(如果确实被阻止)。这是我想要的动作,这是实例2中实际发生的事情,这是我理解的应该在任何一个例子中发生,但在示例1中没有。
  3. 在描述示例1时,我使用术语'方法',当我的意思是'thread'时,我提到了对pthread_kill()的调用。
  4. 此外,引用'使用POSIX线程编程',David R. Butenhof,第6.6.3节p217'pthread_kill':

      

    在一个进程中,一个线程可以向特定线程发送信号   (包括其自身),致电pthread_kill

    如上所述,以下示例ALSO给出了分段错误:

    示例3

    #include <stdio.h>
    #include <string.h>
    #include <string.h>
    #include <signal.h>
    
    
    
    static pthread_t s;
    int value = 0;
    
    void sigHandler(int sig){
        value = 1;
    }
    
    
    int main(char *argv[], int argc){
    
        struct sigaction int_handler = {.sa_handler=sigHandler};
        sigaction(SIGUSR1,&int_handler,0);
        s = pthread_self();
        printf("The value of 'value' is %d.\n", value);
        printf("Process id is: %d.\n", getpid());
        int r = pthread_kill(s, SIGUSR1);
        printf("The value of 'value' is %d.\n", value);
        return 0;
    }
    

    如果sigaction()的调用被signal()的(非便携式)调用取代,则此操作也会失败。考虑到第三个例子,非常简单,我无法找到任何明确说明这是非法行为的文档。事实上,引用的参考文献表明它是允许的!

2 个答案:

答案 0 :(得分:3)

你忘了#include <pthread.h>。这可以在最近的Linux系统上为示例#3修复你的段错误。

--- pthread_kill-self.c.orig    2015-01-06 14:08:54.949000690 -0600
+++ pthread_kill-self.c 2015-01-06 14:08:59.820998965 -0600
@@ -1,6 +1,6 @@
 #include <stdio.h>
 #include <string.h>
-#include <string.h>
+#include <pthread.h>
 #include <signal.h>

然后......

$:- gcc -o pthread_kill-self pthread_kill-self.c -pthread
$:- ./pthread_kill-self 
The value of 'value' is 0.
Process id is: 3152.
The value of 'value' is 1.

答案 1 :(得分:1)

您正在使用printf(),这不是async-signal safe,并且您未正确初始化struct sigaction(特别是信号掩码未定义)

第三,在安装了处理程序的情况下发送SIGUSR1信号不会也不应该终止主线程。你只是发送一个信号,这就是全部。

正如Jens Gustedt在对原始问题的评论中所提到的,两个程序都有不确定的行为。因此,我不会试图准确猜出未定义行为的哪一部分会导致分段错误(在第一个程序中)。

相反,我会向您展示一个有效的例子。

出于调试/测试目的,我想从基于write(2)的async-signal安全输出函数开始:

#define  _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>

#define  MYSIGNAL  SIGUSR1

#define  SECONDS   10

static int wrstr(const int descriptor, const char *p, const char *const q)
{
    while (p < q) {
        ssize_t n;

        n = write(descriptor, p, (size_t)(q - p));
        if (n > (ssize_t)0)
            p += n;
        else
        if (n != (ssize_t)-1)
            return EIO;
        else
        if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
            return errno;
    }

    return 0;
}

static const char *ends(const char *s)
{
    if (s)
        while (*s != '\0')
            s++;
    return s;
}

static int wrout(const char *const p)
{
    if (p != NULL && *p != '\0') {
        int saved_errno, result;
        saved_errno = errno;
        result = wrstr(STDOUT_FILENO, p, ends(p));
        errno = saved_errno;
        return result;
    } else
        return 0;
}

static int wrouti(const int value)
{
    char          buffer[32];
    char         *p = buffer + sizeof buffer;
    unsigned int  u;

    if (value < 0)
        u = -(long)value;
    else
        u = value;

    do {
        *(--p) = '0' + (u % 10U);
        u /= 10U;
    } while (u > 0U);

    if (value < 0)
        *(--p) = '-';

    return wrstr(STDOUT_FILENO, p, buffer + sizeof buffer);
}

static int wrerr(const char *const p)
{
    if (p != NULL && *p != '\0') {
        int saved_errno, result;
        saved_errno = errno;
        result = wrstr(STDERR_FILENO, p, ends(p));
        errno = saved_errno;
        return result;
    } else
        return 0;
}

上述函数是异步信号安全的,因此可以在信号处理程序中使用。 wrout()wrerr()也保持errno不变,这很有用。顺便说一下,通常省略在信号处理程序中保存和恢复errno,尽管我确实认为有一些奇怪的角落情况可能很重要。 wrouti()只是粗略的十进制有符号整数打印机,也是异步信号安全的,但它不会保持errno不变。

接下来,让我们定义信号处理程序本身,以及它的安装程序功能。 (我喜欢这样做,以使main()更简单。)

static volatile sig_atomic_t handled = 0;

static void handler(int signum)
{
    wrerr("Signal received.\n");
    handled = signum;
}

static int install_handler(const int signum)
{
    struct sigaction act;

    /* memset(&act, 0, sizeof act); */   
    sigemptyset(&act.sa_mask);
    act.sa_handler = handler;
    act.sa_flags = 0;

    if (sigaction(signum, &act, NULL))
        return errno;

    return 0;
}

建议使用已注释掉的memset,但不是正确操作所必需的。但是,sigemptyset()是必需的,用于清除阻塞信号集。

接下来,让我们看一下线程函数。你不应该使用sleep(),因为它与信号相互作用;请改用POSIX.1-2001 nanosleep()

static void *worker(void *target)
{
    struct timespec duration, left;
    int retval;

    wrout("Worker started. Sleeping ");
    wrouti((int)SECONDS);
    wrout(" seconds...\n");

    duration.tv_sec = SECONDS;
    duration.tv_nsec = 0;
    left.tv_sec = 0;
    left.tv_nsec = 0;

    while (1) {
        retval = nanosleep(&duration, &left);
        if (retval == 0)
            break;

        if (left.tv_sec <= 0 ||
            (left.tv_sec == 0 && left.tv_nsec <= 0))
            break;

        duration = left;
        left.tv_sec = 0;
        left.tv_nsec = 0;
    }

    wrout("Sleep complete.\n");

    if (target) {
        wrout("Sending signal...\n");

        retval = pthread_kill(*(pthread_t *)target, MYSIGNAL);
        if (retval == 0)
            wrout("Signal sent successfully.\n");
        else {
            const char *const errmsg = strerror(retval);
            wrout("Failed to send signal: ");
            wrout(errmsg);
            wrout(".\n");
        }
    }

    wrout("Thread done.\n");
    return NULL;
}

指向线程函数的指针应指向信号所指向的线程标识符(pthread_t)。

请注意,如果信号传递到此特定线程或被此特定线程捕获,则nanosleep()可能会被信号传递中断。如果发生这种情况,nanosleep()告诉我们剩下多少时间睡觉。上面的循环显示了如何确保至少在指定时间内休眠,即使被信号传递中断也是如此。

最后,main()。我使用标准输入而不是打开特定设备。要重现OP的程序,请在执行时重定向/dev/ttyUSB0的标准输入,即./program < /dev/ttyUSB0

int main(void)
{
    pthread_t main_thread, worker_thread;
    pthread_attr_t attrs;
    struct termios original, settings;
    int result;

    if (!isatty(STDIN_FILENO)) {
        wrerr("Standard input is not a terminal.\n");
        return EXIT_FAILURE;
    }

    if (tcgetattr(STDIN_FILENO, &original) != 0 ||
        tcgetattr(STDIN_FILENO, &settings) != 0) {
        const char *const errmsg = strerror(errno);
        wrerr("Cannot get terminal settings: ");
        wrerr(errmsg);
        wrerr(".\n");
        return EXIT_FAILURE;
    }

    settings.c_lflag = ~ICANON;
    settings.c_cc[VMIN] = 14;

    if (tcsetattr(STDIN_FILENO, TCSANOW, &settings) != 0) {
        const char *const errmsg = strerror(errno);
        tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
        wrerr("Cannot set terminal settings: ");
        wrerr(errmsg);
        wrerr(".\n");
        return EXIT_FAILURE;
    }

    wrout("Terminal is now in raw mode.\n");

    if (install_handler(MYSIGNAL)) {
        const char *const errmsg = strerror(errno);
        wrerr("Cannot install signal handler: ");
        wrerr(errmsg);
        wrerr(".\n");
        return EXIT_FAILURE;
    }

    main_thread = pthread_self();

    pthread_attr_init(&attrs);
    pthread_attr_setstacksize(&attrs, 65536);

    result = pthread_create(&worker_thread, &attrs, worker, &main_thread);
    if (result != 0) {
        const char *const errmsg = strerror(errno);
        tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
        wrerr("Cannot create a worker thread: ");
        wrerr(errmsg);
        wrerr(".\n");
        return EXIT_FAILURE;
    }

    pthread_attr_destroy(&attrs);

    wrout("Waiting for input...\n");

    while (1) {
        char    buffer[256];
        ssize_t n;

        if (handled) {
            wrout("Because signal was received, no more input is read.\n");
            break;
        }

        n = read(STDIN_FILENO, buffer, sizeof buffer);
        if (n > (ssize_t)0) {
            wrout("Read ");
            wrouti((int)n);
            wrout(" bytes.\n");
            continue;

        } else
        if (n == (ssize_t)0) {
            wrout("End of input.\n");
            break;

        } else
        if (n != (ssize_t)-1) {
            wrout("read() returned an invalid value.\n");
            break;

        } else {
            result = errno;

            wrout("read() == -1, errno == ");
            wrouti(result);
            wrout(": ");
            wrout(strerror(result));
            wrout(".\n");
            break;
        }
    }

    wrout("Reaping the worker thread..\n");
    result = pthread_join(worker_thread, NULL);
    if (result != 0) {
        wrout("Failed to reap worker thread: ");
        wrout(strerror(result));
        wrout(".\n");
    } else
        wrout("Worker thread reaped successfully.\n");

    tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
    wrout("Terminal reverted back to original mode.\n");

    return EXIT_SUCCESS;
}

因为使用终端进行测试会更有趣,所以上面很难在返回之前将终端恢复到原来的状态。

请注意,由于termios结构中的VMIN字段设置为14,因此read()阻塞,直到缓冲区中至少有14个字节可用。如果传送信号,如果缓冲区中至少有一个字节,则返回短计数。因此,您不能指望read()始终返回14个字节,并且无论何时传递信号,您都不能指望-1返回errno == EINTR!尝试这个程序非常有用,可以在脑海中澄清这些。

我不记得Linux中的USB串行驱动程序是否曾生成EPIPE或提升SIGPIPE,但在使用管道时肯定会发生这种情况。使用管道时,最常见的原因是在读取后已经返回零(输入结束)时尝试读取。除非忽略或被信号处理程序捕获,否则该过程与分段错误非常相似,除了原因是SIGPIPE信号而不是SIGSEGV。对于类似终端的角色设备,它依赖于驱动程序,我似乎记得。

最后,我在天气(流感)下编写了上述代码,因此tharrr可能存在错误。它应该是POSIX.1 C99代码,并且gcc -Wall -pedantic不会抱怨,但是有一个脑袋,我没有在这里作出任何承诺。修复非常受欢迎!

有问题吗?评论