루프에 fork를 사용하여 16 개의 하위 프로세스를 만든 상위 프로세스가 있습니다. 결국, 모든 하위 프로세스는 SIGUSR1 신호를 보내고, 이는 상위 프로세스의 핸들러 함수에 의해 처리됩니다.C - 여러 자식 - 서로 겹쳐 쓰는 신호
내 문제는 일부 어린이가 다른 어린이의 신호를 처리하는 동안 신호를 보내는 것입니다. 핸들러 함수가 멈추고 현재 신호를 무시하고 새로운 신호를 처리한다는 것을 읽었습니다.
처리기 기능의 시작 부분에 kill(0,SIGSTOP)
을 전송하여 문제를 해결하려고 시도했지만 부모 프로세스도 중지됩니다. 이 신호를 아이들에게만 보낼 수있는 방법이 있습니까?
불가능할 경우 wait, waitpid and kill
을 사용하면 목표를 달성 할 수 있습니까?
struct sigaction new_action;
memset(&new_action, 0, sizeof(new_action));
new_action.sa_handler = my_signal_handler;
new_action.sa_flags = SA_SIGINFO;
if(0 != sigaction(SIGUSR1, &new_action, NULL))
{
printf("Signal handle registration failed. %s\n", strerror(errno));
return -1;
}
for(int i=0; i<16; i++){
pid_t cpid = fork();
if(cpid == 0) // child
{
execv("./counter",argvv); // some arguments to the function
printf("execv failed: %s\n", strerror(errno));
return -1;
}
else{
continue;
}
:
void my_signal_handler(int signum, siginfo_t* info, void* ptr)
{
kill(0, SIGSTOP);
int sonPid = info->si_pid;
char* pipeName = malloc(14 + sizeof(int));//TODO ok?
sprintf(pipeName, "//tmp//counter_%d" , (int) sonPid); //TODO double //?
size_t fdPipe = open(pipeName, O_RDONLY);
int cRead;
int countRead = read(fdPipe,&cRead,sizeof(int));
COUNT+= cRead;
kill(0, SIGCONT);
return;
}
자식 프로세스를 만들기 :
추가 아래 코드, 나는
처리기 기능 등 오픈 읽기에 대한 반환 값을 확인 같은 물건을 왼쪽으로 즉, 그것은 어린이의 카운터 프로그램, 즉 그것은 파일의 일부에서 counc
숯의 모양을 계산 다음 파이프에 인쇄합니다. :
int main(int argc, char** argv){
int counter = 0;
char counc = argv[1][0];
char* filename = argv[2];
off_t offset = atoll(argv[3]);
ssize_t length = atoll(argv[4]);
int fd = open(filename, O_RDWR | O_CREAT);
char* arr = (char*)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
for(int i=0; i<length; i++){
if(arr[i] == counc){
counter++;
}
}
pid_t proid = getpid();
pid_t ppid = getppid();
char* pipeName = malloc(14 + sizeof(pid_t));
sprintf(pipeName, "//tmp//counter_%d" , (int) proid);
size_t fdPipe = mkfifo(pipeName, 0777);
int didopen = open(pipeName,O_WRONLY);
size_t wrote = write(fdPipe,&counter , 1);
if(wrote < 0){
printf(OP_ERR, strerror(errno));
return errno;
}
kill(ppid, SIGUSR1);
//close the pipe and unmap the array
return 1;
}
코드를 [최소, 완료 및 검증 가능한 예제] (https://stackoverflow.com/help/mcve)로 게시하십시오 (페이지를주의 깊게 읽으십시오). – Arash
이 경우에는 설명이 괜찮을 것이라고 생각했지만 곧 코드 예제를 추가하겠습니다. – Gray
이것은 사실이 아닙니다. 처리되는 신호는 처리기가 끝날 때까지 차단되므로 모두가 동일한 신호를 보내면 정상적으로 처리됩니다. 일부 코드는 ** 실제 ** 문제를 식별하는 데 도움이됩니다. Btw, 핸들러를 짧게 유지하고 플래그 만 설정하고 신호 처리기 외부에서 복잡한 처리를 수행하십시오. –