Linux---进程间通信IPC之消息队列

更多点子:linux—目录索引(知识小渠道)


进程间通信(IPC):是指在不同进程之间传播或交换信息。
IPC的方式:通常有管道(无名管道、命名管道)、消息队列、信号量、共享存储、Socket、Streams等(Socket和Streams支持不同主机上的两个进程IPC)
进程间通信的目的:
1.数据传输:一个进程需要将它的数据发给另一个进程
2.资源共享:多个进程之间共享同样的资源
3.通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发送了啥(如进程终止要通知父进程)
4.进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变

消息队列:一种数据结构,队列
消息队列提供了一个从一个进程向另一个进销存发送一块数据的方法
消息队列也有管道一样的不足,就是每个消息的最大长度是有限的,每个消息队列总的字节数是有上限的,系统上消息队列的总数是有上限的

系统能够创建多少个消息队列?
cat/proc/sys/kernel/msgmni
每个消息队列能够装多少个字节?
cat/proc/sys/kernel/msgmnb
队列中每一条记录最大是多少?
cat/proc/sys/kernel/max
这里写图片描述

//消息缓存块:一种数据结构,用于存储信息
struct msgbuf{
long channel;//通道号
char mtext[100];//消息内容,100自定义
}

通道:通道并不真的存在,是一种分类,由消息缓存块中的chnnel通道号决定,进程可以通过消息队列的通道来选择对应类型的消息队列

消息队列基础操作

1.创建/打开消息队列

//创建新的或者打开已经存在的消息队列
//返回值:-1 失败,成功返回消息队列的标识符id
int msgget(key_t key,int flag);

参数:key
每个消息队列都有一个唯一的标识符id,也有一个唯一的关键字
函数会将key与已有关键字进行比较来判断消息队列是否已经创建
生成key:

#include<sys/ipc.h>
#include<sys/types.h>
key_t ftok(const char* pathname,int proj_id);
//pathname是你指定的文件名,要求文件必须存在,“.”这样是当前目录
//id是自序号,int型,只使用8bits,一般弄字母准没错
//返回值:-1 失败,成功返回key值
//这个函数作用就是保证生成的key唯一

参数:flag

//操作消息队列权限
//IPC_CREAT,创建新的消息队列
//IPC_EXCL,IPC_CREAT一起用,如果要创建的消息队列已经存在,返回错
//IPC_NOWAIT,读写消息队列读不上时,不堵塞
//0,如果打开文件,文件存在,输0

代码实现:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>

int main()
{
    key_t key=ftok(".",'a');
    if(key == -1)
        perror("ftok"),exit(1);
    int id=msgget(key,IPC_CREAT|0644);
    if(id == -1)
        perror("msgget"),exit(1);
    printf("create success\n");
    printf("%d\n",id);
    return 0;
}

2.查看消息队列

这里写图片描述

3.删除消息队列

命令:ipcrm -Q key值
这里写图片描述
函数:msgctl(int msqid,int cmd,struct msqid_ds *buf);

#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
//msgctl对msqid标识的消息队列执行cmd操作
//返回值:0 成功,-1 失败
int msgctl(int msqid,int cmd,struct msqid_ds *buf);

参数:

//msqid 消息队列标识符
//cmd 
//{
//  IPC_STAT  读取消息队列,存在buf
//  IPC_SET   设置消息队列的值,值来自buf
//  IPC_RMID  从系统内核删除消息队列,这时buf为0使用,即NULL
//}

这里写图片描述
代码实现:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
int main()
{
    int id=msgget(0x61021ed2,0);
    if(id==-1)
    {
        perror("msgget");
        exit(1);
    }
    printf("消息队列存在\n");
    if(msgctl(id,IPC_RMID,0)==-1)
    {
        perror("msgctl");
        exit(1);
    }
    printf("msgctl success\n");
    return 0;
}

4.写消息队列

函数:

#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
//将内容写到(msgp指向的消息队列对象)里,将(消息队列对象的内容)放进(msqid对应的消息队列)中
//返回值:0 成功,-1 失败
int msgsnd(int msqid,const void* msgp,ssize_t msgsz,int msgflg);

参数:

//msqid 想要写进的消息队列标识符id
//msgp 临时创建的消息队列结构体对象指针
//msgsz 写入内容的大小,不包括通道号chennel,只是char mtext[ ]的大小
//msgflg  0 阻塞,IPC_NOWAIT 非阻塞

这里写图片描述
代码实现:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf {
    long channel;
    char mtext[100];
};

int main( void )
{
    int id = msgget(0x61021ed2, 0); // 打开消息队列,第二个参数为0
    if ( id == -1 ) perror("msgget"),exit(1);
    struct msgbuf mb;
    printf("channel:");
    scanf("%ld", &mb.channel);
    printf("text:");
    scanf("%s", mb.mtext);
    int r = msgsnd(id, &mb, strlen(mb.mtext), 0);
    if ( r == -1 ) perror("msgsnd"),exit(1);
}

5.读消息队列

函数:

#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
//将(msqid对应的消息队列里的内容)放到(msgp指向的消息队列结构体对象),从(msgp指向的对象)中读取内容
//返回值:-1 失败,成功返回消息数据的长度
ssize_t msgrcv(int msqid,void *msgp,size_t msgsz,long msgtyp,int msgflg);

参数:

//msqid 想要读取内容的消息队列标识符id
//msgp 临时创建的消息队列结构体对象指针
//magsz 读取内容的大小,不包括通道号chennel,只是char mtext[ ]的大小
//mstyp 想读取消息所在通道号(channel)
//{
//  mstyp==0  返回队列中第一个消息
//  mstyp > 0 返回队列中channle为mstyp的第一个消息
//  mstyp < 0 返回队列中channel小于或等于mstyp绝对值的消息,如果有多个,取channel最小的消息
//}

这里写图片描述
代码实现:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>

struct msgbuf {

    long channel;
    char mtext[100];
};

int main( void )
{
    int id = msgget(0x61021ed2, 0); 
    //打开消息队列,0x61021685是刚才创建的消息队列关键字
    //第二个参数为0,表示这是打开消息队列而不是创建,即该消息队列存在
    if ( id == -1 )
        perror("msgget"),exit(1);
    struct msgbuf mb;
    printf("channel:");
    int channel;
    scanf("%d", &channel);

    ssize_t r=msgrcv(id,&mb,100,channel,IPC_NOWAIT);
    if(r == -1)
        perror("msgrcv"),exit(1);
    printf("%s\n",mb.mtext);
}

如果有什么不对的地方,可以评论告诉我,望指导!

猜你喜欢

转载自blog.csdn.net/phonycat/article/details/80141530