`
memorymyann
  • 浏览: 266890 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

命名管道实现样例

阅读更多

[root@liumengli name_pipe]# mknod my_pipe p (创建命名管道,也可以通过系统调用来安装)

[root@liumengli name_pipe]# 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[20];
        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;
}

//读管道程序

[root@liumengli name_pipe]# 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[20];
        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命名管道通信,如果对命名管道有兴趣,可以留言.

分享到:
评论

相关推荐

    win32下进程间通信(共享内存)实例分析

    进程间通讯(即:同机通讯)和数据交换有多种方式:消息、共享内存、匿名(命名)管道、邮槽、Windows套接字等多种技术。“共享内存”(shared memory)可以定义为对一个以上的进程是可见的内存或存在于多个进程的虚拟...

    精通Windows.API-函数、接口、编程实例.pdf

    11.2.1 创建命名管道 300 11.2.2 管道监听 302 11.2.3 使用异步I/O进行读写 303 11.2.4 关闭管道实例 307 11.2.5 客户端 307 11.3 剪贴板 310 11.3.1 获取、设置剪贴板数据 310 11.3.2 监视剪贴板 ...

    数据库系统原理实验报告(1).doc

    能够理解命名管道协议与t" "cp/ip协议的差别。能够登陆上sql server数据库服务器。 " "实验内容 " "1、启动sql server 服务。 " "2、打开sql server的企业管理器,连接上sql server服务器。展开左边 " "树状窗口的...

    数据库系统原理实验报告.doc

    能够理解命名管道协议与t" "cp/ip协议的差别。能够登陆上sql server数据库服务器。 " "实验内容 " "1、启动sql server 服务。 " "2、打开sql server的企业管理器,连接上sql server服务器。展开左边 " "树状窗口的...

    精通WindowsAPI 函数 接口 编程实例

    11.2.1 创建命名管道 300 11.2.2 管道监听 302 11.2.3 使用异步I/O进行读写 303 11.2.4 关闭管道实例 307 11.2.5 客户端 307 11.3 剪贴板 310 11.3.1 获取、设置剪贴板数据 310 11.3.2 监视剪贴板 ...

    精通WindowsAPI.pdf

    第1章 Windows应用程序开发入门..........................................................................................16 1.1 第一个实例程序...............................................................

Global site tag (gtag.js) - Google Analytics