2010-07-09 47 views
2

我寫了這個代碼:Ç - 叉 - 等待問題

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <sys/types.h> 
#include <sys/shm.h> 

#define N 512 

void chunk0(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it); 
void chunk1(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it); 
void chunk2(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it); 
void chunk3(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it); 
double get_time(void); 

void main(void) 
{ 
    int i,j,k,iterations=0; 
    int plc=N/4; 
    unsigned int *a=(unsigned int *)malloc(N*N*(sizeof(unsigned int))); 
    unsigned int *b=(unsigned int *)malloc(N*N*(sizeof(unsigned int))); 
    unsigned int shmsz=N*N*(sizeof(unsigned int)); 
    pid_t pid; 

    srand (time(NULL)); 
    double start=get_time(); 

    int shmid; 
    if ((shmid = shmget(IPC_PRIVATE, shmsz, IPC_CREAT | 0666)) < 0) { 
     perror("shmget"); 
     exit(1); 
    } 
    //Now we attach the segment to our data space. 
    char *shm; 
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { 
     perror("shmat"); 
     exit(1); 
    } 
    unsigned int *s = (unsigned int *) shm; 

    for(iterations=0;iterations<1000;iterations++){ 
     printf("Iteration #%d\n",iterations+1); 
     for(i=0;i<N;i++){ 
      for(j=0;j<N;j++){ 
       *(a+(i*N+j))=(rand()%1001); 
       *(b+(i*N+j))=(rand()%1001);; 
       *(s+(i*N+j))=0; 
      } 
     } 

     pid = fork(); 
     if (pid == 0) { 
      chunk0(s,a,b,plc,iterations); 
      break; 
     }else { 
      pid = fork(); 
      if (pid == 0){ 
       chunk1(s,a,b,plc,iterations); 
       break; 
      }else { 
       pid = fork(); 
       if (pid == 0){ 
        chunk2(s,a,b,plc,iterations); 
        break; 
       }else { 
        chunk3(s,a,b,plc,iterations); 
        wait(NULL); 
       } 
      } 
     } 
     wait(NULL); 
    } 

    double end=get_time(); 
    double diff=end-start; 
    printf("\n Time for run this code is: %lf seconds \n",diff); 

} 

void chunk0(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it) 
{ 
    int i,j,k; 

    for(i=0;i<MID;i++){ 
     for(j=0;j<N;j++){ 
      for(k=0;k<N;k++){ 
       *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j))); 
      } 
     } 
    } 
    printf("\tChild process 0 (Iteration %d) is done ***\n",it); 
    exit(0); 
} 
void chunk1(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it) 
{ 
    int i,j,k; 

    for(i=MID;i<MID*2;i++){ 
     for(j=0;j<N;j++){ 
      for(k=0;k<N;k++){ 
       *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j))); 
      } 
     } 
    } 
    printf("\tChild process 1 (Iteration %d) is done ***\n",it); 
    exit(0); 
} 
void chunk2(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it) 
{ 
    int i,j,k; 

    for(i=MID*2;i<MID*3;i++){ 
     for(j=0;j<N;j++){ 
      for(k=0;k<N;k++){ 
       *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j))); 
      } 
     } 
    } 

    printf("\tChild process 2 (Iteration %d) is done ***\n",it); 
    exit(0); 
} 
void chunk3(unsigned int *s, unsigned int *a, unsigned int *b, int MID,int it) 
{ 
    int i,j,k; 

    for(i=MID*3;i<N;i++){ 
     for(j=0;j<N;j++){ 
      for(k=0;k<N;k++){ 
       *(s+(i*N+j))=*(s+(i*N+j))+(*(a+(i*N+k)))*(*(b+(k*N+j))); 
      } 
     } 
    } 

    printf("\tChild process 3 (Iteration %d) is done ***\n",it); 
//  exit(0); 
} 

double get_time(void){ 
    struct timeval stime; 
    gettimeofday (&stime, (struct timezone*)0); 
    return (stime.tv_sec+((double)stime.tv_usec)/1000000); 
} 

計劃不會等待一個迭代完成,並開始下一次迭代!
看看結果:

Iteration #1 
    Child process 0 (Iteration 0) is done *** 
    Child process 3 (Iteration 0) is done *** 
    Child process 1 (Iteration 0) is done *** 
Iteration #2 
    Child process 2 (Iteration 0) is done *** 
    Child process 0 (Iteration 1) is done *** 
    Child process 1 (Iteration 1) is done *** 
    Child process 3 (Iteration 1) is done *** 
Iteration #3 
    Child process 2 (Iteration 1) is done *** 
    Child process 0 (Iteration 2) is done *** 
    Child process 3 (Iteration 2) is done *** 
Iteration #4 
    Child process 1 (Iteration 2) is done *** 
    Child process 2 (Iteration 2) is done *** 
    Child process 1 (Iteration 3) is done *** 
    Child process 3 (Iteration 3) is done *** 
Iteration #5 
    Child process 0 (Iteration 3) is done *** 
    Child process 2 (Iteration 3) is done *** 
    Child process 0 (Iteration 4) is done *** 
    Child process 1 (Iteration 4) is done *** 
    Child process 2 (Iteration 4) is done *** 
    Child process 3 (Iteration 4) is done *** 
Iteration #6 
    Child process 1 (Iteration 5) is done *** 
    Child process 0 (Iteration 5) is done *** 
    Child process 2 (Iteration 5) is done *** 
    Child process 3 (Iteration 5) is done *** 
Iteration #7 
    Child process 0 (Iteration 6) is done *** 
    Child process 1 (Iteration 6) is done *** 
    Child process 2 (Iteration 6) is done *** 
    Child process 3 (Iteration 6) is done *** 

這個是什麼?
我用等待(NULL)但是...

+0

這功課嗎? – 2010-07-09 19:04:33

+0

不,我正在爲某人做某事 – RYN 2010-07-09 19:10:01

+0

順便說一下,'%lf'不是雙倍的正確格式。它只是'%f'。像'printf'這樣的函數具有可變數量的參數會受到默認促銷的影響,這意味着沒有辦法在不提升爲double的情況下傳遞普通的float。 – 2010-07-09 19:19:55

回答

7

wait()只會等到一個子進程退出。你想等到他們都退出了。我認爲你只需要連續調用它3次...

+0

工作!非常感謝你 – RYN 2010-07-09 19:36:50

1

你沒有正確地等待你創建的子進程。你應該這樣做:

pid = fork(); 
if (pid == 0) { 
    chunk0(s,a,b,plc,iterations); 
    break; 
}else { 
    pid = fork(); 
    if (pid == 0){ 
     chunk1(s,a,b,plc,iterations); 
     break; 
    }else { 
     pid = fork(); 
     if (pid == 0){ 
      chunk2(s,a,b,plc,iterations); 
      break; 
     }else { 
      chunk3(s,a,b,plc,iterations); 
      wait(NULL); 
     } 
     wait(NULL); 
    } 
    wait(NULL); 
} 

它看起來很糟糕,你應該重新考慮你的程序邏輯,使它更具可讀性。另外,請記得在退出之前正確清理子進程中的資源(請參閱shmdt)。

要考慮的另一件事是使用shm_openshm_unlink來代替。