0

親プロセスからすべての子プロセスに送信された SIGUSR1 シグナルを処理しようとすると、問題が発生します。子のハンドラーは何もしません。kill コマンドの結果を確認したところ、送信されたメッセージが ok であることを意味する 0 が返されました。誰でもこれを手伝ってもらえますか?以下は、子プロセスのコードです。execl を使用して、子のコードを父と区別します。ハンドラはアラーム コールに対して適切に機能することに注意してください。

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


}
4

1 に答える 1

1

シェルから子を殺すと、子に問題が見られません。

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 を使用するときです。割り込みを受け取りますが、子がメッセージを受け取ったというハンドラーからのメッセージは表示されません – Giannos 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>

子プロセスが SIGINT を取得して終了しました。また、機能していない状態のすべての子プロセスを取り除くために、親で SIGCLD を処理する必要があります。

if (sig == SIGCLD)
{
   // harvest terminated DEFUNCT child process
   pid = waitpid(-1, &status, WNOHANG);
}
于 2012-10-01T03:01:20.253 に答える