问题 消息队列id 和键值KEY区别?

首先要注意一个概念:IPC结构都是内核的结构。也就是说IPC结构由内核维护,对于每个进程都是公共的,不属于某个特定进程。只有这样,IPC结构才能支持它们“进程间通信”的功能。

有两个东西可以标识一个IPC结构:标识符(ID)和键(key)。

Key是IPC结构的内部名。内部即在进程内部使用,这样的标识方法是不能支持进程间通信的。

ID就是IPC结构的外部名。这些进程得到的ID其实是标识了同一个IPC结构,如消息队列同时被进程A和进程B访问。多个进程间就可以通过这个IPC结构通信。

已知一个key,当希望利用这个key创建一个新的IPC时,可以使用get函数,并在flag中指定IPC_CREAT位,例如队列的情况,就是qid = msgget(key, IPC_CREAT)

一、基本概念

  • 消息队列就是一个消息的链表。一条消息可以看作一个有结构的记录。
  • IPC方式之一(进程间通信)
  • 消息可以通过结构类型区分

二、函数学习

2.1 创建消息队列

2.1.1 函数名
msgget
2.1.2 函数原型
int msgget(key_t key, int msgflg);
2.1.3 函数功能
打开或创建消息队列
2.1.4 头文件
  #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
2.1.5 返回值
success:the message queue identifier(消息队列ID)
error:-1

2.1.6 参数说明

key:键值
msgflg:打开标志. IPC_CREAT:标明新创建一个消息队列。
IPC_EXCL:标明打开一个消息队列

2.2 写消息

2.2.1 函数名
msgsnd
2.2.2 函数原型
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
2.2.3 功能
发送消息到消息队列
2.2.4 头文件
  #include <sys/ipc.h>
#include <sys/msg.h>
2.2.5 返回值
success:0
error:-1
2.2.6 参数说明
msqid:消息队列的ID
msgp:指向要发送的消息
msgsz:消息的长度(与结构有关,不含message type)
msgflg:标志位
P.S.General Form of message
       struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
}; The mtext field is an array (or other structure) whose size is speci-
fied by msgsz, a non-negative integer value.

2.3 读消息

2.3.1 函数名
msgrcv
2.3.2 函数原型
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
2.3.3 功能
从消息队列中接收消息
2.3.4 头文件
   #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
2.3.5 返回值
success:实际接收消息的数据长度
error: -1
2.3.6 参数
msqid:消息队列的id
msgp :存放取出的消息
msgsz:希望取到消息的最大长度
msgtyp:消息的类型
msgtyp = 0 ,忽略类型,直接取队列中的第一条
msgtyp >0, 取 消息队列中类型等于msgtyp的第一条消息
msgtyp <0, 取 类型比msgtyp的绝对值要小或者等于的消息,如果有多条消息满足该条件,则取类型号最小的一条。
If msgtyp is less than 0, then the first message in the queue with
the lowest type less than or equal to the absolute value of msgtyp
will be read. msgflg:标志

2.4 删除消息队列(控制)

2.4.1 函数名
msgctl
2.4.2 函数原型
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
2.4.3 函数功能
控制消息队列
2.4.4 头文件
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
2.4.5 返回值
success:0
error :-1
2.4.6 参数说明
msqid:消息队列的id
cmd:操作命令,IPC_RMID 用于删除消息队列
buf :获取内核中的msqid_ds 结构

三、综合实例

发送
/* Send.c*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h> struct msgbuf
{
long msgtype;
char msgtext[1024];
}; int main()
{ int msqid;
int msg_type;
char str[256];
struct msgbuf msgs; /* 创建消息队列 */
msqid = msgget(1024,IPC_CREAT); while (1)
{
printf("Please input message type,0 for quit!\n");
/* 获取消息类型 */
scanf("%d",&msg_type); /* 如果用户输入的消息类型为0,退出该循环 */
if (msg_type == 0)
break; /* 获取消息数据 */
printf("Please input message content!\n");
scanf("%s",str); msgs.msgtype = msg_type;
strcpy(msgs.msgtext,str); /* 发送消息 */
msgsnd(msqid,&msgs,sizeof(struct msgbuf),0); } /* 删除消息队列 */ msgctl(msqid,IPC_RMID,0);
return 0; }
接收
/* Receive.c*/

#include <stdio.h>
//#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h> /* 多进程-子进程 */ int msqid = 0; struct msgt
{
long msgtype;
char msgtext[1024];
}; /* 创建子线程 */
void childprocess()
{
struct msgt msgs;
while(1)
{
/* 接收消息队列 */
msgrcv(msqid,&msgs,sizeof(struct msgt), 0, 0); /* 打印消息队列中的数据 */
printf("message text: %s\n",msgs.msgtext);
} return;
} void main()
{ int i;
int cpid;
/* 打开消息队列 */ msqid = msgget(1024, IPC_EXCL);
/* 创建3个子进程 */
for(i=0;i<3;i++)
{
cpid = fork();//创建子线程???
if (cpid<0) printf("Creat childprocess ERROR\n"); else if(cpid == 0) childprocess(); } }

【Linux】IPC-消息队列的更多相关文章

  1. linux IPC 消息队列

    消息队列函数原型 在建立IPC通讯时(如消息队列,共享内存)必须建立一个ID值.通常情况下,这个ID值由ftok函数得到 #inlcude <sys/types.h> #include & ...

  2. linux IPC 消息队列(二)

    我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来: common.h #ifndef __COMMON_H_ #define __COMMON_H_ #include <std ...

  3. IPC——消息队列

    Linux进程间通信——使用消息队列 下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管 ...

  4. 详解linux进程间通信-消息队列

    前言:前面讨论了信号.管道的进程间通信方式,接下来将讨论消息队列. 一.系统V IPC 三种系统V IPC:消息队列.信号量以及共享内存(共享存储器)之间有很多相似之处. 每个内核中的 I P C结构 ...

  5. Linux进程间通信—消息队列

    四.消息队列(Message Queue) 消息队列就是消息的一个链表,它允许一个或者多个进程向它写消息,一个或多个进程向它读消息.Linux维护了一个消息队列向量表:msgque,来表示系统中所有的 ...

  6. linux进程间通信-消息队列

    一 消息队列的介绍 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构. 我们可以通过发送消息来避免命名管道的 ...

  7. Linux进程间通信-消息队列(mqueue)

    前面两篇文章分解介绍了匿名管道和命名管道方式的进程间通信,本文将介绍Linux消息队列(posix)的通信机制和特点. 1.消息队列 消息队列的实现分为两种,一种为System V的消息队列,一种是P ...

  8. linux 下消息队列发送后没有信息

    在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  9. linux中消息队列<一>

    1 概念 (1)链表式结构组织,存放于内核. (2)通过队列标识来引用. (3)通过一个消息类型来索引指定的数据 2 创建消息队列 #include <sys/msg.h> int msg ...

  10. linux进程间通信消息队列:msgsnd: Invalid argument

    今天写了个消息队列的小测试程序结果send端程序总是出现:msgsnd: Invalid argument,搞了半个小时也没搞明白,后来查资料发现我将(st_msg_buf.msg_type = 0; ...

随机推荐

  1. flask总结01

    一:Flask的基本介绍和小案例 01:flask的基本介绍 Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开 ...

  2. POJ1475 Pushing Boxes 华丽丽的双重BFS

    woc累死了写了两个半小时...就是BFS?我太菜了... 刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化....我死了QWQ ...

  3. poj 1220 NUMBER BASE CONVERSION

    NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Accepted: ...

  4. Subsequence(二分)

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  5. BZOJ - 2818 莫比乌斯反演 初步

    要使用分块的技巧 #include<iostream> #include<algorithm> #include<cstdio> #include<cstri ...

  6. LightOJ - 1197 素数筛

    深夜无事可干啊 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; typedef long lon ...

  7. while循环、运算符和格式化输出以及编码

    一.while循环 1.while就是当的意思,while指当其后面的条件成立,就执行while下面的代码 写一段代码让程序从0打印到100的程序,每次循环+1. count = 0 while co ...

  8. java se系列(二) 关键字、注释、常量、进制转换、变量、数据类型转换、运算符

    1 关键字 1.1 关键字的概述 Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名.方法名.类名.包名. 1.2 常见的关键字 备注 ...

  9. sql常用格式化函数及字符串函数

    一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') YYYY:年份 MM:月份号(01-12) ...

  10. Angular4+NodeJs+MySQL 入门-06 接口配置

    在上篇中说了怎么调用接口,这篇就来说说,接口配置吧. 后端是用NodeJS来写的,由于写后台(以前用的是C#语言)的时候,大部操作都在是对数据库表的增.删.改.查操作, 比如:根据查询出来的数据,然后 ...