多线程程序的行为不符合信号处理的预期

时间:2015-02-18 07:15:34

标签: c linux multithreading signals

我编写了C代码,以了解pthreads在接收信号时的行为方式。为此,我设置了一个使用sigaction的信号处理程序,使用fork来创建子进程,从子进程设置pthreads的数量,并从父进程向子进程发送信号。我从信号处理程序打印线程的id。

我已经读过,当进程有多个线程时,信号将由任何线程处理,并且行为未定义(不确定哪个线程将处理信号)。但我在我的程序中观察到的是,每次信号处理程序只打印一个线程的id,这似乎是父线程的id。我希望每次发送信号时都会打印任意线程的随机ID,但这种情况不会发生。有人可以对此有所了解吗?

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h> 

pthread_t tid[5];

void* doSomeThing2(void *arg)
{
    int i;
    printf("In thread handler.my pid is %x\n",pthread_self());
    i=5;
    while(i--)
    {
            sleep(1);
    }
    return NULL;
}

static void my_completion_handler( int signo, siginfo_t *info, void *context )
{
    int ret;

    printf("In completion handler.my pid is %x\n",pthread_self());

    return;
}

int main()
{
    int ret,err;
    struct sigaction sig_act;
    pid_t mypid;
    int i,j;

    /* Setup the signal handler */
    sigemptyset(&sig_act.sa_mask);
    sig_act.sa_flags = SA_SIGINFO;
    sig_act.sa_sigaction = my_completion_handler;

    /* Map the Signal to the Signal Handler */
    ret = sigaction( SIGTERM, &sig_act, NULL );

    mypid=fork();

    switch(mypid) {
            case -1:
                    printf("fork failed\n");
                    return -1;
            case 0:
                    /* child process */
                    printf("In child process,my pid is %x\n",pthread_self());
                    for(j=0;j<5;j++) {
                            err = pthread_create(&(tid[j]), NULL, &doSomeThing2, NULL);
                            if (err != 0)
                                    printf("\ncan't create thread :[%s]", strerror(err));
                    }

                    for(j=0;j<5;j++)
                    {
                            pthread_join(tid[j], NULL);
                    }
                    exit(0);
            default:
                    /* parent process */
                    printf("Got child pid of %d\n",mypid);
                    i=5;
                    while(i--)
                    {
                            sleep(1);
                            printf("Parent sending signal to child \n");
                            kill(mypid,SIGTERM);
                    }

                    break;
    }

    return 0;
}

当我执行这个程序时,我得到了以下结果:

root@sri-ThinkCentre-M72e:/home/sri/Downloads# ./signal-fork 
Got child pid of 10049
In child process,my pid is b7573700
In thread handler.my pid is b6d71b40
In thread handler.my pid is b6570b40
In thread handler.my pid is b5d6fb40
In thread handler.my pid is b7572b40
In thread handler.my pid is b556eb40
Parent sending signal to child 
In completion handler.my pid is b7573700
Parent sending signal to child 
In completion handler.my pid is b7573700
Parent sending signal to child 
In completion handler.my pid is b7573700
Parent sending signal to child 
In completion handler.my pid is b7573700
Parent sending signal to child 
In completion handler.my pid is b7573700

0 个答案:

没有答案