0

パイプを使用して、親プロセス内からwrite()を使用して文字列を書き込もうとしています。次に、子プロセスを生成して、それを読み取り、単語数を数え、単語数を書き戻します。次に、親プロセスに単語数。私はこれを思いついた:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<sys/wait.h>    
int main(void)
{
int  fd[2], nbytes,status,i,count=0;
pid_t   PID;
char    string[] = "Hello, world\n";
char    readbuffer[80];
pipe(fd);
close(fd[0]);
write(fd[1], string, (strlen(string)+1));

if((PID=fork())<0)
{
    printf("Error\n");
    _exit(0);
}
else if(PID==0)
{

    close(fd[1]);
    nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
    for(i=0;readbuffer[i]!='\0';i++)
    {
        if(readbuffer[i]==' ')
        count++;
    }   
    //open(fd[1]);
    close(fd[0]);
    write(fd[1],&count,1);
    printf("The word count is %d ",count);
    //open(fd[0]);
}
else
{
    wait(&status);
    if(WIFEXITED(status))
{
    close(fd[1]);
    nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
    printf("The word count is %d ",nbytes);
    //open(fd[1]);
}
else
{
    printf("Error\n");
    _exit(0);
}
}
return(0);
}

これはコンパイルされますが、出力が得られません。誰か助けてくれませんか?

4

2 に答える 2

1

コードを試すことはできませんが、子プロセスで両方のパイプの端を閉じているようです。書き込みには fd[1] を使用しますが、(読み取りを行う前に) 閉じられています。

このコードをコンパイルして試す機会がないので、例として取り上げて、問題を修正してください。この例では、2 つの単方向パイプを使用して、親プロセスから子プロセスへ、および子プロセスから親プロセスへデータを送信する方法を示します。

int main(void)
{
        int     fd1[2], fd2[2], nbytes;
        pid_t   childpid;
        char    string[] = "Hello, world!\n";
        char    readbuffer[80];

        pipe(fd1);
        pipe(fd2);


        if((childpid = fork()) == -1)
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)
        {
                /* Child process closes up input side of pipe fd1 */
                close(fd1[0]);
               /* Child process closes up output side of pipe fd2 */
                close(fd2[1]);

                /* Send "string" through the output side of pipe */
                write(fd1[1], string, (strlen(string)+1));
                /* Read in a string from the pipe */
                nbytes = read(fd2[0], readbuffer, sizeof(readbuffer));
                printf("Received string: %s", readbuffer);
                exit(0);
        }
        else
        {
                /* Parent process closes up output side of pipe fd1 */
                close(fd1[1]);
                /* Parent process closes up input side of pipe fd2 */
                close(fd2[0]);

                /* Read in a string from the pipe */
                nbytes = read(fd1[0], readbuffer, sizeof(readbuffer));
                printf("Received string: %s", readbuffer);
                /* Send "string" through the output side of pipe */
                write(fd2[1], string, (strlen(string)+1));
        }

        return(0);
}
于 2013-05-01T12:11:35.193 に答える
0

データをパイプに書き込む前に、パイプの読み取り側を開く必要があります。そうしないと 、SIGPIPEと呼ばれるシグナルが発生します。SIGPIPE が行うことは、可能なときにクライアント プロセスによって受信される同期シグナルです。パイプへの書き込みはもうありません。このシグナルによって実行されるデフォルトのアクションは、プロセスを終了することです。このシグナルは、ソケットに関連付けられたプロセスにのみ送信できますが、ソケットのプロセス グループには送信できません。異常終了を回避するために、プロセスはシグナルをキャッチして処理する必要があります。

于 2014-12-17T14:36:23.363 に答える