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
 */

msgsnd()
       The  msgsnd()  system call appends a copy of the message pointed to by msgp
       to the message queue whose identifier is specified by msqid.

       If sufficient space is available in the queue,  msgsnd()  succeeds  immedi‐
       ately.  (The queue capacity is defined by the msg_qbytes field in the asso‐
       ciated data structure for the message queue.  During  queue  creation  this
       field  is initialized to MSGMNB bytes, but this limit can be modified using
       msgctl(2).)  If insufficient space is available  in  the  queue,  then  the
       default behavior of msgsnd() is to block until space becomes available.  If
       IPC_NOWAIT is specified in msgflg, then the call  instead  fails  with  the
       error EAGAIN.

 msgrcv()
       The msgrcv() system call removes a message  from  the  queue  specified  by
       msqid and places it in the buffer pointed to by msgp.

       The argument msgsz specifies the maximum size in bytes for the member mtext
       of the structure pointed to by the msgp argument.  If the message text  has
       length greater than msgsz, then the behavior depends on whether MSG_NOERROR
       is specified in msgflg.  If MSG_NOERROR is specified, then the message text
       will  be truncated (and the truncated part will be lost); if MSG_NOERROR is
       not specified, then the message isn't removed from the queue and the system
       call fails returning -1 with errno set to E2BIG.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值