我对如何正确使用管道()来传递两个进程之间的整数值有点困惑。
在我的节目,我首先创建一个管道,然后我叉了。 我认为我有“二”管呢?
据我了解,这是我的任务。 我的父经过一个for循环检查的整数值“i”为特定操作,增加一个计数变量,并保存值到一个数组。 每次检查后,我的父母应该通过管道传递一个整数值,“我”我的孩子。 我的孩子然后使用该整数值,确实在一定值检查,并应增加一个计数变量,并将结果保存在[共享?]数组。 最终, 孩子应该回到它的最终计父,谁再打印出两项罪名,而“共享”阵列。
- >我不知道我需要有一个共享的阵列或将结果在所有的保存。 我可能只需要数 - 作业含糊不清,我正在等待来自教授的响应。 也; 我甚至可以做进程间的共享阵列? 这听起来似乎有些问题,给我一个开始。
- >这是我的问题:一, 我该如何使用管道的整数? 我只看到他们的字符数组和以前的答案似乎并不认为这是可能的或法律..? 我不确定。 有没有解决,我能找到它。
- >我如何使用单向管道整数传递给孩子? 而有孩子的回报的东西吗? 我不知道我是如何能......两个管道之间的区分。 我“知道” [或认为我知道]我必须关闭每个管的一个未使用部分,以避免“一些模糊的问题。”
很抱歉的愚蠢的问题; 我一直没教导的过程(除了叉)或管道(所有),但在这个类 - 所以我真的不知道从哪里开始!
继承人我的代码部分 - 它并不漂亮,这是行不通的,我不指望它。 它更像是一个外壳占位符。 一旦我弄清楚如何使用管道 - 我可能使代码才有意义。
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一个子进程,那里的孩子从叉子推出()的样本。 它变得更为复杂,(显然)用于发送和接收数据,但我不能为你做的一切 。 这只是叉子和等待整数(实际上,是由一个int使用的字节数)从孩子。
更新:这个后添加发送+响应双向通信的例子。 看到第二个代码的详细信息列表。
希望能帮助到你。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
// 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<NUM_PIPES; ++i)
{
if (pipe(fd+(i*2)) < 0)
{
perror("Failed to allocate pipes");
exit(EXIT_FAILURE);
}
}
// fork() returns 0 for child process, child-pid for parent process.
if ((pid = fork()) < 0)
{
perror("Failed to fork process");
return EXIT_FAILURE;
}
// if the pid is zero, this is the child process
if (pid == 0)
{
// Child. Start by closing descriptors we
// don't need in this process
close(fd[P1_READ]);
close(fd[P1_WRITE]);
// used for output
pid = getpid();
// wait for parent to send us a value
len = read(fd[P2_READ], &val, sizeof(val));
if (len < 0)
{
perror("Child: Failed to read data from pipe");
exit(EXIT_FAILURE);
}
else if (len == 0)
{
// not an error, but certainly unexpected
fprintf(stderr, "Child: Read EOF from pipe");
}
else
{
// report what we received
printf("Child(%d): Received %d\n", pid, val);
// now double it and send it back
val *= 2;
printf("Child(%d): Sending %d back\n", pid, val);
if (write(fd[P2_WRITE], &val, sizeof(val)) < 0)
{
perror("Child: Failed to write response value");
exit(EXIT_FAILURE);
}
}
// finished. close remaining descriptors.
close(fd[P2_READ]);
close(fd[P2_WRITE]);
return EXIT_SUCCESS;
}
// Parent. close unneeded descriptors
close(fd[P2_READ]);
close(fd[P2_WRITE]);
// used for output
pid = getpid();
// send a value to the child
val = 42;
printf("Parent(%d): Sending %d to child\n", pid, val);
if (write(fd[P1_WRITE], &val, sizeof(val)) != sizeof(val))
{
perror("Parent: Failed to send value to child ");
exit(EXIT_FAILURE);
}
// now wait for a response
len = read(fd[P1_READ], &val, sizeof(val));
if (len < 0)
{
perror("Parent: failed to read value from pipe");
exit(EXIT_FAILURE);
}
else if (len == 0)
{
// not an error, but certainly unexpected
fprintf(stderr, "Parent(%d): Read EOF from pipe", pid);
}
else
{
// report what we received
printf("Parent(%d): Received %d\n", pid, val);
}
// close down remaining descriptors
close(fd[P1_READ]);
close(fd[P1_WRITE]);
// wait for child termination
wait(NULL);
return EXIT_SUCCESS;
}
(编译,例如gcc thisfile.c -o test
)
产量
Parent(2794): Sending 42 to child
Child(2797): Received 42
Child(2797): Sending 84 back
Parent(2794): Received 84