管道进出子进程不起作用

时间:2013-06-23 04:30:02

标签: c operating-system pipe

我正在尝试学习管道,我正在尝试这个程序:

#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子进程,但仍然没有任何反应。关于我可能出错的地方的任何想法?

好的,还有一些额外的调试信息:我将follow-fork-mode设置为child.and它会在文件打开时导致分段错误。

编程接收信号SIGSEGV,分段故障。 [切换到流程28025] 来自/lib/libc.so.6的_IO_file_fopen()中的0x00197f08

1 个答案:

答案 0 :(得分:1)

client()中的此代码看起来很可疑:

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

当然,那应该是:

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

当然,从-=的更改非常重要;其余的都是化妆品(其中一个甚至是有争议的)。


您还应该注意编译器警告。给出:

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(...)问题是问题的一个主要部分,尽管有评论。 fopen()open()的滥用是问题的另一个主要部分。

相关问题