我对如何正确使用pipe()在两个进程之间传递整数值感到有些困惑。
在我的程序中,我首先创建一个管道,然后我把它分叉。我假设我有"两个"管道呢?
据我所知,这是我的任务。 我的父母通过for循环检查整数值" i"对于某个操作,增加count变量,并将值保存到数组中。每次检查后,我的父母应该传递一个整数值," i"通过烟斗给我的孩子。然后我的孩子使用该整数值,对值进行一些检查,并且应该增加一个计数变量,并将结果保存在[shared?]数组中。最终,孩子应该将它的最终计数归还给父母,然后父母打印出两个计数,然后分享"共享"阵列。
- >我不确定我是否需要共享阵列或保存结果。我可能只需要计算 - 家庭作业含糊不清,我正在等待教授的回应。也;我甚至可以在进程之间进行共享数组吗?这对我来说似乎是一个问题的开始。
- >这是我的问题: 一;如何使用整数管道?我只看过他们的角色阵列和以前的答案似乎并不认为这是可能的或合法的??我不确定。我找不到任何决议。
- >如何使用单向管道将整数传递给孩子?并让孩子归还一些东西?我不确定我能如何......区分两个管道。我知道"知道" [或者我想我知道]我必须关闭每个管道的一个未使用的部分以避免"一些模糊的问题"。
抱歉愚蠢的问题;我还没有在本课程中学过流程(除了叉子)或管道(根本没有) - 所以我不确定从哪里开始!
这是我的代码的一部分 - 它并不漂亮,它不起作用,我不期望它。它更像是一个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;
}
答案 0 :(得分:25)
这是一个蹩脚的(并且没有错误检查,顺便说一句)样本,因为我可以鼓励使用管道从父进程向子进程发送int,其中子进程从fork()启动。发送和接收数据变得更加复杂(显然),但我不能为你做所有。这只是分叉并等待来自子进程的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