命名管道实现样例
# mknod my_pipe p (创建命名管道,也可以通过系统调用来安装)# cat name_pipe_read.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "errno.h"
#include "fcntl.h"
int main() {
char buf;
int pipe_read;
int n_read;
pipe_read = open("./my_pipe", O_RDONLY, 0);
//打开管道,只读方式,注意与下面的区别,读者可以自己试试,可能参数有错,自己可以找到
//pipe_read = open("./my_pipe", O_RDONLY|O_NONBLOCK, 0);
if(pipe_read == -1) {
printf("error\n");
return 1;
}
memset(buf, 0, sizeof(buf));
n_read = read(pipe_read, buf, 20);
printf("You read %d chars\n", n_read);
printf("%s\n", buf);
return 0;
}
//读管道程序
# cat name_pipe_read.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "errno.h"
#include "fcntl.h"
int main() {
char buf;
int pipe_read;
int n_read;
pipe_read = open("./my_pipe", O_RDONLY, 0);
if(pipe_read == -1) {
printf("error\n");
return 1;
}
memset(buf, 0, sizeof(buf));
n_read = read(pipe_read, buf, 20);
printf("You read %d chars\n", n_read);
printf("%s\n", buf);
return 0;
}
//写管道程序,2个进程通过my_pipe命名管道通信,如果对命名管道有兴趣,可以留言.
页:
[1]