使用pipe在父级和子级之间传递整数值

我对如何正确使用pipe()在两个进程之间传递整数值感到困惑。

在我的程序中,我首先创建一个管道,然后我把它分叉。 我假设我有“两个”管道呢?

据我所知,这是我的任务。 我的父进行for循环检查某个操作的整数值“i”,增加计数变量,并将值保存到数组中。 每次检查后,我的父母应该通过管道向我的孩子传递一个整数值“i”。 然后我的孩子使用该整数值,对值进行一些检查,并应增加计数变量,并将结果保存在[shared?]数组中。 最终, 孩子应该将它的最终计数返回给父母,父母然后打印出两个计数,以及“共享”数组。

– >我不确定我需要共享数组还是保存结果。 我可能只需要计数 – 家庭作业含糊不清,我正在等待教授的回应。 也; 我甚至可以在进程之间进行共享数组吗? 这对我来说听起来像是一个问题的开始。

– >这是我的问题:一个; 如何使用管道进行整数? 我只看过他们的角色arrays,以前的答案似乎并不认为这是可能的或合法的。? 我不确定。 没有我能找到的解决方案。

– >如何使用单向管道将整数传递给孩子? 并让孩子归还一些东西? 我不确定我怎么能……区分这两个管道。 我确实“知道”[或者我想知道]我必须关闭每个管道的一个未使用的部分,以避免“一些模糊的问题”。

抱歉愚蠢的问题; 我还没有在本课程中学过流程(除了叉子)或管道(根本没有) – 所以我不确定从哪里开始!

这是我的代码的一部分 – 它不漂亮,它不起作用,我不指望它。 它更像是一个shell占位符。 一旦我弄清楚如何使用管道 – 我可能会使代码有意义。

int main(void) { int fd[2]; pid_t childpid; pid_t parentpid; int i; int threecount = 0; int fivecount = 0;; int results [MAXSIZE]; parentpid = getpid(); //Get current process ID number pipe(fd); childpid = fork(); if(childpid == 0){ close(fd[0]); //Closing this for some other reason } int j = 0; if(childpid > 0) close(fd[1]); //Closing this for some reason if( childpid == -1 ) { perror("Failed to fork\n"); return 1; } if (childpid > 0) { for(i = 1; i < MAXSIZE;i++) { if(i % 5 == 0) { fivecount++; i = results[j]; j++; wait(NULL); } } } else if (childpid == 0) { if(i % 3 == 0) //This i here should probably be the i value above, piped to the child { threecount++; i = results[j]; //This should be part of th pipe j++; //Trying to keep count of that shared array, not really the right way to do it though. } } printf("%d %d \n", fivecount,threecount); return 0; 

}

这是一个蹩脚的(并且没有错误检查,顺便说一句)样本,因为我可以鼓励使用管道从父进程向子进程发送int,其中子进程从fork()启动。 发送接收数据变得更加复杂(显然),但我无法为您做任何事情 。 这只是分叉并等待来自子进程的int(实际上是int使用的字节数)。

更新:在此之后添加了发送+响应双向通信示例。 有关更多信息,请参阅第二个代码清单。

希望能帮助到你。

 #include  #include  #include  int main(int argc, char *argv[]) { int fd[2]; int val = 0; // create pipe descriptors pipe(fd); // fork() returns 0 for child process, child-pid for parent process. if (fork() != 0) { // parent: writing only, so close read-descriptor. close(fd[0]); // send the value on the write-descriptor. val = 100; write(fd[1], &val, sizeof(val)); printf("Parent(%d) send value: %d\n", getpid(), val); // close the write descriptor close(fd[1]); } else { // child: reading only, so close the write-descriptor close(fd[1]); // now read the data (will block) read(fd[0], &val, sizeof(val)); printf("Child(%d) received value: %d\n", getpid(), val); // close the read-descriptor close(fd[0]); } return 0; } 

输出:

 Parent(5943) send value: 100 Child(5945) received value: 100 

更新:扩展为包含使用两个管道集的发送+响应

 #include  #include  #include  #include  // some macros to make the code more understandable // regarding which pipe to use to a read/write operation // // Parent: reads from P1_READ, writes on P1_WRITE // Child: reads from P2_READ, writes on P2_WRITE #define P1_READ 0 #define P2_WRITE 1 #define P2_READ 2 #define P1_WRITE 3 // the total number of pipe *pairs* we need #define NUM_PIPES 2 int main(int argc, char *argv[]) { int fd[2*NUM_PIPES]; int val = 0, len, i; pid_t pid; // create all the descriptor pairs we need for (i=0; i 

(用例如gcc thisfile.c -o test编译)

产量

 Parent(2794): Sending 42 to child Child(2797): Received 42 Child(2797): Sending 84 back Parent(2794): Received 84