1

私はパイプを学ぼうとしていて、このプログラムを試しています:

#include<stdio.h>
#include<string.h>  
#include<unistd.h> 
#include<errno.h>
#include<sys/types.h>
#include<fcntl.h>

#define MAXLINE 100
void main(){

int pipe1[2],pipe2[2];
pid_t childpid;
 if(pipe(pipe1)<0){
            perror("Unable to create the pipe for pipe1");
            exit(-1);
    }
    if(pipe(pipe2)<0){
            perror("Unable to create the pipe for pipe1");
            exit(-1);
    }
    childpid=fork();
    printf("The child PID is:%d\n",childpid);
    if(childpid==0){
            printf("In the child process");
            close(pipe1[1]);
            close(pipe2[0]);
            server(pipe1[0],pipe2[1]);
            exit(0);
    }

    close(pipe1[0]);
    close(pipe2[1]);

    client(pipe2[0],pipe1[1]);
    waitpid(childpid,NULL,0);
    exit(0);
}

void client(int readfd,int writefd){

    int n,len;
    char buff[MAXLINE];
    printf("Please enter the name of the file to be read:");
    fgets(buff,MAXLINE,stdin);
    len=strlen(buff);
    if(buff[len-1]=='\n')
            len--;

    write(writefd,buff,len);
    printf("File name written into the pipe\n");
    printf("The num of bytes written are:\n",read(readfd,buff,MAXLINE));
    while((n-read(readfd,buff,MAXLINE))>0){
            printf("Trying to read the content\n");
            write(STDOUT_FILENO,buff,n);
    }
}

void server(int readfd,int writefd){

    int fd,n;
    char buff[MAXLINE + 1];
    write(writefd,"Yello in the server process",strlen("Yello in the server process"));

    if((n=read(readfd,buff,MAXLINE))==0)
            perror("End of file while reading");
    buff[n]='\0';
    if((fd=fopen(buff,O_RDONLY))<0){
            snprintf(buff+n,sizeof(buff)-n,"Can't open, %s",strerror(errno));
            n=strlen(buff);
            write(writefd,buff,n);
    }
    while( (n=read(fd,buff,MAXLINE))>0){
            write(writefd,buff,n);
            close(fd);
    }
}

問題は、ファイル名を入力するとプログラムが終了することです。set "follow-fork-mode child" を設定して子プロセスを gdb しようとしましたが、まだ何も起こりません。私がどこで間違っている可能性があるかについてのアイデアはありますか?

OK、追加のデバッグ情報は次のとおりです。follow-fork-mode を child. に設定すると、ファイルを開くときにセグメンテーション違反が発生します。

プログラム受信信号 SIGSEGV、セグメンテーション違反。[プロセス28025に切り替え] /lib/libc.so.6の_IO_file_fopen()で0x00197f08

4

1 に答える 1

1

このコードclient()は怪しいと思われます:

while((n-read(readfd,buff,MAXLINE))>0){

確かに、それは次のようになります。

while ((n = read(readfd, buff, MAXLINE)) > 0)
{

-もちろん、 からへの変更=は重要です。残りは表面的なものです (そして 1 つは物議をかもしています)。


コンパイラの警告にも注意を払う必要があります。与えられた:

int fd,n;
...
if((fd=fopen(buff,O_RDONLY))<0){

これを重大な警告なしにコンパイルする方法はありません。は、ファイル記述子 ( ) ではなく をfopen()返します。FILE *int


また、奇妙な通信をしているようです。サーバーは、クライアントからファイル名を読み取る前に、クライアントにメッセージを送信します。クライアントである OTOH は、必ずしもそのメッセージを個別に読み取るとは限りません。情報の塊を取得し、取得したバイト数を報告します。ファイルの一部と紹介メッセージが含まれている可能性があります。


ファイルから読み取っているループ内でファイルを閉じるべきではありません。

while( (n=read(fd,buff,MAXLINE))>0){
        write(writefd,buff,n);
        close(fd);
}

クローズはループの外にある必要があります。


このコードは多かれ少なかれ機能します。それは私が望むよりも厄介ですが、多かれ少なかれ機能します。

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

#define MAXLINE 100

static void server(int readfd, int writefd);
static void client(int readfd, int writefd);

int main(void)
{
    int pipe1[2], pipe2[2];
    pid_t childpid;
    if (pipe(pipe1)<0)
    {
        perror("Unable to create the pipe for pipe1");
        exit(-1);
    }
    if (pipe(pipe2)<0)
    {
        perror("Unable to create the pipe for pipe2");
        exit(-1);
    }
    childpid = fork();
    printf("The child PID is:%d\n", childpid);
    if (childpid == 0)
    {
        printf("In the child process\n");
        close(pipe1[1]);
        close(pipe2[0]);
        server(pipe1[0], pipe2[1]);
        exit(0);
    }

    close(pipe1[0]);
    close(pipe2[1]);

    client(pipe2[0], pipe1[1]);
    waitpid(childpid, NULL, 0);
    return 0;
}

static void client(int readfd, int writefd)
{
    int n, len;
    char buff[MAXLINE];
    printf("Please enter the name of the file to be read:");
    fgets(buff, MAXLINE, stdin);
    len = strlen(buff);
    if (buff[len-1]=='\n')
            len--;

    write(writefd, buff, len);
    printf("File name (%.*s) written into the pipe\n", len, buff);
    printf("The num of bytes written are: %d\n", (int)read(readfd, buff, MAXLINE));
    while ((n = read(readfd, buff, MAXLINE)) > 0)
    {
            printf("Trying to read the content\n");
            write(STDOUT_FILENO, buff, n);
    }
}

static void server(int readfd, int writefd)
{
    int fd, n;
    char buff[MAXLINE + 1];

    fprintf(stderr, "Server: %d\n", (int)getpid());
    write(writefd, "Yello in the server process", strlen("Yello in the server process"));

    if ((n = read(readfd, buff, MAXLINE))==0)
        perror("End of file while reading");
    buff[n] = '\0';
    if ((fd = open(buff, O_RDONLY)) < 0)
    {
            snprintf(buff+n, sizeof(buff)-n, "Can't open, %s", strerror(errno));
            n = strlen(buff);
            write(writefd, buff, n);
    }
    else
    {
        while ((n = read(fd, buff, MAXLINE)) > 0)
        {
            if (write(writefd, buff, n) != n)
            {
                fprintf(stderr, "Write failed in server\n");
                break;
            }
        }
        close(fd);
    }
}

コードは、オープンに失敗したファイル記述子を使用しようとしないことに注意してください。クラッシュしません。このn-read(...)問題は、コメントにもかかわらず、問題の主要な部分の 1 つです。fopen()forの誤用はopen()、問題のもう 1 つの主要な部分でした。

于 2013-06-23T05:25:17.537 に答える