2011-01-27 94 views
0

我試圖寫組成的字符數組,一個整數值,並且一個指向管的結構寫入結構的管。該結構表示單鏈表中的節點。閱讀和用C

//Define a linked-list node object 
typedef struct node{ 
    char word[128]; 
    int frequency; 
    struct node *next; 
} NODE; 

該程序的目標是使用管道將節點從幾個併發的子進程傳遞給父進程。我已經實現的管道結構似乎可以正常使用字符數組,但如果我嘗試傳遞整個節點對象,則不起作用。

//for each file argument, create a child process 
     for (i = 1; i < argc; i ++) 
     { 
      pipe(p[i-1]); 
      pid = fork(); 

      if (pid == 0) 
      { 
       //child process 
       close(p[i-1][0]); 
       NODE *tmp; 
       NODE *out = freqCheck(argv[i], tmp); 
       write(p[i-1][1], out, sizeof(NODE)); 
       exit(0); 
      } 
     } 
if (pid > 0){ 
       //parent process 
       int j; 
       for (j = 0; j < argc-1; j++) 
       { 
        close(p[j][1]); 
        NODE *tmp; 
        read(p[j][0], tmp, sizeof(NODE)); 

        printf("%s\n", tmp->word); 
       } 


      } 

當父進程試圖從管道讀取和解釋結構的屬性之一,我只是得到空值回來。

我使用2個元件整數數組的數組跟蹤每個子進程管。上面的printf語句返回null返回時間。

任何想法我做錯了什麼?

代碼爲FrqCheck方法:

//Method for determining the most occuring word 
NODE * freqCheck(char *file, NODE *max) 
{ 
    int i; 
    FILE *f; 
    char str[128]; 

    //Set up head and tail nodes 
    NODE *head = NULL; 
    NODE *tail = NULL; 

    if ((f = fopen(file, "r")) == NULL) 
     { 
      //sprintf(output, "%s could not be opened.", file); 
     }else 
     { 
      //scan each word of the input file 
      while(fscanf(f, "%s ", str) != EOF) 
      { 
       //if the linked-list has no nodes, create one 
       if (head == NULL) 
       { 
        NODE *n; 
        n = (NODE *)malloc(sizeof(NODE)); 
        strcpy(n->word, str); 
        n->frequency = 1; 
        n->next = NULL; 
        head = n; 
        tail = n; 

       }else{ //search the linked list for the found word. 

        NODE *current = head; 
        int found = 0; 

        while((current != NULL) && (found == 0)) 
        { 
         //if the word is found increment the frequency 
         if (strcmp(current->word, str) == 0) 
         { 
          current->frequency ++; 
          found = 1; 
         }else 
         { 
          current = current->next; 
         } 
        } 

        //if the word is not found, create a node and add to the liked-list 
        if (found == 0) 
        { 
         NODE *new; 
         new = (NODE *)malloc(sizeof(NODE)); 
         strcpy(new->word, str); 
         new->frequency = 1; 
         new->next = NULL; 
         tail->next = new; 
         tail = new; 
        } 
       } 
      } 
      //traverse the linked-list and find the word with the maximum frequency 
      NODE *tmp = head; 
      max = tmp; 

      while (tmp != NULL) 
      { 
       if (tmp->frequency > max->frequency) 
       { 
        max = tmp; 
       }else 
       { 
        tmp = tmp->next; 
       } 
      } 
      //sprintf(output, "%s %s %d", file, max->word, max->frequency); 
     } 
    //printf("%s\n", max->word); 
    return max; 
} 

回答

1

你在哪裏你NODE結構分配存儲空間? freqCheck是否分配存儲空間?在父進程,當你調用read()然後printf,你在一個未初始化的NODE指針傳遞,所以當然你要未定義行爲。

+0

是頻率檢查構造鏈表併爲每個節點分配內存。該方法掃描文件中的單詞,並確定哪個單詞出現最多。它返回一個節點實例。 – isometrik 2011-01-27 23:35:29

+0

但是... show source ... – Svisstack 2011-01-27 23:36:06

0

使用緩衝讀誰,直到整個結構中讀取的數據將被readed,因爲從parrent代碼可以從孩子那你讀誰不存在數據的代碼之前執行。

0

你需要考慮兩件事情:
1)執行方面的協調:

這是什麼之類的東西鎖,信號量,事件等都是。他們強制執行命令(您的代碼行)的順序。正是出於這個原因,這些在進程間運行的版本,就像那些爲線程運行的版本一樣。你想)在SEM上的一個信號這裏 - 作家過程應該張貼()的SEM當一個節點已全部發送,閱讀過程中應等待(試圖讀取下一個之前。

2)內存:
進程不共享地址空間。因此,讀者和寫作者的過程都有不同的內存塊。一個管道工作正常,但它就像一個套接字 - 你將所有的對象序列化和反序列化成原始字節流。考慮到你的結構有多小,你可以考慮的另一件事是共享內存。

要考慮的最後一件事:您的節點結構中「下一個」節點的指針需要在子進程中更新。同樣,它將把這些節點複製到它自己的地址空間中的位置,所以'下一個'指針必須相應地更新。