2013-03-24 69 views
0

我有一個程序應該計算txt文件中的字符數,並使用子進程計算字符數然後打印出數量。父母只是在while循環中將文件中的行提供給孩子。該程序的工作原理是它可以打開文件,逐行讀取它,然後如果子進程打印的數量是正確的。如何在子進程和父進程之間傳遞整數

但是現在我想修改它,以便子進程返回數量,而父進程則寫出字符的nr。但是當我嘗試這樣做時,程序給我1153416175而不是文件中的實際字符數,並且它只是卡住了,我必須殺死它。這是爲什麼發生?

#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <stdlib.h> 
#include <string.h> 
#define MAXLINE 100 

int main(int argc, char *argv[]) { 
    int fds[2]; /* file descriptors */ 
    int child; /* child process */ 
    int n; /* size of line in file */ 
    int count, alphacount, total=0; /* used in loop for counting chars in line */ 
    int read_bytes; /* amount of bytes read in read function */ 
    char line[MAXLINE]; 
    FILE *fp; 
    char *str; 

    if (argc < 2) { 
     printf("Format: './rakna <filename>'.\nThe file should have the .txt extension.\n"); 
     exit(1);  
    } else { 
     if (pipe(fds) < 0) { 
      perror("Can't open pipe\n"); 
      exit(1); 
     } 
     child = fork(); 
     if (child==-1) { 
      perror("fork error"); 
      exit(1); 
     } 

     /* child that reads chars and returns amount */ 
     if(child == 0)  
     { 
      /* close(fds[1]); child process close input of pipe Used to be before ..*/ 
      /* count chars through read */ 
      while (read_bytes = read(fds[0], line, sizeof(line)) > 0) { 
       /* check if line is received */ 
       if (read_bytes < 0) { 
        perror("Can't read line"); 
        exit(1); 
       } 
       /* count chars in the line */ 
       else { 
        count=0; 
        alphacount=0; 
        while (line[count] != '\0') 
        { 
         /* counting chars from 'a' to 'z' */ 
         if (line[count] >= 'a' && line[count] <= 'z') 
          alphacount++; 
         /* counting chars from 'A' to 'Z' */ 
         else if (line[count] >= 'A' && line[count] <= 'Z') 
          alphacount++; 
         count++; 
        } 
        /* adding the nr of chars in line to total */ 
        total += alphacount; 
       } 
      } 
      write(fds[1], &total, sizeof(total)); /* passing total nr of chars to parent */ 
      close(fds[0]); /* closing output part of pipe */ 
      exit(0); /* ending child process */ 
     } 

     /* parent that sends chars to child-reader and writes out amount */ 
     else 
     { 
      /* close(fds[0]); Parent process close output of pipe */ 
      fp = fopen(argv[1], "r"); 
      if (fp == 0) { 
       perror("Could not read the file.\n"); 
       exit(1); 
      } 
      while (fgets(line, MAXLINE, fp) != NULL) { 
       n = strlen(line); 
       if (n >= 0) { 
        line[n]='\0'; 
        if (write(fds[1], line, n) != n) { 
         perror("write error in pipe"); 
         exit(1); 
        } 
       } 
       else { 
        perror("problem with fgets"); 
        exit(1); 
       } 
      } 

      int nrofchars; 
      read_bytes = read(fds[0], &nrofchars, sizeof(nrofchars)); 
      printf("The file has %d nr of chars between a-z\n", nrofchars); //<-- Here it f**ks up, it gives me 1153416175 
      close(fds[1]); /* closing input part of pipe */ 
      wait(NULL); /* waits for child to read end of file */ 
     } 
     return 0; 
    } 
} 

回答

1

管道是NOT雙向通信,它是單向的,FD [0]是讀端和FD [1]是寫入 - 結束。

您的代碼使用一個管道作爲雙向(您注意到FD的關閉假設它將成爲雙向),這就是爲什麼你會得到未定義的行爲。

如果你想雙向你需要兩個管道:-)或者你可以使用socketpair(2)創建雙向IPC fds。

如果您需要更多幫助,只需發表評論。

+0

謝謝,已經讀了很多關於管道的知識,但是,不知道... – patriques 2013-03-24 07:16:48

相關問題