管道程序c语言,怎么创建管道

如何创建管道啊

如何创建管道啊,管道是什么

------解决方案--------------------

管道是进程间通信中最古老的方式,它包括无名管道和有名管道两种,前者用于父进

程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信。

------解决方案--------------------

/* 在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);

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值