linux操作系统消息队列
来源:互联网 发布:来访人员软件 编辑:程序博客网 时间:2024/06/09 22:44
所谓消息队列就是指一个消息链表。
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 512struct 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 compilerCC = gcc# Where are include file keptINCLUDE = .# Where to installINSTDIR = /usr/local/bin# Options for developmentCFLAGS = -g -Wall -ansimsgqueue:msgqueue.o$(CC) -D_REENTRANT -o msgqueue msgqueue.o -lpthreadmsgqueue.o:msgqueue.c#$(CC) -I$(INCLUDE) $(CFLAGS) -c msgqueue.c#$(CC) -D_REENTRANT -c msgqueue.c -lpthread$(CC) -c msgqueue.cclean:-rm msgqueue.o msgqueueinstall: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
- linux操作系统消息队列
- Linux操作系统--消息队列
- Linux消息队列编程
- linux消息队列
- linux消息队列操作
- linux IPC-消息队列
- linux消息队列函数
- Linux消息队列操作
- linux 消息队列
- linux IPC-消息队列
- Linux的消息队列
- Linux消息队列
- Linux消息队列
- linux消息队列
- linux消息队列
- Linux消息队列
- linux 消息队列 实例
- linux 消息队列 实例
- 超大型
- 函数指针和指针函数的区别
- C++关于用户输入情况的判定与报错
- 恩和巷6号
- Tiny6410 移植DM9000网卡
- linux操作系统消息队列
- HTML & XML 转义字符
- MySql忘记密码解决
- hdoj 1253 三维bfs()
- 保持简单----纪念丹尼斯•里奇(Dennis Ritchie)
- 最小生成树笔记
- 进程间通信详解 - 匿名管道实现
- 无线网格网(WMN,Wireless Mesh Network)关键技术概览
- 2011我的华为面试