/* 发送消息队列 */

#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 ("发送给解析进程......\n");
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\n", 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\n", 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\n",ds.msg_perm.__key);
printf("mode=%o\n",ds.msg_perm.mode);
printf("current total size=%d\n",ds.__msg_cbytes);
printf("current msg count=%d\n",ds.msg_qnum);
printf("system max allow msg size=%d\n",ds.msg_qbytes);
}

linux消息队列相关操作的更多相关文章

  1. linux消息队列操作

    对消息队列的操作无非有以下三种类型: 1. 打开或创建消息队列消息队列的内核持续性要求每一个消息队列都在系统范围内相应唯一的键值,所以,要获得一个消息队列的描写叙述字,仅仅需提供该消息队列的键值就可以 ...

  2. php中对共享内存,消息队列的操作

    http://www.cnblogs.com/fengwei/archive/2012/09/12/2682646.html php作为脚本程序,通常生命周期都很短,如在web应用中,一次请求就是ph ...

  3. linux消息队列编程实例

    转自:linux 消息队列实例 前言: 消息队列就是一个消息的链表.可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向其中按照一定的规则添加新消息:对消息队列有读权 ...

  4. Linux下mysql相关操作

    Linux下mysql相关操作 1.创建MySQL mysql -u root -p create user 'username'@'%' identified by 'password'; %可以选 ...

  5. Linux下 svn相关操作

    Linux下 svn相关操作 一.首先看看svn安装的位置: 命令: find / -name svn /var/svn :表示安装目录 /user/bin/svn :表示命令目录 可以看到的是svn ...

  6. LINUX消息队列实战之一

    前言 能说能抄能论皆不算,能写能打才是真功夫. 唠叨 反正我也是一个孤独的程序猿,多说一些奇奇怪怪的唠叨也无妨,第一次写消息队列,书本的东西和实战很不同,根据实战总结的一些注意事项会和大家分享,也敲打 ...

  7. linux下进程相关操作

    一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...

  8. linux消息队列通信

    IPC机制 进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的 ...

  9. Linux TCP队列相关参数的总结 转

        在Linux上做网络应用的性能优化时,一般都会对TCP相关的内核参数进行调节,特别是和缓冲.队列有关的参数.网上搜到的文章会告诉你需要修改哪些参数,但我们经常是知其然而不知其所以然,每次照抄过 ...

随机推荐

  1. 申请edu邮箱

    https://blog.csdn.net/w305607610/article/details/86771593 手把手教你申请CCC(City Colleges of Chicago)教育邮箱 台 ...

  2. 最近两周我们接触到的两种线上抓娃娃机的技术实现方案(一种RTSP/一种RTMP)

    线上抓娃娃机需求 最近线上抓娃娃机的项目火爆了,陆陆续续几十款线上抓娃娃机上架,还有一大波正在开发上线中,各大视频云提供商都在蹭热度发布自己的线上抓娃娃机方案,综合了一下,目前线上抓娃娃机的视频需求无 ...

  3. The template root requires exactly one element

    The template root requires exactly one element

  4. SASL mechanism

    <property> <name>hive.spark.client.rpc.sasl.mechanisms</name> <value>DIGEST- ...

  5. Louvain Modularity Fast unfolding of communities in large networks

    Louvain Modularity Fast unfolding of communities in large networks https://arxiv.org/pdf/0803.0476.p ...

  6. CALL FUNCTION 'BAPI_PO_CREATE1' 相关报错

    *&---------------------------------------------------------------------**& Report  ZQJ06*&am ...

  7. PYTHON加密解密字符串

    依赖包安装部分 安装依赖包: pip install pycryptodome 在你的python环境中的下图红框路径中找到 crypto 将其改成 Crypto 代码部分 #!/usr/bin/en ...

  8. 【shell】awk引用外部变量

    在使用awk的过程中,经常会需要引用外部变量,但是awk需要使用单引号将print包起来,导致print后的$引用无效,可以采用下面的方式 例如: #!/bin/bash a="line1 ...

  9. Java for LeetCode 082 Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  10. 郝健: Linux内存管理学习笔记-第2节课【转】

    本文转载自:https://blog.csdn.net/juS3Ve/article/details/80035753 摘要 slab./proc/slabinfo和slabtop 用户空间mallo ...