编了个两个进程通过有名管道传输数据的代码 不知道错在哪 求各位前辈指点
程序代码:
//send.c
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n,fd,rfd;
rfd=open("readin.txt",O_RDONLY,0644);
char buf[1024];
int nbytes=1;//每次读、写的字节数
mknod("fifo",S_IFIFO|0666,0);
fd=open("fifo",O_WRONLY);
while((n=read(rfd,buf,nbytes))!=0)
{
write(fd,buf,n);
}
close(fd);
close(rfd);
return 0;
}
//receive.c
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n,fd,nbytes=1,wfd;
char buf[1024];
wfd=open("writeout1.txt",O_RDONLY|O_CREAT|O_TRUNC,0644);//把已存在的文件截为空文件
fd=open("fifo",O_RDONLY|O_NDELAY);
while((n=read(fd,buf,1))!=-1)
{
if(n==0)
//{sleep(NAPTIME);}
{sleep(20);}
else
{write(wfd,buf,nbytes);}
}
close(fd);
close(wfd);
return 0;
}
send.c从"readin.txt"中读取数据并将其写入有名管道,receive.c从该有名管道读数据,并将数据写入"writeout1.txt",结果我试了一下writeout1.txt中是空的,不明白为什么
[ 本帖最后由 ojpansc 于 2015-5-5 15:49 编辑 ]

