gpt4 book ai didi

c - 用 C 处理进程

转载 作者:行者123 更新时间:2023-11-30 15:54:56 24 4
gpt4 key购买 nike

我在尝试处理从父进程发送到其所有子进程的 SIGUSR1 信号时遇到问题。 child 身上的处理程序什么也不做。我检查了kill命令的结果,它返回0,这意味着发送的消息是ok的。有人能帮忙吗?下面是子进程的代码。我使用 execl 将 child 的代码与父亲的代码区分开来。请注意,该处理程序非常适合警报调用

#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");

}

父亲的代码如下:

#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);


}

最佳答案

当我从 shell 中杀死一个 child 时,我看不到它的问题:

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 杀死 parent 确实会用 USR1 杀死 child :

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

My problem is when i use control -c to the father process. It gets the interrupt but the message from the handler that the child got the message does not appear – Giannos 9 mins ago

问题是您在子进程中收到 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>

您的子进程收到 SIGINT 并终止。此外,有必要在父进程中处理 SIGCLD,以摆脱所有处于失效状态的子进程:

if (sig == SIGCLD)
{
// harvest terminated DEFUNCT child process
pid = waitpid(-1, &status, WNOHANG);
}

关于c - 用 C 处理进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com