zoukankan      html  css  js  c++  java
  • Linux _msg 消息队列 demo

    main1/msg_rcv.c

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MSG_SIZE 80
    
    struct my_msg_st {
        long int msg_type;
        char msg[MSG_SIZE];
    };
    
    int main(void)
    {
        int msgid;
        int ret;
        struct my_msg_st msg;
    
        msgid = msgget((key_t)1235, 0666|IPC_CREAT);
        if (msgid == -1) {
            printf("msgget failed!
    ");
            exit(1);
        }
    
        msg.msg_type = 0;   
        ret = msgrcv(msgid, &msg, MSG_SIZE, 0, 0);
        if (ret == -1) {
            printf("msgrcv failed!
    ");
            exit(1);
        }
    
        printf("received: %s
    ", msg.msg);
    
        ret = msgctl(msgid, IPC_RMID, 0);
        if (ret == -1) {
            printf("msgctl(IPC_RMID) failed!
    ");
            exit(1);
        }
    
        return 0;
    }
    
    

    main1.msg_sed.c

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MSG_SIZE 80
    
    struct my_msg_st {
        long int msg_type;
        char msg[MSG_SIZE];
    };
    
    int main(void)
    {
        int msgid;
        int ret;
        struct my_msg_st msg;
    
        msgid = msgget((key_t)1235, 0666|IPC_CREAT);
        if (msgid == -1) {
            printf("msgget failed!
    ");
            exit(1);
        }
    
        msg.msg_type = 1;   
        strcpy(msg.msg, "Hello world!");
        ret = msgsnd(msgid, &msg, MSG_SIZE, 0);
        if (ret == -1) {
            printf("msgsnd failed!
    ");
            exit(1);
        }
    
        return 0;
    }
    
    

    main2/msg_rcv.c

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MSG_SIZE 80
    
    struct my_msg_st {
        long int msg_type;
        char msg[MSG_SIZE];
    };
    
    int main(void)
    {
        int msgid;
        int ret;
        struct my_msg_st msg;
    
        msgid = msgget((key_t)1235, 0666|IPC_CREAT);
        if (msgid == -1) {
            printf("msgget failed!
    ");
            exit(1);
        }
    
        while(1) {
            msg.msg_type = 0;   
            ret = msgrcv(msgid, &msg, MSG_SIZE, 0, 0);
            if (ret == -1) {
                printf("msgrcv failed!
    ");
                exit(1);
            }
    
            printf("received: %s
    ", msg.msg);
    
            if (strncmp(msg.msg, "exit", 4) == 0) {
                break;
            }
        }
    
        ret = msgctl(msgid, IPC_RMID, 0);
        if (ret == -1) {
            printf("msgctl(IPC_RMID) failed!
    ");
            exit(1);
        }
    
        return 0;
    }
    
    

    main2/msg_snd.c

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MSG_SIZE 80
    
    struct my_msg_st {
        long int msg_type;
        char msg[MSG_SIZE];
    };
    
    int main(void)
    {
        int msgid;
        int ret;
        struct my_msg_st msg;
    
        msgid = msgget((key_t)1235, 0666|IPC_CREAT);
        if (msgid == -1) {
            printf("msgget failed!
    ");
            exit(1);
        }
    
        while(1) {
            fgets(msg.msg, sizeof(msg.msg), stdin);
    
            msg.msg_type = 1;   
            ret = msgsnd(msgid, &msg, MSG_SIZE, 0);
            if (ret == -1) {
                printf("msgsnd failed!
    ");
                exit(1);
            }
    
            if (strncmp(msg.msg, "exit", 4) == 0) {
                break;
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
    yolov3的anchor机制与损失函数详解
    CV资料推荐
    测试用例设计方法总结
    测试需求分析
    bug生命周期
    linux命令一
    linux 命令二
    linux 命令三
    mysql数据库和禅道安装
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384208.html
Copyright © 2011-2022 走看看