Linux消息队列编程实例

/*
  创建消息队列msgget( )
  调整消息队列的参数msgctl(msgid,IPC_SET,struct msqid_ds* )
  发送一条消息msgsnd( )
  接受一条消息msgrcv( )
  移除一条消息msgctl( msgid,IPC_RMID,NULL )
 */
/*创建一个消息队列,并调整其大小,发送一条消息
  再取出该条消息,最后移除该消息队列
 */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MAX_LINE 80
#define MY_MQ_ID 1233
/*消息结构体的一般形式如下:
  typedef struct
  {
    long type;   //用于存放消息代码,必须位于首位
    char message[ LENGHT+1 ];
  }MSG_TYPE_T;
 */
typedef struct
{
    long type;  
    float fval;
    unsigned int uival;
    char strval[ MAX_LINE+1 ];
}MY_TYPE_T;
int main(  )
    {
        int msgid,ret;
        //create the message queue with the id MY_MQ_ID
        msgid=msgget( MY_MQ_ID,0666|IPC_CREAT );
        if( msgid>=0 )
             printf( "Created a Message Queue,message queue identifier is %d/n",msgid );
        //modify the size of message queue
        struct msqid_ds buf;
        ret=msgctl( msgid,IPC_STAT,&buf );
        printf( "The origianl size of queue is %d/n",buf.msg_qbytes );
        
        buf.msg_qbytes=4096;
        ret=msgctl( msgid,IPC_SET,&buf );
        if( ret==0 )
            printf( "Size sucessfully changed for queue,message queue identifier is %d/n",msgid );
        //send a message
        MY_TYPE_T myMessage;
        myMessage.type=1L;   //消息的类型,msgrcv会用到
        myMessage.fval=128.256;
        myMessage.uival=512;
        strncpy( myMessage.strval,"This is a test./n",MAX_LINE );
        ret=msgsnd( msgid,( struct msgbuf* )&myMessage,sizeof( MY_TYPE_T ),0 ); //0是消息旗标
        if( ret!=-1 )
            printf( "Message send successfully./n" );
        //read a message
        MY_TYPE_T recMessage;
        ret=msgrcv( msgid,( struct msgbuf* )&recMessage,sizeof(MY_TYPE_T),1,0 );//这个地方Message Type要和欲接受的消息类型相同
        if( ret!=-1 )
            {
                printf( "/nRead a message from the queue/n" );
                printf( "Message Type:%ld/n",recMessage.type );
                printf( "Float value:%f/n",recMessage.fval );
                printf( "Uint value:%d/n",recMessage.uival );
                printf( "String value:%s/n",recMessage.strval );
            }
        //destroy a message queue
        ret=msgctl( msgid,IPC_RMID,NULL );
        if( ret!=-1 )
            printf( "Message queue %d sucessfully removed./n",msgid );
        
        return 0;
    }
/*还有很多实际创建时的细节,可以通过man进行查找
  使用命令来查看IPC队列:
  ipcs -q
  ipcs -q -i $msgid
  ipcrm -q $msgid
 */




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值