내 목표는 주 프로세스와 그 "fork"하위 프로세스를 상호 통신하는 것입니다. 신호 전달을 통해 통신이 이루어집니다.sigsuspend()가 신호에 대해 반응하지 않습니다.
SIGUSR1 신호를 기다리는 동안 첫 번째 자식이 대기 상태가되면 내 문제가 발생합니다.
나는 왜 그것이 그 지점에 갇혀 있는지 전혀 모릅니다. 콘솔을 통해 신호를 보내더라도 그 자식 프로세스는주의를 기울이지 않는 것처럼 보입니다.
아무도 도와 줄 수 있습니까? 여기
코드를 제공
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int N = 5;
int _pipe[2];
pid_t children[5];
void main(){
pid_t parent_pid;
pid_t pid;
int i = 0;
sigset_t set;
sigfillset(&set);
parent_pid = getpid();
fprintf(stderr,"I am main process, here comes my pid %u\n",getpid());
if (0>pipe(_pipe)) fprintf(stderr,"Error when creating pipe");
//Start creating child processes
while (i < N){
pid = fork();
if (pid == 0){
close(_pipe[1]);
break;
}
else{
fprintf(stderr,"Created child with pid %u\n",pid);
children[i] = pid;
write(_pipe[1],&pid,sizeof(pid_t));
}
i = i+1;
}
i = 0;
// What main process does
if (pid>0){
close(_pipe[0]);
close(_pipe[1]);
sigdelset(&set,SIGUSR2);
sigdelset(&set,SIGTERM);
sigdelset(&set,SIGKILL);
// Main process sends signal to each child
while(i < N){
kill(children[i],SIGUSR1);
fprintf(stderr,"Sent SIGUSR1 to child %u\n",children[i]);
// .. Now just wait for SIGUSR2 arrival
sigsuspend(&set);
i = i+1;
}
}
// What children do
else{
// Wait for main process SIGUSR1 delivery
sigdelset(&set,SIGUSR1);
sigsuspend(&set);
fprintf(stderr, "SIGUSR1 arrived child %u from its father",getpid());
// Once SIGUSR1 has arrived, pipe is read N times
while((i < N) && (read(_pipe[0],&pid,sizeof(pid_t))>0)){
children[i] = pid;
i = i+1;
}
close(_pipe[0]);
// After reading pipe, a reply is sent to parent process
kill(parent_pid,SIGUSR2);
}
}
덕분에 많이 @ 제이슨하지만 진짜 문제는 그 "sigsuspended"때 수신 신호를 RAS 방법을 정의하는 데되지 않았습니다. 다음 날에 내가 한 마지막 코드를 복구 할 것입니다. – xsubira