我有这段代码,我必须让程序块反复等待信号。我的老师希望我们使用 sigsuspend 和 mask 而不是暂停或睡眠。我不熟悉 sigsuspend 或掩码,我知道 sigsuspend() 用掩码给出的掩码临时替换调用进程的信号掩码,然后暂停进程,直到传递一个信号,其动作是调用信号处理程序或终止进程。但是我该如何实现它。
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
unsigned Conta = 0;
void mypause(int sign)
{
switch (sign)
{
case SIGINT:
printf("You've pressed ctrl-C\n");
printf("I'm running waiting for a signal...\n");
Conta++;
break;
case SIGQUIT:
printf("You've pressd ctrl-\\n");
printf("Number of times you've pressed CTRL-C: %d", Conta);
exit(0);
break;
}
}
int main()
{
alarm(3);
printf("I'm Alive\n");
signal(SIGINT, mypause);
signal(SIGQUIT, mypause);
printf("I'm running, waiting for a signal...\n");
while (1)
{
}
return (0);
}