首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux:将sleep()与信号相结合

Linux:将sleep()与信号相结合
EN

Stack Overflow用户
提问于 2016-04-19 01:51:29
回答 1查看 98关注 0票数 0

您知道我在哪里可以看到不能与sleep()命令一起使用的信号和函数的列表吗?

例如,您可以看到以下代码:

代码语言:javascript
复制
// this program presents how to block signal SIGINT
// while running in critical region
#include    <signal.h>
#include        <stdio.h>
#include        <unistd.h>
#include        <stdlib.h>
#include        <stdio.h>

static void sig_int(int);

int main(void)  //6
{
  sigset_t  newmask, oldmask, zeromask;

  if (signal(SIGINT, sig_int) == SIG_ERR)
    fprintf(stderr,"signal(SIGINT) error");

  sigemptyset(&zeromask);

  sigemptyset(&newmask);
  sigaddset(&newmask, SIGINT);

  /* block SIGINT and save current signal mask */
  if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
    fprintf(stderr,"SIG_BLOCK error");

  /* critical region of code */
  printf("In critical region: SIGINT will be blocked for 3 sec.\n");
  printf("Type Ctrl-C in first 3 secs and see what happens.\n");
  printf("Then run this program again and type Ctrl-C when 3 secs elapsed.\n");
  fflush(stdout);


  sleep(3);

  printf("end sleep");
  /* allow all signals and pause */
  if (sigsuspend(&zeromask) != -1)
    fprintf(stderr,"sigsuspend error");

  printf("after return from sigsuspend: ");

  /* reset signal mask which unblocks SIGINT */
  if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
    fprintf(stderr,"SIG_SETMASK error");

  /* and continue processing ... */
  exit(0);
}

static void sig_int(int signo)
{
  printf("\nIn sig_int: SIGINT\n"); fflush(stdout);
  return;
}

程序在睡眠()之后不会被唤醒。你知道为什么吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-22 16:46:58

如果你使用strace,你可以看到你的程序实际做了什么

strace ./my-sig-program

如果休眠再也没有返回,我猜任务已经收到了SIGSTOP (这个是不能被截取的)或者SIGTSTP (你可以用一个信号处理程序截取的这个),导致操作系统暂停整个进程,直到收到一个SIGCONT。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36700990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档