C 多进程暂停和恢复程序

C 多进程暂停和恢复程序,c,linux,signals,resume,pause,C,Linux,Signals,Resume,Pause,我仔细检查了一下,但我找不到任何我要找的东西。有人能举一个简单的例子来说明如何暂停一段时间(而不是给定的时间,比如sleep())然后继续程序吗?我尝试了一些方法,但它只是暂停,然后我唯一能做的就是在第二次打印之前终止程序F: #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #i

我仔细检查了一下,但我找不到任何我要找的东西。有人能举一个简单的例子来说明如何暂停一段时间(而不是给定的时间,比如
sleep()
)然后继续程序吗?我尝试了一些方法,但它只是暂停,然后我唯一能做的就是在第二次打印之前终止程序F:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include<termios.h>
#include<signal.h>
#include <sys/wait.h>

struct sigaction act;
sigset_t to_start, to_stop;




int main(int argc, char *argv[])
{



  int i=0;
printf("BEFORE SIGSUSPEND");




sigemptyset(&to_stop);
sigsuspend(&to_stop);

printf("AFTER SIGSUSPEND");
fflush(stdout)





    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构动作法;
信号设置为启动、停止;
int main(int argc,char*argv[])
{
int i=0;
printf(“在SIGSTAND之前”);
SIG清空设置(&to_停止);
SIGSSUSPEND(&to_stop);
printf(“在sigsupspend之后”);
fflush(标准输出)
返回0;
}

您的子进程应该以要发送的信号开始 封锁

然后,您需要确保:

  • 信号的传递不会终止进程(=>您需要为它设置一个信号处理程序(一个空的就可以了))
  • 可以发送信号(=>SIGSSUSPEND将执行此操作)
  • 代码:

    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    void handler(int X) { }
    int main(void)
    {
        sigset_t to_stop;
        sigemptyset(&to_stop);
        sigaction(SIGINT /*you'd probably use SIGUSR1 here*/,
          &(struct sigaction){ .sa_handler = handler }, 0);
    
        puts("BEFORE");
        /*SIGINT will now wake up the process without killing it*/
        sigsuspend(&to_stop);
        puts("AFTER");
    }
    
    #包括
    #包括
    #包括
    无效处理程序(int X){}
    内部主(空)
    {
    信号设置为停止;
    SIG清空设置(&to_停止);
    sigaction(SIGINT/*您可能会在这里使用SIGUSR1*/,
    &(struct sigaction){.sa_handler=handler},0);
    出售(“之前”);
    /*SIGINT现在将唤醒进程而不终止它*/
    SIGSSUSPEND(&to_stop);
    出售(“之后”);
    }
    
    您应该能够使用
    Ctrl+C
    来尝试此操作。在实际代码中,您可能应该使用
    SIGUSR1
    /
    SIGUSR1
    或其中一个实时信号