Linux高级编程基础——进程间通信之用一个消息队列实现多个进程的通信

进程间通信之用一个消息队列实现多个进程的通信

  1. 进程a向进程B发送hello world,进程B接收打印,

  2. 进程c向进程d发送自己的学号班级姓名,进程d接收打印,

  3. 用同一个消息队列实现

                      这是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
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值