IO进程——消息队列

目录

1.特点

2.操作步骤

3.函数接口

3.1创建消息队列

3.2添加消息

3.3读取消息

3.4删除消息队列

4.操作命令


1.特点

  1. 消息队列就是一个消息的列表。用户可以在消息队列中添加消息、读取消息等。
  2. 消息队列可以按照类型来发送/接收消息
  3. 在linux下消息队列的大小有限制。
  • 消息队列个数最多为16个;
  • 消息队列总容量最多为16384字节;
  • 每个消息内容最多为8192字节。

2.操作步骤

  1. 创建key值
  2. 创建或打开消息队列
  3. 添加消息
  4. 读取消息
  5. 删除消息队列

3.函数接口

3.1创建消息队列

#include <sys/msg.h>

int msgget(key_t key, int flag);

功能:创建或打开一个消息队列

参数:

        key值

        flag:创建消息队列的权限IPC_CREAT|IPC_EXCL|0666

返回值:

        成功:msgid

        失败:-1

示例:

#include <stdio.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/msg.h>

int main(int argc, char const *argv[])
{
    int msgid;
    key_t key;
    key = fork("./app", 'a');
    if (key < 0)
    {
        perror("fork err");
        return -1;
    }
    //创建消息队列
    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgget(key, 0666);
        }
        else
        {
            perror("smsget err");
            return -1;
        }
    }
    
    return 0;
}

3.2添加消息

int msgsnd ( int msqid , const void * msgp , size_t size , int flag );

功能:添加消息

参数:

        msqid:消息队列的ID

        msgp:指向消息的指针。常用消息结构msgbuf如下:

        struct msgbuf{

                        long mtype; //消息类型

                        char mtext[N]}//消息正文

                        }

        size:发送的消息正文的字节数

        flag:IPC_NOWAIT消息没有发送完成函数也会立即返回

                        0:直到发送完成函数才返回 (阻塞)

返回值:

        成功:0

        失败:-1

示例:

#include <stdio.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/msg.h>

struct msgbuf
{
    long mtype; //消息类型
    char ch; //消息正文
};

int main(int argc, char const *argv[])
{
    int msgid;
    key_t key;
    key = fork("./app", 'a');
    if (key < 0)
    {
        perror("fork err");
        return -1;
    }
    //创建消息队列
    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgget(key, 0666);
        }
        else
        {
            perror("smsget err");
            return -1;
        }
    }

    struct msgbuf msg = {100, 'a'};
    printf("%d\n", sizeof(msg));
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);   
    
    return 0;
}

3.3读取消息

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

功能:读取消息

参数:

        msgid:消息队列的ID

        msgp:存放读取消息的空间

        size:接受的消息正文的字节数

        msgtype:0:接收消息队列中第一个消息。

                     大于0:接收消息队列中第一个类型为msgtyp的消息.

                     小于0:接收消息队列中类型值不小于msgtyp的绝对值且类型值又最小的消息。

        flag:0:若无消息函数会一直阻塞

                        IPC_NOWAIT:若没有消息,进程会立即返回ENOMSG

返回值:

        成功:接收到的消息的长度

        失败:-1

示例:

#include <stdio.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/msg.h>

struct msgbuf
{
    long mtype; //消息类型
    char ch; //消息正文
};


int main(int argc, char const *argv[])
{
    int msgid;
    key_t key;
    key = fork("./app", 'a');
    if (key < 0)
    {
        perror("fork err");
        return -1;
    }
    //创建消息队列
    msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if (msgid <= 0)
    {
        if (errno == EEXIST)
        {
            msgget(key, 0666);
        }
        else
        {
            perror("smsget err");
            return -1;
        }
    }
    //添加消息
    struct msgbuf msg = {100, 'a'}, msg1;
    printf("%d\n", sizeof(msg));
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);   
    
    //再添加一条消息
    msg.mtype = 200;
    msg.ch = 'b';
    msgsnd(msgid, &msg, sizeof(msg) - sizeof(long), 0);

    //读取消息
    msgrcv(msgid, &msg1, sizeof(msg) - sizeof(long), 100, 0);
    printf("%c\n",msg1.ch);
}

3.4删除消息队列

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

功能:对消息队列的操作,删除消息队列

参数:

        msqid:消息队列的队列ID

        cmd:

                IPC_STAT:读取消息队列的属性,并将其保存在buf指向的缓冲区中。

                IPC_SET:设置消息队列的属性。这个值取自buf参数。

                IPC_RMID:从系统中删除消息队列。

        buf:消息队列缓冲区

返回值:

        成功:0

        失败:-1

4.操作命令

ipcs -q:查看创建的消息队列

ipcrm -q [msgid]:删除消息队列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值