⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pub_msgfun.c

📁 自已结累的Unix下C语言开发函数库
💻 C
字号:
/* *消息对列的一些函数 *zyq - shilyu - cff 0901 *functionnum 009 */ #include "./../inc/pub.h"#if !defined( DEBUG )#define DEBUG#endif/* 001 *产生新的关键字 * -1 错误  > 0 成功 */key_t GetKey( char *pathname, char proj ) {	key_t mykey ;	if( ( mykey = ftok( pathname, proj ) ) == -1 )	   return ERROR ;	   	return mykey ;}/* 002 *  函数名称  : CrtMsgQ ( MsgQName )                                           *  功能      : 建立一个消息队列(若对列存在先删除再建立)                                          *  参数(输入):                                                            *    long  MsgQName : 消息队列的名字                                      *  返回      : 消息队列的标识符                                           */                                              int  CrtMsgQ ( MsgQName )long MsgQName  ;{   int  pMsgQid   ;   pMsgQid = msgget ( (key_t)MsgQName , 0666|IPC_CREAT ) ;   msgctl ( (int)pMsgQid , (int)IPC_RMID , NULL ) ;   pMsgQid = 0 ;   pMsgQid = msgget ( (key_t)MsgQName , 0666|IPC_CREAT ) ;   if ( pMsgQid < 0 )       return ERROR    ;   else       return pMsgQid  ;}/* 003*  函数名称  : CrtMsgQ_C( MsgQName )                                     *  功能      : 建立一个消息队列(若存在获得不建立 不存在建立)                              *  参数(输入):                                                           *    long  MsgQName : 消息队列的名字                                     *  返回      : 消息队列的标实符                                          */int  CrtMsgQ_C ( MsgQName )long MsgQName  ;{int  pMsgQid   ;    pMsgQid = msgget ( (key_t)MsgQName , 0666|IPC_CREAT ) ;    if ( pMsgQid < 0 )        return ERROR    ;    else        return pMsgQid  ;}/* 004 * 删除存在的消息对列 * 0 成功 -1 错误 */ int RemoveQ( int qid ){ 	if( msgctl( qid, IPC_RMID, 0 ) == -1 )      return ERROR ; 	      return SUCCESS ;}/* 005 * 获得消息对列有关属性的数据 * 0 成功 -1 错误 */int GetQ_Ds( int qid, struct msqid_ds *qbuf ){	if( msgctl( qid, IPC_STAT, qbuf ) == -1 )	   return ERROR ;	   return SUCCESS ;}/* 006 * 改变消息对列有关属性的数据 * 0 成功 -1 错误 */ int ChangeQ_Mode( int qid, char *mode ) {   struct msqid_ds tmpbuf ;      GetQ_Ds( qid, &tmpbuf ) ;      sscanf( mode, "%o", tmpbuf.msg_perm.mode ) ;      if( msgctl( qid, IPC_SET, &tmpbuf ) == -1 )      return ERROR ;		return SUCCESS ;} /* 007 *从消息对列中读出最旧的一条记录数据 * 0 成功 -1 错误 */ int   MsgRcv ( MsgQid , MsgContent , MsgKey , MsgFlag , MsgLen )int   MsgQid       ;  /*消息对列标识符          */char  *MsgContent  ;  /*返回接收的字符串        */long  *MsgKey      ;  /*返回对列中所取的消息类型*/int   MsgFlag      ;  /*读消息对列标志          */int   *MsgLen      ;  /*返回从对列中读出的长度  */{	 /* self define message buffer for msqsnd and msgrcv calls */    struct    {      long  pMsgKey                     ;      char  pMsgContent[ MAX_QUE_PACKSIZE ] ;    } Message                             ;    int   RcvLen                          ;    memset ( Message.pMsgContent , NULL , MAX_QUE_PACKSIZE ) ;    if( *MsgKey != 0 )        fprintf ( stderr , "%d" , *MsgKey ) ;    RcvLen = msgrcv ( (int)MsgQid , (void *)&Message , (unsigned long)MAX_QUE_PACKSIZE , (long)*MsgKey , (int)MsgFlag ) ;    if ( RcvLen <= 0 )         return ERROR  ;    else    {        memcpy ( MsgContent , Message.pMsgContent , RcvLen ) ;        *MsgKey  = Message.pMsgKey ;        *MsgLen = RcvLen           ;         return SUCCESS             ;    }} /* 008 *从消息对列中读出指定的消息类型的记录数据 * -1 错误 >0 返回读出的长度 */ int   MsgRcvC ( MsgQid , MsgContent , MsgKey , MsgFlag )int   MsgQid       ;char  *MsgContent  ;long  MsgKey       ;int   MsgFlag      ;{   struct   {     long  pMsgKey                     ;     char  pMsgContent[ MAX_QUE_PACKSIZE ] ;   } Message                             ;   int   RcvLen                          ;   memset ( Message.pMsgContent , NULL , MAX_QUE_PACKSIZE ) ;   if( MsgKey != 0 )       fprintf ( stderr , "%d" , MsgKey ) ;   RcvLen = msgrcv ( (int)MsgQid , (void *)&Message , (unsigned long)MAX_QUE_PACKSIZE , (long)MsgKey , (int)MsgFlag ) ;   if ( RcvLen <= 0 )        return ERROR  ;   else   {       memcpy ( MsgContent , Message.pMsgContent , RcvLen ) ;       return RcvLen ;   }}  /* 009 *  函数名称 : MsgSnd ( MsgQid , MsgContent , MsgKey , MsgLen )       *  功能     : 发送信息到消息队列                                     *  输入参数 :                                                        *   int  MsgQid       :   消息队列的标识符                           *   char * MsgContent :   要发送的消息字符串                         *   long MsgKey       :   对列关键字                                 *   int  MsgLen       :   消息的长度                                 *  返回值   :  0 成功 -1 发送错误               */int   MsgSnd ( MsgQid , MsgContent , MsgKey , MsgLen )int   MsgQid       ;char  *MsgContent  ;long  MsgKey       ;int   MsgLen       ;{   struct   {      long  pMsgKey                     ;      char  pMsgContent[ MAX_QUE_PACKSIZE ] ;   } Message                             ;   int   SndLen                          ;   Message.pMsgKey = MsgKey ;   memset ( Message.pMsgContent , NULL , MAX_QUE_PACKSIZE ) ;   memcpy ( Message.pMsgContent , MsgContent , MsgLen ) ;   SndLen = msgsnd ( (int)MsgQid , (const void *)&Message , (unsigned long)MsgLen , (int)IPC_NOWAIT ) ;   if ( SndLen < 0 )      return ERROR   ;   else      return SUCCESS ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -