【问题标题】:Wake up a process from sleep with a signal使用信号从睡眠中唤醒进程
【发布时间】:2021-04-10 13:29:36
【问题描述】:

我让一个进程进入休眠状态。当一个进程进入睡眠状态时,它被标记为处于特殊状态并从调度程序的运行队列中删除,我想通过命令行发送信号来唤醒它,我该怎么做。假设我有这个使用sleep 100 秒的C 代码。

我需要发送什么信号才能唤醒它并使返回值成为从睡眠中剩余的秒数?

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

int main(void)
{
    printf("Current Proc ID is: %d. Wake me Up!\n", getpid());

    int time_remain = sleep(100);
    printf("Time Remain of sleep: %d\n", time_remain);
    return 0;
}

【问题讨论】:

标签: c linux sleep


【解决方案1】:

将信号处理程序分配给信号,然后发送该信号。

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

void do_nothing(int ignore) {
}

int main(void)
{
    signal(SIGUSR1, do_nothing);
    printf("Remaining = %d\n", sleep(100)); // i.e the return value after 5 seconds will be 95
    return 0;
}
imac:barmar $ ./testsleep &
[1] 2179
imac:barmar $ kill -USR1 %1
Remaining = 95
[1]+  Done                    ./testsleep

【讨论】:

  • 这里不是有点复杂吗?我的意思是,可能有唤醒进程的信号。还是谢谢你
  • AFAIK 没有这样的信号。
猜你喜欢
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
相关资源
最近更新 更多