练习使用消息队列
来源:互联网 发布:c语言排列组合公式 编辑:程序博客网 时间:2024/06/06 03:00
/*** @file msg.c* @version 1.0* @brief message queue interface* @note*/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <sys/msg.h>#include <sys/types.h>#include <sys/ipc.h>/*** @brief create message queue* @param piMsgId message id* @return return msg create status* @retval 0 sucess* @retval -1 create msg failed* */int msg_queue_create(int *piMsgId){ int iMsgId = 0; iMsgId = msgget(IPC_PRIVATE, IPC_CREAT|IPC_EXCL|00666); if(iMsgId < 0) { fprintf(stdout, "create msg error:errno=%d, err_msg=%s\n", errno, strerror(errno)); return -1; } if(piMsgId) { *piMsgId = iMsgId; } return 0;}/*** @brief send message* @param iMsgId message id* @param pMsg message want to send* @param iMsgSize message size* @return return msg send status* @retval 0 sucess* @retval -1 send msg failed* @retval -2 message length is less than sizeof(long)*/int msg_queue_send(int iMsgId, void *pMsg, int iMsgSize){ int iRet = 0; int iSendMsg = iMsgSize - sizeof(long); if(iSendMsg < 0) { fprintf(stdout, "message length is less than sizeof(long)=%lu\n", sizeof(long)); return -2; } iRet = msgsnd(iMsgId, pMsg, iSendMsg, 0); if(iRet < 0) { fprintf(stdout, "send msg error:errno=%d, err_msg=%s\n", errno, strerror(errno)); return -1; } return 0;}/*** @brief recv message* @param iMsgId message id* @param pMsg save recv message* @param iMsgSize message size* @return return msg recv status* @retval 0 sucess* @retval -1 recv msg failed* @retval -2 message length is less than sizeof(long)*/int msg_queue_recv(int iMsgId, void *pMsg, int iMsgSize){ int iRet = 0; int iRecvLen = iMsgSize - sizeof(long); if(iRecvLen < 0) { fprintf(stdout, "message length is less than sizeof(long)=%lu\n", sizeof(long)); return -2; } iRet = msgrcv(iMsgId, pMsg, iRecvLen, 0, 0); if(iRet < 0 || iRet > iMsgSize) { fprintf(stdout, "send msg error:errno=%d, err_msg=%s\n", errno, strerror(errno)); return -1; } return 0;}/*** @brief delete message queue* @param iMsgId message id* @return return delete msg queue status* @retval 0 sucess* @retval -1 delete msg queue failed*/int msg_queue_delete(int iMsgId){ if(iMsgId < 0) { printf("===>[%s, %d]error msg id %d\n", __FUNCTION__, __LINE__, iMsgId); return -1; } if(msgctl(iMsgId, IPC_RMID, 0) == -1) { fprintf(stdout, "delete msg error:errno=%d, err_msg=%s\n", errno, strerror(errno)); return -1; } return 0;}/********************* local structure **************************************/typedef struct _msg_st_{ long int msg_type; char acMsg[1024];}MSG_ST;/*** @brief message queue test* @return main function return status.*/int main(void){ int iMsgId = 0; MSG_ST stMsg; if(msg_queue_create(&iMsgId) == -1) { return -1; } printf("===>[%s, %d]iMsgId=%d\n", __FUNCTION__, __LINE__, iMsgId); do { memset(&stMsg, 0, sizeof(MSG_ST)); snprintf(stMsg.acMsg, sizeof(stMsg.acMsg), "Hello"); stMsg.msg_type = 1; if(msg_queue_send(iMsgId, &stMsg, sizeof(MSG_ST)) == -1) { break; } memset(&stMsg, 0, sizeof(MSG_ST)); snprintf(stMsg.acMsg, sizeof(stMsg.acMsg), "asdfasdf234"); stMsg.msg_type = 2; if(msg_queue_send(iMsgId, &stMsg, sizeof(MSG_ST)) == -1) { break; } memset(&stMsg, 0, sizeof(MSG_ST)); if(msg_queue_recv(iMsgId, &stMsg, sizeof(MSG_ST)) == -1) { break; } printf("===>[%s, %d]%ld %s\n", __FUNCTION__, __LINE__, stMsg.msg_type, stMsg.acMsg); memset(&stMsg, 0, sizeof(MSG_ST)); if(msg_queue_recv(iMsgId, &stMsg, sizeof(MSG_ST)) == -1) { break; } printf("===>[%s, %d]%ld %s\n", __FUNCTION__, __LINE__, stMsg.msg_type, stMsg.acMsg); }while(0); msg_queue_delete(iMsgId); return 0;}
阅读全文
0 0
- 练习使用消息队列
- 使用消息和消息队列
- 初次使用消息队列
- 如何使用消息队列
- 消息队列的使用
- linux 消息队列使用
- linux 使用消息队列
- Vxworks消息队列使用
- 消息队列的使用
- 使用消息队列
- 为什么使用消息队列?
- 消息队列的使用
- 消息队列使用场景
- 使用rabbitmq消息队列
- 消息队列使用场景
- 消息队列 使用场景
- 消息队列使用总结
- Linux编程练习 --SystemV消息队列
- 微信APP下载页面
- 【TensorFlow实战计算机视觉】 之 猫狗大战
- JMeter简单压力测试
- JZOJ5444. 【NOIP2017提高A组冲刺11.2】救赎
- git过滤
- 练习使用消息队列
- BFS(广度优先搜索)
- 解决Nginx无法站外访问
- spring定时器--时间设置规则
- 常用的数组API
- Spark性能优化:数据倾斜调优
- python:读取视频,处理后,实时计算帧数fps
- sourceInsight4破解笔记
- js压缩代码还原成正常格式