//
// main.c
// MSG
//
// Created by Freax
//
#include <stdio.h>
#include <pwd.h>
#include <sys/timeb.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <string.h>
#define PATH_MAX 1024
#define MSG_RD 0400 /* read permission for the queue */
#define MSG_WR 0200 /* write permission for the queue */
#define MSG_RW MSG_RD | MSG_WR
#define MSGSIZE 1024 /* a resonable size for a message */
#define MSGTYPE 1 /* a type ID for a message */
typedef struct mbuf { /* a generic message structure */
long mtype;
char mtext[MSGSIZE + 1]; /* add 1 here so the message can be 1024 */
} MSGBUF; /* characters long with a '\0' termination */
int queue_empty(int msg_id);
/*
* 根据name制作key
*/
int ipc_makekey(const char *name)
{
return 5700;
}
int queue_create(const char *name)
{
key_t msgkey = ipc_makekey(name);
int msg_id = -1;
/* create a message queue with read/write permissions */
if ((msg_id = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
printf("create mesage queue error !!");
}
//printf("msg_id ====>> %d\n", msg_id);
queue_empty(msg_id);
return msg_id;
}
int queue_connect(const char *name)
{
int msg_queue_id = -1; /* to hold the message queue id */
struct msqid_ds qs_buf;
int msgkey = ipc_makekey(name);
/* make sure the initial # of bytes is 0 in our buffer */
qs_buf.msg_qbytes = 0x3000;
/* now we have a key, so let's create a message queue */
if ((msg_queue_id = msgget(msgkey, IPC_EXCL)) == -1) {
printf("Can't connect message queuen\n");
}
/* now stat the queue to get the default msg_qbytes value */
if ((msgctl(msg_queue_id, IPC_STAT, &qs_buf)) == -1) {}
/* decrement msg_qbytes and copy its value */
qs_buf.msg_qbytes -= 1;
//new_bytes = qs_buf.msg_qbytes;
return msg_queue_id;
}
int queue_delete(int qid)
{
if (qid == -1) { /* no queue to remove */
return -1;
}
if (msgctl(qid, IPC_RMID, NULL) == -1) {
printf("WARNING: message queue deletion failed.");
}
return -1;
}
int queue_send(int qid, void *buf, int size, int nowait)
{
if (msgsnd(qid, buf, size, 0) < 0) {
printf("send msg error!\n");
return -1;
}
return 0;
}
int queue_recv(int qid, void *msg, int size, int nowait)
{
/*
* remove the message by reading from the queue
*/
if (msgrcv(qid, msg, MSGSIZE, 1, nowait) == -1) {
printf("Could not read from queue\n");
return -1;
}
return 0;
}
int queue_readq_interruptable(int flag)
{
return -1;
}
/*
* 清空队列中的message
*/
int queue_empty(int msg_id)
{
struct msqid_ds info;
if (msgctl(msg_id, IPC_STAT, &info)) perror("msgctl IPC_STAT error ");
while (info.msg_qnum > 0) {
msgrcv(msg_id, NULL, MSGSIZE, 1, IPC_NOWAIT);
msgctl(msg_id, IPC_STAT, &info);
}
return 0;
}
/*
* 取得消息队列状态
*/
int queue_status(int msg_id)
{
struct msqid_ds info;
if (msgctl(msg_id, IPC_STAT, &info)) perror("msgctl IPC_STAT error ");
//printf("Current # of messages on queue\t %ld\n", info.msg_qnum);
return 0;
}
/*
* 取得消息队列消息的数量
*/
int queue_status_num_msg(int msg_id)
{
struct msqid_ds info;
if (msgctl(msg_id, IPC_STAT, &info)) perror("msgctl IPC_STAT error ");
return (int)info.msg_qnum;
}
int main(int argc, const char * argv[])
{
int msg_id = queue_create("FOLDER_SYSTEM_IPC_MSG_NAME");
char *qry_str = "qry_str=qry_str=qry_str=qry_str=qry_str";
MSGBUF msg_buf;
memset(msg_buf.mtext, 0x00, sizeof(msg_buf.mtext));
memcpy(msg_buf.mtext, qry_str, strlen(qry_str));
msg_buf.mtype = 1;
for (int i=0; i<100; i++) {
printf("消息队列中消息的个数 =>> %d\n", queue_status_num_msg(msg_id));
queue_send(msg_id, &msg_buf, (int)strlen(qry_str) + 1, 1);
}
return 0;
}
本文介绍了一个使用消息队列进行进程间通信的C语言示例程序。该程序包括消息队列的创建、连接、发送和接收消息等功能,并演示了如何清空消息队列及获取队列状态。
384

被折叠的 条评论
为什么被折叠?



