Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4733555
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-07-01 20:23:19

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

本程序由两个.c文件组成,msgQueueRecv.c和msgQueueSend.c,msgQueueRecv.c里面创建消息队列并阻塞式从消息队列接收消息,最后删除消息队列(消息队列在内核创建);msgQueueSend.c获取以创建的消息队列,向消息队列发送消息。请参考《unix环境高级编程》辅助学习。

msgQueueRecv.c
#include
#include
#include
#include
#include
#include

#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
#include
#include
#include
#include
#include

#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;
}
阅读(1355) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

ubuntuer2010-07-07 17:16:28

msgsnd与msgrcv函数出现invalid参数的问题 1. 这里的lMsgType 不要设置为0,我就是因为设置为了debug了良久 stMsgSend.lMsgType = 1; 2. msgrcv出现段错误 msgrcv(iMsgId, &stMsgRecv, BUFF_LEN, 1, 0); 这里的BUFF_LEN设置的大点没关系,这是缓冲区的大小!! 今天这两个问题 折腾的我够郁闷的