• 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;
    }
    
  • 相关阅读:
    jQuery 遍历函数 ,javascript中的each遍历
    定时器:右下角滑动信息通知
    nopad++将制表符替换为换行符
    使用git提交远程仓库
    (转)解决windows解决windows 7 部分程序图标显示不正常的问题
    设置gvim的字体大小
    mysql乱码
    (转)notepad++去重
    查看linux硬件的信息
    虚拟机安装centos6.5出现Error processing drive:pci-0000:00:10-scsi-0:0:0:0问题
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384208.html
Copyright © 2020-2023  润新知