11_alarm()和可重入函数

pause()函数
   将进程置为可中断睡眠状态。然后它调用内核函数schedule(),使linux进程调度器找到另一个进程来运行。
   pause使调用者进程挂起,直到一个信号被捕获
NAME
       alarm - set an alarm clock for delivery of a signal

SYNOPSIS
       #include <unistd.h>

       unsigned int alarm(unsigned int seconds);

DESCRIPTION
       alarm()  arranges for a SIGALRM signal to be delivered to the calling process in
       seconds seconds.

       If seconds is zero, any pending alarm is canceled.

       In any event any previously set alarm() is canceled.

RETURN VALUE
       alarm() returns the number of seconds remaining until any  previously  scheduled
       alarm  was  due  to  be  delivered, or zero if there was no previously scheduled
       alarm.
/*
    信号处理函数遇上可重入和不可重入函数
    可重入函数概念
    1、为了增强程序的稳定性,在信号处理函数中应使用可重入函数。 
    2、所谓可重入函数是指一个可以被多个任务调用的过程,任务在调用时
      不必担心数据是否会出错。因为进程在收到信号后,就将跳转到信号处理
      函数去接着执行。如果信号处理函数中使用了不可重入函数,那么信号
      处理函数可能会修改原来进程中不应该被修改的数据,这样进程从信号
      处理函数中返回接着执行时,可能会出现不可预料的后果。不可再入函数
      在信号处理函数中被视为不安全函数。
  3、满足下列条件的函数多数是不可重入的:
  (1)使用静态的数据结构,如getlogin(),gmtime(),getgrgid(),getgrnam(),
            getpwuid()以及getpwnam()等等;
  (2)函数实现时,调用了malloc()或者free()函数;
  (3)实现时使用了标准I/O函数的
*/
/*
    alarm函数,设置一个闹钟延迟发送信号
    告诉linux内核n秒中以后,发送SIGALRM信号;

    使用命令发送信号:
        hzmct@U-64:~$ kill -14  pid
        hzmct@U-64:~$ kill -ALRM  pid
        hzmct@U-64:~$ kill -SIGALRM  pid
*/

//使用闹钟周期性发送SIGALRM信号
#if 1
void  myhandle(int num)
{
    printf("recv signal id num : %d \n", num);
    //kill -alram ` ps -aux | grep 01aram | grep -v vi | awk '{print $2}' ` 
    alarm(1);//信号调用函数调用alarm(),可以周期1s发送此信号
}
void test()
{
    printf("main ....begin\n");
    //注册SIGALRM 信号处理函数
    if (signal(SIGALRM, myhandle) == SIG_ERR)
    {
        perror("func signal err\n");
        return ;
    } 
    //间接递归
    //myhandle----->alarm=====>myhandle
    alarm(1);//延迟1s发送一个信号SIGALRM

    while(1) 
    {
        pause();
        printf("pause return\n");
    }
}

#endif
/*
在信号处理函数中,尽量不使用全局变量和静态变量的函数。
    特别是这个变量在程序中随时读写。
    可能会出现age30 num40的现象
*/
#if 0
typedef struct _Teacher
{
    int age;
    int num;    
}Teacher;

Teacher g_t;//全局变量

void printfGlobTeacher()
{
    printf("g_t.age:%d \n", g_t.age);
    printf("g_t.num:%d \n", g_t.num);
    //p = malloc(100);
}

void  myhandle(int num)
{
    printf("recv signal id num : %d \n", num);
    printfGlobTeacher();
    alarm(1);
}

void test()
{
    Teacher t1, t2;
    t1.age = 30;
    t1.num = 30;
    t2.age = 40;
    t2.num = 40;
    printf("main ....begin\n");
    //注册信号处理函数
    if (signal(SIGALRM, myhandle) == SIG_ERR)
    {
        perror("func signal err\n");
        return ;
    } 
    alarm(1);
    //不断的修改全局变量的值
    while(1) 
    {
        g_t = t1;
        g_t = t2;
    }
}
#endif

//2次调用alarm
#if  0
void sig_arm(int signo)
{
    printf("收到SIGALRM 信号\n");
    system("date");
    _exit(0);

}
void test()
{
    signal(SIGALRM, sig_arm);
    system("date");
    alarm(20);
    sleep(4);
    //再次调用alarm时,上一次的alarm()时间没用完,剩余的秒数16作为本次
    //alarm的初始值,16s以后
    printf("剩余的秒数为 = %d\n", alarm(30));//30不起作用
    pause();

}
//整个程序的执行时间为  4 + 16 = 20s
/*
    hzmct@U-64:/study/linuxtest/day02/01signal$ ./dm04_alarm
    2018年 08月 21日 星期二 13:12:12 CST
    剩余的秒数为 = 16
    收到SIGALRM 信号
    2018年 08月 21日 星期二 13:12:46 CST

*/
#endif
/*
 alarm(0)取消以前闹钟的设置时间,其余留值作为alarm()
 的返回值
*/
#if  0
void sig_arm(int signo)
{
    printf("收到SIGALRM 信号\n");
    system("date");
    _exit(0);

}
void test()
{
    signal(SIGALRM, sig_arm);
    system("date");
    alarm(10);
    sleep(4);
    printf("剩余的秒数为 = %d\n", alarm(0));//
    pause();
}
/*
    运行结果:此时调用pause后,因没有捕捉到其他信号处理函数
  ,造成函数永久被挂起并且因为pause阻塞
    hzmct@U-64:/study/linuxtest/day02/01signal$ ./dm04_alarm
    2018年 08月 21日 星期二 13:07:23 CST
    剩余的秒数为 = 6

*/
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化这段代码: IF VR(v_alarm1).0 <> ax_alarm.ax_dial THEN VR(v_alarm1).0 = ax_alarm.ax_dial IF VR(v_alarm1).1 <> ax_alarm.ax_scr1_updown THEN VR(v_alarm1).1 = ax_alarm.ax_scr1_updown IF VR(v_alarm1).2 <> ax_alarm.ax_scr1_halftone THEN VR(v_alarm1).2 = ax_alarm.ax_scr1_halftone IF VR(v_alarm1).3 <> ax_alarm.ax_scr1_scraper THEN VR(v_alarm1).3 = ax_alarm.ax_scr1_scraper IF VR(v_alarm1).4 <> ax_alarm.ax_scr2_updown THEN VR(v_alarm1).4 = ax_alarm.ax_scr2_updown IF VR(v_alarm1).5 <> ax_alarm.ax_scr2_halftone THEN VR(v_alarm1).5 = ax_alarm.ax_scr2_halftone IF VR(v_alarm1).6 <> ax_alarm.ax_scr2_scraper THEN VR(v_alarm1).6 = ax_alarm.ax_scr2_scraper IF VR(v_alarm1).7 <> ax_alarm.ax_scr3_updown THEN VR(v_alarm1).7 = ax_alarm.ax_scr3_updown IF VR(v_alarm1).8 <> ax_alarm.ax_scr3_halftone THEN VR(v_alarm1).8 = ax_alarm.ax_scr3_halftone IF VR(v_alarm1).9 <> ax_alarm.ax_scr3_scraper THEN VR(v_alarm1).9 = ax_alarm.ax_scr3_scraper IF VR(v_alarm1).10 <> ax_alarm.ax_goin_spin THEN VR(v_alarm1).10 = ax_alarm.ax_goin_spin IF VR(v_alarm1).11 <> ax_alarm.ax_output_spin THEN VR(v_alarm1).11 = ax_alarm.ax_output_spin IF VR(v_alarm1).12 <> ax_alarm.ax_tl THEN VR(v_alarm1).12 = ax_alarm.ax_tl IF VR(v_alarm1).13 <> ax_alarm.ax_work1 THEN VR(v_alarm1).13 = ax_alarm.ax_work1 IF VR(v_alarm1).14 <> ax_alarm.ax_work2 THEN VR(v_alarm1).14 = ax_alarm.ax_work2 IF VR(v_alarm1).15 <> ax_alarm.ax_work3 THEN VR(v_alarm1).15 = ax_alarm.ax_work3 IF VR(v_alarm2).0 <> ax_alarm.ax_work4 THEN VR(v_alarm2).0 = ax_alarm.ax_work4 IF VR(v_alarm2).1 <> ax_alarm.ax_work5 THEN VR(v_alarm2).1 = ax_alarm.ax_work5 IF VR(v_alarm2).2 <> ax_alarm.ax_work6 THEN VR(v_alarm2).2 = ax_alarm.ax_work6 IF VR(v_alarm2).3 <> ax_alarm.ax_work7 THEN VR(v_alarm2).3 = ax_alarm.ax_work7 IF VR(v_alarm2).4 <> ax_alarm.ax_work8 THEN VR(v_alarm2).4 = ax_alarm.ax_work8 IF VR(v_alarm2).5 <> ax_alarm.ax_work9 THEN VR(v_alarm2).5 = ax_alarm.ax_work9 IF VR(v_alarm2).6 <> ax_alarm.ax_work10 THEN VR(v_alarm2).6 = ax_alarm.ax_work10 IF VR(v_warn1).0 <> ax_warn.ax_dial THEN VR(v_warn1).0 = ax_warn.ax_dial IF VR(v_warn1).1 <> ax_warn.ax_scr1_updown THEN VR(v_warn1).1 = ax_warn.ax_scr1_updown IF VR(v_warn1).2 <> ax_warn.ax_scr1_halftone THEN VR(v_warn1).2 = ax_warn.ax_scr1_halftone IF VR(v_warn1).3 <> ax_warn.ax_scr1_scraper THEN VR(v_warn1).3 = ax_warn.ax_scr1_scraper IF VR(v_warn1).4 <> ax_warn.ax_scr2_updown THEN VR(v_warn1).4 = ax_warn.ax_scr2_updown IF VR(v_warn1).5 <> ax_warn.ax_scr2_halftone THEN VR(v_warn1).5 = ax_warn.ax_scr2_halftone IF VR(v_warn1).6 <> ax_warn.ax_scr2_scraper THEN VR(v_warn1).6 = ax_warn.ax_scr2_scraper IF VR(v_warn1).7 <> ax_warn.ax_scr3_updown THEN VR(v_warn1).7 = ax_warn.ax_scr3_updown IF VR(v_warn1).8 <> ax_warn.ax_scr3_halftone THEN VR(v_warn1).8 = ax_warn.ax_scr3_halftone IF VR(v_warn1).9 <> ax_warn.ax_scr3_scraper THEN VR(v_warn1).9 = ax_warn.ax_scr3_scraper IF VR(v_warn1).10 <> ax_warn.ax_goin_spin THEN VR(v_warn1).10 = ax_warn.ax_goin_spin IF VR(v_warn1).11 <> ax_warn.ax_output_spin THEN VR(v_warn1).11 = ax_warn.ax_output_spin IF VR(v_warn1).12 <> ax_warn.ax_tl THEN VR(v_warn1).12 = ax_warn.ax_tl IF VR(v_warn1).13 <> ax_warn.ax_work1 THEN VR(v_warn1).13 = ax_warn.ax_work1 IF VR(v_warn1).14 <> ax_warn.ax_work2 THEN VR(v_warn1).14 = ax_warn.ax_work2 IF VR(v_warn1).15 <> ax_warn.ax_work3 THEN VR(v_warn1).15 = ax_warn.ax_work3 IF VR(v_warn2).0 <> ax_warn.ax_work4 THEN VR(v_warn2).0 = ax_warn.ax_work4 IF VR(v_warn2).1 <> ax_warn.ax_work5 THEN VR(v_warn2).1 = ax_warn.ax_work5 IF VR(v_warn2).2 <> ax_warn.ax_work6 THEN VR(v_warn2).2 = ax_warn.ax_work6 IF VR(v_warn2).3 <> ax_warn.ax_work7 THEN VR(v_warn2).3 = ax_warn.ax_work7 IF VR(v_warn2).4 <> ax_warn.ax_work8 THEN VR(v_warn2).4 = ax_warn.ax_work8 IF VR(v_warn2).5 <> ax_warn.ax_work9 THEN VR(v_warn2).5 = ax_warn.ax_work9 IF VR(v_warn2).6 <> ax_warn.ax_work10 THEN VR(v_warn2).6 = ax_warn.ax_work10
03-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值