gpt4 book ai didi

c - 两个子进程通过命名管道进行通信

转载 作者:行者123 更新时间:2023-11-30 16:56:15 26 4
gpt4 key购买 nike

我有一个主程序,它创建两个子进程和一个命名管道(FIFO)。每个子进程通过 execv() 执行一个名为“sendSignal”的程序。“sendSignal”的参数之一是主程序中的 FIFO。

children 将互相发送信号。它通过主程序(在变量firstShooter中)中的一个参数来决定哪个信号首先射击。

我想知道这两个 child 如何通过这个命名管道向对方发送他们的 pid。

主程序如下:

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

/* this program should be provided with 2 arguments */

int main(int argc, char **argv)
{

char str1[15];
char str2[15];
char fileDescriptor[15];
char *my_args[4];
char *myfifo = "myfifo";

int fd, pipeCheck;
pid_t pid1, pid2, wid;


/* If the user does not provide the argument to determin which child is firing first */
if(argc != 2)
{
fprintf(stderr,"%s: 2 arguments needed, got %d\n",argv[0],argc-1);
exit(1);
}

/* create the FIFO (named pipe) */
pipeCheck = mkfifo(myfifo, 0666);

/* check if the named pipe was created properly if not output an error */
if(pipeCheck == -1)
{
fprintf(stderr, "%s: Error creating named pipe: %s\n",argv[0], strerror(errno));
exit(1);
}


pid1 = fork();

if (pid1 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}

if(pid1 == 0)
{


my_args[0] = "sendSignal";
my_args[1] = argv[1];
my_args[2] = myfifo; // the named pipe as arguemnt
my_args[3] = NULL;
execv("sendSignal",my_args);
fprintf(stderr,"sendSignal cannot be executed by first child...");
exit(-1);
}


pid2 = fork();

if(pid2 < 0)
{
fprintf(stderr, ": fork failed: %s\n", strerror(errno));
exit(1);
}

if(pid2 == 0)
{

my_args[0] = "sendSignal";
my_args[1] = argv[1];
my_args[2] = myfifo; // named pipe as arguemnt
my_args[3] = NULL;
// printf("this is converted = %s\n",my_args[1]);
execv("sendSignal",my_args);
fprintf(stderr,"sendSignal cannot be executed by second child...");
exit(-1);
}

close(fd);
unlink(myfifo);


wid = wait(NULL);


return 0;

}

这是发送信号:

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


void sig_handler(int signo)
{
if(signo == SIGUSR1)
printf("signal received\n");

}

int main(int argc, char **argv)
{

char abspath[256] = "";
getcwd(abspath, 256);
strrchr(abspath, '/');

if(signal(SIGUSR1,sig_handler) == SIG_ERR)
printf("\n Cannot catch the signal\n");

char *myfifo = "myfifo";

int firstShooter = atoi(argv[1]); //define the first process to send the signal

int fd;
char str1[15];
char str2[15];
char pid1[15];
char pid2[15];

fd = open(argv[2],O_RDWR);


if(firstShooter == 1)
{
sprintf(pid1,"%d",getpid());
write(fd,pid1,sizeof(pid1));
}

if(firstShooter == 2)
{
sprintf(pid2,"%d",getpid());
write(fd,pid2,sizeof(pid2));
}


read(fd,str1,sizeof(str2));
read(fd,str2,sizeof(str2));

close(fd);

printf("str1 = %s\n",str1);
printf("str2 = %s\n",str2);


return 0;
}

最佳答案

您的两个子进程都有相同的参数:

my_args[0] = "sendSignal";
my_args[1] = argv[1];
my_args[2] = myfifo; // the named pipe as argument
my_args[3] = NULL;

firstShooter 参数没有意义,因为进程无法将自己标识为第一或第二。

我建议再加一个参数——进程索引。 sendSignal 函数逻辑可以这样修改:

char pid1[15];
char pid2[15];
int processIndex = atoi(argv[3]);

fd = open(argv[2],O_RDWR);

if (processIndex == firstShooter)
{
// Send firstShooter PID
sprintf(pid1,"%d",getpid());
write(fd,pid1,sizeof(pid1));

// Got the other PID
read(fd,pid2,sizeof(pid2));
}
else
{
// Got the firstShooter PID
read(fd,pid1,sizeof(pid1));

// Send the other PID
sprintf(pid2,"%d",getpid());
write(fd, pid2, sizeof(pid2));
}

close(fd);

printf("pid1 = %s\n",pid1);
printf("pid2 = %s\n",pid2);

关于c - 两个子进程通过命名管道进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com