`
microjava
  • 浏览: 309382 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

c语言编程之消息队列

阅读更多
//msg.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

#define    MSGKEY	1000

struct msgStru
{
    long    msgType;
    char    msgText[4096];
};

main()
{
   int   iMsgId;
   struct msgStru slQMsg,slRcvMsg;
   int   ilRc;

   iMsgId = msgget(MSGKEY,IPC_EXCL);/*检查消息队列是否存在*/
   if ( iMsgId < 0 ){
       iMsgId = msgget(MSGKEY,IPC_CREAT|0666);/*创建消息队列*/
       if ( iMsgId < 0 ){
           printf("create msgQ error! errno=%d[%s]\n",errno,strerror(errno));
           exit(-1);
       }
   }

   slQMsg.msgType = 100;
   strcpy(slQMsg.msgText,"Hello mcfeng.");
   ilRc = msgsnd(iMsgId, &slQMsg, sizeof(struct msgStru),IPC_NOWAIT);
   if( ilRc < 0 ) {
       printf("msgsnd()写消息队列失败,errno=%d[%s]\n",errno,strerror(errno));
       exit(-1);
   }
   
   ilRc = msgrcv(iMsgId,&slRcvMsg,sizeof(struct msgStru),0,0);/*接收消息队列*/
   printf("text=[%s]\n",slRcvMsg.msgText);

   msgctl(iMsgId,IPC_RMID,0);  /* 删除消息队列 */
   exit(0);
}



//receive.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>


#define MSGKEY 1024

struct msgstru
{
   long msgtype;
   char msgtext[2048];

};

main()
{
  int imsgID;
  struct msgstru slQMsg,slRcvMsg;
  int ilrc;
  char str[1024];


  imsgID=msgget(MSGKEY,IPC_EXCL );//检查消息队列是否存在
  if(imsgID < 0){
        printf("消息不存在! errno=%d [%s]\n",errno,strerror(errno));
        exit(-1);
    }

   ilrc = msgrcv(imsgID,&slRcvMsg,sizeof(struct msgstru),0,0);/*接收消息队列*/
   printf("text=[%s]\n",slRcvMsg.msgtext);

   msgctl(imsgID,IPC_RMID,0); //删除消息队列
   exit(0);
}



//send.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>


#define MSGKEY 1024

struct msgstru
{
   long msgtype;
   char msgtext[2048]; 

};

main()
{
  int imsgID;
  struct msgstru slQMsg,slRcvMsg;
  int ilrc;
  char str[2048];

  printf("请输入字符串:");
  scanf ("%s",str);

  imsgID=msgget(MSGKEY,IPC_EXCL );  //检查消息队列是否存在
  if(imsgID < 0){
    imsgID=msgget(MSGKEY,IPC_CREAT|0666);//创建消息队列
    if(imsgID <0 ){
	printf("创建消息失败! errno=%d [%s]\n",errno,strerror(errno));
	exit(-1);
    }
  }  

  slQMsg.msgtype = 100;
  strcpy(slQMsg.msgtext, str);
  ilrc = msgsnd(imsgID,&slQMsg,sizeof(struct msgstru),IPC_NOWAIT); //发送消息队列
  if ( ilrc < 0 ) {
       printf("msgsnd()写消息队列失败,errno=%d[%s]\n",errno,strerror(errno));
       exit(-1);

  }
  exit(0);

}



本程序实例介绍linux环境下利用消息队列进行进程间通信,主要介绍ftok函数,msgget创建消息队列和获取已存在消息队列,
msgctl获取消息队列结构信息,msgsnd发送消息到消息队列,msgrcv从消息队列中获取消息。

本程序由两个.c文件组成,msgQueueRecv.c和msgQueueSend.c,msgQueueRecv.c里面创建消息队列并阻塞式从消息队列接收消息,
最后删除消息队列(消息队列在内核创建);msgQueueSend.c获取以创建的消息队列,向消息队列发送消息。

请参考《unix环境高级编程》辅助学习。

//msgQueueRecv.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define BUFF_LEN 1024
#define RET_ERROR 1
#define RET_OK 0

int main()
{
    char *pcPathName = "/";       	/* pathname for ftok */
    int iProjectId = 5; 		/* id for ftok */
    key_t kId; 				/* key id */
    struct msqid_ds stMsgInfo; 		/* msg queue info structure */
    struct msgRecvStructure
    {
        long lMsgType; 			/* msg type */
        char acMsgRecvBuff[BUFF_LEN]; 	/* send msg buffer */
    }stMsgRecv;
    int iRet;
    int iMsgId; 			/* msg queue id */
    int iFlag = IPC_CREAT | IPC_EXCL | 0666; 	/* msg queue creating flag */
    
    /* build key using ftok function */
    kId = ftok(pcPathName, iProjectId);
    if ( kId == -1 )
    {
        printf("building key error, ftok() error!\n");
        return RET_ERROR;
    }
    /* create msg queue */
    iMsgId = msgget(kId, iFlag);
    if ( iMsgId == -1 )
    {
        printf("create msg queue error, msgget() error!\n");
        return RET_ERROR;
    }

    /* recv msg from msg queue */
    iRet = msgrcv(iMsgId, &stMsgRecv, BUFF_LEN, 1, 0);
    if ( iRet == -1 )
    {
        printf("msg recv error, msgrcv() error!\n");
        msgctl(iMsgId, IPC_RMID, &stMsgInfo); 	/* delete msg queue from kernel */
        return RET_ERROR;
    }
    else
    {
        printf("msg recv content: %s\n", stMsgRecv.acMsgRecvBuff);
    }

    msgctl(iMsgId, IPC_RMID, &stMsgInfo); 	/* delete msg queue from kernel */
    
    return RET_OK;
}



msgQueueSend.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define BUFF_LEN 1024
#define RET_ERROR 1
#define RET_OK 0

int main()
{
    char *pcPathName = "/"; 	/* pathname for ftok */
    int iProjectId = 5; 	/* id for ftok */
    key_t kId; 			/* key id */
    struct msqid_ds stMsgInfo; 	/* msg queue info structure */
    struct msgSendStructure
    {
        long lMsgType; 			/* msg type */
        char acMsgSendBuff[BUFF_LEN]; 	/* send msg buffer */
    }stMsgSend;
    int iRet;
    int iMsgId; 			/* msg queue id */
    int iFlag = 0666; 			/* msg queue creating flag */
    
    /* build key using ftok function */
    kId = ftok(pcPathName, iProjectId);
    if ( kId == -1 )
    {
        printf("building key error, ftok() error!\n");
        return RET_ERROR;
    }
    /* get existing msg queue */
    iMsgId = msgget(kId, iFlag);
    if ( iMsgId == -1 )
    {
        printf("get msg queue error, msgget() error!\n");
        return RET_ERROR;
    }

    /* send msg to msg queue */
    stMsgSend.lMsgType = 1;
    strcpy(stMsgSend.acMsgSendBuff, "hello,world");
    iRet = msgsnd(iMsgId, &stMsgSend, strlen("hello,world")+1, IPC_NOWAIT);
    if ( iRet == -1 )
    {
        printf("msg send error, msgsnd() error!\n");
        return RET_ERROR;
    }
    
    return RET_OK;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics