주 프로그램의 부모로부터 두 명의 자식을 만듭니다. 첫 번째와 두 번째 자식은 생성 된 후 (다른 자식의 PID와 함께) 프로그램을 실행합니다 (signalSender). signalSender는 시그널 핸들러를 가지고 있으며 프로세스간에 시그널을 보내기 위해 사용됩니다. 첫 번째 자식이 signalShooter를 실행할 때 두 번째 자식의 PID는 인수로 0으로 지정됩니다. 두 번째 자식이 sigShooter를 실행할 때 첫 번째 자식의 pid가 인수로 제공됩니다.두 자식 프로세스간에 신호를 전송합니다.
1) 두 번째 자식이 첫 번째 자식에게 신호를 보낸 후 신호 처리기를 통해 첫 번째 자식의 PID를 찾고 싶습니다. 전역 변수 pid_t pid2에 저장하려고했지만 작동하지 않습니다.
2) 나는이 두 아이 사이의 신호를 100 번 보내야하지만 어디에서 'for loop'와 'wait'신호를 사용해야하는지 잘 모릅니다.
The main program:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int main()
{
pid_t pid1,pid2,wid;
char *my_args[5];
int aInt = 368
char str[15];
pid1 = fork();
if (pid1 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid1 == 0)
{
my_args[0] = "sigperf1";
my_args[1] = "0";
my_args[2] = NULL;
execv("signalSender",my_args);
fprintf(stderr,"signalSender cannot be executed...");
exit(-1);
}
pid2 = fork();
if(pid2 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}
if(pid2 == 0)
{
sprintf(str, "%d", pid1);
my_args[0] = "sigperf1";
my_args[1] = str;
my_args[2] = NULL;
// printf("this is converted = %s\n",my_args[1]);
execv(“signalSender",my_args);
fprintf(stderr,"signalSender cannot be executed...");
exit(-1);
}
wid = wait(NULL);
}
signalSender :
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
pid_t pid2;
struct sigaction act;
void sighandler(int signum, siginfo_t *info, void *ptr)
{
printf("Received signal %d\n", signum);
printf("Signal originates from process %lu\n",
(unsigned long)info->si_pid);
pid2 = info->si_pid;
}
int main(int argc,char **argv)
{
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &act, NULL);
pid_t current, pidOther;
current = getpid();
pidOther = atol(argv[1]);
if(pidOther != 0) // we are in the second child
{
kill(pidOther,SIGUSR1); //sending signal from second child to first
}
if(pidOther == 0) // we are in the first child
{
kill(pid2,SIGUSR1);
}
return 0;
}
찾고있는 기능이 '일시 중지'또는 'sigsuspend'라고 생각합니다. – user3386109