进程通信(3) ----- 消息队列

文章目录

  • 一、实验目的
  • 二、实验内容
  • 三、实验要求
  • 四、实验步骤及操作
  • 五、程序源码
    • 消息队列 queue.c


一、实验目的

1.了解进程通信中的消息队列
2.掌握进程通信中的消息队列编程模型

二、实验内容

消息队列就是消息的链表,一个消息队列允许一个或多个进程向它写消息或者读消息。本次实验的内容是:
1.创建和打开消息队列
2.向队列中写入消息
3.读取队列中的消息
4.删除消息队列

三、实验要求

实验前需要提前准备一台PC机,并在PC机上搭建Linux Ubuntu环境。

四、实验步骤及操作

1.打开Linux系统,新建一个终端
2.新建文件
3.编写代码
4.编译运行

五、程序源码

消息队列 queue.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define BUFSZ 512

/* Message structure */
struct msg {
    long msg_type;
    char msg_text[BUFSZ];
};

int main(int argc, char *argv[])
{
    int qid; /* The queue identifier */
    key_t key; /* The queue key */
    int len; /* Length of data sent */
    int len1; /* Length of message */
    struct msg pmsg; /* Pointer to message structure 指向消息结构的指针*/

    key = 80;

    /* Create the queue */
    printf("\nCreat Queue:\n");
    if((qid = msgget(key, IPC_CREAT | 0666)) < 0) {
        perror("msgget:create");
        exit(EXIT_FAILURE);
    }
    printf("\tcreated queue id = %d\n", qid);
     
    /* Open the queue again */
    if((qid == msgget(key, 0)) < 0) {
        perror("msgget:open");
        exit(EXIT_FAILURE);
    }
    printf("\topened queue id = %d\n", qid);
    printf("Creat Success!\n\n");
    
    puts("Send a message to queue:");
    if((fgets((&pmsg)->msg_text, BUFSZ, stdin)) == NULL) {
        puts("no message to post");
        exit(EXIT_SUCCESS);
    }
    /* Associate the message with this process 将消息与此进程关联*/
    pmsg.msg_type = getpid();
    /* Add the message to the queue */
    len = strlen(pmsg.msg_text);
    if((msgsnd(qid, &pmsg, len, 0)) < 0) {
        perror("msgsnd");
        exit(EXIT_FAILURE);
    }
    puts("Send Success!");  //消息发布
    
    printf("\nRead all message from queue:\n");
    /* Retrieve and display a message from the queue 从队列检索并显示一条消息*/
    len1 = msgrcv(qid, &pmsg, BUFSZ, 0, 0);
    if(len1 > 0) {
        (&pmsg)->msg_text[len1] = '\0';
        printf("\treading queue id: %05d\n", qid);
        printf("\tmessage type: %05ld\n", (&pmsg)->msg_type);
		printf("\tmessage length: %d bytes\n", len1); 
        printf("\tmessage text: %s", (&pmsg)->msg_text);
    } else {
        perror("msgrcv");
        exit(EXIT_FAILURE);
    }
    puts("Read Success!");
    
    printf("\nRemove Queue:\n");
    if((msgctl(qid, IPC_RMID, NULL)) < 0) {
        perror("msgctl");
        exit(EXIT_FAILURE);
    }
    printf("\tqueue %d removed Success!\n", qid);
    
    exit(EXIT_SUCCESS);
}

进程通信(3) ----- 消息队列_第1张图片

你可能感兴趣的:(嵌入式Linux,C应用编程,物联网,ubuntu,linux,c语言,网络)