我有一个多线程程序。主线程使用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),并且也有一个处理程序。该处理程序设置了一个全局变量,以允许我更改类(class)设置的计时器到期时间。但是不确定为什么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;
.
.
.
最佳答案
从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的内容。并据此进行调整。
关于c - getchar和SIG34,实时事件34,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19003940/