嵌入式Linux C进程间通信(三)——消息队列

简介: 嵌入式Linux C进程间通信(三)——消息队列

前述


System V IPC

0a2653c851af460fa595bd959398a8f1.png

特点:

与管道有所不同,它完全使用了不同的实现机制,与文件没任何的关系,也就是说内核不再以文件的形式来管理System V IPC

System V IPC不再以文件的形式存在,因此没有文件描述符这个东西,但是它有类似的“标识符”

任何进程之间通信时,都可以使用System V IPC来通信

POSIX也可以支持消息队列、共享内存、信号量能在unix下运行


一、消息队列的创建和删除


消息队列的本质就是由内核创建的用于存放消息的链表,由于是存放消息的,所以我们就把这个链表称为消息队列。


1.1 消息的组成


1.消息编号:识别信息用

2.消息正文:真正的信息内容

0eacb84100b54626af849e6b562bf92a.png


1.2 创建


int msgget(key_t key, int msgflg);


0a2653c851af460fa595bd959398a8f1.png


使用ftok生成key值


0eacb84100b54626af849e6b562bf92a.png2d65d23f6d4748949b924e4057485923.png


1.2 查看系统的消息队列


0a2653c851af460fa595bd959398a8f1.png0eacb84100b54626af849e6b562bf92a.png


1.3 消息队列的创建


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#define MSGNAME "./msg_file"
int main(int argc, char const *argv[])
{
    key_t key;
    int msgid;
    key = ftok(MSGNAME, 'k');
    msgid = msgget(key, IPC_CREAT | 0655);
    if (msgid < 0)
    {
        perror("msgid error!");
        exit(1);
    }
    printf("msgid = %d\n",msgid);
    return 0;
}


1.4 获取属性及删除


进程结束时,system v ipc不会自动删除,进程结束后,使用ipcs依然能够查看到


长方形

重启OS

使用ipcrm命令删除

int msgctl 参数:int mspid,int cmd,struct mspid_ds *buf

ipcrm -Q msgkey移除用msqkey创建的消息队列

ipcrm -q msqid移除用msqid标识的消息队列

0a2653c851af460fa595bd959398a8f1.png

msgctl


0a2653c851af460fa595bd959398a8f1.png0eacb84100b54626af849e6b562bf92a.png2d65d23f6d4748949b924e4057485923.png2e9b90b2ca334476abebe75bafe6eeaa.png


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#define MSGNAME "./msg_file"
int main(int argc, char const *argv[])
{
    key_t key;
    int msgid;
    key = ftok(MSGNAME, 'k');
    msgid = msgget(key, IPC_CREAT | 0655);
    if (msgid < 0)
    {
        perror("msgid error!");
        exit(1);
    }
    printf("key = %x\n", key);
    printf("msgid = %d\n", msgid);
    msgctl(msgid,IPC_RMID,NULL);
    return 0;
}

0a2653c851af460fa595bd959398a8f1.png


二、消息队列的发送和接收


2.1 发送


int msgsnd(int msqid,const void *msgp, size_t msgsz,int msgflg);

0eacb84100b54626af849e6b562bf92a.png


2.2 接收


ssize_ t msgrcv(int msqid, void *msgp, size_ t msgsz, long msgtyp, int msgflg);

2d65d23f6d4748949b924e4057485923.png


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#define MSGNAME "./msg_file"
int msgid;
struct msgbuf
{
    long mtype;
    char mtext[1024];
};
void my_exit(int msg)
{
    msgctl(msgid,IPC_RMID,NULL);
}
int main(int argc, char const *argv[])
{
    key_t key;
    pid_t pid;
    signal(SIGINT, my_exit);
    key = ftok(MSGNAME, 'k');
    msgid = msgget(key, IPC_CREAT | 0655);
    if (msgid < 0)
    {
        perror("msgid error!");
        exit(1);
    }
    printf("key = %x\n", key);
    printf("msgid = %d\n", msgid);
    pid = fork();
    if (pid < 0)
    {
        perror("fork perror!\n");
        exit(1);
    }
    if (pid == 0)
    {
        long mtype;
        char buffer[1024];
        struct msgbuf pmsgbuf;
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            memset(&pmsgbuf, 0, sizeof(struct msgbuf));
            printf("Please input send msg:\n");
            scanf("%s", buffer);
            strcpy(pmsgbuf.mtext, buffer);
            printf("Please input send msg id:\n");
            scanf("%ld", &mtype);
            pmsgbuf.mtype = mtype;
            if(msgsnd(msgid,(void *)&pmsgbuf,sizeof(pmsgbuf),IPC_NOWAIT) < 0)
            {
                perror("msg snd error!");
                exit(1);
            }
        }
    }
    else if(pid > 0)
    {
        size_t size;
        long mtype;
        char buffer[1024];
        struct msgbuf pmsgbuf;
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            memset(&pmsgbuf, 0, sizeof(struct msgbuf));
            size = msgrcv(msgid, &pmsgbuf,sizeof(pmsgbuf),3,0);
            if (size < 0)
            {
                perror("msg rcv error!");
                exit(1);
            }
            printf("recv data:%s\n", pmsgbuf.mtext);
            sleep(1);
        }
    }
    return 0;
}


三、任意进程通信及特点


多个进程是如何共享到同一个消息队列?


1.fork前创建

2.创建者把ID保存到某文件,共享进程读出ID即可+

3.使用ftok函数,利用与创建者相同的“路径名”和8位整形数,生成相同的key值

消息队列的特点


1.传送有格式的消息流

2.多进程网状旁叉通信时,消息队列是上上之选

3.能实现大规模数据的通信


3.1 msg_read.c


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#define MSGNAME "./msg_file"
struct msgbuf
{
    long mtype;
    char mtext[1024];
};
int main(int argc, char const *argv[])
{
    key_t key;
    key = ftok(MSGNAME, 'k');
    int msgid = msgget(key, IPC_CREAT | 0777);
    if (msgid < 0)
    {
        perror("msgid error!");
        exit(1);
    }
    printf("key = %x\n", key);
    printf("msgid = %d\n", msgid);
    size_t size;
    long mtype;
    char buffer[1024];
    struct msgbuf pmsgbuf;
    while (1)
    {
        memset(buffer, 0, sizeof(buffer));
        memset(&pmsgbuf, 0, sizeof(struct msgbuf));
        size = msgrcv(msgid, &pmsgbuf,sizeof(pmsgbuf),3,0);
        if (size < 0)
        {
            perror("msg rcv error!");
            exit(1);
        }
        printf("recv data:%s\n", pmsgbuf.mtext);
        sleep(1);
    }
}


3.2 msg_write.c


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#define MSGNAME "./msg_file"
struct msgbuf
{
    long mtype;
    char mtext[1024];
};
int main(int argc, char const *argv[])
{
    key_t key;
    key = ftok(MSGNAME, 'k');
    int msgid = msgget(key, 0777 | IPC_CREAT);
    if (msgid < 0)
    {
        perror("msgid error!");
        exit(1);
    }
    printf("key = %x\n", key);
    printf("msgid = %d\n", msgid);
    size_t size;
    long mtype;
    char buffer[1024];
    struct msgbuf pmsgbuf;
    while (1)
    {
        memset(buffer, 0, sizeof(buffer));
        memset(&pmsgbuf, 0, sizeof(struct msgbuf));
        printf("Please input send msg:\n");
        scanf("%s", buffer);
        strcpy(pmsgbuf.mtext, buffer);
        printf("Please input send msg id:\n");
        scanf("%ld", &mtype);
        pmsgbuf.mtype = mtype;
        if(msgsnd(msgid,(void *)&pmsgbuf,sizeof(pmsgbuf),IPC_NOWAIT) < 0)
        {
            perror("msg snd error!");
            exit(1);
        }
    }
}


3.3 成果展示


2019082413041482.gif

相关文章
|
1天前
|
Linux
Linux源码阅读笔记13-进程通信组件中
Linux源码阅读笔记13-进程通信组件中
|
1天前
|
消息中间件 安全 Java
Linux源码阅读笔记13-进程通信组件上
Linux源码阅读笔记13-进程通信组件上
|
1天前
|
Linux
Linux源码阅读笔记10-进程NICE案例分析2
Linux源码阅读笔记10-进程NICE案例分析2
|
1天前
|
Linux 数据安全/隐私保护
|
1天前
|
Linux
Linux源码阅读笔记09-进程NICE案例分析1
Linux源码阅读笔记09-进程NICE案例分析1
|
1天前
|
存储 Linux API
Linux源码阅读笔记08-进程调度API系统调用案例分析
Linux源码阅读笔记08-进程调度API系统调用案例分析
|
1天前
|
Linux API
Linux源码阅读笔记07-进程管理4大常用API函数
Linux源码阅读笔记07-进程管理4大常用API函数
|
5天前
|
NoSQL 关系型数据库 MySQL
Linux学习记录---(1、基本命令)
该博客文章提供了Linux系统中基本命令的使用记录,包括文件和目录操作、Redis服务管理、MySQL数据库操作以及Tomcat服务器的启动和检查。
Linux学习记录---(1、基本命令)
|
3天前
|
Linux
会玩这10个Linux命令,一定是个有趣的IT男!
会玩这10个Linux命令,一定是个有趣的IT男!
|
2天前
|
安全 Linux 开发者
Linux笔记之ldd命令详解
`ldd`命令是Linux环境下一个非常实用的工具,用于显示一个程序运行时所需的共享库依赖。它帮助开发者和系统管理员快速诊断程序运行问题,特别是在处理"找不到库文件"或者"错误的库文件版本"等错误时。然而,出于安全的考虑,对于不信任的可执行文件,应该慎用 `ldd`命令,可以考虑使用其他工具如 `objdump`。总的来说,懂得如何妥善且安全地使用 `ldd`,对于维护一个稳定和高效的Linux系统来说,是非常重要的。
18 9

热门文章

最新文章