linux下什么情况下用队列,linux下消息队列使用

这几天在学习消息队列使用,终于编写了一个简单的测试程序,拿出来与大家分享下:

/******************************************************************************

版权所有 (C), 2001-2011,

******************************************************************************

文 件 名 : drv_app_msg_send.c

版 本 号 : 初稿

作 者 : zhangjinlei

生成日期 : 2012年5月16日星期三

最近修改 :

功能描述 : 消息队列发送函数

函数列表 :

main

修改历史 :

1.日 期 : 2012年5月16日星期三

作 者 : zhangjinlei

修改内容 : 创建文件

******************************************************************************/

/*----------------------------------------------*

* 包含头文件 *

*----------------------------------------------*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "drv_app_common.h"

/*----------------------------------------------*

* 外部变量说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 外部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 内部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 全局变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 模块级变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 常量定义 *

*----------------------------------------------*/

/*----------------------------------------------*

* 宏定义 *

*----------------------------------------------*/

#define DRV_APP_MSG_QUE_FILE_NAME "." /* 消息队列指定文件名 */

#define DRV_APP_MSG_QUE_BUF_SIZE 255 /* 消息队列数据缓冲区大小 */

/* 每一条消息队列对应数据结构体 */

typedef struct msgtype {

int iMsgType;

char cBuffer[DRV_APP_MSG_QUE_BUF_SIZE+1];

}DRV_APP_MSG_QUE_S;

/*****************************************************************************

函 数 名 : main

功能描述 : 消息队列发送函数

输入参数 : void

输出参数 : 无

返 回 值 : ERROR_SUCCESS :表示函数执行成功

ERROR_FAILED :表示函数执行失败

修改历史 :

1.日 期 : 2012年5月16日星期三

作 者 : zhangjinlei

修改内容 : 新生成函数

*****************************************************************************/

int main(IN const int iargc, OUT char **ppargv)

{

DRV_APP_MSG_QUE_S sQueMsg; /* 单条消息队列对应结构体 */

key_t iKey; /* 消息队列对应的键值 */

int iMsgID; /* 消息队列对应的ID值 */

const char cSendMsg[40]= "app send msg data!";

int iRet;

(void)iargc;

(void)ppargv;

/* 获取消息队列键值 */

if ((iKey = ftok(DRV_APP_MSG_QUE_FILE_NAME, 'a')) == -1)

{

printf("Creat Key Error, ikey = %d, line = %d\n", iKey, __LINE__);

return ERROR_FAILED;

}

/* 获取消息队列ID值 */

if ((iMsgID = msgget(iKey, IPC_CREAT))== -1)

{

printf("Creat Message Error: ikey = %d, line = %d\n", iMsgID, __LINE__);

return ERROR_FAILED;

}

for (; ;)

{

sQueMsg.iMsgType = DRV_APP_MSG_QUE_REPORT_TYPE;

memcpy(sQueMsg.cBuffer, cSendMsg, sizeof(cSendMsg));

/* 如果消息满,则阻塞 */

iRet = msgsnd(iMsgID, &sQueMsg, sizeof(sQueMsg.cBuffer), 0);

if (0 > iRet)

{

printf("App send msg failed,iret = %d, line = %d\n", iRet, __LINE__);

}

(void)sleep(1);

memset(&sQueMsg, 0, sizeof(DRV_APP_MSG_QUE_S));

/* 只接收DRV_APP_MSG_QUE_REPORT_TYPE*/

iRet = msgrcv(iMsgID, &sQueMsg, sizeof(sQueMsg.cBuffer), DRV_APP_MSG_QUE_REPORT_BACK, 0);

if (0 > iRet)

{

printf("App receive msg failed,iret = %d, line = %d\n", iRet, __LINE__);

}

printf("send msg is :%s, line = %d\n",sQueMsg.cBuffer, __LINE__);

(void)sleep(1);

}

}

客户端代码:

/******************************************************************************

版权所有 (C), 2001-2011,

******************************************************************************

文 件 名 : drv_app_msg_receive.c

版 本 号 : 初稿

作 者 : zhangjinlei

生成日期 : 2012年5月17日星期四

最近修改 :

功能描述 : 消息队列接收文件

函数列表 :

main

修改历史 :

1.日 期 : 2012年5月17日星期四

作 者 : zhangjinlei

修改内容 : 创建文件

******************************************************************************/

/*----------------------------------------------*

* 包含头文件 *

*----------------------------------------------*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "drv_app_common.h"

/*----------------------------------------------*

* 外部变量说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 外部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 内部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 全局变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 模块级变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 常量定义 *

*----------------------------------------------*/

/*----------------------------------------------*

* 宏定义 *

*----------------------------------------------*/

#define DRV_APP_MSG_QUE_FILE_NAME "." /* 消息队列指定文件名 */

#define DRV_APP_MSG_QUE_BUF_SIZE 255 /* 消息队列数据缓冲区大小 */

/* 每一条消息队列对应数据结构体 */

typedef struct msgtype {

int iMsgType;

char cBuffer[DRV_APP_MSG_QUE_BUF_SIZE+1];

}DRV_APP_MSG_QUE_S;

/*****************************************************************************

函 数 名 : main

功能描述 : 消息队列接收函数

输入参数 : void

输出参数 : 无

返 回 值 : int

调用函数 :

被调函数 :

修改历史 :

1.日 期 : 2012年5月17日星期四

作 者 : zhangjinlei

修改内容 : 新生成函数

*****************************************************************************/

int main(IN const int iargc, OUT char **ppargv)

{

DRV_APP_MSG_QUE_S sQueMsg; /* 单条消息队列对应结构体 */

key_t iKey; /* 消息队列对应的键值 */

int iMsgID; /* 消息队列对应的ID值 */

const char cSendMsg[40]= "service send msg data!";

int iRet;

(void)iargc;

(void)ppargv;

if ((iKey = ftok(DRV_APP_MSG_QUE_FILE_NAME, 'a')) == -1)

{

printf("Creat Key Error, ikey = %d, line = %d\n", iKey, __LINE__);

return ERROR_FAILED;

}

if((iMsgID = msgget(iKey, IPC_CREAT)) == -1)

{

printf("Creat iMsgID Error, iMsgID = %d, line = %d\n", iMsgID, __LINE__);

return ERROR_FAILED;

}

for (; ;)

{

memset(&sQueMsg, 0, sizeof(sQueMsg));

iRet = msgrcv(iMsgID, &sQueMsg, sizeof(sQueMsg.cBuffer), DRV_APP_MSG_QUE_REPORT_TYPE, 0);

if (0 > iRet)

{

printf("Service receive msg error, line = %d\n", __LINE__);

}

(void)sleep(1);

printf("send msg is :%s, line = %d\n",sQueMsg.cBuffer, __LINE__);

sQueMsg.iMsgType = DRV_APP_MSG_QUE_REPORT_BACK;

memcpy(sQueMsg.cBuffer, cSendMsg, sizeof(cSendMsg));

iRet = msgsnd(iMsgID, &sQueMsg, sizeof(sQueMsg.cBuffer), 0);

if (0 > iRet)

{

printf("Service send msg error, line = %d\n", __LINE__);

}

(void)sleep(1);

}

}

公共文件:

/******************************************************************************

版权所有 (C), 2001-2011,

******************************************************************************

文 件 名 : drv_app_common.h

版 本 号 : 初稿

作 者 : zhangjinlei

生成日期 : 2012年5月16日星期三

最近修改 :

功能描述 : drv_app_common.h 的头文件

函数列表 :

修改历史 :

1.日 期 : 2012年5月16日星期三

作 者 : zhangjinlei

修改内容 : 创建文件

******************************************************************************/

#ifndef __DRV_APP_COMMON_H__

#define __DRV_APP_COMMON_H__

#ifdef __cplusplus

#if __cplusplus

extern "C"{

#endif

#endif /* __cplusplus */

/*----------------------------------------------*

* 包含头文件 *

*----------------------------------------------*/

/*----------------------------------------------*

* 外部变量说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 外部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 内部函数原型说明 *

*----------------------------------------------*/

/*----------------------------------------------*

* 全局变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 模块级变量 *

*----------------------------------------------*/

/*----------------------------------------------*

* 常量定义 *

*----------------------------------------------*/

/*----------------------------------------------*

* 宏定义 *

*----------------------------------------------*/

#define IN

#define OUT

#define INOUT

#define ERROR_SUCCESS 0

#define ERROR_FAILED 1

#define DRV_APP_MSG_QUE_REPORT_TYPE 0x01 /* 报站器数据消息 */

#define DRV_APP_MSG_QUE_REPORT_BACK 0x02 /* App 模块回应报站器消息 */

#ifdef __cplusplus

#if __cplusplus

}

#endif

#endif /* __cplusplus */

#endif /* __DRV_APP_COMMON_H__ */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值