几次运行后POSIX计时器挂起

时间:2016-04-27 04:44:43

标签: c linux multithreading timer posix

我在程序的主要功能中创建了一个POSIX计时器。主程序的每个线程都设置定时器,以便在它到期时,信号处理程序更新一个唤醒同一进程的下一个线程的变量。

计时器大部分时间都正常工作,但并非总是如此。它有时会完成完整的执行,而在其他运行中,它会挂断。可能的原因是什么?我的怀疑与信号传递有关。

以下是代码:

#define _GNU_SOURCE
#define _POSIX_C_SOURCE 199309
#include <sched.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syscall.h>
#define NUM_THREADS 10

#define CLOCKID CLOCK_REALTIME
#define SIG SIGUSR1
int ret;
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
long long freq_nanosecs;
sigset_t mask;
struct sigaction sa;

sem_t sem[NUM_THREADS];
sem_t mute;

pthread_t tid[NUM_THREADS];
int state = 0;

static void handler(int sig, siginfo_t *si, void *uc)
{
    ret = sem_post(&sem[(state+1)%NUM_THREADS]);
        if (ret)
        {
            printf("Error in Sem Post\n");
        }
        state++;
}

void *threadA(void *data_)
{  
    int i = 0, s,n,value;

    long int loopNum;
    int turn = (intptr_t)data_;
    struct timespec tval_result,tval_result2;

    int sid = syscall(SYS_gettid);
    FILE *fp;
    fp=fopen("ipc.out","a");    
    fprintf(fp,"thread_%d %d\n",turn,sid);  
    fclose(fp); 

    int counter=0;

    while(1)
    {
        ret = sem_wait(&sem[turn]);
        if (ret)
        {
            printf("Error in Sem Post\n");
        }
        //printf("Thread # -> %d\n",turn);

        its.it_value.tv_sec = 0;
        its.it_value.tv_nsec = 14000;
        its.it_interval.tv_sec = 0;
        its.it_interval.tv_nsec = 0;

        ret = timer_settime(timerid, 0, &its, NULL);
        if ( ret < 0 )
            perror("timer_settime");

        // Some heavy work

    counter++;

    if(counter==100)
    break;
    }
    printf("finished %d\n",turn);

}

int main(int argc, char *argv[])
{
    int data = 0;
    int err,i;

    sa.sa_flags = SA_RESTART;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    sigaction(SIG, &sa, NULL);

    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIG;
    sev.sigev_value.sival_ptr = &timerid;
    ret = timer_create(CLOCKID, &sev, &timerid);
    if ( ret < 0 )
        perror("timer_create");

    sem_init(&sem[0], 0, 1); 
    for ( i = 1; i < NUM_THREADS; ++i)
        {
            sem_init(&sem[i], 0, 0); 
        }

    while(data < NUM_THREADS)
    {
        //create our threads
        err = pthread_create(&tid[data], NULL, threadA, (void *)(intptr_t)data);
        if(err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));

        data++;
    }

    pthread_exit(NULL);
}

据此,该程序应该打印

finished 0
finished 1
finished 2
finished 3
finished 4
finished 5
finished 6
finished 7
finished 8
finished 9

有些时候它会像这样打印,但大部分时间,程序都会挂断。

1 个答案:

答案 0 :(得分:1)

信号处理程序具有竞争条件。一旦sem_post被调用,其他一个线程就可以开始运行,并且它的计时器可以在当前信号处理程序完成之前触发。这将导致在另一个线程中再次调用信号处理程序。此时state没有被第一个线程递增,因此第二个信号处理程序调用将最终在错误的信号量上调用sem_post

解决此问题的一种方法是在调用state之前确保sem_post递增:

static void handler(int sig, siginfo_t *si, void *uc)
{
    state++;
    ret = sem_post(&sem[(state)%NUM_THREADS]);
    if (ret)
    {
        printf("Error in Sem Post\n");
    }
}

请注意,此解决方案仍然包含一个问题。它不能确保printf调用的顺序正确。