如何创建管道啊
如何创建管道啊,管道是什么
------解决方案--------------------
管道是进程间通信中最古老的方式,它包括无名管道和有名管道两种,前者用于父进
程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信。
------解决方案--------------------
/* 在Linux系统下,有名管道可由两种方式创建:命令行方式mknod系统调用和函数
mkfifo。下面的两种途径都在当前目录下生成了一个名为myfifo的有名管道:
方式一:mkfifo("myfifo","rw");
方式二:mknod myfifo p
生成了有名管道后,就可以使用一般的文件I/O函数如open、close、read、write等来
对它进行操作。下面即是一个简单的例子,假设我们已经创建了一个名为myfifo的有名管道。
*/
/* 进程一:读有名管道*/
#include
#include
void main() {
FILE * in_file;
int count = 1;
char buf[80];
in_file = fopen("mypipe", "r");
if (in_file == NULL) {
printf("Error in fdopen.\n");
exit(1);
}
while ((count = fread(buf, 1, 80, in_file)) > 0)
printf("received from pipe: %s\n", buf);
fclose(in_file);
}
/* 进程二:写有名管道*/
#include
#include
void main() {
FILE * out_file;
int count = 1;
char buf[80];
out_file = fopen("mypipe", "w");
if (out_file == NULL) {
printf("Error opening pipe.");
exit(1);
}
sprintf(buf,"this is test data for the named pipe example\n");
fwrite(buf, 1, 80, out_file);
fclose(out_file);
}