您的位置:首页 > 其它

利用命名管道复制文件

2013-04-25 22:56 447 查看
1. 定义头文件apue.h

#ifndef _APUE_H_

#define _APUE_H_

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <assert.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <fcntl.h>

#include <errno.h>

#include <dirent.h>

#include <signal.h>

#include <sys/time.h>

void err_exit(char *m){

perror(m);

exit(EXIT_FAILURE);

}

#endif /* _APUE_H_ */

2. fifor.c, 把文件Makefile的内容读取到管道tp中

#include "../apue.h"

int main(void){

mkfifo("tp", 0644);

int infd;

infd=open("Makefile", O_RDONLY);

if(infd==-1)

err_exit("open error");

int outfd;

outfd=open("tp", O_WRONLY);

if(outfd==-1)

err_exit("open error");

char buf[1024];

int n;

n=read(infd, buf, 1024);

write(outfd, buf, n);

return 0;

}

3.02fifow.c, 从管道tp中读取数据并写入文件Makefile2

#include "../apue.h"

int main(void){

int fd;

fd=open("tp", O_RDONLY);

if (fd==-1)

err_exit("open error");

int infd;

infd=open("Makefile2", O_WRONLY|O_CREAT, 0644);

if (infd==-1)

err_exit("open error");

char buf[1024];

int n;

while ((n=read(fd, buf, 1024))>0){

write(infd, buf, n);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  命名管道 mkfifo