unix XSI IPC-消息队列例程
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,注意事项:linux(2.4.22)限制:可发送最长消息字节数为8192队列最大容量字节数 16384队列最大队列容量数 16key_t ftok(char* path,int id)使用说明:fto
千家信息网最后更新 2024年09月22日unix XSI IPC-消息队列例程
注意事项:
linux(2.4.22)限制:
- 可发送最长消息字节数为8192
- 队列最大容量字节数 16384
- 队列最大队列容量数 16
key_t ftok(char* path,int id)使用说明:
- ftok创建一个键,是内核中的队列在外部的ID号,由于消息队列处于内核中,只有创建者和内核知道队列在内核里面的ID号,这样其它的进程就无法知道内核里面队列ID号,所以要关联一个外部键进行关联
- id (1-255)
- 返回内核消息队列的ID号
其它的注意就查看一下unix高级环境编程吧,或者有些问题需要讨论就回我吧!!
server.c
- #include "msg.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, char** argv)
- {
- int queid = open_msg("/root",100);
- while(1)
- {
- fputs("请输入要发送的类型:1 or 2\n", stdout);
- int type;
- scanf("%d",&type);
- switch(type)
- {
- case MYTYPE_ONE:
- {
- msg_send(queid,"MYTYPE_ONE", MYTYPE_ONE);
- break;
- }
- case MYTYPE_TWO:
- {
- msg_send(queid,"MYTYPE_TWO", MYTYPE_TWO);
- break;
- }
- default:
- {
- fputs("输入类型错误,请重新输入\n",stdout);
- break;
- }
- }
- fputs("输入:q 为退出,其它表示继续\n",stdout);
- if(getchar() == 'q')
- {
- fputs("退出成功!\n",stdout);
- break;
- }
- else
- {
- fputs("继续发送消息\n",stdout);
- }
- }
- //不发送退出需要奖队列移除
- del_que(queid);
- return 0;
- }
client.c
- #include "msg.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(int argc, char** argv)
- {
- int queid = open_msg("/root",100);
- while(1)
- {
- fputs("请接收要发送的类型:1 or 2\n", stdout);
- int type;
- scanf("%d",&type);
- switch(type)
- {
- case MYTYPE_ONE:
- {
- msg_rec(queid,MYTYPE_ONE);
- break;
- }
- case MYTYPE_TWO:
- {
- msg_rec(queid,MYTYPE_TWO);
- break;
- }
- default:
- {
- fputs("输入类型错误,请重新输入\n",stdout);
- break;
- }
- }
- fputs("输入:q 为退出,其它表示继续\n",stdout);
- if(getchar() == 'q')
- {
- fputs("退出成功!\n",stdout);
- break;
- }
- else
- {
- fputs("继续发送消息\n",stdout);
- }
- }
- //队列移除
- del_que(queid);
- return 0;
- }
msg.c
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include<string.h>
- #include"msg.h"
- //如果存在队列则打开,没有则创建
- int open_msg(char* path, int id)
- {
- //获取IPC对象的一个键
- key_t key = ftok(path, id);
- if(-1 == key)
- {
- perror("ftok\n");
- exit(1);
- }
- //创建一个队列
- int queid = msgget(key, IPC_CREAT|0666);
- if(-1 == queid)
- {
- perror("msgget\n");
- exit(1);
- }
- return queid;
- }
- //发送消息到队列
- void msg_send(key_t key,char* text, long msgtype)
- {
- //初始化内容
- struct MSG tmp;
- memset(&tmp,sizeof(struct MSG),0);
- tmp.mytype = msgtype;
- strcpy(tmp.mytext,text);
- //发送消息
- if(msgsnd(key, &tmp, TEXTSIZE, 0))
- {
- perror("msgsnd\n");
- exit(1);
- }
- }
- //从消息队列获取消息并显示
- void msg_rec(key_t key,long msgtype)
- {
- struct MSG tmp;
- if(-1 == msgrcv(key, &tmp, TEXTSIZE, msgtype, MSG_NOERROR))
- {
- perror("msgrcv\n");
- exit(1);
- }
- printf("receive content: %s\n",tmp.mytext);
- }
- //删除队列,即使队列里面还有消息也一起删除
- void del_que(key_t key)
- {
- if(msgctl(key,IPC_RMID,NULL))
- {
- perror("msgsnd\n");
- exit(1);
- }
- }
msg.h
- #ifndef MSG_H
- #define MSG_H
- #include <sys/types.h>
- #define TEXTSIZE 100
- #define ARRYSIZE 2
- #define MYTYPE_ONE 1
- #define MYTYPE_TWO 2
- struct MSG
- {
- long mytype;
- char mytext[TEXTSIZE];
- };
- int open_msg(char*,int);
- void msg_send(key_t,char*,long);
- #endif // end MSG_H
队列
消息
输入
内核
类型
最大
成功
字节
容量
错误
关联
最长
高级
事项
使用说明
内容
创建者
只有
对象
注意事项
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
考研数据库系统概论视频
互联网金融网络技术问题
网络安全管理学专升本
云顶之弈端游哪个区服务器好一点
基于大智慧的软件开发
软件开发工程师杭州月薪
北京宽捷网络技术有限公司
苹果激活apple id服务器
为什么别人访问不了我的数据库
epic出现服务器离线怎么办
线刷会不会清除数据库
昆明 软件开发兼职
数据库dao设计或整合
数据库应用技术文科
云服务器升级对ip的影响
广工大数据库试卷
100台服务器的辐射
舟山OA软件开发招聘
阿里腾讯投资网络安全
软件开发 与客户沟通
软件开发用什么显卡
如何学好网络技术基础
福清市软件开发有限公司
云办公软件开发程序
数据库dao设计或整合
软件开发等价类划分
网络安全小报文字内容
数据库表清空
山东即刻在线互联网科技
青岛德伦网络技术