gpt4 book ai didi

c - 如何在 Unix 中将参数从父进程传递给子进程?

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:49 24 4
gpt4 key购买 nike

在我的代码中我想...

1) 父进程会创建一个至少有10个的数组元素

2)子进程会计算所有元素的产生数组内有奇数索引

3) 子进程将提供结果计算完成后传递给父进程,然后传递给子进程将终止

4) parent在得到子进程的结果

5)父进程最终会输出结果。

现在下面的CODE LOGIC很容易写了

int cal(int arr[10]) {
int i=0;
int sum = 0;
for (i=1; i<10; i=i+2) {
sum = sum + arr[i];
}

return sum;
} // end of calc

int main() {
int arr[] = { 10, 20, 25, 5, 6, 45, 87, 98, 23, 45};
int sum = cal(arr);

printf("Sum of all odd indexs element is : %d", sum);

return 0;
} // end of main

下面是使用 fork() 创建子进程的代码

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main() {
pid t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls","ls",NULL);

}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
} // end of main

我的问题是...

  • 我将如何使用CODE LOGIC 并将其与使用 fork() 创建的子进程相结合?如果 pid == 0,则子进程的创建成功,所以我认为这是我们插入步骤 2 的代码的地方... 2) 子进程将计算所有元素的生成数组内的索引为奇数。

  • 父进程如何将数组发送给子进程,以便子进程对索引为奇数的元素求和?

更新代码:我将上面的两个代码合并为一个

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

/*
calculate the production of all elements with odd index inside the array
*/
int cal(int arr[10]) {
int i=0;
int sum = 0;
for (i=1; i<10; i=i+2) {
sum = sum + arr[i];
}

return sum;
} // end of calc

int main() {
pid t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
print("I am the child process");
// the child process will calculate the production
// of all elements with odd index inside the array
calc();
// the child process will provide the result to the parent process
// when it finish calculation and then the child process will terminate
exit(0);

}
else { /* parent process */
/* parent will wait for the child to complete */
printf("I am the parent, waiting for the child to end");
// the parent process will create an array with at least 10 element
int arr[] = { 1, 2, 5, 5, 6, 4, 8, 9, 23, 45 };
int sum = calc(arr);
wait(NULL);
printf("Child completed calculating the production of all elements with odd index inside the array");
// the parent will calculate the production after it get the result from the child process
// the parent process will finally output the results.
printf("Sum of all odd indexs element is : %d", sum);
}
return 0;
} // end of main

最佳答案

inter-process communication (IPC) 机制允许您在进程之间传递信息。

通常,fork是在类 Unix 系统中创建新进程的唯一方法。此时,子进程继承父进程的代码和地址空间。这意味着此时 child 是 parent 的副本(在某种程度上,请参见上面的链接)。

在现代 Unix 变体和 Linux 中,fork使用写时复制页面实现。这只是意味着当父进程或子进程试图修改共享内存页面时,操作系统会创建该页面的副本。现在 parent 和 child 有自己的内存页。

系统调用exec用新的过程镜像替换当前过程镜像。这意味着父进程和子进程现在不会共享任何内存页或代码。

在您的程序中,您不应调用 execlp()。利用写时复制机制的优势。在定义arr 之后,在CODE LOGIC 程序的main() 函数中执行fork()。然后从子进程访问arr。使用 wait()系统调用使父级被阻塞,直到子级未完成。

您应该使用 IPC 从子进程返回结果。在您的情况下,管道是最佳选择。但很明显,您做的实验作业是关于 Unix 进程的,而不是关于 IPC 的。所以你可以通过子进程的退出代码返回结果。将结果传递给 exit() 函数。请注意,您只能传递 8 位(请参阅我的回答下的评论)。

这是一个工作代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int calc(int *arr, int n) {
int sum = 0;
for (i = 1; i < n; i += 2) {
sum = sum + arr[i];
}
return sum;
}

int main(void) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = sizeof(arr) / sizeof(arr[0]);

pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
}
else if (pid == 0) {
printf("I am the child process\n");

int child_sum = calc(arr, n);
exit(child_sum);
}
else {
printf("I am the parent process\n");

int parent_sum = calc(arr, n);

int child_sum;
if (wait(&child_sum) == -1) {
perror("wait failed");
}
else {
printf("Sum by child: %d\n", child_sum);
}

printf("Sum by parent: %d\n", parent_sum);
}

return 0;
}

关于c - 如何在 Unix 中将参数从父进程传递给子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574822/

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