赞
踩
特点:
fifo文件可以用io函数进行操作
进程间通信:
使用场景:
创建方式:
创建管道
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
int m = mkfifo(argv[1],0664);
if(m == -1) {
perror("mkfifo");
return -1;
}
return 0;
}
向管道写数据
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *msg = "this is a test!\n";
int fd =open(argv[1], O_WRONLY);
if(fd == -1) {
perror("open fifo faild");
return -1;
}
write(fd, msg, strlen(msg));
close(fd);
return 0;
}
向管道读取数据
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc, char *argv[]) {
char buf[128];
int fd =open(argv[1], O_RDONLY);
if(fd == -1) {
perror("open fifo faild");
return -1;
}
int r = read(fd, buf, 128);
write(1, buf, r);
close(fd);
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。