练习使用消息队列

来源:互联网 发布: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;}