如何解决sigsuspend 后子进程没有醒来 (C)
我必须创建一个程序来生成 2 个孩子,每个孩子都必须生成一个随机数。之后,生成最小数字的孩子必须向另一个孩子发送 SIGUSR1。在我的情况下,我想向孩子 1 发送一个 SIGCONT 以唤醒他,以便他可以将 SIGUSR1 发送到另一个进程,但孩子 1 没有醒来......有什么帮助吗?提前致谢。
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#define N 2
int getRand(int upper)
{
srand(time(0));
int random;
random = rand() % upper;
return random;
}
void sighandler(int signo)
{
if (signo == SIGUSR1)
{
printf("Received SIGUSR1,my PID is %d\n\n",getpid());
exit(0);
}
if (signo == SIGUSR2)
{
printf("Received SIGUSR2.. I woke up! (My PID is %d)\n\n",getpid());
}
}
int main (int argc,char **argv)
{
int i,r,m,b;
int status = 0;
int tabcpid[N],ppid,wpid;
int fd[2],fdbool[2]; //fd [0 = read] [1 = write]
sigset_t set,zeromask;
struct sigaction action;
//Gestione segnali
sigemptyset(&zeromask);
sigemptyset(&action.sa_mask);
action.sa_handler = sighandler;
action.sa_flags = 0;
sigemptyset(&set);
sigaddset(&set,SIGUSR1);
sigprocmask(SIG_BLOCK,&set,NULL);
if (sigaction(SIGUSR1,&action,NULL) == -1)
{
perror("Error while doing sigaction.\n\n");
}
if (pipe(fd) == -1)
{
printf("Error opening pipe fd!\n\n");
exit(1);
}
if (pipe(fdbool) == -1)
{
printf("Error opening pipe fdbool!\n\n");
exit(1);
}
printf("\nPipes opened successfully. Forking ...\n\n");
sleep(2);
for (i = 0; i < N; i++)
{
if ((tabcpid[i] = fork()) == 0) //Child code
{
int n = atoi(argv[1]);
m = getRand(n);
b = 20;
ppid = getppid();
printf("I'm the son process #%d with PID: %d\n",i + 1,getpid());
printf("Random number in interval 0 - %d: %d\n\n",n,m);
sleep(2);
if (i == 0)
{
close(fd[0]);
write(fd[1],&m,sizeof(int));
close(fd[1]);
printf("Suspending..\n\n");
sigsuspend(&zeromask);
printf("So' ripartitoo\n\n");
/*
close(fdbool[1]);
read(fdbool[0],&b,sizeof(int));
close(fdbool[0]);
printf("--- b value: %d\n\n",b);
if (b == 0)
{
printf("I'm the process %d and I got the lowest number,SIGUSR1 sent to my brother.\n\n",getpid());
kill(tabcpid[1],SIGUSR1);
}
sleep(2);
*/
}
else
{
close(fd[1]);
read(fd[0],&r,sizeof(int));
close(fd[0]);
int lower = (r < m) ? r : m;
int igotlower = (m < r) ? 1 : 0;
printf("--- igotlower value: %d\n\n",igotlower);
close(fdbool[0]);
write(fdbool[1],&igotlower,sizeof(int));
close(fdbool[1]);
//printf("Got %d from other child process,while i got %d.\nThe smallest number is %d.\nMy PID is %d and the other process' PID is %d.\n\n",lower,getpid(),tabcpid[0]);
//sleep(2);
if (igotlower == 1)
{
printf("I'm the process %d and I got the lowest number,SIGUSR1 sent to my brother..\n\n",getpid());
kill(tabcpid[0],SIGUSR1);
sigsuspend(&zeromask);
}
else
{
printf("I'm sending SIGCONT to %d\n\n",tabcpid[0]);
kill(tabcpid[0],SIGCONT);
sigsuspend(&zeromask);
}
}
sleep(2);
}
sleep(2);
}
wait(&status);
close(fdbool[1]);
read(fdbool[0],sizeof(int));
close(fdbool[0]);
kill(tabcpid[b],SIGUSR1);
wait(&status);
printf("It's me the father... it's all over,we're done!\n\n");
sleep(2);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。