C 进出子进程的管道不工作

C 进出子进程的管道不工作,c,operating-system,pipe,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; i

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

#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);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义MAXLINE 100
void main(){
int pipe1[2],pipe2[2];
pid_t childpid;
如果
client()
中的(管道(管道1)此代码看起来可疑:

while((n-read(readfd,buff,MAXLINE))>0){
当然,这应该是:

while ((n = read(readfd, buff, MAXLINE)) > 0)
{
当然,从
-
=
的变化是重要的;其余的都是装饰性的(其中一个甚至是有争议的)


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

int fd,n;
...
if((fd=fopen(buff,O_RDONLY))<0){
闭合应在回路外


这段代码或多或少能起作用;它比我想要的更混乱,但它能起到或多或少的作用

#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);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义MAXLINE 100
静态void服务器(int-readfd、int-writefd);
静态无效客户端(int-readfd、int-writefd);
内部主(空)
{
int pipe1[2],pipe2[2];
pid_t childpid;
if(管道(管道1)0)
{
if(write(writefd,buff,n)!=n)
{
fprintf(stderr,“在服务器中写入失败\n”);
打破
}
}
关闭(fd);
}
}
请注意,代码没有尝试使用无法打开的文件描述符。它不会崩溃;
n-read(…)
问题是问题的一个主要部分,尽管有注释。错误使用
fopen()
作为
open()
是问题的另一个主要部分。

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){
闭合应在回路外


这段代码或多或少能起作用;它比我想要的更混乱,但它能起到或多或少的作用

#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);
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义MAXLINE 100
静态void服务器(int-readfd、int-writefd);
静态无效客户端(int-readfd、int-writefd);
内部主(空)
{
int pipe1[2],pipe2[2];
pid_t childpid;
if(管道(管道1)0)
{
if(write(writefd,buff,n)!=n)
{
fprintf(stderr,“在服务器中写入失败\n”);
打破
}
}
关闭(fd);
}
}

请注意,代码没有尝试使用无法打开的文件描述符。它不会崩溃;
n-read(…)
问题是问题的一个主要部分,尽管有注释。错误使用
fopen()
用于
open()
是问题的另一个主要部分。

首先,您的程序没有包含必要的include,即
#include
。您复制并粘贴了错误消息,因此如果您未能创建
pipe2
,您将对
pipe1
失败的原因感到疑惑。
main()的返回类型
int
,而不是
void
。不要忘了用换行符结束消息;直到换行符输出后,消息才会出现。@user1295872您使用的是哪个系统?首先,您的程序不包含必要的include,即
#include
。您复制并粘贴了错误消息,因此如果无法创建ode>pipe2
,您会想知道为什么
pipe1
失败是
int
,而不是
void
。别忘了用换行符来结束消息;在换行符输出之前,消息不会出现。@user1295872您使用的是哪个系统?我想OP也应该描述他使用的是哪个系统?因为我们正在创建进程,所以这个问题仍然不是很重要ear.提到
/lib/libc.so.6
,它是一个基于Unix的系统(很可能是一个基于Linux的系统);实际上,哪一个并不重要……就本程序而言,它们都是相同的。我认为OP还应该描述他在使用哪个系统?这不是很重要吗?因为我们正在创建进程,所以问题仍然不是很清楚。提到
/lib/libc.So.6
,它是一个基于Unix的系统(很可能是一个基于Linux的系统);哪一个真的不太重要……就本程序而言,它们都是一样的。