POSIX timer hangs up after a few runs
我在程序的主函数中创建了一个 POSIX 计时器。主程序的每个线程都在设置计时器,以便在它到期时,信号处理程序更新一个变量,该变量唤醒同一进程的下一个线程。
计时器大部分时间都可以正常工作,但并非总是如此。它有时会完成完整的执行,而在其他运行中,它会挂起。可能的原因是什么?我的怀疑与信号传递有关。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #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\ "); } 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\ ",turn,sid); fclose(fp); int counter=0; while(1) { ret = sem_wait(&sem[turn]); if (ret) { printf("Error in Sem Post\ "); } //printf("Thread # -> %d\ ",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\ ",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("\ can't create thread :[%s]", strerror(err)); data++; } pthread_exit(NULL); } |
据此,这个程序应该打印
1 2 3 4 5 6 7 8 9 10 | finished 0 finished 1 finished 2 finished 3 finished 4 finished 5 finished 6 finished 7 finished 8 finished 9 |
有时会这样打印,但大多数时候,程序会挂起。
信号处理程序有一个竞争条件。一旦
解决此问题的一种方法是确保在调用
之前增加
1 2 3 4 5 6 7 8 9 10 | 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\ "); } } |
请注意,此解决方案仍然存在一个问题。它不能确保