Linux使用c中的管道在父进程和子进程之间传递值?
一个进程创建一个子进程,将x增加1,然后将x值发送给子进程,子进程可以将x乘以10,然后将x传递回父进程,依此类推。你至少应该这样做5次 输出应如下所示: 初始值0 家长: 操作后的x值:1 儿童: 手术后x值:10 家长: 手术后x值:11 儿童: 运行后x值:110 家长: 操作后的x值:111 孩子 操作后的x值:1110 我所拥有的是Linux使用c中的管道在父进程和子进程之间传递值?,c,pipe,C,Pipe,一个进程创建一个子进程,将x增加1,然后将x值发送给子进程,子进程可以将x乘以10,然后将x传递回父进程,依此类推。你至少应该这样做5次 输出应如下所示: 初始值0 家长: 操作后的x值:1 儿童: 手术后x值:10 家长: 手术后x值:11 儿童: 运行后x值:110 家长: 操作后的x值:111 孩子 操作后的x值:1110 我所拥有的是 #include <sys/types.h> #include <stdio.h> #include <unistd.h&g
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#define READ 0
#define WRITE 1
int main()
{
pid_t pid;
int mypipefd[2];
int ret;
int i;
int x=0;
int result,result2;
ret = pipe(mypipefd);
if(ret ==-1) //test for pipe
{
perror("pipe"); //show error and exit
exit(1);
}
printf("initial value %d\n", x);
pid = fork();
for(i=0;i<5;i++)
{
if(pid == 0)
{
/*Child process*/
result2=result*10;
write(mypipefd[WRITE],&result2,sizeof(result2));
printf("Child:\nx value after operation: %d\n", x);
close(mypipefd[WRITE]);
read(mypipefd[READ],&result2,sizeof(result2));
printf("=======================================\n");
close(mypipefd[READ]);
}
else if(pid > 0)
{
/*Parent process*/
result=x++;
write(mypipefd[READ],&result,sizeof(result));
printf("Parent:\nx value after operation: %d\n", x);
close(mypipefd[WRITE]);
read(mypipefd[WRITE],&result,sizeof(result));
printf("=======================================\n");
close(mypipefd[READ]);
exit(0);
}
else
{
perror("fork");
exit(1);
}
}
}
#包括
#包括
#包括
#包括
#定义读取0
#定义写入1
int main()
{
pid_t pid;
int-mypipefd[2];
int ret;
int i;
int x=0;
int结果,result2;
ret=管道(mypipefd);
if(ret==-1)//测试管道
{
perror(“管道”);//显示错误并退出
出口(1);
}
printf(“初始值%d\n”,x);
pid=fork();
对于(i=0;i=0)
{
/*父进程*/
结果=x++;
写入(mypipefd[READ],&result,sizeof(result));
printf(“父项:\nx操作后的值:%d\n”,x);
关闭(mypipefd[WRITE]);
读取(mypipefd[WRITE],&result,sizeof(result));
printf(“==========================================================\n”);
关闭(mypipefd[READ]);
出口(0);
}
其他的
{
佩罗尔(“福克”);
出口(1);
}
}
}
问题是,我的代码怎么了?我试着在管道上读写,但似乎不起作用
我的代码现在的输出是什么:
初始值0
家长:
操作后的x值:1
儿童:
操作后的x值:0
儿童:
操作后的x值:0
儿童:
操作后的x值:0
儿童:
操作后的x值:0
儿童:
操作后的x值:0我认为要解决的第一个问题是,当您尝试读取/写入数据时,管道的两端都处于打开状态。如果要将数据写入管道,则
close(fd[READ_END])
write(...)
close(fd[WRITE_END])
其次,unix中的管道是单工的。你似乎在试图同时从管道读写。如果你想这样做,你必须打开两条管道
我修改了您的程序,向您展示了如何为父/子对象从管道读取数据,然后向管道写入数据。希望这对你有帮助
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 25
#define READ 0
#define WRITE 1
int main(void)
{
pid_t pid;
//open two pipes, one for each direction
int mypipefd[2];
int mypipefd2[2];
/* create the pipe */
if (pipe(mypipefd) == -1 || pipe(mypipefd2) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
/* now fork a child process */
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
if (pid > 0) { /* parent process */
int writeValue=10;
int readValue=0;
close(mypipefd[READ]); //close read end, write and then close write end
write(mypipefd[WRITE],&writeValue,sizeof(writeValue)); //write to pipe one
printf("Parent: writes value : %d\n", writeValue);
close(mypipefd[WRITE]);
close(mypipefd2[WRITE]); //close write end, read, and then close read end
read(mypipefd2[READ],&readValue,sizeof(readValue));
printf("Parent: reads value : %d\n", readValue);
close(mypipefd2[READ]);
}
else { /* child process */
int writeValue=20;
int readValue=0;
close(mypipefd[WRITE]); //close write end, read, and then close read end
read(mypipefd[READ],&readValue,sizeof(readValue));
printf("child: read value : %d\n", readValue);
writeValue+=readValue;
close(mypipefd[READ]);
close(mypipefd2[READ]); //close read end, write and then close write end
write(mypipefd2[WRITE],&writeValue,sizeof(writeValue));
printf("child: writeValue value : %d\n", writeValue);
close(mypipefd2[WRITE]);
}
return 0;
}
这可以扩展到您的应用程序
if (pid > 0) { /* parent process */
result1++;
close(mypipefd[READ]); //close read end, write and then close write end
write(mypipefd[WRITE],&result1,sizeof(result1)); //write to pipe one
printf("Parent:\n x value after operation: %d\n", result1);
close(mypipefd[WRITE]);
close(mypipefd2[WRITE]); //close write end, read, and then close read end
read(mypipefd2[READ],&result1,sizeof(result1));
close(mypipefd2[READ]);
}
else { /* child process */
close(mypipefd[WRITE]); //close write end, read, and then close read end
read(mypipefd[READ],&result2,sizeof(result2));
result2*=10;
printf("child:\n x value after operation %d\n", result2);
close(mypipefd[READ]);
close(mypipefd2[READ]); //close read end, write and then close write end
write(mypipefd2[WRITE],&result2,sizeof(result2));
close(mypipefd2[WRITE]);
}
如果你把它放在循环中,理论上它会工作。但是,由于上下文切换和其他与操作系统相关的调度,这两个进程之间的执行顺序将混乱。这意味着读写操作不一定是按顺序进行的,而且也不起作用。仔细阅读这个问题
.我认为要解决的第一个问题是,当您尝试读取/写入数据时,管道的两端都是打开的。如果要将数据写入管道,则
close(fd[READ_END])
write(...)
close(fd[WRITE_END])
其次,unix中的管道是单工的。你似乎在试图同时从管道读写。如果你想这样做,你必须打开两条管道
我修改了您的程序,向您展示了如何为父/子对象从管道读取数据,然后向管道写入数据。希望这对你有帮助
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define BUFFER_SIZE 25
#define READ 0
#define WRITE 1
int main(void)
{
pid_t pid;
//open two pipes, one for each direction
int mypipefd[2];
int mypipefd2[2];
/* create the pipe */
if (pipe(mypipefd) == -1 || pipe(mypipefd2) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
/* now fork a child process */
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
if (pid > 0) { /* parent process */
int writeValue=10;
int readValue=0;
close(mypipefd[READ]); //close read end, write and then close write end
write(mypipefd[WRITE],&writeValue,sizeof(writeValue)); //write to pipe one
printf("Parent: writes value : %d\n", writeValue);
close(mypipefd[WRITE]);
close(mypipefd2[WRITE]); //close write end, read, and then close read end
read(mypipefd2[READ],&readValue,sizeof(readValue));
printf("Parent: reads value : %d\n", readValue);
close(mypipefd2[READ]);
}
else { /* child process */
int writeValue=20;
int readValue=0;
close(mypipefd[WRITE]); //close write end, read, and then close read end
read(mypipefd[READ],&readValue,sizeof(readValue));
printf("child: read value : %d\n", readValue);
writeValue+=readValue;
close(mypipefd[READ]);
close(mypipefd2[READ]); //close read end, write and then close write end
write(mypipefd2[WRITE],&writeValue,sizeof(writeValue));
printf("child: writeValue value : %d\n", writeValue);
close(mypipefd2[WRITE]);
}
return 0;
}
这可以扩展到您的应用程序
if (pid > 0) { /* parent process */
result1++;
close(mypipefd[READ]); //close read end, write and then close write end
write(mypipefd[WRITE],&result1,sizeof(result1)); //write to pipe one
printf("Parent:\n x value after operation: %d\n", result1);
close(mypipefd[WRITE]);
close(mypipefd2[WRITE]); //close write end, read, and then close read end
read(mypipefd2[READ],&result1,sizeof(result1));
close(mypipefd2[READ]);
}
else { /* child process */
close(mypipefd[WRITE]); //close write end, read, and then close read end
read(mypipefd[READ],&result2,sizeof(result2));
result2*=10;
printf("child:\n x value after operation %d\n", result2);
close(mypipefd[READ]);
close(mypipefd2[READ]); //close read end, write and then close write end
write(mypipefd2[WRITE],&result2,sizeof(result2));
close(mypipefd2[WRITE]);
}
如果你把它放在循环中,理论上它会工作。但是,由于上下文切换和其他与操作系统相关的调度,这两个进程之间的执行顺序将混乱。这意味着读写操作不一定是按顺序进行的,而且也不起作用。仔细阅读这个问题
.谢谢,修复了。。。你能帮忙吗谢谢,修好了。。。你能帮忙吗我想我有两个管子。。一个用于读取,一个用于写入,但不确定为什么父进程只处理一次。。我有一个for循环5次。你只有一个管道。管道的两端有读和写。正如一位专家所说,一次只能打开一端。因此,要使一个进程能够从管道读写,它必须打开两个,即必须对两个文件描述符调用pipe()命令两次。非常感谢您的时间,我现在理解了双向管道的概念。然而,我仍然困惑于如何将值从父级传递给子级,然后再传递给父级,正如问题所问。请看我为您发布的程序。父对象写入管道,子对象读取该值,更新该值,并将其传递回父对象,然后父对象读取该值。没问题:)是的,我仔细看了看。但我只需要一个变量就可以传入parent to do(x++),然后返回child to do(x=x*10),我不知道如何修改它:(我想我有两个管道..一个用于读取,一个用于写入,但不确定为什么父进程只处理一次..我有一个for循环5次。您只有一个管道。管道的两端都是读取和写入的。正如所说,一次只能打开一端。因此,要让一个进程能够从管道读取和写入,它必须打开两个,我例如,你必须调用管道()在两个文件描述符上命令两次。非常感谢您的时间,我现在理解了双向管道的概念。但是,我仍然不知道如何将值从父级传递给子级,然后再传递给父级。请看我为您发布的程序。父级写入管道,子级读取该值,然后更新,pa把它还给父母,然后让他们看。没问题:)是的,我仔细看了看。但我只需要一个变量就可以传入parent to do(x++),然后返回child to do(x=x*10),我不知道如何修改它:(