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

📄 msg_ctrl.c

📁 消息队列控制函数
💻 C
字号:


#include "UNIXMSG.H" 

MsgQueue MsgQ; 

/* ------------------------------------------------------------------------- */ 
int  openmsg(long MsgKey) 
{ 
  int  MsgId; 

  if((MsgId = msgget(MsgKey, MSGPERMS | IPC_CREAT)) < 0) 
    printf("Message error: creat message!\n"); 
  return MsgId; 
} 

/* ------------------------------------------------------------------------- */ 
int  msgq_rmid(int MsgId) 
{ 
#if __hpux                                       /* HP 9000 */       \ 
    | (i386 & M_I386 & unix & __unix & M_UNIX & (_SCO_COFF | _SCO_ELF)) 
                                                 /* SCO UNIX */ 
  struct msqid_ds buf; 

  return msgctl(MsgId, IPC_RMID, &buf); 
#else 
  return msgctl(MsgId, IPC_RMID); 
#endif 
} 

/* ------------------------------------------------------------------------- */ 
int  rcvmsg_nowait(long MsgKey, USGC *Buff, long *MsgType) 
{ 
  int  MsgId, MsgLen; 

  MsgId = openmsg(MsgKey); 
  MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, IPC_NOWAIT); 
  if(MsgLen < 0) return MsgLen; 
  *MsgType = MsgQ.type; 
  memcpy(Buff, MsgQ.buf, MsgLen); 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  rcvmsg_type_nowait(long MsgKey, USGC *Buff, long MsgType) 
{ 
  int  MsgId, MsgLen; 

  MsgId = openmsg(MsgKey); 
  MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, MsgType, IPC_NOWAIT); 
  if(MsgLen < 0) return MsgLen;        /* -1: no message received */ 
  memcpy(Buff, MsgQ.buf, MsgLen); 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  rcvmsg_wait(long MsgKey, USGC *Buff) 
{ 
  int  MsgId, MsgLen; 

  MsgId = openmsg(MsgKey); 
  MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, MSG_NOERROR); 
  if(MsgLen < 0) 
    printf("Message error: recive message!\n"); 
  memcpy(Buff, MsgQ.buf, MsgLen); 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  rcvmsg_type(long MsgKey, USGC *Buff, long *MsgType) 
{ 
  int  MsgId, MsgLen; 

  MsgId = openmsg(MsgKey); 
  MsgLen = msgrcv(MsgId, &MsgQ, MAX_MSGQ_SIZE, 0, MSG_NOERROR); 
  if(MsgLen < 0) 
    printf("Message error: recive message!\n"); 
  *MsgType = MsgQ.type; 
  memcpy(Buff, MsgQ.buf, MsgLen); 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  sndmsg(long MsgKey, USGC *Buff, int MsgLen) 
{ 
  int  MsgId, rc; 

  if(MsgLen < 0) return -1; 
  MsgId = openmsg(MsgKey); 
  memcpy(MsgQ.buf, Buff, MsgLen); 
  MsgQ.type = 1; 
  rc = msgsnd(MsgId, &MsgQ, MsgLen, 0); 
  if(rc < 0) return rc; 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  sndmsg_type(long MsgKey, USGC *Buff, long MsgType, int MsgLen) 
{ 
  int  MsgId, rc; 

  if(MsgLen < 0) return -1; 
  MsgId = openmsg(MsgKey); 
  memcpy(MsgQ.buf, Buff, MsgLen); 
  MsgQ.type = MsgType; 
  rc = msgsnd(MsgId, &MsgQ, MsgLen, 0); 
  if(rc < 0) return rc; 
  return MsgLen; 
} 

/* ------------------------------------------------------------------------- */ 
int  delmsg(long MsgKey) 
{ 
  int  MsgId; 

  /* 
  if((MsgId = msgget(MsgKey, MSGPERMS)) < 0 || 
      msgq_rmid(MsgId) < 0) 
  { 
    printf("Message error: no this message queue or remove it error!\n"); 
    return -1; 
  } 
  */ 
  if((MsgId = msgget(MsgKey, MSGPERMS)) < 0) 
  { 
    printf("Message error: no this message queue!\n"); 
    return MsgId; 
  } 
  if(msgq_rmid(MsgId) < 0) 
  { 
    printf("Message error: remove msgq!\n"); 
    return -1; 
  } 
  return MsgId; 
} 

/* End of file */ 

⌨️ 快捷键说明

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