Linux 下的消息队列(posix和system)

1.posix

        1.1 mqueue.h

#include 
#include 
#include 
#include 
#include 


    void set_timespec_api(int waitMs, struct timespec *ts);
    int mq_unlink_api(const char *pName);
    mqd_t mq_open_api(const char *pName, int oFlag, unsigned int mode, struct mq_attr *pAdddr);

    int mq_send_api(mqd_t msgId, const char *pMsg, unsigned int msgLen, int waitMs, unsigned int msgPrio);
    int mq_receive_api(mqd_t msgId, char *pMsg, unsigned int msgLen, int waitMs, unsigned int *msgPrio);
    int mq_close_api(mqd_t msgId);

        1.2 mqueue.c

        

#include "mqueue.h"


void set_timespec_api(int waitMs, struct timespec *ts)
{
  time_t sec = 0, t = 0, nsec = 0;
  if (0 == waitMs)
  {
    nsec = 0;
  }
  else
  {
    nsec = waitMs * 1000000;
  }
  t = time(NULL) + 1;
  if (-1 == clock_gettime(CLOCK_REALTIME, ts) || (int)(ts->tv_sec - t) > 30)
  {
    ts->tv_sec = t;
    ts->tv_nsec = 0;
  }
  nsec += ts->tv_nsec;
  if (nsec > 1000000000)
  {
    sec = nsec / 1000000000;
    nsec = nsec % 1000000000;
  }
  ts->tv_sec += sec;
  ts->tv_nsec += nsec;
  return;
}
int mq_unlink_api(const char *pName)
{
  int ret = mq_unlink(pName);
  return ret;
}
mqd_t mq_open_api(const char *pName, int oFlag, unsigned int mode, struct mq_attr *pAddr)
{
  if (!pAddr)
  {
    return -1;
  }
  mqd_t msgId;
  printf("%s,%d,%x\n",pName,oFlag,mode);
  msgId = mq_open(pName, oFlag, mode, pAddr);
  return msgId;
}

int mq_send_api(mqd_t msgId, const char *pMsg, unsigned int msgLen, int waitMs, unsigned int msgPrio)
{
  int ret = -1;
  struct timespec timeout;
  if (-1 == waitMs)
  {
    ret = mq_send(msgId, pMsg, msgLen, msgPrio);
  }
  else
  {
    set_timespec_api(waitMs, &timeout);
    ret = mq_timedsend(msgId, pMsg, msgLen, msgPrio, &timeout);
  }
  return ret;
}
int mq_receive_api(mqd_t msgId, char *pMsg, unsigned int msgLen, int waitMs, unsigned int *msgPrio)
{
  struct timespec timeout;
  int ret = -1;
  if (-1 == waitMs)
  {
    ret = mq_receive(msgId, pMsg, msgLen, msgPrio);
  }
  else
  {
    set_timespec_api(waitMs, &timeout);
    ret = mq_timedreceive(msgId, pMsg, msgLen, msgPrio, &timeout);
  }
  return ret;
}
int mq_close_api(mqd_t msgId)
{
  int ret = mq_close(msgId);
  return ret;
}

int mq_getaddr_api(mqd_t msgId, struct mq_attr *pAddr)
{
  struct mq_attr attr_;
  bzero(&attr_, sizeof(attr_));
  int ret = mq_getattr(msgId, &attr_);
  if (pAddr && 0 == ret)
  {
    pAddr->mq_curmsgs = attr_.mq_curmsgs;
    pAddr->mq_flags = attr_.mq_flags;
    pAddr->mq_maxmsg = attr_.mq_maxmsg;
    pAddr->mq_msgsize = attr_.mq_msgsize;
  }
  return ret;
}

        1.3 main.c

#include 
#include 
#include 
#include 
#include "mqueue.h"
#define testName "/msgqueue"  //posix 标准 要已经/开头
mqd_t testMq = -1;
void *threadFunc1(void *p)
{
    sleep(1);
    while(1)
    {
        int ret = mq_send_api(testMq,"hello",strlen("hello"),-1,0);
        sleep(2);
    }
    pthread_exit(0);
}
void *threadFunc2(void *p)
{
    struct mq_attr attr_ = {0};
    bzero(&attr_,sizeof(attr_));
    mq_unlink_api(testName);
     char buf[8192] = {0};  
    attr_.mq_maxmsg = 10;  //最大10
    attr_.mq_msgsize = 8192; //最大8192

    
    testMq = mq_open_api(testName,O_CREAT|O_RDWR|O_EXCL,0666,&attr_);
    if(-1 == testMq)
    {
        printf("mq open fail testMq = %d,errno = %d,strerror = %s\n",testMq,errno,strerror(errno));
        return (void*)-1;
    }
    while(1)
    {
        int ret = mq_receive_api(testMq,buf,sizeof(buf),-1,NULL);//sizeof(buf)这个参数要大于等于 msgsize
        printf("buf = %s\n",buf);
        sleep(1);
    }
    pthread_exit(0);
}

int main()
{
    

    pthread_t thid1,thid2;
    pthread_create(&thid1,NULL,threadFunc1,NULL);
    pthread_create(&thid2,NULL,threadFunc2,NULL);

    long ret = 0;
    pthread_join(thid1,(void**)&ret);
    pthread_join(thid2,(void**)&ret);


    
}

2.system V

你可能感兴趣的:(c语言编程,linux,c++,c语言)