linux操作系统消息队列

简介: 所谓消息队列就是指一个消息链表。int msgget(key_t, int flag):创建和打开队列int msgsnd(int msqid, struct msgbuf *msgp, size_t msgsz, int flag):发送消息,msgid是消息队列的id,msgp是消息内容所在的缓冲区,msgsz是消息的大小,msgflg是标志。
所谓消息队列就是指一个消息链表。
int msgget(key_t, int flag):创建和打开队列
int msgsnd(int msqid, struct msgbuf *msgp, size_t msgsz, int flag):发送消息,msgid是消息队列的id,msgp是消息内容所在的缓冲区,msgsz是消息的大小,msgflg是标志。

int msgrcv(int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp, int flag):接受消息,msgtyp是期望接收的消息类型。

msgqueue.c文件内容如下;

#include<sys/types.h>
#include<sys/ipc.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h> // 增加线程支持

#define BUFSZ 512

struct message{
    long msg_type;
    char msg_text[BUFSZ];
};

#define MSG_SIZE sizeof(struct message)

char app_exit = 0;
void *thread_funtion(void *parg);

int main()
{
    int qid;
    key_t key;
    int len;
    int res;
    pthread_t a_thread;
    struct message msg;

    if((key = ftok(".",'a')) == -1){ // ftok 获得一个key
        perror("ftok");
        exit(1);
    }

    if((qid = msgget(key,IPC_CREAT|0666)) == -1){ // 创建一个消息队列
        perror("msgget");
        exit(1);
    }

    printf("opened queue %d\n",qid);
    puts("Please enter the message to queue:");
    if((fgets(msg.msg_text,BUFSZ,stdin)) == NULL){ // 从标准输入获得buffer
        puts("no message");
        exit(1);
    }
    
    msg.msg_type = getpid();
    len = strlen(msg.msg_text) + sizeof(msg.msg_type);
    if((msgsnd(qid,&msg,len,0)) < 0){ // 发送消息
        perror("message posted");
        exit(1);
    }

    /*
    memset(&msg,0,sizeof(msg)); // 清除内存为0

    if(msgrcv(qid,&msg,len,0) < 0){ // 接收消息
        perror("message recv");
        exit(1);
    }

    printf("message is:%s\n",(&msg)->msg_text);
    if((msgctl(qid,IPC_RMID,NULL))<0){
        perror("msgctl");
        exit(1);
    }
	*/
    res = pthread_create(&a_thread,NULL,thread_funtion,(void *)&qid);
    
    printf("The msgrcv thread is create sucess!\n");

	while((app_exit =  getchar()) != 'e'){sleep(50);}
    
    printf("exit main funtion!\n");
    exit(0);
}

void *thread_funtion(void *parg)
{
    struct message msg;
    int qid;
    
    qid = *((int *)parg);
    memset(&msg,0,MSG_SIZE);
    while(app_exit != 'e'){
        if(msgrcv(qid,&msg,MSG_SIZE) < 0){
            sleep(50);
            continue;
        }
        printf("message is:%s\n",(&msg)->msg_text);
        if(msgctl(qid,IPC_RMID,NULL) < 0){
            perror("msgctl");
        }
        sleep(50);
    }
}

Makefile文件类型如下;

all:msgqueue

# which compiler
CC = gcc

# Where are include file kept
INCLUDE = .

# Where to install
INSTDIR = /usr/local/bin

# Options for development
CFLAGS = -g -Wall -ansi

msgqueue:msgqueue.o
	$(CC) -D_REENTRANT -o msgqueue msgqueue.o -lpthread

msgqueue.o:msgqueue.c
#	$(CC) -I$(INCLUDE) $(CFLAGS) -c msgqueue.c
#	$(CC) -D_REENTRANT -c msgqueue.c -lpthread
	$(CC) -c msgqueue.c

clean:
	-rm msgqueue.o msgqueue

install:msgqueue
	@if [-d $(INSTDIR) ];\
        then \
        cp msgqueue $(INSTDIR);\
        chmod a+x $(INSTDIR)/msgqueue;\
        chmod og-w $(INSTDIR)/msgqueue;\
        echo "Install in $(INSTDIR)";\
    else \
       echo "Sorry,$(INSTDIR) does not exist";\
    fi 


目录
相关文章
|
6天前
|
Linux 数据安全/隐私保护 虚拟化
Linux技术基础(1)——操作系统的安装
本文是龙蜥操作系统(Anolis OS) 8.4 的安装指南,用户可以从[龙蜥社区下载页面](https://openanolis.cn/download)获取ISO镜像。安装方法包括物理机的光驱和USB闪存方式,以及虚拟机中的VMware Workstation Pro设置。安装过程涉及选择语言、配置安装目标、选择软件集合和内核,设置Root密码及创建新用户。安装完成后,可通过文本模式或图形化界面验证系统版本,如Anolis OS 8.4,标志着安装成功。
|
14天前
|
存储 缓存 算法
Linux--系统结构与操作系统
Linux--系统结构与操作系统
|
18天前
|
Linux 网络安全 数据安全/隐私保护
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
98 0
|
24天前
|
缓存 Linux Shell
Linux进程解析(冯诺依曼体系结构,操作系统,进程初步解析)
Linux进程解析(冯诺依曼体系结构,操作系统,进程初步解析)
45 1
|
18天前
|
安全 Linux 网络安全
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
55 0
|
7天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
24 0
|
9天前
|
算法 Linux 调度
根基已筑!Anolis OS 23.1 预览版本搭载 Linux 6.6 内核和工具链升级完成
Anolis OS 23.1 对软件包的选择和组合进行了重新规划与决策,满足更为广泛的应用场景需求。
|
10天前
|
消息中间件 Linux API
Linux进程间通信(IPC) Linux消息队列:讲解POSIX消息队列在Linux系统进程间通信中的应用和实践
Linux进程间通信(IPC) Linux消息队列:讲解POSIX消息队列在Linux系统进程间通信中的应用和实践
11 1
Linux进程间通信(IPC) Linux消息队列:讲解POSIX消息队列在Linux系统进程间通信中的应用和实践
|
11天前
|
安全 Linux 开发者
分析Linux桌面操作系统的迅速增长及其未来前景
最近技术圈新闻“层出不穷”,尤其是在最近,Linux桌面操作系统的市场份额迅速增长,Linux桌面操作系统的市场份额近期呈现火速增长的趋势,这一数据虽然看似不太引人注目,但实际上却具有重要的意义,达到了历史新高。了解Linux的开发者想必都知道,历经30年的努力,Linux系统的份额才在不久前达到了3%,而如今仅用了八个月的时间就新增了1%,显示出开源操作系统正迅速升温。尽管Windows和macOS仍然主导着桌面操作系统市场,但前者的份额波动较小,后者则略有下滑。虽然Linux的表现出色,但要想取得主导地位还有一段距离,有些开发者认为这是因为缺乏一个适用于所有Linux发行版的标准化桌面界面
17 1
分析Linux桌面操作系统的迅速增长及其未来前景
|
14天前
|
存储 缓存 供应链
『Linux升级路』冯诺依曼体系结构与操作系统
『Linux升级路』冯诺依曼体系结构与操作系统

热门文章

最新文章

相关产品

  • 云消息队列 Kafka 版
  • 消息服务
  • 云消息队列 MQ