2012-04-22 7 views
0

これはパイプとプロセスに関する小さなCプログラムです。父親のプロセスは2つの子プロセスを作成し、最初はチェーンから数値を読み取り、2つ目は文字を読み取ります。私はWORDを求めて始めました。これは保護を追加していませんでした。これは単なるテストなので、約20文字と言えば、父親のプロセスは最初のパイプの数字と2番目のパイプの文字を書きます。子どもがfork()を使っている場合、彼が子供の場合は、彼が父親であれば、最初のパイプから数値を読み込み、別の子供を作成して文字を読むでしょう。コンパイル後WRITEとREADの間違いは何ですか?

# include <stdio.h> 
# include <unistd.h> 
# include <string.h> 
# include <fcntl.h> 

main() 
{ 
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n"); 
    char * word; 
    printf("please type the word: \n"); 
    scanf("%s",word); 
    printf("Now 2 pipes will be created\n"); 
    int fd1[2]; 
    int fd2[2]; 
    pipe(fd1); pipe(fd2); 
    printf("Now the father will write numbers in the first pipe, and letters in the second\n"); 
    int i; 
    char numbers[20]; int j=0; 
    char caracters[20]; int k=0; 
    for (i=0;i<20;i++) 
    { 
     if(word[i]>='0' && word[i]<='9') //if number 
     { 
      close(fd1[0]); //closing reading 
      write(fd1[1],word[i],2); 

     } 
     else 
     { 
      close(fd2[0]); 
      write(fd2[1],word[i],2); 
     } 

    } 
    printf("The father has wrote in the 2 pipes, now its time for the sons\n"); 
    int f=fork(); 
    if(f==0) //first son 
    { 
     for(i=0;i<20;i++) {   
      close(fd1[1]); //closing writing 
      read(fd1[0],numbers[j],strlen(numbers[j])+1); 
      j++; 

     } 
     printf("first son read everything, he got %d Numbers\n", j); 
    } 
    else 
    { 
     f=fork(); 
     if(f==0) 
     { 
      for(i=0;i<20;i++) {   
      close(fd2[1]); //closing writing 
      read(fd2[0],caracters[k],strlen(caracters[k])+1); 
      k++; 

     } 
     printf("second son read everything, he got %d caracters\n", j); 
    } 
} 

In function 'main': 
Line 25: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 31: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 41: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 41: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 54: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 54: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 60: error: expected declaration or statement at end of input 

答えて

2

writeのプロトタイプとread

ssize_t write(int fd, const void *buf, size_t count); 

ssize_t read(int fd, void *buf, size_t count); 
ある引数2 write/ readへのポインタであるべきです。しかし、あなたが送信している(実際には整数である)文字 word[i]あると numbers[i]

も、また、あなたのstrlen

と同じ問題は、配列だけではなくポインタとしてwordを宣言します。そうでなければ、ポインタが指している任意の場所に書き込むでしょう。または、ポインタとして保持したい場合は、mallocいくつかのメモリ。すべてこの後

、ちょうど

EDITを訴えているあなたの関数に代わりnumbers[j]またはwords[i]wordnumbersを渡します。また、あなたの最後のforの文for(i=0;i<20;i++)は、代わりに閉じ括弧となりLine 60: error: expected declaration or statement at end of inputエラー

+0

私はread(fd2 [0]、&caracters [k]、1)を使用しました。 < - 私は他の人と同じことをしました。エラーはなくなりましたが、エラーは1つしかありませんでした。入力の最後に宣言またはステートメントがあります。 –

+0

@AliBassam私の編集 –

+0

を修正しました。実行した後、単語を入力してタイプし、次に2つのprintfsを表示します(今は父が書いています....)。他には何もありません..... –

0

を欠い

write(fd1[1],word[i],2); 
この行います:の

write(fd1[1],(void*)&word[i],2); 

...つまり、データそのものの値ではなく、データの位置にPOINTERを渡します。

+0

私はread(fd2 [0]、&caracters [k]、1)を使用しました。 < - 私は他の人と同じことをしました。エラーはなくなりましたが、私は1つしかありませんでした。 エラー:入力の最後に宣言または宣言があります –

+0

どの行番号ですか?どのラインに対応していますか? –

+0

閉鎖ブラケットが必要でした。実行した後に、単語を入力してタイプし、次に2つのprintfsを表示します(今は父が書いています....)。 –

関連する問題