我有一个多线程程序。主线程使用 getchar 来关闭所有其他线程及其自身。我在其中一个子线程中使用了计时器功能。该线程使用 SIG34 来实现计时器到期。
在某些时候,我收到SIG34
如下。这影响了我的主线程中的 getchar,我的程序只是中止了。请帮助我理解相同的内容。
Program received signal SIG34, Real-time event 34.
0x00007ffff6ea38cd in read () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0 0x00007ffff6ea38cd in read () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff6e37ff8 in _IO_file_underflow () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff6e3903e in _IO_default_uflow () from /lib/x86_64-linux-gnu/libc.so.6
#3 0x00007ffff6e2fb28 in getchar () from /lib/x86_64-linux-gnu/libc.so.6
#4 0x0000000000401eef in main (argc=1, argv=0x7fffffffe178) at ../../src/SimMain.c:186
笔记:
在子线程中,我已经为定时器信号分配了 SIGRTMIN(在我的系统上转换为 SIG34)并且还有一个处理程序。此处理程序设置一个全局变量,让我更改课程后计时器到期。但不确定为什么 getchar 存在问题。
定时器初始化和使用:
/* Timer macros */
#define CLOCKID CLOCK_REALTIME
#define SIGRT_OFFSET 4 // was 0 before, hence, SIG34, now it is SIG38
#define SIG (SIGRTMIN + SIGRT_OFFSET)
void cc_timer_init()
{
// Install the timer handler...
struct sigevent sev;
long long freq_nanosecs;
struct sigaction disc_action;
/* Establish timer_handler for timer signal */
memset (&disc_action, 0, sizeof (disc_action));
disc_action.sa_flags = SA_SIGINFO; //0 before
disc_action.sa_sigaction = disc_timer_handler;
sigaction(SIG, &disc_action, NULL);
myState = INIT_STATE;
/* Create the timer */
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
timer_create(CLOCKID, &sev, &timerid);
/* Set itimerspec to start the timer */
freq_nanosecs = TMR_TV_NSEC;
v_itimerspec.it_value.tv_sec = TMR_TV_SEC;
v_itimerspec.it_value.tv_nsec = freq_nanosecs % 1000000000;
v_itimerspec.it_interval.tv_sec = 0;
v_itimerspec.it_interval.tv_nsec = 0;
}
static void disc_timer_handler(int sig, siginfo_t *si, void *uc)
{
/* Global variable that I set */
State = MID_1_STATE;
}
/* In another part...*/
.
.
.
case INIT_STATE :
{
v_itimerspec.it_value.tv_sec = TMR_TV_SEC;
timer_settime(timerid, 0, &v_itimerspec, NULL);
ret_val = SUCCESS;
}
break;
.
.
.