Example for Message Queue
来源:互联网 发布:王者荣耀段位网络查询 编辑:程序博客网 时间:2024/05/21 10:18
http://www.cs.cf.ac.uk/Dave/C/node25.html
下面是一个利用message queue实现ipc的例子。
要注意的是,当进程退出后,message queue仍然存在。
/*
* =====================================================================================
*
* Filename: message_send.c
*
* Description:
*
* Version: 1.0
* Created: 09/02/2010 02:06:54 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
int
main()
{
int msqid;
// int msgflg = IPC_CREAT | IPC_EXCL | 0666;
int msgflg = IPC_CREAT | IPC_EXCL | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
fprintf(stderr, "msgget: Calling msgget(%#lx,/%#o)/n", key, msgflg);
if ((msqid = msgget(key, msgflg )) < 0) {
perror("msgget");
exit(1);
}
/*
* We'll send message type 1
*/
sbuf.mtype = 1;
fprintf(stderr,"msgget: msgget succeeded: msqid = %d/n", msqid);
strcpy(sbuf.mtext, "Did you get this?");
buf_length = strlen(sbuf.mtext) + 1 ;
/*
* Send a message.
*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d/n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: /"%s/" Sent/n", sbuf.mtext);
exit(0);
}
/*
* =====================================================================================
*
* Filename: message_rec.c
*
* Description:
*
* Version: 1.0
* Created: 09/02/2010 02:13:44 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
int
main()
{
int msqid;
key_t key;
ssize_t msg_size;
message_buf rbuf;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
if ((msqid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}
/*
* Receive an answer of message type 1.
*/
if ( (msg_size = msgrcv(msqid, &rbuf, MSGSZ, 1, 0))< 0) {
perror("msgrcv");
exit(1);
}
/*
* Print the answer.
*/
printf("%d/n%s/n", msg_size, rbuf.mtext);
exit(0);
}
- Example for Message Queue
- Message Queue
- message queue
- Message Queue
- Message queue
- 【zookeeper】Interrupted while waiting for message on queue
- Java Queue Example
- Linux IPC - Message Queue
- Message Queue: MSMQ/MQSeries
- 3.13 Message Queue任务
- windows, thread ,message queue
- POSIX message queue
- Examining a Message Queue
- IPC SystemV Message Queue
- hdu1509 Windows Message Queue
- zoj2724-Windows Message Queue
- HDU1509-Windows Message Queue
- Message Queue和handle
- UML学习三:标准建模语言UML的动态建模机制
- spy++
- ECMAScript 继承机制实现
- 字符串处理
- Temporary Objects
- Example for Message Queue
- flex 邮件格式验证 只能验证一个邮件地址
- 售前生活的趣事
- 构造最优二叉查找树的时间复杂度分析
- com套间
- UML学习四:UML在微软的VS与Visio间的应用 VEA
- 生活茶
- 关于跨线程调用——捕获了对错误线程的调用
- 开发部测验,备受打击