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

    消息队列可以实现两个没有关系的进程之间的通信。

    创建了一个消息队列后,进程可以往里面放消息,也可以取消息。因为这个消息队列是有名字的,所以就算是两个没有关系的进程,也能通信。

    而且人性化的一点是,可以自己定义消息的结构体。

    几个重要的函数:

    //创建消息队列,如果存在就直接获取
    int msgget(key_t, key, int msgflg);
    //第一个参数是key整形,用于区分不同的队列
    //返回key命名的队列id,唯一标识一个队列
    
    
    
    //发送消息
    int msgsend(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg); 
    //msgid是标识队列的id
    //msg_ptr是自己定义的消息结构体,注意结构体里面是长整型开始
    
    //所以要定义成这样
    struct my_message{  
        long int message_type;  
        /* The data you wish to transfer*/  
    }; 
    
    
    //获取消息
    int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);  
    //参数跟上面一样

    效果图:

    Server的代码:(用于取消息)

    #include <unistd.h>  
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <string.h>  
    #include <errno.h>  
    #include <sys/msg.h> 
    
    struct msg_st  
    {  
        long int msg_type;  
        char text[256];  
    };  
    
    
    int main(){
    
        int msgid = -1;  
    
          struct msg_st data;  
    
        long int msgtype = 0;
    
        msgid = msgget((key_t)1234,IPC_CREAT);
    
        if(msgid == -1){ 
    
            printf("msgget failed
    ");  
    
            exit(EXIT_FAILURE);  
    
            } 
    
        printf("Server start!
    ");
    
        while(1){
    
            
    
            if(msgrcv(msgid, (void*)&data, 256, msgtype, 0) == -1){
                        printf("msgrcv failed
    ");  
                        exit(EXIT_FAILURE);  
                }//end if
    
            
            //success
            printf("from client:%s
    ",data.text);
    
    
        }//end while
    
    
    
    
    
    
    
    
    }

    Client.c的代码:(用于发消息)

    #include <unistd.h>  
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <string.h>  
    #include <errno.h>  
    #include <sys/msg.h>
    
    struct msg_st  
    
    {  
    
        long int msg_type;  
    
        char text[256];  
    
    };  
    
    
    int main(){
    
        struct msg_st data;
        int msgid = -1;
        char buffer[256];
    
        memset(buffer,'',256);
    
        msgid = msgget((key_t)1234,IPC_CREAT); 
    
        if(msgid == -1){
            printf("msgget failed
    ");  
                exit(EXIT_FAILURE);  
            }  
    
        while(1){
    
            printf("Enter a string
    ");
            fgets(buffer,256,stdin);
            strcpy(data.text,buffer);
            data.msg_type=1;
            
            msgsnd(msgid, (void*)&data,256, 0);
    
    
    
    
    
    
        }
    
    
    
    
    
    
    }
  • 相关阅读:
    7牛管理凭证生成错误
    安卓截屏如何实现将摄像头显示画面截下来
    realm怎样支持hashmap
    Cordova Android项目如何做代码混淆
    cnmp安装失败,报错npm ERR! enoent ENOENT: no such file or directory,
    iOS中关于字符 “&”的作用?
    float 保留两位小数
    关于iOS声音识别的框架
    iOS崩溃日志
    QT分析之WebKit
  • 原文地址:https://www.cnblogs.com/wzben/p/5436721.html
Copyright © 2011-2022 走看看