c - getchar 和 SIG34,实时事件 34

标签 c linux multithreading signals

我有一个多线程程序。主线程使用 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/

相关文章:

c - 磁盘数据结构的体系结构

c99 转到过去的初始化

c - 外部 undefined symbol

linux - 如何在 Linux 中查看正在使用的文件

c++ - 在appimage中添加资源

android - Realm :添加的记录,无法在其他线程中检索

无法使stm32f4-discovery的ADC工作

linux - 杀死旧进程后启动新进程(如果存在)

ruby-on-rails - ActionMailer::Base.default_url_options线程安全吗?

c++ - 是否有任何不关注 Java 的优秀并发/并行书籍?