gpt4 book ai didi

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

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

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

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

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

家长写(8)

child 读(8)

child 写(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 created\n");
exit(EXIT_FAILURE);
}

if(pipe(pipe2) < 0){
perror("Pipe 2 not created\n");
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%zd\n", rd_stat_child);
for(int i = 0; i < 4; i++){
str[i] = ((char)(((int)str[i])-32));
}

printf("\nFinal str in child: %s\n", str);
ssize_t wr_stat_child = write(pipe2[1], str, 8*sizeof(char));
printf("wc%zd\n", 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 8\n");
exit(EXIT_FAILURE);
}else{
ssize_t wr_stat_parent = write(pipe1[1], str, 8*sizeof(char));
printf("wp%zd\n", 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%zd\n", rd_stat_parent);
printf("\nParent Recieved\n %s", str);
}else{
perror("Parent error while reading\n");
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 个字符 + 1 个终止字符)

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

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

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

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

关于c - 在子进程和父进程之间发送数据时如何使用两个管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57776861/

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