c/c++ linux 进程间通信系列6,使用消息队列(message queue)

linux 进程间通信系列6,使用消息队列(message queue)

概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了。

1,创建消息队列(message queue)

2,写消息到消息队列(message queue)

3,从消息队列(message queue)读消息

3,删除消息队列(message queue)

1,创建消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int main(){
  int msgid;

  msgid = msgget(IPC_PRIVATE, 0600);
  if(msgid < 0){
    perror("msgget");
    return 1;
  }

  printf("%d
", msgid);
  return 0;
}

github源代码

用下面的命令,能够查看到上面的程序创建的共享内存。

ipcs -q

执行后的结果:

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x00000000 32768      ys         600        0            0      

2,写消息到消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>

#define MTEXTSIZE 10

int main(int argc, char* argv[]){
  int msgid;
  struct msgbuf{
    long mtype;
    char mtext[MTEXTSIZE];
  }mbuf;

  if(argc != 2){
    printf("wrong argc");
    return 1;
  }

  msgid = atoi(argv[1]);

  mbuf.mtype = 777;
  memset(mbuf.mtext, 0, sizeof(mbuf.mtext));
  mbuf.mtext[0] = 'A';

  if(msgsnd(msgid, &mbuf, MTEXTSIZE, 0) != 0){
    perror("msgsnd");
    return 1;
  }

  return 0;
}

github源代码

执行方法:【ipcs -q】执行后,得到下面的数字。

./a.out 789884

执行后:ipcs -q 发现, message下面的数字从0变为1了,说明消息队列里有了一个消息。

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x00000000 32768      ys         600        10           1  

3,从消息队列(message queue)读消息

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>

#define MTEXTSIZE 10

int main(int argc, char* argv[]){
  int msgid, msgtype;
  
  struct msgbuf{
    long mtype;
    char mtext[MTEXTSIZE];
  }mbuf;

  if(argc != 3){
    printf("wrong argc");
    return 1;
  }

  msgid = atoi(argv[1]);
  msgtype = atoi(argv[2]);

  if(msgrcv(msgid, &mbuf, MTEXTSIZE, msgtype, 0) <= 0){
    perror("msgrcv");
    return 1;
  }

  printf("%c
", mbuf.mtext[0]);
  return 0;
}

github源代码

执行方法:必须指定在写入消息是的type,也就是777

./a.out 32768 777

执行后,ipcs -q发现,message下面的数字,由1变为0了,说明消息队列里没有消息了。

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x00000000 32768      ys         600        0            0     

4,删除消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>

int main(int argc, char* argv[]){
  int msgid;
  msqid_ds mds;

  if(argc != 2){
    printf("wrong argv
");
    return 1;
  }

  msgid = atoi(argv[1]);

  if(msgctl(msgid, IPC_RMID, &mds) != 0){
    perror("msgctl");
    return 1;
  }

  return 0;
}

执行方法:

./a.out 32768

执行后,ipcs -q发现,消息队列本身都没有了。

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages 

用命令行删除共享内存:【ipcs -q】执行后,得到下面的数字。

ipcrm -q id

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9823131.html