• 竞态条件与sigsuspend函数


    一、利用pause和alarm函数实现sleep函数

    #include <unistd.h>

    int pause(void);
    pause函数使调用进程挂起直到有信号递达。如果信号的处理动作是终止进程,则进程终止,pause函数没有机会返回;如果信号的处理动作是忽略,则进程继续处于挂起状态,pause不返回;如果信号的处理动作是捕捉,则调用了信号处理函数之后pause返回-1,errno设置为EINTR,所以pause只有出错的返回值。错误码EINTR表示“被信号中断”。

    alarm函数可以参考这里

    下面使用pause和alarm实现sleep(3)函数,称为mysleep:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    #include<stdio.h>
    #include<signal.h>
    #include<unistd.h>

    void sig_alrm(int signo)
    {
        /* nothing to do */
    }


    unsigned int mysleep(unsigned int nsecs)
    {
        struct sigaction newact, oldact;
        unsigned int unslept;

        newact.sa_handler = sig_alrm;
        sigemptyset(&newact.sa_mask);
        newact.sa_flags = 0;
        sigaction(SIGALRM, &newact, &oldact);

        alarm(nsecs);
        pause();

        unslept = alarm(0);
        sigaction(SIGALRM, &oldact, NULL);

        return unslept;
    }

    int main(void)
    {
        while (1)
        {
            mysleep(2);
            printf("Two seconds passed ");
        }
        return 0;
    }

    1. main函数调用mysleep函数,后者调用sigaction注册了SIGALRM信号的处理函数sig_alrm。
    2. 调用alarm(nsecs)设定闹钟。
    3. 调用pause等待,内核切换到别的进程运行。
    4. nsecs秒之后,闹钟超时,内核发SIGALRM给这个进程。
    5. 从内核态返回这个进程的用户态之前处理未决信号,发现有SIGALRM信号,其处理函数是sig_alrm。
    6. 切换到用户态执行sig_alrm函数,进入sig_alrm函数时SIGALRM信号被自动屏蔽,从sig_alrm函数返回时SIGALRM信号自动解除屏蔽。然后自动执行系统调用sigreturn再次进入内核,再返回用户态继续执行进程的主控制流程(main函数调用的mysleep函数)。
    7. pause函数返回-1,然后调用alarm(0)取消闹钟,调用sigaction恢复SIGALRM信号以前的处理动作。


    需要注意的是虽然sig_alrm函数什么都没干,但还是得注册作为SIGALRM的处理函数,因为SIGALRM信号的默认处理是终止进程,这也是在mysleep函数返回时要恢复SIGALRM信号原来的sigaction的原因。此外,mysleep函数的返回值表示“未睡到”的时间,即unslept,当尚未计时到nsecs而pause函数先被其他信号处理函数所中断返回,在外界看来就是在sleep期间被其他信号处理函数中断了,则mysleep返回非0值,即unslept。如sleep(3)的man 手册写的返回值:

    RETURN VALUE: Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted  by  a signal handler.

    当然如果是被SIGALRM handler所中断,则表示睡眠时间到,mysleep返回值为0。


    二、竞态条件与sigsuspend函数

    现在重新审视上面的mysleep函数,设想这样的时序:

    1. 注册SIGALRM信号的处理函数。
    2. 调用alarm(nsecs)设定闹钟。
    3. 内核调度优先级更高的进程取代当前进程执行,并且优先级更高的进程有很多个,每个都要执行很长时间
    4. nsecs秒钟之后闹钟超时了,内核发送SIGALRM信号给这个进程,处于未决状态。
    5. 优先级更高的进程执行完了,内核要调度回这个进程执行。SIGALRM信号递达,执行处理函数sig_alrm之后再次进入内核。
    6. 返回这个进程的主控制流程,alarm(nsecs)返回,调用pause()挂起等待。
    7. 可是SIGALRM信号已经处理完了,还等待什么呢?
    出现这个问题的根本原因是系统运行的时序(Timing)并不像我们写程序时所设想的那样。虽然alarm(nsecs)紧接着的下一行就是pause(),但是无法保证pause()一定会在调用alarm(nsecs)之后的nsecs秒之内被调用。由于异步事件在任何时候都有可能发生(这里的异步事件指出现更高优先级的进程),如果我们写程序时考虑不周密,就可能由于时序问题而导致错误,这叫做竞态条件(Race Condition)。


    如何解决上述问题呢?我们可能会想到,在调用pause之前屏蔽SIGALRM信号使它不能提前递达就可以了。看看以下方法可行吗?
    1. 屏蔽SIGALRM信号;
    2. alarm(nsecs);
    3. 解除对SIGALRM信号的屏蔽;
    4. pause();


    从解除信号屏蔽到调用pause之间存在间隙,SIGALRM仍有可能在这个间隙递达。要消除这个间隙,我们把解除屏蔽移到pause后面可以吗?
    1. 屏蔽SIGALRM信号;
    2. alarm(nsecs);
    3. pause();
    4. 解除对SIGALRM信号的屏蔽;


    这样更不行了,还没有解除屏蔽就调用pause,pause根本不可能等到SIGALRM信号。要是“解除信号屏蔽”和“挂起等待信号”这两步能合并成一个原子操作就好了,这正是sigsuspend函数的功能。sigsuspend包含了pause的挂起等待功能,同时解决了竞态条件的问题,在对时序要求严格的场合下都应该调用sigsuspend而不是pause。


    #include <signal.h>

    int sigsuspend(const sigset_t *sigmask);


    和pause一样,sigsuspend没有成功返回值,只有执行了一个信号处理函数之后sigsuspend才返回,返回值为-1,errno设置为EINTR。

    调用sigsuspend时,进程的信号屏蔽字由sigmask参数指定,可以通过指定sigmask来临时解除对某个信号的屏蔽,然后挂起等待,当sigsuspend返回

    时,进程的信号屏蔽字恢复为原来的值,如果原来对该信号是屏蔽的,从sigsuspend返回后仍然是屏蔽的。

    “If a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask of the process is set to its value before the call to sigsuspend.”

    以下用sigsuspend重新实现mysleep函数:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
     
    /*************************************************************************
        > File Name: mysleep.c
        > Author: Simba
        > Mail: dameng34@163.com
        > Created Time: 2012年12月16日 星期日 21时30分42秒
     ************************************************************************/

    #include<stdio.h>
    #include<signal.h>
    #include<unistd.h>

    void sig_alrm(int signo)
    {
        /* nothing to do */
    }

    unsigned int mysleep(unsigned int nsecs)
    {
        struct sigaction newact, oldact;
        sigset_t newmask, oldmask, suspmask;
        unsigned int unslept;

        /* set our handler, save previous information */
        newact.sa_handler = sig_alrm;
        sigemptyset(&newact.sa_mask);
        newact.sa_flags = 0;
        sigaction(SIGALRM, &newact, &oldact);

        /* block SIGALRM and save current signal mask */
        sigemptyset(&newmask);
        sigaddset(&newmask, SIGALRM);
        sigprocmask(SIG_BLOCK, &newmask, &oldmask);

        alarm(nsecs);

        suspmask = oldmask;
        sigdelset(&suspmask, SIGALRM); /* make sure SIGALRM isn't block */

        sigsuspend(&suspmask); /* wait for any signal to be caught */

        /* some signal has been caught. SIGALRM is now blocked */
        unslept = alarm(0);
        sigaction(SIGALRM, &oldact, NULL); /* reset previous action */

        /* reset signal mask, which unblocks SIGALRM */
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        return(unslept);
    }

    int main(void)
    {
        while (1)
        {
            mysleep(2);
            printf("Two seconds passed ");
        }
        return 0;
    }


    如果在调用mysleep函数时SIGALRM信号没有屏蔽:
    1. 调用sigprocmask(SIG_BLOCK, &newmask, &oldmask);时屏蔽SIGALRM。
    2. 调用sigsuspend(&suspmask);时解除对SIGALRM的屏蔽,然后挂起等待待。
    3. SIGALRM递达后suspend返回,自动恢复原来的屏蔽字,也就是再次屏蔽SIGALRM。
    4. 调用sigprocmask(SIG_SETMASK, &oldmask, NULL);时再次解除对SIGALRM的屏蔽。


    程序参考:《linux c 编程一站式学习》

  • 相关阅读:
    C语言-const和volatile深度分析
    C语言循环语句工程用法
    C分支语句的工程用法
    函数对象分析
    关于protel电路图导入word
    单片机程序编写与下载
    关于Protel输出PDF原理图
    关于定时器、外部中断的一些理解
    锁存、译码介绍
    关于Proteus电压设置
  • 原文地址:https://www.cnblogs.com/alantu2018/p/8477306.html
Copyright © 2020-2023  润新知