永发信息网

Linux下管道通信出错

答案:2  悬赏:40  手机版
解决时间 2021-02-19 00:32
  • 提问者网友:沉默的哀伤
  • 2021-02-18 20:34
程序功能:
两个进程之间通过管道通信。父进程(表现为client函数)接受用户输入一个文件名,然后通过管道传递给子进程(在server函数内),子进程打开这个文件,成功后,将这个文件的内容读出,并通过管道将读取的内容传递给父进程,父进程读取管道的内容,并显示在屏幕上

现在有源程序Pipe.cpp,编译链接后得到可执行文件Pipe,在Linux下面执行:
$./Pipe
Pipe.cpp
这时结果出错,无法显示Pipe.cpp文件的内容,按照源程序的意思应该是可以显示Pipe.cpp文件的内容的

源程序如下

//Pipe.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include
#include

#include
using namespace std;

const int MAXLINE = 128;

void client(int readfd, int writefd)
{
size_t len;
size_t n;
char buff[MAXLINE];

fgets(buff, MAXLINE, stdin); //读取一个文件名
len = strlen( buff );
if ( buff[len - 1] == '\n' ) //如果最后一个字符为回车
{
len--;
}

if (write(writefd, buff, len) == -1 ) //将文件名写入管道
{
cerr << "client write error" << endl;
exit(0);
}
else
{
cout << "client write pathname successfully!" << endl;
}

while ( (n = read(readfd, buff, MAXLINE)) > 0) //读取管道内容
{
buff[n] = '\0'; //加上结束标志
cout << buff << endl;
}
}

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

if ( (n = read(readfd, buff, MAXLINE) ) == 0 )//从管道内读取一个文件名
{
cerr << "end-of-file while reading pathename" << endl;
}
else if ( n == -1)
{
cerr << "server read error" << endl;
}
buff[n] = '\0';

if ( (fd = open(buff, O_RDONLY) ) < 0 ) //打开这个文件
{
cerr << "open(buff, O_RDONLY) error" << endl;
snprintf( buff + n, sizeof(buff) - n, " :can't open, %s\n", strerror(errno));
n = strlen(buff);
write( writefd, buff, n);
}
else
{
cout << "open " << buff << " successfully!" << endl;

while( (n = read(fd, buff, MAXLINE) > 0 ) ) //读取文件里的内容
{


if ( write(writefd, buff, n) == -1) //将读取的内容写入管道
{
cerr << "server write(writefd, buff, n) error" << endl;
exit(0);
}
//余下的程序
else
{
cout << "server write successfully!" << endl;
}
}
close(fd);
}
}

int main(int argc,char **argv)
{
int pipe1[2], pipe2[2];
pid_t childpid;

pipe(pipe1); //创建管道
pipe(pipe2);

if( (childpid = fork() ) == 0)
{
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;
}
最佳答案
  • 五星知识达人网友:不如潦草
  • 2021-02-18 21:36
问题在这个while上
不是
while( (n = read(fd, buff, MAXLINE) > 0 ) )//读取文件里的内容
应该是(注意括号括的地方)
while( (n = read(fd, buff, MAXLINE) )> 0 )//读取文件里的内容
{
全部回答
  • 1楼网友:天凉才是好个秋
  • 2021-02-18 22:37
兄弟,以后遇到这样的问题,要学会解决! 一般遇到所编写的程序和自己预想的结果不一样的时候,你应该想到的是单步调试,看看哪一步出现了问题。一步一步纠正。兄弟快运行gdb吧。他能帮你解决!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯