博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
消息队列
阅读量:7230 次
发布时间:2019-06-29

本文共 1848 字,大约阅读时间需要 6 分钟。

#include "public.h"

void msg_show_attr(int msg_id,struct msqid_ds msg_info)

{

 int ret=-1;

 sleep(1);
 ret=msgctl(msg_id,IPC_STAT,&msg_info);//获取消息
 if (-1==ret)
 {
  printf("获得消息信息失败\n");
  return ;
 }
 printf("\n");
 printf("现在队列中字节数:%d\n",msg_info.msg_cbytes);
 printf("队列中消息数:%d\n",msg_info.msg_qnum);
 printf("队列照哦给你最大字节数:%d\n",msg_info.msg_qbytes);
 printf("最后发送消息的进程pid:%d\n",msg_info.msg_lspid);
 printf("最后接受消息的进程pid:%d\n",msg_info.msg_lrpid);
 printf("最后发送消息的时间:%s\n",ctime(&(msg_info.msg_stime)));
 printf("最后接受消息的时间:%s\n",ctime(&(msg_info.msg_rtime)));
 printf("最后变化时间:%s\n",ctime(&(msg_info.msg_ctime)));
 printf("消息UID是:%d\n",msg_info.msg_perm.uid);
 printf("消息GID是:%d\n",msg_info.msg_perm.gid);
}

int main()

{
 int ret=-1;
 int msg_flags,msg_id;
 key_t key;
 struct msgmbuf{
  int mtypes;
  char mtext[10];
 };
 struct msqid_ds msg_info;
 struct msgmbuf msg_mbuf;

 int msg_sflags,msg_rflags;

 char *msgpath="/ipc/msg/";
 key=ftok(msgpath,'b');
 if (key!=-1)
 {
  printf("成功创建KEY\n");
 }
 else
 {
  printf("建立KEY失败\n");
 }
 msg_flags=IPC_CREAT|IPC_EXCL;
 msg_id=msgget(key,msg_flags|0x0666);
 if (-1==msg_id)
 {
  printf("消息建立失败\n");
  return 0;
 }
 msg_show_attr(msg_id,msg_info);
 msg_sflags=IPC_NOWAIT;
 msg_mbuf.mtypes=10;
 memcpy(msg_mbuf.mtext,"测试消息",sizeof("测试消息"));//复制字符串
 ret=msgsnd(msg_id,&msg_mbuf,sizeof("测试消息"),msg_sflags);
 
 if (-1==ret)
 {
  printf("发送消息失败\n");
 }
 msg_show_attr(msg_id,msg_info);
 msg_rflags=IPC_NOWAIT|MSG_NOERROR;
 ret=msgrcv(msg_id,&msg_mbuf,10,10,msg_rflags);
 if (-1==ret)
 {
  printf("接受消息失败\n");
 }
 else
 {
  printf("接受消息成功,长度:%d\n",ret);
 }
 msg_show_attr(msg_id,msg_info);
 msg_info.msg_perm.uid=8;
 msg_info.msg_perm.gid=8;
 msg_info.msg_qbytes=12345;
 ret=msgctl(msg_id,IPC_SET,&msg_info);//设置消息属性
 if (-1==ret)
 {
  printf("设置消息属性失败\n");
  return 0;
 }
 msg_show_attr(msg_id,msg_info);
 ret=msgctl(msg_id,IPC_RMID,NULL);
 if (-1==ret)
 {
  printf("删除消息失败\n");
  return 0;
 }

 return 0;

}

 

转载地址:http://tipfm.baihongyu.com/

你可能感兴趣的文章
echo -en
查看>>
Mysql 复制(Replication)实现
查看>>
我的友情链接
查看>>
jar生成exe可执行的程序
查看>>
date 转化为 指定格式的String
查看>>
使用virtualbox安装RHEL 6.2+Oracle 11g
查看>>
系统的域名服务
查看>>
MySQL 复制表结构
查看>>
《Effective C#》条款8:确保0为值类型的有效状态
查看>>
动态迁移应用服务器(Esxi 动态迁移技术,业务不间断,在线迁移)
查看>>
systemd coding style
查看>>
warning: control reaches end of non-void function
查看>>
Tkinter, a Gui for python
查看>>
android开发之webservice介绍
查看>>
纯js页面跳转整理
查看>>
目标:嗯,每天进步一点点,每周坚持写一点
查看>>
ros 安装教程
查看>>
使用charles抓包https,配置了证书,还是乱码的解决方案
查看>>
Javascript的this用法
查看>>
解决nginx 504 Gateway Time-out的一些方法
查看>>