gpt4 book ai didi

c - 使用管道在父子之间传递整数值

转载 作者:太空狗 更新时间:2023-10-29 16:30:49 25 4
gpt4 key购买 nike

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

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

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

-> 我不确定是否需要共享数组或是否需要保存结果。我可能只需要计数 - 家庭作业模棱两可,我正在等待教授的回复。还;我什至可以在进程之间做一个共享数组吗?对我来说,这听起来像是某个问题的开始。

-> 以下是我的问题:一;如何使用管道表示整数?我只看到它们用于字符数组,以前的答案似乎不认为这是可能的或合法的..?我不确定。我找不到解决办法。

-> 如何使用单向管道将整数传递给 child ?让 child 还东西?我不确定我如何能够……区分这两个管道。我确实“知道”[或认为我知道]我必须关闭每个管道的一个未使用部分以避免“一些模糊的问题”。

抱歉提出愚蠢的问题;我还没有在这门课上学过进程(除了 fork)或管道(根本)——所以我真的不确定从哪里开始!

这是我的部分代码 - 它不漂亮而且不起作用,我不希望它起作用。它更像是一个 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() 启动的。发送接收数据变得更加复杂(显然),但我不能为您做一切。这只是 fork 并等待来自 child 的 int(实际上,是 int 使用的字节数)。

更新:在此之后添加了send+response双向通信示例。有关详细信息,请参阅第二个代码 list 。

希望对您有所帮助。

#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

关于c - 使用管道在父子之间传递整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12864265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com