代碼實際做的是從父進程獲取輸入,並通過管道將其發送給子進程。子進程將其反轉,然後通過另一個管道將其發送回父進程。代碼中沒有waitpid()
或wait()
函數。這裏發生父進程和子進程的切換?
問題是:過程切換如何在這裏工作? write()
和read()
函數如何在這裏工作?
下面是代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <iostream>
#define li long int
using namespace std;
void ReverseAString(char input[])
{
li length = strlen(input),i;
char hold;
for(i=0;i<length/2;i++)
{
hold = input[i];
input[i] = input[length-(i+1)];
input[length-(i+1)] = hold;
}
}
int main()
{
pid_t ChildOrParentId;
int fifoParent[2],fifoChild[2],in;
if(pipe(fifoParent)==-1)
{
cout<<"Problem in creating Parent's Pipe"<<endl;
perror("Parent's Pipe");
exit(1);
}
if(pipe(fifoChild)==-1)
{
cout<<"Problem in creating Child's Pipe"<<endl;
perror("Child's Pipe");
exit(1);
}
ChildOrParentId = fork();
if(ChildOrParentId==0)
{
char buf[100],collected[100];
close(fifoParent[0]);
close(fifoChild[1]);
in = 0;
while(read(fifoChild[0],buf,1)>0)
{
collected[in]=buf[0];
in++;
}
collected[in]=0;
cout<<"Read from Child "<<collected<<endl;
ReverseAString(collected);
cout<<"After Reversing: "<<collected<<endl;
write(fifoParent[1],collected,sizeof(collected));
close(fifoParent[1]);
}
else
{
char buf[100],collected[100];
close(fifoParent[1]);
close(fifoChild[0]);
in = 0;
cout<<"Enter a string: ";
gets(buf);
write(fifoChild[1],buf,sizeof(buf));
close(fifoChild[1]);
while(read(fifoParent[0],buf,1)>0)
{
collected[in] = buf[0];
in++;
}
collected[in] = 0;
cout<<"Read from Parent "<<collected<<endl;
}
return 0;
}
輸出窗口看起來像這樣:
Enter a string: abc // abc input given
Read from child abc
After reversing: cba
Read from parent cba
你是什麼意思的「他們如何工作」? – 2014-09-01 18:45:04
這是「工作」,因爲這是管道所做的 - 你爲一端寫了一些東西,然後從另一端讀取它。你在問管道是如何工作的?或者子進程如何繼承文件描述符?或者是其他東西? – 2014-09-01 18:46:23
詢問「從孩子abc讀取」如何執行?上面給出的輸出是通過在父進程和子進程之間來回切換產生的......對吧?哪些陳述導致了這一點? – 2014-09-01 18:49:22