Process 使用管道进行数字减法的两个过程
很难使两个进程通过管道进行通信并交替地减去一个数字 输出应如下所示: 过程1:9 进程2:8 过程1:7 到目前为止我所做的:Process 使用管道进行数字减法的两个过程,process,fork,pipe,communicate,Process,Fork,Pipe,Communicate,很难使两个进程通过管道进行通信并交替地减去一个数字 输出应如下所示: 过程1:9 进程2:8 过程1:7 到目前为止我所做的: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { int p2c[2]; int c2p[2]; in
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int p2c[2];
int c2p[2];
int n = 9;
pipe(p2c);
pipe(c2p);
write(p2c[1], &n, sizeof(int));
if(fork() == 0) {
read(p2c[0], &n, sizeof(int));
printf("Got from parent: %d", n);
n--;
write(c2p[1], &n, sizeof(int));
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}
else{
read(c2p[0], &n, sizeof(int));
printf("Got from child: %d", n);
n--;
write(p2c[1], &n; sizeof(int));
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){
int p2c[2];
int c2p[2];
int n=9;
管道(p2c);
管道(c2p);
写入(p2c[1],&n,sizeof(int));
如果(fork()==0){
读取(p2c[0],&n,sizeof(int));
printf(“从父级获取:%d”,n);
n--;
写入(c2p[1],&n,sizeof(int));
关闭(p2c[0]);
关闭(p2c[1]);
关闭(c2p[0]);
关闭(c2p[1]);
出口(0);
}
否则{
读取(c2p[0],&n,sizeof(int));
printf(“从子项获取:%d”,n);
n--;
写入(p2c[1],&n;sizeof(int));
关闭(p2c[0]);
关闭(p2c[1]);
关闭(c2p[0]);
关闭(c2p[1]);
}
返回0;
}
输出为:
从父母那里得到:9
从孩子那里得到:8
让这两个进程将数字减至0的正确方法是什么?有意义的是,您只得到“来自父进程:9来自子进程:8”因此,您需要,您需要一个while或for循环,用于子进程和父进程以获得您期望的结果,并且这些循环的停止条件为(n<0)递减后n或写入管道末端关闭:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int p2c[2];
int c2p[2];
int n = 9;
pipe(p2c);
pipe(c2p);
// this is important to prevent deadlock situation, at least one of both processes
// must start a write operation before while loop, unless the read will block and
// and each process still waiting the other to write something on the pipe
write(p2c[1], &n, sizeof(int));
if(fork() == 0) {
int readStatus;
while(1){
readStatus=read(p2c[0], &n, sizeof(int));
// when read returns 0, this means the write end of pipe was closed, so we have to break the loop
// because no more data to recieve
if(readStatus == 0) break;
printf("Got from parent: %d\n", n);
n--;
// we check if n less than 0, if yes we are finished
if(n < 0) break;
write(c2p[1], &n, sizeof(int));
}
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}
else{
int readStatus;
while(1){
readStatus= read(c2p[0], &n, sizeof(int));
if(readStatus == 0) break;
printf("Got from child: %d\n", n);
n--;
if(n < 0) break;
write(p2c[1], &n, sizeof(int));
}
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){
int p2c[2];
int c2p[2];
int n=9;
管道(p2c);
管道(c2p);
//这对于防止死锁情况很重要,至少两个进程中的一个进程是如此
//必须在while循环之前启动写操作,除非读操作将阻塞和
//每个进程仍在等待另一个进程在管道上写入内容
写入(p2c[1],&n,sizeof(int));
如果(fork()==0){
int-readStatus;
而(1){
readStatus=read(p2c[0],&n,sizeof(int));
//当read返回0时,这意味着管道的写入端已关闭,因此我们必须中断循环
//因为没有更多的数据可以接收
如果(readStatus==0)中断;
printf(“从父项获取:%d\n”,n);
n--;
//我们检查n是否小于0,如果是,则完成
如果(n<0)断裂;
写入(c2p[1],&n,sizeof(int));
}
关闭(p2c[0]);
关闭(p2c[1]);
关闭(c2p[0]);
关闭(c2p[1]);
出口(0);
}
否则{
int-readStatus;
而(1){
readStatus=read(c2p[0],&n,sizeof(int));
如果(readStatus==0)中断;
printf(“从子项获取:%d\n”,n);
n--;
如果(n<0)断裂;
写入(p2c[1],&n,sizeof(int));
}
关闭(p2c[0]);
关闭(p2c[1]);
关闭(c2p[0]);
关闭(c2p[1]);
}
返回0;
}
这太完美了。我知道我发布的只是一个读写过程,所以输出是合乎逻辑的。但是,我在这段时间内做了这个想法,但是我的输出看起来像:来自父母:9来自父母:8。。。。。。从儿童处收到:8从儿童处收到:7以此类推。。我想我的问题是在处理关闭(我当时做了)。这太完美了,谢谢!