进程间通信之用一个消息队列实现多个进程的通信
-
进程a向进程B发送hello world,进程B接收打印,
-
进程c向进程d发送自己的学号班级姓名,进程d接收打印,
-
用同一个消息队列实现
这是a进程
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
struct msg{
long msg_types;
char msg_buf[512];
};
int main()
{
key_t key;
key = ftok (".",5);
int msgdis;
if ((msgdis = msgget (key,IPC_CREAT|777)) == -1) { perror("msgget : \n"); exit(1); }
printf ("消息队列为:%d \n",msgdis);
struct msg pmsg;
pid_t pid;
pid = getpid();
pmsg.msg_types = pid;
printf ("设置的消息类型为: %d \n",pid);
sprintf (pmsg.msg_buf,"%s","hello world");
int len;
len = strlen (pmsg.m