2014-01-27 1 views
0

이것은 부모 프로세스에서 두 개의 하위 프로세스를 생성하는 C 코드입니다. 성공적으로 생성됩니까? 나의 현재 출력은 다음과 같습니다이 코드는 상위 프로세스에서 두 개의 하위 프로세스를 성공적으로 생성하게됩니까?

당신은 id가 29509
당신이 자식 프로세스 1에 있고 당신의 부모 ID가 당신은 누구의 ID입니다 29,511
당신이에 부모 프로세스에 29,509
입니다 부모 프로세스에 자식 프로세스 2이고 부모 ID는 29509입니다.

왜 내 출력의 세 번째 줄에는 부모 ID가 다른가요 ??

#include<stdio.h> 
    #include<unistd.h> 
    int main(){ 
      pid_t child1,child2; 
      int c,d,e; 
      child1=fork(); 
      if(child1==0){ 
        c=getppid(); 
        printf("you are in child process 1 and your parent id is %d\n",c); 

      } 
      else{ 
        child2=fork(); 
        e=getpid(); 
        printf("You are in parent process whose id is %d\n",e); 
      } 
      if(child2==0){ 
        d=getppid(); 
        printf("you are in child process 2 and your parent id is %d\n",d); 
      } 
    } 

출력 fork 두 번째 후

You are in parent process whose id is 29509 
you are in child process 1 and your parent id is 29509 
You are in parent process whose id is 29511 
you are in child process 2 and your parent id is 29509 

답변

1

, 부모와 두 번째 자식은 "부모 프로세스"printf을 실행 모두이다.

1

e=getpid(); printf("You are in parent process whose id is %d\n",e);

이 라인은 부모와 아이에 의해 실행됩니다.

관련 문제