包装的几个 MessageQ 的函数

本文提供了MessageQ消息队列的编程示例,包括消息队列的附着、分离、读写等核心操作函数,并介绍了如何根据程序PID进行消息的定向发送与接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于MessageQ的编程资料网上比较少,bea提供的文档里有比较详细的说明。多读几次MessageQ的文档就能写出程序来。下面是几个MessageQ的操作函数:

#include <stdio.h>
#include 
<stdlib.h>
#include 
<string.h>
#include 
"p_entry.h"
#include 
"p_return.h"
#include 
"p_symbol.h"
#include 
<unistd.h>
#include 
<sys/types.h>
ExpandedBlockStart.gifContractedBlock.gif
/**//*attach到消息队列里去,qid为消息队列的id*/
int qattach(short qid)
ExpandedBlockStart.gifContractedBlock.gif
{
   q_address   q_addr;
   int32       attach_mode;
   int32       dmq_status;
   int32       q_num_len;
   int32       q_type;
   
char           q_num_str[4];
   
int           rc;
     attach_mode 
= PSYM_ATTACH_BY_NUMBER;
     q_type      
= PSYM_ATTACH_PQ;
     memset(q_num_str, 
0x004);
     (
void)sprintf( q_num_str, "%d", qid );
     q_num_len   
= (int32) strlen( q_num_str );
     dmq_status 
= pams_attach_q( &attach_mode, &q_addr, &q_type,
                       q_num_str, 
&q_num_len,
                       (int32 
*0,
                       (int32 
*0,
                       (int32 
*0,
                       (
char *0,
                       (
char *0);
     
if(dmq_status==PAMS__SUCCESS)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
            
return(0);
     }

     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
{
           
return ( dmq_status );
   }

}

ExpandedBlockStart.gifContractedBlock.gif
/**//*从消息队列里detach*/
int32 qdetach( )
ExpandedBlockStart.gifContractedBlock.gif
{
    int32  dmq_status;
    dmq_status 
= pams_exit( );
    
if(dmq_status==PAMS__SUCCESS)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return(0);
    }

    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
           
return ( dmq_status );
  }

}

ExpandedBlockStart.gifContractedBlock.gif
/**//*从消息队列读取消息,msg_area为消息内容,msg_size为读取长度,qid为队列编号*/
int32 qread(
char *msg_area, short *msg_size, short *qid)
ExpandedBlockStart.gifContractedBlock.gif
{

   
char        priority;
   q_address   msg_source;
   
short       msg_class;
   
short       msg_type;
   
short       msg_area_len;
   
short       msg_data_len;
   int32       timeout;
   int32       dmq_status;
   
char           *qgroup, *getenv( );
   
int           i;
ExpandedSubBlockStart.gifContractedSubBlock.gif   priority       
= 0;                 /**//*  get all messages           */
ExpandedSubBlockStart.gifContractedSubBlock.gif   msg_area_len   
= *msg_size;         /**//*  size of the message buffer */
   timeout      
= 0;
   dmq_status 
= pams_get_msgw(
ExpandedSubBlockStart.gifContractedSubBlock.gif           msg_area,       
/**//*  where msg is written   */
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&priority,      /**//*  get all messages       */
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&msg_source,    /**//*  q_address of sender    */
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&msg_class,     /**//*  class of received msg  */
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&msg_type,      /**//*  type of received msg   */
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&msg_area_len,  /**//*  size of message buffer */
ExpandedSubBlockStart.gifContractedSubBlock.gif           msg_size,  
/**//*  actual size of the msg */
           
&timeout,
           (int32 
*0,
           (
struct PSB *0,
           (
struct show_buffer *0,
           (int32 
*0,
           (int32 
*0,
           (int32 
*0,
           (
char *0 );

   
if ( dmq_status == PAMS__SUCCESS )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
        
return0 );
   }

   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
           
return ( dmq_status );
   }


}

ExpandedBlockStart.gifContractedBlock.gif
/**//*往消息队列里发送消息,msg_area为消息内容,msg_size为消息长度,qdest为消息队列编号*/
int32 qwrite(
char *msg_area, short msg_size, short qdest)
ExpandedBlockStart.gifContractedBlock.gif
{
   
char        priority;
   
char        delivery;
   
char        uma;
   
short       msg_class;
   
short       msg_type;
   int32       dmq_status;
   int32       timeout;
   
struct PSB  put_psb;
   
char           *qgroup, *getenv( );
   q_address   qdest_addr;
   
int         i;

ExpandedSubBlockStart.gifContractedSubBlock.gif   priority    
= 0;                 /**//* No priority                          */
ExpandedSubBlockStart.gifContractedSubBlock.gif   msg_class   
= msg_type    = 0;   /**//* No type or class at the moment       */
ExpandedSubBlockStart.gifContractedSubBlock.gif   delivery    
= PDEL_MODE_NN_MEM;  /**//* No Notification and nonrecoverable   */
ExpandedSubBlockStart.gifContractedSubBlock.gif   timeout     
= 100;               /**//* Wait 10 seconds before giving up     */
ExpandedSubBlockStart.gifContractedSubBlock.gif   uma         
= PDEL_UMA_DISCL;    /**//* If can't deliver it, DISCard and Log */
   qgroup
=getenv("DMQ_GROUP_ID");
   qdest_addr.au.queue
=(int16)qdest;
   qdest_addr.au.group
=(int16)atoi(qgroup);

   dmq_status 
= pams_put_msg(
       msg_area,
           
&priority,
ExpandedSubBlockStart.gifContractedSubBlock.gif           
&qdest_addr,        /**//* passed in */
           
&msg_class,
           
&msg_type,
           
&delivery,
           
&msg_size,
           
&timeout,
           
&put_psb,
           
&uma,
           (q_address 
*0,
           (int32 
*0,
           (
char *0,
           (
char *0 );


   
if ( dmq_status == PAMS__SUCCESS )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
        
return0 );
   }

   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
{
           
return ( dmq_status );
   }

}

 编译:
[root@localhost src]# cc -o ../bin/test -I  $BEADIR/include test.c  -L $BEADIR/lib -ldmq
在同一个消息队列中,你也可以根据程序的pid来发送和获得只属于本应用程序的消息。

 

转载于:https://www.cnblogs.com/tingsking/archive/2008/08/14/1267906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值