2012-11-05 27 views
2

開發一個程序是使用IPC機制執行以下問題之一:方式 - 「通道」。通過將其擴展到較低階的行列式來實現方陣行列式的計算。 「主」過程發送作業「驅動」過程,但後者執行決定因素的計算,然後計算主過程的結果。換句話說,需要使用管道功能。我有一個工作計劃,但沒有IPC機制。我不知道管道功能以及它是如何工作的。通過管道函數計算C中的行列式矩陣。修改代碼

#include<stdio.h> 
#include<math.h> 
#include<stdlib.h> 

int determinant(int n, double mat[n][n]) 
{ 
    int i,j,i_count,j_count, count=0; 
    double array[n-1][n-1], det=0; 
    if(n==1) return mat[0][0]; 
    if(n==2) return (mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]); 

    for(count=0; count<n; count++) 
    { 
     i_count=0; 
     for(i=1; i<n; i++) 
     { 
      j_count=0; 
      for(j=0; j<n; j++) 
      { 
       if(j == count) continue; 
       array[i_count][j_count] = mat[i][j]; 
       j_count++; 
      } 
      i_count++; 
     } 
     det += pow(-1, count) * mat[0][count] * determinant(n-1,array); 
    } 
    return det; 
} 

int main() 
{ 
    int i, j, dim; 
    printf("Enter n\n"); 
    scanf("%d", &dim); 
    double matrix[dim][dim]; 
    printf("Enter matrix:\n"); 
    for(i = 0; i < dim; i++) 
    { 
     for(j = 0; j < dim; j++) 
     { 
      scanf("%lf \n", &matrix[i][j]); 
     } 
    } 
    double x = determinant(dim, matrix); 
    printf("Determinant = %g\n", x); 
    return 0; 
} 

回答

1

的幾個注意事項:管道是單向的,一個進程寫入和過程reads.So當你開始管道,如果一個進程從管道讀取,應關閉管,反之亦然的寫入一側。否則,你冒險將數據發送給自己,而不是發送給其他進程。管道可以在fork()之前啓動,並且子進程繼承管道(就像繼承整個堆棧,除了pid)。
在這個例子中父親過程中輸入要求的矩陣,然後將其發送給子進程,子進程,計算行列式和它打印在屏幕上:

#include<stdio.h> 
#include<math.h> 
#include<stdlib.h> 
#include <unistd.h> 

int determinant(int n, double mat[n][n]) 
{ 
    int i,j,i_count,j_count, count=0; 
    double array[n-1][n-1], det=0; 
    if(n==1) return mat[0][0]; 
    if(n==2) return (mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]); 

    for(count=0; count<n; count++) 
    { 
     i_count=0; 
     for(i=1; i<n; i++) 
     { 
      j_count=0; 
      for(j=0; j<n; j++) 
      { 
       if(j == count) continue; 
       array[i_count][j_count] = mat[i][j]; 
       j_count++; 
      } 
      i_count++; 
     } 
     det += pow(-1, count) * mat[0][count] * determinant(n-1,array); 
    } 
    return det; 
} 

int main() 
{ 
    int i, j, dim; 
    int fd[2]; 
    pipe(fd); 
    pid_t pid=fork(); 
    if(pid) 
    { 
     // Father process 
     close(fd[0]); // close the reading side of the pipe 
     char buffer[100]; 
     printf("Enter n\n"); 
     fgets(buffer,100,stdin); 
     dim=atoi(buffer);  // gets a float with atof 
     double matrix[dim][dim]; 
     printf("Enter matrix:\n"); 
     for(i = 0; i < dim; i++) 
     { 
      for(j = 0; j < dim; j++) 
      { 
       fgets(buffer,100,stdin); 
       matrix[i][j]=atof(buffer); 
      } 
     } 
     write(fd[1],&dim,sizeof(double)); // write the size of the matrix 
     write(fd[1], matrix, dim*dim*sizeof(double)); // write the matrix 
     close(fd[1]); 
    } 
    else 
    { 
     // Child process 
     close(fd[1]); // close the writing side of the pipe 
     int dim; 
     read(fd[0], &dim, sizeof(double)); // read the dimension 
     double matrix[dim][dim]; // read the matrix 
     read(fd[0], matrix, dim*dim*sizeof(double)); 
     printf("%d",determinant(dim, matrix)); 
     close(fd[0]); 
    } 
    return 0; 
} 

重要:如果尺寸是否定的,你可能會遇到分段錯誤或其他問題,所以也要測試這個dim是一個可接受的值。

在這個例子中我使用了fgets,因爲我不喜歡scanf,這帶來了關於清除緩衝區的問題。不幸的是如何清除緩衝區還取決於系統,fflush(stdin)在windows上很好用,但是在linux上你必須起訴另一種方法。假如你能夠清理輸入緩衝區,可以自由使用scanf。

+0

完美的解釋。謝謝你;) – rustock