C 消息队列

//

//  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;

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值