Linux进程通信之消息队列

网友投稿 588 2022-05-29

拥有梦想是一种智力,实现梦想是一种能力

概述

若是一个多线程的进程,由于各个线程共享一个地址空间,可以直接通过变量的形式进行通信。而进程,由于各个进程独占一个地址空间,我们需要一种通信机制来完成进程间的数据交互。本章介绍的是消息队列,进程间的通信机制有以下几种:

无名管道(pipe)

有名管道 (fifo)

信号(signal)

System  V  IPC

共享内存(share memory)

消息队列(message queue)

信号灯集(semaphore set)

套接字(socket)

之间有区分与各自的运用场景,其中套接字通常使用在网络服务,其他只能在本地场景下使用。笔者以后会逐一学习,本章介绍System  V  IPC中的消息队列。

System  V  IPC

System  V IPC引入了三种高级进程间的通信机制。一个IPC对象包含消息队列、共享内寸和信号量。

共享内存

消息队列

信号灯集

System V IPC对象

每个IPC对象有唯一的ID

IPC对象创建后一直存在,直到被显式地删除

每个IPC对象有一个关联的KEY(其中进程的私有对象KTY值为0)

命令查看IPC对象

IPC对象是全局对象,可用ipcs,ipcrm等命令查看或删除

ipcs -q: 只显示消息队列

ipcs -s: 只显示信号量

ipcs -m: 只显示共享内存

ipcs –help: 其他的参数

Linux进程通信之消息队列

函数操作

创建一个IPC对象

进程创建IPC对象之前,先ftok生成一个key值。

#include  

#include

key_t  ftok(const char *path,  int proj_id);

成功时返回合法的key值,失败时返回EOF

path  存在且可访问的文件的路径

proj_id  用于生成key的数字,不能为0

消息队列

消息队列是System V IPC对象的一种,由消息队列ID来唯一标识。消息队列就是一个消息的列表。用户可以在消息队列中添加消息、读取消息等

特点是消息队列可以按照类型来发送/接收消息。

消息队列结构

消息格式

通信双方首先定义好统一的消息格式,用户根据应用需求定义结构体类型

其中,首成员类型为long,代表消息类型(正整数),其他成员都属于消息正文

typedef struct {

long mtype;

char mtext[64];

} MSG;

消息队列使用步骤

打开/创建消息队列   msgget

向消息队列发送消息   msgsnd

从消息队列接收消息   msgrcv

控制消息队列   msgctl

消息队列创建/打开 – msgget

#include

#include

int msgget(key_t key, int msgflg);

成功时返回消息队列的id,失败时返回EOF

key 和消息队列关联的key  IPC_PRIVATE 或 ftok

msgflg  标志位  IPC_CREAT|0666

消息发送 – msgsnd

#include

#include

int msgsnd(int msgid, const void *msgp, size_t size,

int msgflg);

成功时返回0,失败时返回-1

msgid   消息队列id

msgp    消息缓冲区地址

size    消息正文长度

msgflg   标志位 0 或 IPC_NOWAIT

消息接收 – msgrcv

#include

#include

int msgrcv(int msgid, void *msgp, size_t size, long msgtype,

int msgflg);

成功时返回收到的消息长度,失败时返回-1

msgid   消息队列id

msgp   消息缓冲区地址

size   指定接收的消息长度

msgtype   指定接收的消息类型

msgflg   标志位   0 或 IPC_NOWAIT

控制消息队列 – msgctl

#include

#include

int msgctl(int msgid, int cmd, struct msqid_ds *buf);

成功时返回0,失败时返回-1

msgid    消息队列id

cmd    要执行的操作  IPC_STAT / IPC_SET / IPC_RMID

buf   存放消息队列属性的地址

示例

两个进程通过消息队列轮流将键盘输入的字符串发送给对方,接收并打印对方发送的消息

A.c

#include

#include

#include

#include

#include

typedef struct {

long mtype;

char mtext[64];

} MSG;

#define LEN (sizeof(MSG) – sizeof(long))

#define TypeA 100

#define TypeB 200

int main()

{

int msgid;

key_t key;

MSG buf;

if ((key = ftok(“.”, ‘q’)) == -1)

{

perror(“ftok”);

exit(-1);

}

if ((msgid = msgget(key, IPC_CREAT|0666)) < 0)

{

perror(“msgget”);

exit(-1);

}

while ( 1 )

{

buf.mtype = TypeB;

printf("input > ");

fgets(buf.mtext, 64, stdin);

msgsnd(msgid, &buf, LEN, 0);

if (strcmp(buf.mtext, "quit\n") == 0) exit(0);

msgrcv(msgid, &buf, LEN, TypeA, 0);

if (strcmp(buf.mtext, "quit\n") == 0) break;

printf("recv message : %s", buf.mtext);

}

msgctl(msgid, IPC_RMID, NULL);

return 0;

}

B.c

#include

#include

#include

#include

#include

typedef struct {

long mtype;

char mtext[64];

} MSG;

#define LEN (sizeof(MSG) – sizeof(long))

#define TypeA 100

#define TypeB 200

int main()

{

int msgid;

key_t key;

MSG buf;

if ((key = ftok(“.”, ‘q’)) == -1)

{

perror(“ftok”);

exit(-1);

}

if ((msgid = msgget(key, IPC_CREAT|0666)) < 0)

{

perror(“msgget”);

exit(-1);

}

while ( 1 )

{

msgrcv(msgid, &buf, LEN, TypeB, 0);

if (strcmp(buf.mtext, "quit\n") == 0) break;

printf("recv message : %s", buf.mtext);

buf.mtype = TypeA;

printf("input > ");

fgets(buf.mtext, 64, stdin);

msgsnd(msgid, &buf, LEN, 0);

if (strcmp(buf.mtext, "quit\n") == 0) exit(0);

}

msgctl(msgid, IPC_RMID, NULL);

return 0;

}

这样,当A是发送端,B进程在等待接收。B接收到数据,成为发送端,A进程等待接收。直到一方发送'quit',发送端进程结束,接收端删除消息队列,结束。

注意的是,进程发送对方的类型,接收时指定自己的类型。

Linux 任务调度

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【愚公系列】2021年12月 二十三种设计模式(四)-原型模式
下一篇:G Suit 介绍
相关文章