我有一个多线程程序。主线程使用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;
.
.
.
答案 0 :(得分:2)
来自ubuntu pthreads信息表(LinuxThreads)):
In addition to the main (initial) thread, and the threads that the
program creates using pthread_create(3), the implementation creates
a "manager" thread. This thread handles thread creation and
termination. (Problems can result if this thread is inadvertently
killed.)
- Signals are used internally by the implementation. On Linux 2.2 and
later, the first three real-time signals are used.
其他实现使用前两个RT信号。将SIGRTMIN设置在线程管理使用的这两个/三个信号之上。看看你的pthreads(7)手册页中有关SIGRTMIN的内容。并相应调整。