0
vfork()로 프로그램을 작성하고 parent가 n 개의 자식을 만들고 매개 변수가있는 아들 수를 삽입하고 싶습니다. 그리고 나는 예를 들어 아들의 수를 합계를 :vfork()로 자식 수 합계
./sum 4
The sum of the child: 10
The sum of the parent: 10
(1 + 2 + 3 + 4)
이 내가 와서 작은 코드입니다,하지만 난 무한 루프를 얻을.
int n = atoi(argv[1]);
int i = 1;
pid_t pid;
int sumchild = 0;
int sumparent = 0;
while(i <= n){
pid = vfork();
if(pid == 0){
sumchild = sumchild + i;
}
i++;
}
printf("The sum of the child: %i ", sumchild);
sumparent = (1 + n) * (n/2);
printf("The sum of the parent: %i \n", sumparent);
난 당신이
fork()
처럼
wait()
을 필요로하지 않는다는 것을 들었지만, 내가 여기에 무한 루프를 얻는 이유를 모르겠어요.
vfork()
은 어떻게 사용해야합니까? 코드를 올바르게 작성했거나 실수를 했습니까?