实现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:~ $