zoukankan      html  css  js  c++  java
  • linux消息队列相关操作

    /* 发送消息队列 */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>

    struct mymsg
    {
    long mtype; /* message type, must be > 0 */
    char mtext[32];
    };


    #define KEY (key_t)0x1fff
    int
    main ( int argc, char *argv[] )
    {
    key_t key;
    int msgid;
    int res;
    key = KEY;
    struct mymsg msg;

    printf ("发送给解析进程...... ");
    strcpy (msg.mtext, "haha");

    /* 创建消息队列 */
    msgid = msgget (key, 0666 | IPC_CREAT);
    if (msgid == -1)
    {
    perror ("msgget");
    }
    msg.mtype = 1;

    res = msgsnd (msgid, &msg, sizeof(struct mymsg), 0);
    printf ("fa ok msg.mtext = %s ", msg.mtext);

    return 0;
    }

     ========================================================

    /* 接收消息队列 */

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>

    struct mymsg
    {
    long mtype; /* message type, must be > 0 */
    char mtext[32]; /* message data */
    };

    #define KEY (key_t)0x1fff
    int main ( int argc, char *argv[] )
    {
    key_t key;
    int msgid;
    int res;
    key = KEY;

    struct mymsg msg;
    /* 创建消息队列 */
    msgid = msgget (key, 0666 | IPC_CREAT);
    if (msgid == -1)
    {
    perror ("msgget");
    }
    msg.mtype = 1;

    /* libnids抓包信息 */
    int i=1;
    while(i--)
    {
    res = msgrcv (msgid, &msg, sizeof(struct mymsg), 0, 0);
    printf ("shou ok, msg.mtext = %s ", msg.mtext);
    }

    return 0;
    }

     ==========================================================

    /*删除消息队列*/

    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>

    /* 消息队列通信的key */
    #define KEY (key_t)0x1fff

    int rmqueue(key_t key)
    {
    int msgid, res;

    msgid = msgget (key, 0);
    res = msgctl(msgid, IPC_RMID, 0);
    return 0;
    }

    int main ( int argc, char *argv[] )
    {
    key_t key;

    key = KEY;
    rmqueue(key);

    return 0;
    }

     ==========================================================

    /* 获取消息队列的状态信息 */

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>

    #define NIDS_PRO_KEY (key_t)0x1fff

    int main()
    {
    key_t key;
    key = NIDS_PRO_KEY;
    int id = msgget(key,0);
    if (id == -1) perror("shmget"),exit(-1);
    struct msqid_ds ds;
    msgctl(id,IPC_STAT,&ds);
    printf("key=%x ",ds.msg_perm.__key);
    printf("mode=%o ",ds.msg_perm.mode);
    printf("current total size=%d ",ds.__msg_cbytes);
    printf("current msg count=%d ",ds.msg_qnum);
    printf("system max allow msg size=%d ",ds.msg_qbytes);
    }

  • 相关阅读:
    C/C++ 编写一个通用的Makefile 来编译.c .cpp 或混编
    C/C++ 定义接口文件格式
    MySql存储过程例子1
    项目所遇问题
    linux下编译C++程序无法链接Mysql的问题
    linux 同步时间 调试core内核
    CentOS安装与更新git
    03 js基本数据类型、 js运算符1
    02 js运行原理 、js开发工具介绍 、js程序入门、 js基本语法
    01 js基本介绍
  • 原文地址:https://www.cnblogs.com/etangyushan/p/3989199.html
Copyright © 2011-2022 走看看