0

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

# 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
4

2 に答える 2

3

writeとのプロトタイプread

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

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

write/への引数2readはポインタである必要があります。しかし、送信するのは文字(実際には整数)word[i]であり、numbers[i]

あなたでも同じ問題strlen

wordまた、単なるポインタではなく配列として宣言します。それ以外の場合は、ポインターが指しているランダムな場所に書き込みます。または、ポインタとして保持したい場合は、mallocそのためのメモリが必要です。

このすべての後、文句を言っている関数の代わりに、wordまたは関数に渡すだけですnumbersnumbers[j]words[i]

編集:また、最後のforステートメントfor(i=0;i<20;i++)には閉じ中括弧がないため、Line 60: error: expected declaration or statement at end of inputエラーが発生します

于 2012-04-22T19:02:26.347 に答える
0

それ以外の:

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

これを行う:

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

...つまり、データ自体の値ではなく、データの場所にPOINTERを渡します。

于 2012-04-22T19:04:22.693 に答える