Linux IPC实践(6) --System V消息队列(3)
来源:互联网 发布:java停顿用法 编辑:程序博客网 时间:2024/05/16 05:12
消息队列综合案例
消息队列实现回射客户/服务器
server进程接收时, 指定msgtyp为0, 从队首不断接收消息
server进程发送时, 将mtype指定为接收到的client进程的pid
client进程发送的时候, mtype指定为自己进程的pid
client进程接收时, 需要将msgtyp指定为自己进程的pid, 只接收消息类型为自己pid的消息;
- // client/server进程接收/发送的数据结构
- const int MSGMAX = 8192;
- struct msgBuf
- {
- long mtype; //保存客户进程的pid(需要将pid强制转换成为long)
- char mtext[MSGMAX]; //保存客户进程真实发送的数据
- };
- //server.cpp
- void echoServer(int msgid)
- {
- struct msgBuf buf;
- int nrcv;
- while (true)
- {
- bzero(&buf, sizeof(buf));
- if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)
- err_exit("msgrcv error");
- cout << "recv: " << buf.mtext;
- if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)
- err_exit("msgsnd error");
- }
- }
- int main()
- {
- key_t key = ftok("/tmp/echoSeed", 0x1234);
- int msgid = msgget(key, IPC_CREAT|0666);
- if (msgid == -1)
- err_exit("msgget error");
- echoServer(msgid);
- }
- //client.cpp
- void echoServer(int msgid)
- {
- struct msgBuf buf;
- int nrcv;
- while (true)
- {
- bzero(&buf, sizeof(buf));
- if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)
- err_exit("msgrcv error");
- cout << "recv: " << buf.mtext;
- if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)
- err_exit("msgsnd error");
- }
- }
- int main()
- {
- key_t key = ftok("/tmp/echoSeed", 0x1234);
- int msgid = msgget(key, IPC_CREAT|0666);
- if (msgid == -1)
- err_exit("msgget error");
- echoServer(msgid);
- }
附-ftok用法
- #include <sys/types.h>
- #include <sys/ipc.h>
- key_t ftok(const char *pathname, int proj_id);
描述信息:
The ftok() function uses the identity(象征) of the file named by the given pathname (which must refer
to an existing, accessible file[必须是一个已经存在,并且可访问的文件]) and the least significant(有效的) 8 bits[有效的最低8位] of proj_id (which must be nonzero) to generate a key_t type System V IPC key, suitable
for use with msgget(2), semget(2), or shmget(2). The resulting value is the same for all pathnames that name the same file, when the same value of proj_id
is used(如果文件名与proj_id的有效位全都相同的话, 则生成的key一定也是相同的). The value returned should be different when
the (simultaneously existing) files or the project IDs differ.
RETURN VALUE On success, the generated key_t value is returned. On failure -1 is returned,
with errno indicating the error as for the stat(2) system call.
- Linux IPC实践(6) --System V消息队列(3)
- Linux IPC实践(6) --System V消息队列(3)
- Linux IPC实践--System V消息队列(3)
- Linux IPC实践(4) --System V消息队列(1)
- Linux IPC实践(5) --System V消息队列(2)
- Linux IPC实践--System V消息队列(1)
- Linux IPC实践 --System V消息队列(2)
- Linux IPC实践(4) --System V消息队列(1)
- Linux IPC实践(5) --System V消息队列(2)
- linux ipc system v 消息队列源码
- Linux ipc------System V消息队列
- 消息队列(system V IPC)
- Linux进程间通信(IPC)编程实践(三) 详解System V消息队列(1)
- [linux系统编程]System V IPC 消息队列
- 细说linux IPC(十):system V 消息队列
- linux—进程通信IPC--system v-消息队列
- Linux进程间通信(IPC)编程实践(四) 详解System V消息队列(2)(msgsnd & msgrcv)
- Linux C编程--进程间通信(IPC)5--System V IPC 机制1--消息队列
- Tomcat8.0+Mysql5.7配置jndi
- 为什么linux平台下软件安装不是双击执行
- 线程池_uThreadPool
- npm的package.json中的dependencies版本号
- 开发的好习惯和坏习惯
- Linux IPC实践(6) --System V消息队列(3)
- Spring中JDK的动态代理和CGLIB代理的区别
- Linux IPC实践(7) --Posix消息队列
- 找到了一个SysuThesis模板,大喜
- 数据链路层笔记
- Linux IPC实践(8) --共享内存/内存映射
- cocos2d-x中关于touch事件的响应
- 期末复习总结
- SAP支持宝典:#MM#如何在归档后启用归档信息系统