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 <sys/stat.h>
// int mkfifo(const char *pathname, mode_t mode);
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main()
{
    char buf[30]={0};
    int nread;
    if((mkfifo("./file",0666)==-1) && (errno != EEXIST)){
        printf("make file error\n");
        perror("why");
    }

    int fd = open("./file",O_RDONLY);
    if(fd<0)
    {
        perror("open fd");
    }
    printf("open fifo success\n");
    while(1){
        read(fd,buf,30);
        printf("read from fifo buf:%s\n",buf);
        sleep(1);
   }
    return 0;
}
写端代码:
#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>
// int mkfifo(const char *pathname, mode_t mode);
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int cnt=0;
    char *str ="massage from fifo.";

    int fd = open("./file",O_WRONLY);
    printf("open fifo success\n");
    while(1){
        write(fd,str,strlen(str));
        printf("write to file buf: %s\n",str);
        sleep(1);
    }
    close(fd);
    return 0;
}

2.3.1实践结果

2.消息队列

消息队列
        消息队列是消息的链表,存放在内核中并由消息队列标识符标识,消息队列克服了信号传递信息少、管 道只能承载无格式字节流以及缓冲区大小受限等缺陷。消息队列包括 POSIX 消息队列和 System V 消息队列。 消息队列是 UNIX 不同进程之间实现共享资源的一种机制,UNIX 允许不同进程将格式化的数据流以消息队列形式发送给任意进程,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。
        笔者测试了一下 

2.1编码实践

//获取
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <string.h>
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:%x\n",key);

    int msg_id = msgget(key,IPC_CREAT|0777);//creat msaaage queue fhui id
    if(msg_id == -1){
        printf("get queue failed\n");
    }
    msgrcv(msg_id,&readBuf,sizeof(readBuf.mtext),888,0);//sort is 888
    printf("get from que:%s\n",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 <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <string.h>
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:%x\n",key);


    int msg_id = msgget(key,IPC_CREAT|0777);//duiliecsu  meiyoujiuchuangjian duiliehao
    if(msg_id == -1){
        printf("get queue failed\n");
    }
    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:%s\n",readBuf.mtext);
    msgctl(msg_id,IPC_RMID,NULL);

//send massage

    return 0;
}

2.2实践结果

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈俊帆Linux_Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值