分类:
2010-06-23 11:37:11
#include"stdio.h"
#include"stdlib.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"string.h"
#include"fcntl.h"
#include"errno.h"
#define FIFO1 "/tmp/fifo"
#define MAXLINE 100
void client1(int);
void client2(int);
int main(int argc,char **argv)
{
int writefd;
writefd=open(FIFO1,O_WRONLY,0);
client1(writefd);
client2(writefd);
close(writefd);
unlink(FIFO1);
exit(0);
}
void client1(int writefd1)//实现从文件写到管道,自己创建一个aa.txt文件
{
char buff[MAXLINE];
int fd;
memset(buff,0,sizeof(buff));
fd=open("aa.txt",O_CREAT|O_RDWR,S_IRWXU);
read(fd,buff,sizeof(buff));
write(writefd1,buff,sizeof(buff));
close(fd);
}
void client2(int writefd2)//)//实现从管道写到另一个文件
{
char buff[MAXLINE];
int fd;
fd=open("bb.txt",O_CREAT|O_RDWR,S_IRWXU);
read(writefd2,buff,sizeof(buff));
write(fd,buff,sizeof(buff));
close(fd);
}