将值从子进程传递到父进程

先决条件 : 管道()和叉子()基本 编写一个C程序,其中子进程获取一个输入数组,并使用pipe()和fork()将其发送给父进程,然后在父进程中打印它。

null

例如: 假设我们在子进程中有一个数组a[]={1,2,3,4,5},那么输出应该是12345。

Input:  1 2 3 4 5
Output: 1 2 3 4 5

// C program for passing value from
// child process to parent process
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define MAX 10
int main()
{
int fd[2], i = 0;
pipe(fd);
pid_t pid = fork();
if (pid > 0) {
wait(NULL);
// closing the standard input
close(0);
// no need to use the write end of pipe here so close it
close(fd[1]);
// duplicating fd[0] with standard input 0
dup(fd[0]);
int arr[MAX];
// n stores the total bytes read successfully
int n = read(fd[0], arr, sizeof (arr));
for ( i = 0;i < n/4; i++)
// printing the array received from child process
printf ( "%d " , arr[i]);
}
else if ( pid == 0 ) {
int arr[] = {1, 2, 3, 4, 5};
// no need to use the read end of pipe here so close it
close(fd[0]);
// closing the standard output
close(1);
// duplicating fd[0] with standard output 1
dup(fd[1]);
write(1, arr, sizeof (arr));
}
else {
perror ( "error" ); //fork()
}
}


执行上述代码的步骤:

  • 编译 gcc程序名称。C
  • 跑,写 /a.out
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享