栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

linux 系统编程--进程(2)--IPC(1)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux 系统编程--进程(2)--IPC(1)

1、进程间的通信概念引入         进程间通信(interprocess communication ,简称 IPC)指两个进程之间的通信。系统中的每一个进程都有各自的地址空间,并且相互独立、隔离,每个进程都处于自己的地址空间中。所以同一个进程的不同模块 (譬如不同的函数)之间进行通信都是很简单的,譬如使用全局变量(当然这种说法有点抽象)等。

     

由上图机制及相关资料 总结如下: ⚫ UNIX IPC :管道、 FIFO 、信号; ⚫ System V IPC :信号量、消息队列、共享内存; ⚫ POSIX IPC :信号量、消息队列、共享内存; ⚫ Socket IPC :基于 Socket 进程间通信 2、管道和 FIFO

        

2.1无名管道

        普通管道 pipe:通常有两种限制,一是单工,数据只能单向传输;二是只能在父子或者兄弟进程间使用,较为局限,实用型不高;

2.2、FIFO

·        该管道又称有名管道,去除了普通管道的第二种限制,并且允许在不相关(不是父子或兄

弟关系)的进程间进行通讯,一种更为灵活的方式。可以进行全双工的通讯。创建的管道可以确定好其路径,与路径绑定(当然也可以不用)。 2.3、编程实践
//最低层的逻辑还是和无名管道流一样 有一方流出及一方接收
//Api: mkfifo
//读端代码
#include 
// int mkfifo(const char *pathname, mode_t mode);
#include 
#include 
#include 
#include 
int main()
{
    char buf[30]={0};
    int nread;
    if((mkfifo("./file",0666)==-1) && (errno != EEXIST)){
        printf("make file errorn");
        perror("why");
    }

    int fd = open("./file",O_RDONLY);
    if(fd<0)
    {
        perror("open fd");
    }
    printf("open fifo successn");
    while(1){
        read(fd,buf,30);
        printf("read from fifo buf:%sn",buf);
        sleep(1);
   }
    return 0;
}
写端代码:
#include            
#include 
// int mkfifo(const char *pathname, mode_t mode);
#include 
#include 
#include 
#include 
#include 
int main()
{
    int cnt=0;
    char *str ="massage from fifo.";

    int fd = open("./file",O_WRONLY);
    printf("open fifo successn");
    while(1){
        write(fd,str,strlen(str));
        printf("write to file buf: %sn",str);
        sleep(1);
    }
    close(fd);
    return 0;
}
2.3.1实践结果

2.消息队列 消息队列         消息队列是消息的链表,存放在内核中并由消息队列标识符标识,消息队列克服了信号传递信息少、管 道只能承载无格式字节流以及缓冲区大小受限等缺陷。消息队列包括 POSIX 消息队列和 System V 消息队列。 消息队列是 UNIX 下不同进程之间实现共享资源的一种机制,UNIX 允许不同进程将格式化的数据流以消息队列形式发送给任意进程,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。         笔者测试了一下  2.1编码实践
//获取
#include 
#include 
#include 
#include 
#include 
#include 
struct msgbuf{
            long mtype;//消息类型
            char mtext[128];//massage data
};
//ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
//int msgget(key_t key, int msgflg);
int main()
{
    struct msgbuf readBuf;
    key_t key;
    key = ftok(".",'z');
    printf("key:%xn",key);

    int msg_id = msgget(key,IPC_CREAT|0777);//creat msaaage queue fhui id
    if(msg_id == -1){
        printf("get queue failedn");
    }
    msgrcv(msg_id,&readBuf,sizeof(readBuf.mtext),888,0);//sort is 888
    printf("get from que:%sn",readBuf.mtext);
    struct msgbuf sendbuf={988,"thanks for reach"};
    msgsnd(msg_id,&sendbuf,strlen(sendbuf.mtext),0);
    msgctl(msg_id,IPC_RMID,NULL);
    return 0;
}
#include 
#include 
#include 
#include 
#include 
#include 
struct msgbuf{
            long mtype;//消息类型
            char mtext[128];//massage data
};
//ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
//int msgget(key_t key, int msgflg);
int main()
{
    struct msgbuf sendBuf={888,"this is massage from queue"};
    struct msgbuf readBuf;
    key_t key;
    key = ftok(".",'z');
    printf("key:%xn",key);


    int msg_id = msgget(key,IPC_CREAT|0777);//duiliecsu  meiyoujiuchuangjian duiliehao
    if(msg_id == -1){
        printf("get queue failedn");
    }
    msgsnd(msg_id,&sendBuf,strlen(sendBuf.mtext),0);
    printf("send over;n");
    msgrcv(msg_id,&readBuf,sizeof(readBuf.mtext),988,0);
    printf("return from que:%sn",readBuf.mtext);
    msgctl(msg_id,IPC_RMID,NULL);

//send massage

    return 0;
}
2.2实践结果

        发送消息的进程可以一直阻塞 直到有进程读取到消息再退出  不涉及文件操作  管道涉及文件操作。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号