C语言中的处理过程
我在尝试处理从父进程发送到其子进程所有进程的SIGUSR1信号时遇到问题。子对象上的处理程序不执行任何操作。我检查了kill命令的结果,它返回0表示发送的消息正常。有人能帮忙吗?下面是子进程的代码。我使用execl来区分孩子的代码和父亲的代码。请注意,该处理程序对于报警调用非常有效C语言中的处理过程,c,C,我在尝试处理从父进程发送到其子进程所有进程的SIGUSR1信号时遇到问题。子对象上的处理程序不执行任何操作。我检查了kill命令的结果,它返回0表示发送的消息正常。有人能帮忙吗?下面是子进程的代码。我使用execl来区分孩子的代码和父亲的代码。请注意,该处理程序对于报警调用非常有效 #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #incl
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
/*Global declerations*/
int alarmflag=0;
double result=0;
int fd[2];
/*Handler for the alarm and SIGUSR1 signal*/
void signal_handler (int sig)
{
printf("******************");
if(sig==SIGALRM)
{
printf("Im child with pid:%d im going to die my value is %lf \n",getpid(),result);
alarmflag=1;
}
if(sig==SIGUSR1)
{
printf("gotit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
}
}
double p_calculation ()
{
int i=2;
result=3;
double prosimo=-1;
while(!alarmflag)
{
prosimo=prosimo*(-1);
result=result+(prosimo*(4/((double)i*((double)i+1)*((double)i+2))));
i=i+2;
}
}
main(int argc,char *argv[])
{
int fd[2];
/*handling signals*/
signal(SIGALRM,signal_handler);
signal(SIGUSR1,signal_handler);
/*Notify for execution time*/
printf("PID : %d with PPID : %d executing for %d seconds \n",getpid(),getppid(),atoi(argv[1]));
/*end this after the value passed as argument*/
alarm(atoi(argv[1]));
p_calculation();
/*Notify for finish*/
printf("Done!!!\n");
}
#包括
#包括
#包括
#包括
#包括
/*全球减赤*/
int-alarmflag=0;
双结果=0;
int-fd[2];
/*报警和SIGUSR1信号的处理程序*/
无效信号处理器(int sig)
{
printf(“*******************”);
if(sig==SIGALRM)
{
printf(“具有pid的Im子项:%d Im将死亡我的值为%lf\n”,getpid(),result);
alarmflag=1;
}
if(sig==SIGUSR1)
{
printf(“明白了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n”);
}
}
双p_计算()
{
int i=2;
结果=3;
双prosimo=-1;
而(!alarmflag)
{
prosimo=prosimo*(-1);
结果=结果+(prosimo*(4/((双)i*((双)i+1)*((双)i+2));
i=i+2;
}
}
main(int argc,char*argv[])
{
int-fd[2];
/*处理信号*/
信号机(信号机、信号处理机);
信号(SIGUSR1,信号处理器);
/*通知执行时间*/
printf(“PID:%d,PPID:%d,执行时间为%d秒\n”、getpid()、getppid()、atoi(argv[1]);
/*在值作为参数传递后结束此操作*/
警报(atoi(argv[1]);
p_计算();
/*通知完成*/
printf(“完成!!!\n”);
}
父亲的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
pid_t *childs; //array for storing childs pids
int number_of_childs;//variable for the number of childs
int count_controls=0;
/*Handler for the SIGINT signal*/
void control_handler(int sig)
{
int j;
for (j=0;j<number_of_childs;j++)
{
kill(childs[j],SIGUSR1);
}
}
main (int argc,char *argv[]){
int i,child_status;
int fd[2];
char cast[512];
int pid;
number_of_childs=atoi(argv[1]);
signal(SIGINT,control_handler);
childs=malloc(number_of_childs*sizeof (pid_t));
if(pipe(fd)==-1)
{
perror("pipe");exit(1);
}
for (i=0;i<number_of_childs;i++){
pid=fork();
/*Create pipes to communicate with all children*/
/*Fathers code goes here*/
if(pid!=0)
{
printf("Parent process: PID= %d,PPID=%d, CPID=%d \n",getpid(),getppid(),pid);
childs[i]=pid; // Keep all your childs in an array
printf("Child:%d\n",childs[i]);
}
/*If you are a child*/
else
{
/*Change the code for the childs and set the time of execution*/
sprintf(cast,"%d",i+1);
execl("./Child.out","",cast,NULL);
}
}
/*Father should never terminate*/
while (1);
}
#包括
#包括
#包括
#包括
#包括
孩子们//用于存储childs PID的数组
孩子的整数//用于孩子数量的变量
int count_controls=0;
/*SIGINT信号的处理程序*/
无效控制处理器(int sig)
{
int j;
对于(j=0;j当我从shell中杀死一个孩子时,我看不出他有什么问题:
test]$ ./a.out 120
PID : 7406 with PPID : 7035 executing for 120 seconds
******************gotit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
******************Im child with pid:7406 im going to die my value is -nan
Done!!!
使用INT杀死的父对象确实会使用USR1杀死子对象:
test]$ ./a.out 1 30
Parent process: PID= 7490,PPID=7035, CPID=7491
Child:7491
PID : 7491 with PPID : 7490 executing for 40 seconds
******************gotit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
我的问题是,当我对父进程使用control-c时,它会得到
但中断处理程序发出的消息,即子进程已获得
消息未出现–吉安诺斯9分钟前
问题是您在子进程中得到了SIGINT。
尝试在子级中添加SIGINT的处理程序,然后运行测试
按下ctrl-c后,您可以看到当前实现发生了什么:
serge 7685 7035 99 08:01 pts/3 00:00:08 ./a.out 1
serge 7686 7685 30 08:01 pts/3 00:00:02 [Child.out] <defunct>
嗯……那么kill命令有什么问题吗?我使用If语句检查了If((kill(pid,SIGUSR1)==0){printf(“succes”)}我的问题是,当我对父进程使用control-c时,它得到了中断,但处理程序发出的消息表明子进程得到了消息。基本上,逻辑是第一个子进程得到1秒的执行时间,第二个子进程得到2秒,第三个子进程得到3秒,依此类推……为SIGINT添加另一个处理程序在child中暂停一下,效果很好!非常有用,Serge非常感谢!!是的,否则您的孩子会在您发送信号之前退出。
if (sig == SIGCLD)
{
// harvest terminated DEFUNCT child process
pid = waitpid(-1, &status, WNOHANG);
}