zoukankan      html  css  js  c++  java
  • 进程通信之消息队列

    消息队列就是一些消息的列表。用户可以在消息队列中添加消息和读取消息等。消息存在于内核中,有“队列ID”来标识

     

    msgget函数语法:

    msgsnd函数语法:

    msgrcv函数语法:

    msgctl函数语法:

     添加消息代码:

    /* msgsnd.c 添加消息*/
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define  BUFFER_SIZE        512
    
    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;
        
        /*根据不同的路径和关键字产生标准的key*/
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /*创建消息队列*/
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d
    ",qid);
        while(1)
        {
            printf("Enter some message to the queue:");
            if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
            {
                puts("no message");
                exit(1);
            }
            
            msg.msg_type = getpid();
            /*添加消息到消息队列*/
            if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
            {
                perror("message posted");
                exit(1);
            }
            if (strncmp(msg.msg_text, "quit", 4) == 0)
            {
                break;
            }
        }
        exit(0);
    }

    读取消息的程序代码:

    /* msgrcv.c 读取消息*/
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define  BUFFER_SIZE        512
    
    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;
        
        /*根据不同的路径和关键字产生标准的key*/
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /*创建消息队列*/
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d
    ", qid);
        do
        {
            /*读取消息队列*/
            memset(msg.msg_text, 0, BUFFER_SIZE);
            if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0)
            {
                perror("msgrcv");
                exit(1);
            }
            printf("The message from process %d : %s", 
    msg.msg_type, msg.msg_text);        
            
        } while(strncmp(msg.msg_text, "quit", 4));
    /*从系统内核中移走消息队列 */
        if ((msgctl(qid, IPC_RMID, NULL)) < 0)
        {
            perror("msgctl");
            exit(1);
        }    
        return 0;
    }
  • 相关阅读:
    RxJava开发精要3-向响应式世界问好
    RxJava开发精要2-为什么是Observables?
    RxJava开发精要1-从.NET到RxJava
    为你的应用加速
    Android最佳性能实践(二)——分析内存的使用情况
    Android最佳性能实践(一)——合理管理内存
    Android 性能优化之使用MAT分析内存泄露问题
    给 Android 开发者的 RxJava 详解
    优化 Android 线程和后台任务开发
    资深谷歌安卓工程师对安卓应用开发的建议
  • 原文地址:https://www.cnblogs.com/yihujiu/p/5598113.html
Copyright © 2011-2022 走看看