我试图学习如何在课堂上学习更大的作业。我按照tutorial中的步骤操作,但它对我不起作用。当我输入命令" kill [process id]"睡眠不会停止而只是继续。我试过杀死了孩子和父母身份证,没有任何反应。有任何想法吗?这就是我所拥有的:
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
volatile sig_atomic_t done = 0;
void term(int signum)
{
printf("Caught!\n");
done = 1;
}
int main(int argc, char *argv[])
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
int pid = getpid();
int parentID = getppid();
printf("PID is %d and Parent is %d \n",pid, parentID);
int loop = 0;
while (!done)
{
printf("PID is %d and Parent is %d \n",pid, parentID);
int t = sleep(10);
/* sleep returns the number of seconds left if
* interrupted */
while (t > 0)
{
printf("Loop run was interrupted with %d "
"sec to go, finishing...\n", t);
t = sleep(t);
}
printf("Finished loop run %d.\n", loop++);
}
printf("done.\n");
return 0;
}
答案 0 :(得分:1)
工作正常:
> cat > sig.c # paste your code
> gcc sig.c
> ./a.out &
[1] 20549
PID is 20549 and Parent is 15574
PID is 20549 and Parent is 15574
> kill 20549
Caught!
Loop run was interrupted with 1 sec to go, finishing...
> Finished loop run 0.
done.
>
[1] Done ./a.out