2014-11-04 1 views
-1

글쎄, 형제를 처리 할 신호를 보내야하지만 어떻게해야할지 모르겠다. 나는 그들을 pidx와 pidy에 저장하려고 시도했지만, 음수와 같은 이상한 값을 얻기 때문에 그것이 잘못되었다고 생각합니다. 내 문제에 대한 간단한 예를 들며, 세 번째 자녀가 형제 자매의 아들을 가질 수 있다면 해결할 수 있다고 생각합니다. 나는 (POSIX)와 C를 컴파일하기 위해 우분투를 사용하고있다.프로세스 트리의 형제와 PID 공유하기

#include <stdio.h> 
#include <unistd.h> 
main(void) 
{ 
pid_t pid; 
int x, pidx, pidy; 

for(x=1;x<=3;x++) 
    { 

    pid=fork(); 
    if(pid) 
    { 
     printf("I'm the process %d\n",getpid()); 
     sleep(2); 
    } 

    else{ 
     //X process 
     if (x==0){ 

      printf("I'm the child %d, my parent is %d\n",getpid(),getppid()); 
      pidx=getpid(); 
      sleep(2); 
      exit(0); 
     } 

    //Y process 

     if (x==1) 
     { 
      printf("I'm the child %d, my parent is %d\n",getpid(),getppid()); 
      pidy=getpid(); 
      sleep(2); 
      exit(0); 

    } 

     //Z process 

     if (x==2) 
     { 
      printf("I'm the child %d, my parent is %d, and my siblings are \n",getpid(),getppid(), pidx, pidy); 
      sleep(2); 
      exit(0); 

     } 


    } 
return 0; 
} 

내 원본 코드, 너무 길어서 죄송합니다. Bisabuelo는 grandgrandson (나는 영어로 그 단어에 대해 확실하지 않다)을 의미한다, abuelo는 손자를 의미한다. 터미널에서 요청하면 Z부터 다른 프로세스로 신호를 보내야하지만 형제와의 커뮤니케이션 방법을 모르면 계속할 수 없습니다.

#include <sys/types.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <sys/wait.h> 
void a(){ 
    printf("Hi, I'm A with PID %d", getpid()); 
    execlp("pstree","pstree","-la", NULL); 

} 
void b(){ 
    printf("Hi, I'm B with PID %d", getpid()); 
    execlp("pstree","pstree","-la", NULL); 

} 
void x(){ 
     printf("Hi, I'm X with PID %d", getpid()); 
    execlp("ls","ls","-la", NULL); 

} 
void y(){ 
    printf("Hi, I'm Y with PID %d", getpid()); 
    execlp("ls","ls","-la", NULL); 

} 


int main(int argc, char *argv[]) 
{ 
int i, e; 
int bisabuelo, abuelo; 
pid_t pid1, pid2, pid3, pidx, pidy; 
bisabuelo=getpid(); 
    if ((pid1=fork())==0){ 
     abuelo=getpid(); 
     if ((pid2=fork())==0){ 
      for (i=0;i<3;i++) 

      { 
        if ((pid3=fork())>0){ 
        if (i==0){ 
        printf("I'm a grandson (%d) son of %d, grandson of %d\n",getpid(), getppid(), bisabuelo); 
        signal(SIGUSR1,b); 
        sleep(0); 
        } 

       } 

       else{ 
        sleep(0); 
        switch (i){ 
         case 0:{ 
         pidx=getpid(); 
         printf("I'm the grandgrandson? X (%d) son of %d, grandson of %d, grandgrandson? of %d\n",getpid(), getppid(), abuelo, bisabuelo); 
         signal(SIGUSR1,x); 
         sleep(7); 
         printf("I'm X(%d) and die \n", getpid()); 
         exit(0); 
        break; 
         } 
         case 1:{ 
         pidy=getpid(); 
         printf("I'm the grandgrandson? Y (%d) son of %d, grandson of %d, grandgrandson? of %d\n",getpid(), getppid(), abuelo, bisabuelo); 
         sleep(5); 
         signal(SIGUSR1,y); 
         printf("Soy Y(%d) y muero \n", getpid()); 
         exit(0); 


        break; 
         } 
         case 2:{ 
         printf("I'm the grandgrandson? Z (%d) son of %d, grandson of %d, grandgrandson? of %d\n",getpid(), getppid(), abuelo, bisabuelo); 
         printf("My sibling X is %d and my sibling Z is %d", pidx, pidy); 

         switch (argv[1][0]); 
        { 
         case 'A': 
          //kill(abuelo, SIGUSR1); 
          printf("Me han pasado A"); 
          break; 
         case 'B': 
          //kill(getppid, SIGUSR1); 
          printf("Me han pasado B"); 

          break; 

         default: 
          printf("No ha introducido ningún argumento"); 
          break; 
        } 


         sleep(3); 
         printf("I'm Z (%d) and I die \n", getpid()); 
         exit(0); 
        break; 
         } 
        } 
        } 
       } 
        wait(&e); 
        wait(&e); 
        wait(&e); 
        sleep(1); 
        printf("I'm B(%d)and I die \n", getpid()); 


     } 
     else{ 
      printf("I'm A (%d, son of %d)\n", getpid(), getppid()); 
      signal(SIGUSR1,a); 
      wait(&e); 
      sleep(1); 
      printf("I'm A(%d) and i die \n", getpid()); 

     } 
    } 

     else{ 
      printf("I'm arb (%d)\n", getpid()); 
      wait(&e); 
      printf("I'm arb(%d)and I die\n", getpid()); 
     } 
     sleep(1); 

    exit (0); 
} 
+1

"나는 그것이 틀렸다고 생각합니다": 왜? 어떤 행동을 보았습니까? 그리고 예상했던 것과 다른 점은 무엇입니까? –

+0

성 차별 주의자. '형제 자매'는 더 정치적으로 정확했을 것입니다 :) –

+0

음수와 같은 이상한 값을 얻기 때문에 저는 잘못 생각합니다. 그리고 성교자가되는 것에 대해 유감스럽게 생각합니다.하지만 우리나라의 과정은 남성적인데, 나는 그것을 형제라고 부릅니다 :/나는 편집하려고 노력할 것입니다. –

답변

1

신호를 여러 프로세스로 보내려면 "프로세스 그룹"을 사용할 수 있습니다. 기본적으로 pid= -x으로 신호를 보내면 시스템이 프로세스 그룹 x의 모든 프로세스에 신호를 전달하게됩니다. 모든 "형제"프로세스 (및 다른 프로세스)가 동일한 프로세스 그룹에 있는지 확인해야합니다. 자세히보기 : man 2 kill, http://en.wikipedia.org/wiki/Process_group

+0

조심스럽고, getpid와 getppid는 에러시 -1을 반환 할 것이다 .sigkill을 프로세스 그룹 -1에 보내지 마라. –