【问题标题】:Is there a difference between "I/O block status" and "sleep"?“I/O 块状态”和“睡眠”有区别吗?
【发布时间】:2020-09-24 06:51:22
【问题描述】:

Can SIGCONT wake up the sleeping process?

我前天得知信号处理程序未能使进程的“睡眠”。

以同样的方式,我尝试使用信号处理程序“读取”失败。

代码如下。

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

void signal_handler(int signo)
{
        write(1, "\nI've got signal\n", 17);
        return;
}

int main()
{
        char buf[10];

        signal(SIGINT, signal_handler);

        read(0, buf, 1);

        write(1, buf, 1);

        return 0;
}

但是,在执行信号处理程序之后,进程又回到了 I/O 块状态。

还执行了以下代码进行重新验证。

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

void signal_handler(int signo)
{
        write(1, "\nI've got signal\n", 17);
        return;
}

int main()
{
        char buf[10];

        signal(SIGINT, signal_handler);

        sleep(100);

        write(1, "awake", 5);

        return 0;
}

在这种情况下,在收到信号处理程序后,进程不再处于休眠状态。

有没有办法在收到信号后退出阻塞状态,继续进程? (输入失败)

【问题讨论】:

    标签: c linux process io operating-system


    【解决方案1】:

    根据signal()函数的文档:

    signal() 的行为因 UNIX 版本而异,并且 历史上在不同版本的 Linux 中有所不同。避免其 使用:使用 sigaction(2) 代替。请参阅下面的可移植性。

    ...

    便携性

       The only portable use of signal() is to set a signal's disposition to
       SIG_DFL or SIG_IGN.  The semantics when using signal() to establish a
       signal handler vary across systems (and POSIX.1 explicitly permits
       this variation); do not use it for this purpose.
    

    因此,您应该使用sigaction() 而不是signal()。

    如果您在程序中将 signal() 替换为 sigaction(),它应该会按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2012-12-16
      相关资源
      最近更新 更多