C语言 如何在子进程和父进程之间发送数据时使用两个管道?



我正在学习系统调用、fork 和管道。我正在创建一个 C 程序,其中父进程将字符数组发送到子进程,子进程将数组的前 4 个字符大写并将其发回。数组从父级正确发送到子级,子级进行转换,甚至正确写入第二个管道,但父进程无法从管道 2 读取新数组。

我也尝试关闭不必要的描述符,但这不起作用。我在某处读到父进程可能在从管道中读取某些内容之前完成,为此我尝试了等待功能(但我可能做错了。我不确定。

我尝试检查进程发送和接收的值的大小,

家长写 (8(

儿童阅读 (8(

儿童写作 (8(

家长阅读 (1(

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(int argc, char *argv[]){
int pipe1[2];
int pipe2[2];
char str[8], str1[8];
pid_t pid;
if(pipe(pipe1) < 0){
perror("Pipe 1 not createdn");
exit(EXIT_FAILURE);
}
if(pipe(pipe2) < 0){
perror("Pipe 2 not createdn");
exit(EXIT_FAILURE);
} 
pid = fork();
if (pid == 0){
close(pipe1[1]);
close(pipe2[0]);
printf("nChild Process");
ssize_t rd_stat_child = read(pipe1[0], str, 8*sizeof(char));
if(rd_stat_child > 0){
printf("rc%zdn", rd_stat_child);
for(int i = 0; i < 4; i++){
str[i] = ((char)(((int)str[i])-32));
}
printf("nFinal str in child: %sn", str);
ssize_t wr_stat_child = write(pipe2[1], str, 8*sizeof(char));
printf("wc%zdn", wr_stat_child);
if(wr_stat_child != sizeof(str)){
perror("Sending to parent failed");
exit(EXIT_FAILURE);
}
}else{
perror("Child failed to read");
exit(EXIT_FAILURE);
}
}else if (pid > 0){
close(pipe1[0]);
close(pipe2[1]);
printf("nParent Process");
printf("nEnter a 8 character string: ");
scanf("%s", str);
if(sizeof(str)/(8*sizeof(char)) != 1){
perror("Size of string greater than 8n");
exit(EXIT_FAILURE);
}else{
ssize_t wr_stat_parent = write(pipe1[1], str, 8*sizeof(char));
printf("wp%zdn", wr_stat_parent);
if(wr_stat_parent != sizeof(str)){
perror("Parent failed writing.n");
exit(EXIT_FAILURE);
}
ssize_t rd_stat_parent = read(pipe2[0], str, 8*sizeof(char));
close(pipe2[0]);
if(rd_stat_parent <= sizeof(str)){
printf("rp%zdn", rd_stat_parent);
printf("nParent Recievedn %s", str);
}else{
perror("Parent error while readingn");
exit(EXIT_FAILURE);
}
}
}
return 0;
}

预期输出

父进程 (输入(>> lkybzqgv

子进程 (进程(>> LKYBzqgv

父进程 (输出(>> LKYBzqgv

实际输出

父进程 (输入(>> lkybzqgv

子进程 (进程(>> LKYBzqgv

父进程 (输出(>> kybzqgv

您的字符串处理已损坏。您需要一个长度为 9 的数组来保存长度为 8 的字符串。(请记住,c 中的字符串以零结尾(。不要写scanf("%s", str);来读取字符串!!这和用gets()一样糟糕。它允许您溢出缓冲区(这实际上发生在您的情况下(。读取如下字符串:

scanf("%8s", str);

这将读取最多 8 个(非空格(字符,并将它们与 str 中的零终止一起存储(再次记住 str 必须足够大,可容纳 8 个 charecter + 1 个终止字符(

然后要检查字符串的长度,请使用strlen(),不要使用sizeof()sizeof只能告诉保存字符串的数组的大小,或者指向字符串的指针的大小。请记住,保存字符串的数组必须至少比字符串大 1 个字符,但允许大于该字符。数组的大小在创建时是固定的。它不会根据您放入的内容而改变大小。

哦,顺便说一下。您不发送/接收终止字符,因此您必须在调用 read(( 后自行手动设置:

read(pipe1[0], str, 8);
str[8] = 0;

您的代码可能存在其他问题,但除非您修复字符串问题,否则您将有未定义的行为,其他一切都无关紧要。

最新更新