【发布时间】:2020-01-20 01:47:29
【问题描述】:
我试图在 20 秒后终止我的具有多个功能的 c 程序(杀死所有子进程和父进程,关闭文件)。我试过闹钟(),定时器(),时钟()。当我们只有一个主函数和一个处理函数时,它就可以工作。即使我将变量保持为全局变量,clock() 也会在每个函数中从 0 重新开始。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include<stdbool.h>
#include <ctype.h>
#include<sys/wait.h>
#include<signal.h>
#include <sys/mman.h>
#include<sys/time.h>
#define INTERVAL 2
int t=0;
void display_message()
{
printf("In the handler");
//kill(0,SIGKILL);
t=1;
}
void calling2()
{
signal(SIGALRM, display_message);
sleep(3);
}
void calling()
{
signal(SIGALRM, display_message);
alarm(2);
int i;
for(i=0;i<3;i++)
{
//printf("\nStarting fork for loop i=%d \n",i);
pid_t pID = fork();
if (pID == 0) // child
{
calling2();
if(t==1)
{
printf("we have exceeded 2 seconds killing the process");
kill(0,SIGKILL);
exit(0);
}
exit(0);
kill(pID,SIGKILL);
}
else if(pID>0)
{
// printf("\nhello from the father");
if(t==1)
{
printf("killing the process");
kill(0,SIGKILL);
exit(0);
}
printf("\nhello from the father");
}
}
}
如您所见,我尝试从不同的函数调用信号,以便它可以捕获信号并且处理程序可以执行但处理程序永远不会执行。
编辑:再试一次
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include<stdbool.h>
# include <ctype.h>
# include<sys/wait.h>
# include<signal.h>
# include <sys/mman.h>
# include<sys/time.h>
# define INTERVAL 2
int t=0;
void display_message()
{
kill(0,SIGKILL);
t=1;
}
void calling2()
{
sleep(3);
}
void calling()
{
signal(SIGALRM, display_message);
int i;
for(i=0;i<3;i++)
{
pid_t pID = fork();
if (pID == 0) // child
{
calling2();
if(t==1)
{
printf("killing the process");
kill(0,SIGKILL);
exit(0);
}
exit(0);
}
else if(pID>0)
{
if(t==1)
{
printf("killing the process");
kill(0,SIGKILL);
exit(0);
}
printf("\nhello from the father");
}
}
}
int main()
{
signal(SIGALRM, display_message);
alarm(2);
calling();
}
O/P:
hello from the father
hello from the father
hello from the father
hello from the father
hello from the father
hello from the father
error: Failed with return code 22
【问题讨论】:
-
You shouldn't use
printf()in a signal handler,无论是直接调用还是间接调用。 -
调用序列
exit(0); kill(pID,SIGKILL);应该会丢失kill()—exit()不会返回。此外,在编程中,一致性很重要(对于高质量的代码)。例如,#include和标题名称之间应始终有一个空格,无论是#include <stdbool.h>还是#include "project.h"。 -
要杀死所有子进程,您需要在某处列出子 PID。好吧,我想你可以试试
kill()的一些特殊情况——pid的值0或-1甚至-getpgrp()可能是你杀死进程所需要的。当进程终止时,所有文件都会关闭。除非您在某个地方有所有打开文件的列表,否则最好将其留给系统。因此,您需要决定是kill(0, SIGTERM)还是kill(-1, SIGHUP)或其他什么东西会为您完成这项工作。 -
在 EDIT 中添加了更改。还是不行