消息队列——The message queue
来源:互联网 发布:c语言库函数的数量 编辑:程序博客网 时间:2024/06/08 10:15
//消息队列:数据通讯
// 点对多,离线通讯。
// ftok();索取KEY值
//
// msgget();创建
// msgctl();删除
// msgsnd();发送
// msqrcv();接收
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "proto.h"
int main()
{
int msgid; //
key_t key;
struct msg_st sbuf;
// 获取消息队列关联的键。
key = ftok(KEYPATH,KEYPROJ);
if(key < 0)
{
perror("ftok()");
exit(1);
}
//int msgget(key_t key, int msgflg);
//参数msgflg为:消息队列的建立标志和存取权限
msgid = msgget(key,0);
//success 返回队列标识,failed ,return -1;
if(msgid < 0)
{
perror("msgget()");
exit(1);
}
//mtype :从消息队列内读取的消息形态。(0:消息队列中所有消息都会被
//读取)
sbuf.mtype = 1; /*!!*/
strcpy(sbuf.name,"Alan");
sbuf.math = rand()%100;
sbuf.chinese = rand()%100;
if(msgsnd(msgid,&sbuf,sizeof(sbuf)-sizeof(long),0) < 0)
{
perror("msgsnd");
exit(1);
}
puts("ok!");
exit(0);
}
----------------------------------------------------------------------------------------------------------------
// 消息队列
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "proto.h"
int main()
{
int msgid;
struct msg_st rbuf;
key_t key;
key = ftok(KEYPATH,KEYPROJ);
if(key < 0)
{
perror("ftok()");
exit(1);
}
msgid = msgget(key,IPC_CREAT|0600);
if(msgid < 0)
{
perror("msgget()");
exit(1);
}
while(1)
{
if(msgrcv(msgid,&rbuf,sizeof(rbuf)-sizeof(long),0,0) < 0)
{
perror("msgrcv()");
exit(1);
}
printf("Name:%s\n",rbuf.name);
printf("Math:%d\n",rbuf.math);
printf("Chinese:%d\n",rbuf.chinese);
}
msgctl(msgid,IPC_RMID,NULL);
exit(0);
}
--------------------------------------------------------------------------------------------------------
#ifndef PROTO_H__
#define PROTO_H__
#define KEYPATH "/etc/services"
#define KEYPROJ 'S'
#define NAMESIZE 32
struct msg_st
{
long mtype;
char name[NAMESIZE];
int math;
int chinese;
};
#endif
- 消息队列——The message queue
- 消息队列 (Message Queue)
- 消息队列 ( Message Queue )
- 消息队列(Message Queue)
- 消息队列(Message Queue)
- 消息队列(Message Queue)
- 消息队列 posix message queue
- 消息队列(Message Queue)
- 消息队列(Message Queue)
- 消息队列(message queue)
- 6.3 消息队列 message queue
- ipc 消息队列message queue
- Posix message queue 消息队列
- IPC 消息队列(message queue)
- Message queue消息队列,MQ
- 【十】 进程间通信——[System V IPC对象]消息队列(message queue)
- Android HIDL 官方文档(五)—— 快速消息队列(Fast Message Queue ,FMQ)
- MSMQ(MicroSoft Message Queue,微软消息队列)
- remotely work and hire
- [Windows]_[C/C++]_[WIN32根据程序名(映像名称)终止外部程序]
- myeclipse8.5 集成svn
- SignalR推送服务在Android的实现 SignalA
- org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
- 消息队列——The message queue
- [互联网面试笔试汇总C/C++-26] Socket 长连接与短连接,心跳(keep-alive)
- asp.net读取磁盘文件、删除实例代码
- C# where
- vc 线程的创建,暂停和结束
- 变量 类型 运算符 笔记整理02
- SHTSC记 13.10.26
- Asp.Net生成静态页面的实现方法
- Matlab基本函数-cylinder函数