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!\n");
        exit(1);
    }

    msg.msg_type = 0;   
    ret = msgrcv(msgid, &msg, MSG_SIZE, 0, 0);
    if (ret == -1) {
        printf("msgrcv failed!\n");
        exit(1);
    }

    printf("received: %s\n", msg.msg);

    ret = msgctl(msgid, IPC_RMID, 0);
    if (ret == -1) {
        printf("msgctl(IPC_RMID) failed!\n");
        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!\n");
        exit(1);
    }

    msg.msg_type = 1;   
    strcpy(msg.msg, "Hello world!");
    ret = msgsnd(msgid, &msg, MSG_SIZE, 0);
    if (ret == -1) {
        printf("msgsnd failed!\n");
        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!\n");
        exit(1);
    }

    while(1) {
        msg.msg_type = 0;   
        ret = msgrcv(msgid, &msg, MSG_SIZE, 0, 0);
        if (ret == -1) {
            printf("msgrcv failed!\n");
            exit(1);
        }

        printf("received: %s\n", msg.msg);

        if (strncmp(msg.msg, "exit", 4) == 0) {
            break;
        }
    }

    ret = msgctl(msgid, IPC_RMID, 0);
    if (ret == -1) {
        printf("msgctl(IPC_RMID) failed!\n");
        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!\n");
        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!\n");
            exit(1);
        }

        if (strncmp(msg.msg, "exit", 4) == 0) {
            break;
        }
    }

    return 0;
}
posted @ 2016-04-01 12:50  夜色下的港湾  Views(489)  Comments(0Edit  收藏  举报