9.信号-异步通信方式//依据RT-Thread内核编程的学习记录(非内核实现)

信号,信号不同于一般得IPC变量,信号不需要初始化,也没有删除;信号有安装,监控,发送,响应的操作。为什么没有安装操作的原因是,信号大部分是被系统所使用的,是已经创建过的,信号室友具体标号的,例如我们能使用的SIGUSR1,SIGUSR2;还有我们可以自己定义的部分。

/*
* Each of the following macros expand to a positive integral constant
* expression that is the signal number corresponding the the specified
* condition.
*/
#define SIGABRT   1 /* abort                         */
#define SIGFPE    2 /* arithmetic exception          */
#define SIGILL    3 /* illegal instruction           */
#define SIGINT    4 /* attention request from user   */
#define SIGSEGV   5 /* bad memory access             */
#define SIGTERM   6 /* termination request           */
#endif

/* (these following macros are not part of the ANSI standard,
* but private to this implementation)
*/
#define SIGSTAK   7  /* stack overflow                */
#define SIGRTRED  8  /* run-time redirection error    */
#define SIGRTMEM  9  /* run-time memory error         */
/* Signal numbers 10 and 11 are available for the user */
#define SIGUSR1  10
#define SIGUSR2  11
#define SIGPVFN  12 /* pure virtual function called   */
#define SIGCPPL  13 /* miscellaneous exception from C++ library */
#define SIGOUTOFHEAP 14 /* ::operator new or new[] cannot allocate memory */
/* Signal numbers 15-31 are reserved to the implementation */
/* Signal numbers 32 and larger are for more user signals */


/*不过在我学得这个版本里,还有没有32以上的*/
//因为
#define sig_mask(sig_no)    (1u << sig_no)
#define sig_valid(sig_no)   (sig_no >= 0 && sig_no < RT_SIG_MAX)

1.信号的安装


/* 安装信号 */
    rt_signal_install(SIGUSR1, Signal_Handler_Function);


/*你可以理解Signal_Handler_Function是中断函数入口*/

2.信号的监控

/*信号的监控*/

rt_signal_unmask(SIGUSR1);

//SIGUSR1就是我上面提到过的,系统预留给我们的部分

/*
/* Signal numbers 10 and 11 are available for the user */
#define SIGUSR1  10
#define SIGUSR2  11

*/

3.信号的发送

/*信号的发送*/

rt_thread_kill(tid1, SIGUSR1);

/*tid1指我们初始化已经运行的线程,SIGUSR1指信号*/

4.信号的响应

/*信号的接收*/
void Signal_Handler_Function(int sig)
{
    //sig指接收到的信号

}
/*不同于IPC,信号没有等待接受的部分*/

//例程中指出信号会有丢失的可能,在多对一的情况下,的确可能造成信号的丢失

5.例程

/*
 * 程序清单:信号例程
 *
 * 这个例子会创建一个线程,线程安装信号,然后给这个线程发送信号。
 *
 */
#include <rtthread.h>

#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5

static rt_thread_t tid1 = RT_NULL;

/* 线程1的信号处理函数 */
void thread1_signal_handler(int sig)
{
    rt_kprintf("thread1 received signal %d\n", sig);
}

/* 线程1的入口函数 */
static void thread1_entry(void *parameter)
{
    int cnt = 0;

    /* 安装信号 */
    rt_signal_install(SIGUSR1, thread1_signal_handler);
    rt_signal_unmask(SIGUSR1);

    /* 运行10次 */
    while (cnt < 10)
    {
        /* 线程1采用低优先级运行,一直打印计数值 */
        rt_kprintf("thread1 count : %d\n", cnt);

        cnt++;
        rt_thread_mdelay(100);
    }
}

/* 信号示例的初始化 */
int signal_sample(void)
{
    /* 创建线程1 */
    tid1 = rt_thread_create("thread1",
                            thread1_entry, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
    
    if (tid1 != RT_NULL)       
        rt_thread_startup(tid1);

    rt_thread_mdelay(300);

    /* 发送信号 SIGUSR1 给线程1 */
    rt_thread_kill(tid1, SIGUSR1);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiangxistu/article/details/82828127
今日推荐