简体   繁体   English

在线程库的C函数中添加信号处理程序

[英]Adding signal handler to a function in C for a thread library

I am writing a basic user level thread library. 我正在编写一个基本的用户级线程库。 The function prototype for thread creation is thr_create (start_func_pointer,arg) { make_context(context_1,start_func) } 用于创建线程的函数原型为thr_create (start_func_pointer,arg) { make_context(context_1,start_func) }

start_func will be user defined and can change depending on user/program start_func将由用户定义,并且可以根据用户/程序进行更改

once after creation of thread, if I start executing it using swapcontext(context_1,context_2) 创建线程后一次,如果我开始使用swapcontext(context_1,context_2)执行它

the function start_func would start running. 函数start_func将开始运行。 Now , if a signal comes in , I need to handle it. 现在,如果有信号进入,我需要处理它。 Unfortunately, I just have the handle to start_func so I cant really define signal action inside the start_func 不幸的是,我只有start_func的句柄,所以我无法在start_func内真正定义信号动作

is there a way I can add a signal handling structure inside the start_function and point it to my code. 有没有一种方法可以在start_function中添加信号处理结构并将其指向我的代码。 something like this 像这样的东西

thr_create (start_func_pointer,arg) { start_func.add_signal_hanlding_Structure = my_signal_handler(); make_context(context_1,start_func) } thr_create (start_func_pointer,arg) { start_func.add_signal_hanlding_Structure = my_signal_handler(); make_context(context_1,start_func) } Does anybody know how posix does it ? thr_create (start_func_pointer,arg) { start_func.add_signal_hanlding_Structure = my_signal_handler(); make_context(context_1,start_func) }有人知道posix是怎么做的吗?

If you are talking about catching real signals from the actual operating system you are running on I believe that you are going to have to do this application wide and then pass the signals on down into each thread (more on this later). 如果您正在谈论从实际的操作系统中捕获实际信号,那么我相信您将必须在整个应用程序中进行操作,然后将信号向下传递到每个线程中(稍后再介绍)。 The problem with this is that it gets complicated if two (or more) of your threads are trying to use alarm which uses SIGALRM -- when the real signal happens you can catch it, but then who do you deliver it to (one or all of the threads?). 问题是,如果您的两个(或多个)线程试图使用使用SIGALRM alarm会变得很复杂-当真正的信号发生时,您可以捕获它,但是将信号传递给谁(一个或全部)的线程?)。

If you are talking about sending and catching signals just among the threads within a program using your library then sending a signal to a thread would cause it to be marked ready to run, even if it were waiting on something else previously, and then any signal handling functionality would be called from your thread resume code. 如果您正在谈论使用库在程序中的线程之间发送和捕获信号,那么向线程发送信号将导致该信号被标记为可以运行,即使它之前正在等待其他东西,然后再发出任何信号处理功能将从您的线程恢复代码中调用。 If I remember from your previous questions you had a function called thread_yield which was called to allow the next thread to run. 如果我记得您以前的问题,则有一个名为thread_yield的函数被调用以允许下一个线程运行。 If this is the case then thread_yield needs to check a list of pending signals and preform their actions before returning to where ever thread_yield was called (unless one of the signal handlers involved killing the current thread, in which case you have to do something different). 如果是这种情况,那么thread_yield需要检查待处理信号列表并执行其操作,然后再返回到调用thread_yield位置(除非其中一个信号处理程序涉及杀死当前线程,在这种情况下,您必须执行其他操作) 。

As far as how to implement registering of signal handlers, in POSIX that is done by system calls made by the main function (either directly or indirectly). 至于如何实现信号处理程序的注册,在POSIX中是由主要功能(直接或间接)进行的系统调用完成的。 So you could have: 因此,您可以:

static int foo_flag = 0;

static void foo_handle(int sig) {
     foo_flag = 1;
}

int start_func(void * arg) {
    thread_sig_register(SIGFOO, foo_handle);

    thread_pause();
    // this is a function that you could write that would cause the current thread
    // to mark itself as not ready to run and then call thread_yield, so that
    // thread_pause() will return only after something else (a signal) causes the
    // thread to become ready to run again.


    if (foo_flag) {
        printf("I got SIGFOO\n");
    } else {
        printf("I don't know what woke me up\n");
    }
    return 0;
}

Now, from another thread you can send this thread a SIGFOO (which is just a signal I made up for demonstration purposes). 现在,您可以从另一个线程向该线程发送一个SIGFOO(这只是我为演示目的制作的信号)。

Each of your thread control blocks (or whatever you are calling them) will have to have a signal handler table (or list, or something) and a pending signal list or a way to mark the signals as pending. 每个线程控制块(或您要调用的任何块)都必须具有信号处理程序表(或列表,或其他内容)和未决的信号列表,或将信号标记为未决的方法。 The pending signals will be examined (possibly in some priority based order) and the handler action is done for each pending signal before returning to that threads normal code. 将检查待处理信号(可能以某种优先级顺序),并在返回该线程正常代码之前为每个待处理信号执行处理程序操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM