如何将参数从父进程传递给Unix中的子进程?
里面我的代码我想...如何将参数从父进程传递给Unix中的子进程?
1)父进程会创建一个数组有至少10 元素
2)子进程将计算生产的所有元素 用阵列
3)内奇数索引的子进程将提供结果 到当它完成计算,然后子进程 将终止
父进程4)母公司将计算生产后,从子进程
5)父进程将最终输出 结果得到 结果。
现在代码逻辑很容易写是向下跌破
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
这里是代码使用叉创建一个子进程()
#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
我的问题是。 ..
我该如何使用代码逻辑并结合使用fork()创建子进程?如果pid == 0,那么创建一个子进程是成功的,所以我认为这就是我们插入第二步的代码的地方...... 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系统中创建新进程的唯一方法。在那儿,子进程继承父代码和地址空间。这意味着孩子在这个时间点是父母的重复(在某种程度上,请参见上面的链接)。
在现代Unix变种和Linux中,fork是使用写时复制页面实现的。这仅表示当父进程或子进程尝试修改共享内存页面时,操作系统会创建此页面的副本。现在父母和孩子有自己的内存页面。
系统调用exec用新的过程映像替换当前过程映像。这意味着父进程和子进程现在不会共享任何内存页面或代码。
在你的程序中,你不应该打电话execlp()
。使用写入时复制机制的优点。在定义arr
之后,在您的CODE LOGIC程序中,功能中的main()
功能。然后从子进程访问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;
}
退出状态是一个糟糕的选择子进程将其计算结果传递给其父进程,因为只有8位可供其使用(在符合POSIX的系统上,但我们似乎假设POSIX或类似的使用fork,wait和' unistd.h')。 OP的结果很可能太大了。 –
非常感谢您对我有用的评论。 – boriaz50
从子进程读取结果的通常做法是设置一个FIFO对,以便孩子可以将结果写入FIFO并关闭它,父母可以读取并解释它们。例如,这就是shell中的'variable = $(somecommand)'。 –
这里execlp
会给你的子进程新的地址空间。所以你实际上不能从父进程向子进程发送一个参数。但是你可以做如下,
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid;
/* fork a child process */
pid = fork();
int sum = 0;
int arr[] = { 10, 20, 25, 5, 6, 45, 87, 98, 23, 45};
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
int i=0;
for (i=1; i<10; i=i+2) {
sum = sum + arr[i];
}
return sum;
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
int i=0;
for (i=1; i<10; i=i+2) {
sum = sum + arr[i];
}
printf("%d\n",sum);
}
return 0;
}
PS- 这里阵列的fork()的声明后,所以它是常见的两种父子进程。
所以,现在你只能通过'ls'传递'argv [0]'。如果您想传递更多参数,请将它们放在命令行中。这就是:'execlp(“/ path/to/your/executable”,“argument-that-become- $ 0”,“argument-that-become- $ 1”,“argument-that-become- $ 2”,“etc” ,NULL)'。 –
要在2进程之间进行通信,可以使用信号('kill()'和'signal()'或'sigaction()') – YaatSuka
@YaatSuka,呃? OP不仅仅希望发送(非信息携带)信号,他们希望传递明确的值。信号没有关于传送顺序的保证,其中有几个含义不能被覆盖(你不能通过'SIGKILL'传递'9',并且程序不以任何方式解释为退出,所以它没有用作发送未知值的整数的方法)。 –