系统调用如何让parent等待child
System call how to make parent wait for child
这是我在 C 中的代码系统调用。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int n;
int i;
pid_t pid;
int time = 1000;
int sum = 0;
int main(void) {
printf("n: ");
scanf("%d", &n);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of 1 to %d: %d\n", n, sum); // this is ok
} else {
// parent
wait(&time);
printf("Sum of 1 to %d: %d\n", n, sum); // this always return 0;
}
return 0;
}
我不知道为什么在parent的代码块中,和总是等于0。
如何让 parent 等待 child 还是我做错了什么?
这里的问题是变量 sum 不被父进程和子进程共享,在调用 fork() 之后,子进程将拥有自己的变量 sum 副本。
从 POSIX api 使用 shmget(),shmat()。或者使用 pthread,它将为新创建的线程共享相同的内存 space。
更新 - -
将共享内存添加到您的代码中希望这会有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
int n;
int i;
pid_t pid;
int time = 1000;
int main(void) {
int shmid;
int *sum;
printf("n: ");
scanf("%d", &n);
/*request the shared memory from the OS using the shmget()*/
shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
/* shmat() returns a char pointer which is typecast here
to int and the address is stored in the int pointer. */
sum = (int *) shmat(shmid, 0, 0);
for (i = 1; i <= n; i++) {
*sum += i;
}
printf("Sum of 1 to %d: %d\n", n, *sum); // this is ok
/* each process should "detach" itself from the
shared memory after it is used */
shmdt(sum);
} else {
// parent
wait(&time);
sum = (int *) shmat(shmid, 0, 0);
printf("Sum of 1 to %d: %d\n", n, *sum); // this always return 0;
shmdt(sum);
/*delete the cretaed shared memory*/
shmctl(shmid, IPC_RMID, 0);
}
return 0;
}
等待 child 作品。然而,你的期望是错误的。
Apparent只有你认为在fork之后child进程中的计算在parent进程中是可见的。他们不是。 child 是 fork 时 parent 程序的新副本。那时,parent 的 sum
为 0 并保持不变。
有几种机制可以将数据从child传递到parent(搜索词是进程间通信,IPC)。
- 退出()状态
- 文件
- 共享内存
- 管道
- 信号
- 消息队列
- 我还有什么想念的
这是我在 C 中的代码系统调用。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int n;
int i;
pid_t pid;
int time = 1000;
int sum = 0;
int main(void) {
printf("n: ");
scanf("%d", &n);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of 1 to %d: %d\n", n, sum); // this is ok
} else {
// parent
wait(&time);
printf("Sum of 1 to %d: %d\n", n, sum); // this always return 0;
}
return 0;
}
我不知道为什么在parent的代码块中,和总是等于0。 如何让 parent 等待 child 还是我做错了什么?
这里的问题是变量 sum 不被父进程和子进程共享,在调用 fork() 之后,子进程将拥有自己的变量 sum 副本。 从 POSIX api 使用 shmget(),shmat()。或者使用 pthread,它将为新创建的线程共享相同的内存 space。 更新 - - 将共享内存添加到您的代码中希望这会有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
int n;
int i;
pid_t pid;
int time = 1000;
int main(void) {
int shmid;
int *sum;
printf("n: ");
scanf("%d", &n);
/*request the shared memory from the OS using the shmget()*/
shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
/* shmat() returns a char pointer which is typecast here
to int and the address is stored in the int pointer. */
sum = (int *) shmat(shmid, 0, 0);
for (i = 1; i <= n; i++) {
*sum += i;
}
printf("Sum of 1 to %d: %d\n", n, *sum); // this is ok
/* each process should "detach" itself from the
shared memory after it is used */
shmdt(sum);
} else {
// parent
wait(&time);
sum = (int *) shmat(shmid, 0, 0);
printf("Sum of 1 to %d: %d\n", n, *sum); // this always return 0;
shmdt(sum);
/*delete the cretaed shared memory*/
shmctl(shmid, IPC_RMID, 0);
}
return 0;
}
等待 child 作品。然而,你的期望是错误的。
Apparent只有你认为在fork之后child进程中的计算在parent进程中是可见的。他们不是。 child 是 fork 时 parent 程序的新副本。那时,parent 的 sum
为 0 并保持不变。
有几种机制可以将数据从child传递到parent(搜索词是进程间通信,IPC)。
- 退出()状态
- 文件
- 共享内存
- 管道
- 信号
- 消息队列
- 我还有什么想念的