消息队列实现多进程通信

实现A进程和B进程互相收发消息

client_a.c

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

typedef struct {
    long mtype;
    char mtext[256];
} msg_t;

#define MSG_SIZE ((sizeof(msg_t) - sizeof(long)))
#define MSG_TYPE 200

int msg_queue_id;
int p_pid;
int c_pid;

void *p_task(void *argv) {
    while (1) {
        msg_t msg;
        msgrcv(msg_queue_id, &msg, MSG_SIZE, 100, 0);
        printf("receive msg = %s\n", msg.mtext);
        if (strncmp(msg.mtext, "quit", 4) == 0) {
            kill(c_pid, SIGKILL);
            break;
        }
    }
}

void *c_task(void *argv) {
    while (1) {
        msg_t msg;
        msg.mtype = MSG_TYPE;
        fgets(msg.mtext, sizeof(msg.mtext), stdin);
        msg.mtext[strlen(msg.mtext) - 1] = '\0';
        msgsnd(msg_queue_id, &msg, MSG_SIZE, 0);
        if (strncmp(msg.mtext, "quit", 4) == 0) {
            break;
        }
    }
    exit(EXIT_SUCCESS);
}

void signal_handle(int signo)
{
    waitpid(-1, NULL, 0);
    msgctl(msg_queue_id, IPC_RMID, NULL);
    exit(EXIT_SUCCESS);
}

int main(int argc, const char *argv[]) {
    key_t key;
    if((key = ftok("/home/wj/Desktop", 'h')) == -1) {
        PRINT_ERR("get key error");
    }
    if ((msg_queue_id = msgget(key, IPC_CREAT|0666)) == -1) {
        PRINT_ERR("msgget error");
    }
    p_pid = getpid();
    c_pid = fork();
    if (c_pid > 0) {
        signal(SIGCHLD, signal_handle);
        p_task(NULL);
    } else if (c_pid == 0) {
        c_task(NULL);
    }
    return 0;
}

client_b.c

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

typedef struct {
    long mtype;
    char mtext[256];
} msg_t;

#define MSG_SIZE ((sizeof(msg_t) - sizeof(long)))
#define MSG_TYPE 100

int msg_queue_id;
int p_pid;
int c_pid;

void *p_task(void *argv) {
    while (1) {
        msg_t msg;
        msgrcv(msg_queue_id, &msg, MSG_SIZE, 200, 0);
        printf("receive msg = %s\n", msg.mtext);
        if (strncmp(msg.mtext, "quit", 4) == 0) {
            kill(c_pid, SIGKILL);
            break;
        }
    }
}

void *c_task(void *argv) {
    while (1) {
        msg_t msg;
        msg.mtype = MSG_TYPE;
        fgets(msg.mtext, sizeof(msg.mtext), stdin);
        msg.mtext[strlen(msg.mtext) - 1] = '\0';
        msgsnd(msg_queue_id, &msg, MSG_SIZE, 0);
        if (strncmp(msg.mtext, "quit", 4) == 0) {
            break;
        }
    }
    exit(EXIT_SUCCESS);
}

void signal_handle(int signo)
{
    waitpid(-1, NULL, 0);
    msgctl(msg_queue_id, IPC_RMID, NULL);
    exit(EXIT_SUCCESS);
}

int main(int argc, const char *argv[]) {
    key_t key;
    if((key = ftok("/home/wj/Desktop", 'h')) == -1) {
        PRINT_ERR("get key error");
    }
    if ((msg_queue_id = msgget(key, IPC_CREAT|0666)) == -1) {
        PRINT_ERR("msgget error");
    }
    p_pid = getpid();
    c_pid = fork();
    if (c_pid > 0) {
        signal(SIGCHLD, signal_handle);
        p_task(NULL);
    } else if (c_pid == 0) {
        c_task(NULL);
    }
    return 0;
}

运行结果

client_a

ubuntu@ubuntu:~ $ ./cla
hello
world
receive msg = nihao
receive msg = beijing
quit
ubuntu@ubuntu:~ $ 

client_b

ubuntu@ubuntu:~ $ ./clb
receive msg = hello
receive msg = world
nihao
beijing
receive msg = quit
ubuntu@ubuntu:~ $
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值