linux进程间通信-----System V消息队列总结实例

metabase 容器dev模式启动失败-System limit for number of file watchers metabase提供了一个容器化编译能力,默认开发可以在容器内通过yarn dev启动开发环境,但如果使用默认linux配置,会遇到如下错误 ENOSPC: System limit for number of file watchers reached, watch '/app/source/resources/frontend_client/app/fonts/lato-v16-latin/lato-v16-latin-900.woff2 System limit for number of file 阅读详情

什么是消息队列?

消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法,消息队列具有内核持续性;
每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值;
消息队列也有管道一样的不足,就是每个消息的最大长度是有上限的(MSGMAX),每个消息队列的总的字节数是有上限的(MSGMNB),系统上消息队列的总数也有一个上限(MSGMNI)

cat /proc/sys/kernel/msgmax 最大消息长度限制,8192=8K
cat /proc/sys/kernel/msgmnb 消息队列总的字节数,16384 = 16K
cat /proc/sys/kernel/msgmni 消息条目数,169


内核为每个IPC对象维护一个数据结构,在调用msgctl的时候就会用到;

struct msqid_ds {
   struct ipc_perm msg_perm;     /* Ownership and permissions */
   time_t          msg_stime;    /* Time of last msgsnd(2) */
   time_t          msg_rtime;    /* Time of last msgrcv(2) */
   time_t          msg_ctime;    /* Time of last change */
   unsigned long   __msg_cbytes; /* Current number of bytes in
                                    queue (nonstandard) */
   msgqnum_t       msg_qnum;     /* Current number of messages
                                    in queue */
   msglen_t        msg_qbytes;   /* Maximum number of bytes
                                    allowed in queue */
   pid_t           msg_lspid;    /* PID of last msgsnd(2) */
   pid_t           msg_lrpid;    /* PID of last msgrcv(2) */
};

//The ipc_perm structure is defined as follows (the highlighted fields are settable using IPC_SET):

struct ipc_perm {
   key_t          __key;       /* Key supplied to msgget(2) */
   uid_t          uid;         /* Effective UID of owner */
   gid_t          gid;         /* Effective GID of owner */
   uid_t          cuid;        /* Effective UID of creator */
   gid_t          cgid;        /* Effective GID of creator */
   unsigned short mode;        /* Permissions */
   unsigned short __seq;       /* Sequence number */
};


主要函数API:

1:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
对消息队列进行控制
msqid: 由msgget函数返回的消息队列标识码
cmd:是将要采取的动作,(有三个可取值)
IPC_STAT(将消息队列的msqid_ds结构体的值复制到当前buf指向的内存空间,读取消息队列的信息)
IPC_SET(当前buf指向的内存空间的值复制给消息队列的msqid_ds结构体的值,设置消息队列的信息)
IPC_RMID(删除消息队列)

2:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
用来创建和访问一个消息队列
key: 某个消息队列的名字, 比如0x1234, 0x3232.....
msgflg:由九个权限标志构成,它们的用法和创建文件时使用的mode模式标志是一样的;
If msgflg specifies both IPC_CREAT and IPC_EXCL and a message queue already exists for key, then msgget() fails with errno set to EEXIST
返回值:成功返回一个非负整数,即该消息队列的标识码;失败返回-1
返回的错误信息:
On failure, errno is set to one of the following values:
EACCES A message queue exists for key, but the calling process does not have permission to access the queue, and does not have the CAP_IPC_OWNER capability.
EEXIST A message queue exists for key and msgflg specified both IPC_CREAT and IPC_EXCL.
ENOENT No message queue exists for key and msgflg did not specify IPC_CREAT.
ENOMEM A message queue has to be created but the system does not have enough memory for the new data structure.
ENOSPC A message queue has to be created but the system limit for the maximum number of message queues (MSGMNI) would be exceeded.

3:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
参数msgp指向类似于下面的结构体,形式一样,但是具体的值自己改,msgbuf这个名字一定要改:
struct msgbuf {
   long mtype;       /* message type, must be > 0 */
   char mtext[1];    /* message data ,长度可以自己改,不能超过系统规定的长度*/
};
msgid: 由msgget函数返回的消息队列标识码
msgp:是一个指针,指针指向准备发送的消息,
msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型
msgflg:控制着当前消息队列满或到达系统上限时将要发生的事情,msgflg=IPC_NOWAIT表示队列满不等待,返回EAGAIN错误。

4:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
msgid: 由msgget函数返回的消息队列标识码
msgp:是一个指针,指针指向准备接收的消息,
msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型
msgtype:它可以实现接收优先级的简单形式
msgflg:控制着队列中没有相应类型的消息可供接收时将要发生的事
返回值:成功返回实际放到接收缓冲区里去的字符个数,失败返回-1

msgtype=0返回队列第一条信息
msgtype>0返回队列第一条类型等于msgtype的消息 
msgtype<0返回队列第一条类型小于等于msgtype绝对值的消息,并且是满足条件的消息类型最小的消息

msgflg=IPC_NOWAIT,队列没有可读消息不等待,返回ENOMSG错误。
msgflg=MSG_NOERROR,消息大小超过msgsz时被截断 
msgtype>0且msgflg=MSG_EXCEPT,接收类型不等于msgtype的第一条消息。

5:

#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *pathname, int proj_id);
用这个函数为消息队列产生一个key值;


实例1:

进程先创建一个消息队列,并向该消息队列中写入消息,消息类型是自身的PID;然后睡眠3秒之后再从消息队列中按照消息类型读取消息;
从运行结果可以看出,当写入之后,消息队列的大小变为了12, 当将消息读走之后消息队列的大小变为了0;

/*************************************************************************
	> File Name: msg1.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月17日 星期四 20时25分44秒
 ************************************************************************/

#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
using namespace std;


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

/*
生成一个key值,ftok(首先要先创建一个文件myMsgFile)
创建消息队列,msgget
生成消息结构体对象并填充消息类型-一般是PID和数据内容
将消息写到消息队列
睡眠3秒
从消息队列里面读出来
*/

int main()
{
    key_t key;
    key = ftok("./myMsgFile", 'k');
   
    int msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if(msgid == -1){
        if(errno == EEXIST){
            key = ftok("./myMsgFile", 'k');
            msgid = msgget(key, IPC_CREAT | 0666);
        }
        else{
            cout << "msgget error..." << endl;
            exit(-1);
        }
    }

    struct msg_buf msgbuf;
    int msg_type = getpid();
    msgbuf.mtype = msg_type;
    strcpy(msgbuf.mtext, "message data");
    
    int ret = msgsnd(msgid, &msgbuf, strlen(msgbuf.mtext), IPC_NOWAIT);
    if(ret == -1){
        cout << "msgsnd error..." << endl;
        exit(-1);
    }

    sleep(3);
    memset(&msgbuf, 0, sizeof(msgbuf));

    ret = msgrcv(msgid, &msgbuf, sizeof(msgbuf), msg_type, IPC_NOWAIT); 
    if(ret == -1){
        cout << "msgrcv error..." << endl;
        exit(-1);
    }
    
    cout << "receive the data is: " << msgbuf.mtext << endl;

    exit(0);
}





备注:ipcs查看系统的共享内存,消息队列,信号量    ipcrm -q msqid删除消息队列

实例2:

这次是父子进程通过消息队列来传递信息;
进程先创建一个消息队列,并向该消息队列中写入消息,消息类型是自身的PID;然后又向该消息队列中写入消息,消息类型是111;此时查看消息队列已用了26字节;
然后睡眠5秒之后创建子进程,子进程再从消息队列中获得消息类型位111的消息并读走;读走了13字节,还有一个消息,剩下13字节;

/*************************************************************************
	> File Name: msg1.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月17日 星期四 20时25分44秒
 ************************************************************************/

#include <iostream>
#include <cstdlib>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;

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

/*
生成一个key值,ftok(首先要先创建一个文件myMsgFile)
创建消息队列,msgget
生成消息结构体对象并填充消息类型-一般是PID和数据内容
将消息写到消息队列
睡眠3秒
从消息队列里面读出来
*/

int main()
{
    key_t key;
    key = ftok("./myMsgFile", 'k');
   
    int msgid = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
    if(msgid == -1){
        if(errno == EEXIST){
            key = ftok("./myMsgFile", 'k');
            msgid = msgget(key, IPC_CREAT | 0666);
        }
        else{
            cout << "msgget error..." << endl;
            exit(-1);
        }
    }

    struct msg_buf msgbuf;
    int msg_type = getpid();
    msgbuf.mtype = msg_type;
    strcpy(msgbuf.mtext, "message1 data");
    
    int ret = msgsnd(msgid, &msgbuf, strlen(msgbuf.mtext), IPC_NOWAIT);
    if(ret == -1){
        cout << "msgsnd error..." << endl;
        exit(-1);
    }

    memset(&msgbuf, 0, sizeof(msgbuf));
    msgbuf.mtype = 111;
    strcpy(msgbuf.mtext, "message2 data");
    ret = msgsnd(msgid, &msgbuf, strlen(msgbuf.mtext), IPC_NOWAIT);
    if(ret == -1){
        cout << "msgsnd error..." << endl;
        exit(-1);
    }


    sleep(5);
    pid_t pid = fork();
    if(pid < 0){
        cout << "fork error..." << endl;
        exit(-1);
    }
    else if (pid == 0){
        memset(&msgbuf, 0, sizeof(msgbuf));
        ret = msgrcv(msgid, &msgbuf, sizeof(msgbuf), 111, IPC_NOWAIT); //获得第二条消息
        if(ret == -1){
            cout << "msgrcv error..." << endl;
            exit(-1);
        }
        cout << "child process receive the data is: " << msgbuf.mtext << endl;
        cout << "child exit..." << endl;
        exit(0);
    }
    
    int child_status;
    wait(&child_status);
    
    cout << "parent exit..." << endl;
    exit(0);
}




IPC之三:使用 System V 消息队列进行进程间通信的实例 IPC 是 Linux 编程中一个重要的概念,IPC 有多种方式,本文主要介绍消息队列(Message Queues),消息队列可以完成同一台计算机上的进程之间的通信,相比较管道,消息队列要复杂一些,但使用起来更加灵活和方便,本文针对 System V 消息队列,并给出了多个具体的实例,每个实例均附有完整的源代码;本文所有实例在 Ubuntu 20.04 上编译测试通过,gcc版本号为:9.4.0;本文适合 Linux 编程的初学者阅读。 阅读详情

相关推荐

IPC之四:使用 POSIX 消息队列进行进程间通信的实例

IPC 是 Linux 编程中一个重要的概念,IPC 有多种方式,本文主要介绍消息队列(Message Queues),消息队列可以完成同一台计算机上的进程之间的通信,相比较管道,消息队列要复杂一些,但使用起来更加灵活和方便,Linux 既支持 UNIX SYSTEM V 的消息队列,也支持 POSIX 的消息队列,本文针对 POSIX 消息队列,POSIX 标准引入了一个简单的基于文件的接口,使应用程序可以轻松地与消息队列进行交互;本文给出了多个具体的实例,每个实例均附有完整的源代码。

whowin 1195

解决System limit for number of file watchers reached

问题背景 系统: deepin 详情: 在用vue脚手架写项目的时候,用npm run serve启动服务,报错Error: ENOSPC: System limit for number of file watchers reached 解决办法 原因:文件监视程序的系统产生了限制,达到了默认的上限,需要增加限额。 查看限额 cat /proc/sys/fs/inotify/max_user_watch 临时增加限额 sudo sysctl fs.inotify.max_user_watches=524

lucky__peng的博客 1万+

AHP层次分析法在项目风险评估中的实战应用(含Excel模板)

本文详细介绍了AHP层次分析法在项目风险评估中的实战应用。通过构建风险层次模型、利用判断矩阵量化主观判断,并结合Excel工具进行权重计算与一致性检验,将模糊的定性风险转化为清晰的量化排序。文章以新产品上线项目为例,提供了完整的分析流程和可操作的Excel模板,帮助项目经理高效识别并优先处理核心风险。

weixin_29061465的博客 570

统信系统 Error: ENOSPC: System limit for number of file watchers reache

统信系统 Error: ENOSPC: System limit for number of file watchers reache

qq_46344419的博客 1355

鼎捷T100——二次开发:维护作业添加非实体字段

非实体字段添加

weixin_52342654的博客 1155

Frida+Objection保姆级教程:从环境搭建到实战Hook(含Wallbreaker插件使用)

本文提供了一份从零开始的Frida与Objection移动安全动态分析实战指南。详细讲解了Python环境配置、Frida-Server部署、Objection核心命令使用以及Wallbreaker插件安装与应用。通过实战案例演示如何Hook网络请求(如HttpURLConnection和OkHttp3),帮助安全研究人员快速掌握应用运行时行为分析与漏洞挖掘技能。

weixin_28318011的博客 98

Linux 踩坑-错误Error: ENOSPC: System limit for number of file watchers

问题: 系统: Ubuntu18 问题发生场景: 在使用react脚手架写案例的时候,使用指令 npm start 不成功,发生 Error: ENOSPC: System limit for number of file watchers错误。 解决办法 Error: ENOSPC: System limit for number of file watchers错误,是文件监视程序的系统产生了限制,达到了默认的上限,需要增加限额。 可以使用指令 $ cat /proc/sys/fs/inotify/ma

qq_40021015的博客 3458

ThinkpadCPU锁频0.4G问题

这篇帖子总结的很好,有这种问题可以试一试:但是切记,不要病急乱投医,冷静下来,慢慢分析和排除故障,有时候,并不需要我们知道所有的原理,只要问题解决了就是OK的。ThinkpadCPU锁频0.4G,具体是过程是今天我需要去楼下打印东西,要带着笔记本电脑,拔掉电源我就去楼下。一般这种问题很有可能是电源导致的,先不要病急乱投医,刷BIOS,装系统什么的,没用。但是确实解决了我的问题,记录下来。带着电脑回到工位,插上电源。突然我突发奇想,拔掉电源,神奇的事情发生了,CPU频率复活了,再插上电源,CPU完全恢复了。

小幽余生不加糖 1万+

ubuntu下vscode报错 System limit for number of file watchers reached

System limit for number of file watchers reached”意思是“达到文件观察者数量的系统限制”。错误产生的原因是负责监控 Linux 文件系统的。查看 Inotify 在内核中的默认配置。程序达到了系统默认的限制上限。

无心使然云中漫步 2114

分布式计算Ray框架面试题及参考答案

动态任务图执行引擎是一种能够根据任务之间的依赖关系和运行时的实际情况,动态构建和执行任务图的系统。在 Ray 中,动态任务图执行引擎允许任务在运行时根据数据可用性、计算资源状态等因素,灵活地决定后续任务的执行顺序和方式。对于 AI 应用来说,其往往具有动态性的特点。例如,在深度学习模型的训练过程中,可能需要根据训练的结果动态调整下一轮训练的参数、数据增强方式或者模型结构。动态任务图执行引擎可以很好地支持这些动态需求。它能够根据 AI 应用中各个任务的实时输出和依赖关系,动态地生成和调整任务图。

大模型大数据攻城狮的专栏 1349

PostgreSQL的学习心得和知识总结(一百二十六)|深入理解PostgreSQL数据库SQL执行时间计算 \timing 和 log_duration的使用和原理

1、参考书籍:《PostgreSQL数据库内核分析》2、参考书籍:《数据库事务处理的艺术:事务管理与并发控制》3、

孤傲小二~阿沐的博客 981

linux进程间通信(system v消息队列,阻塞式)实例

消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点。作为早期unix通信机制之一的信号能够传送的信息量有限,后来虽然POSIX 1003.1b在信号的实时性方面作了拓广,使得信号在传递信息量方面有了相当程度的改进,但是信号这种通信方式更像"即时"的通信方式,它要求接

zhangzhenghe的专栏 4513

车载软件刷写工具vFlash --- 自动化接口(Automation API)应用简介

车载软件刷写工具vFlash --- 自动化接口(Automation API)应用简介

Soly_kun的博客 359

linux测试消息队列阻塞,Linux 进程间通信(system v 消息队列, 阻塞式)实例

消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点。作为早期unix通信机制之一的信号能够传送的信息量有限,后来虽然POSIX 1003.1b在信号的实时性方面作了拓广,使得信号在传递信息量方面有了相当程度的改进,但是信号这种通信方式更像"即时"的通信方式,它要求接受信号的进程在某个时间范围内对信号做出反应,因此该信号最多在接受信号进程的生命周期内才有意义,信号所传递的信息是接近于随...

weixin_31002379的博客 600

基于STM32+华为云设计的智能鱼缸

本项目基于STM32单片机设计一款基于物联网的智能鱼缸系统,并通过应用侧接口开发上位机APP实现远程对鱼缸参数的检测和控制。该系统包括水温检测、水质检测、自动或手动换水、氛围灯灯光变换和自动或手动喂食等功能,可以帮助用户更好地管理鱼缸,提高鱼儿的生存率,同时增强用户对家庭装饰品的掌控感和科技感。

1万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

linux 消息队列_【Linux】消息队列MQ的实际应用(POSIX和 System V)

1 消息队列(Message Queue)在应用开发中,生产者,消费者的模型非常常见,一方产生数据并把数据放入队列中,而另一方从队列中取数据,先进先出。同样,在操作系统内核中,也实现了类似的功能,队列中存放的是“消息”。称之为消息队列,消息也可理解为数据。主要用途是进程间通信(IPC),所谓通信,就是进行数据交互。2 接口标准(POSIX和 System V)关于标准接口,Linux系统中提供了P...

weixin_39854326的博客 373

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

linux 查看 active mq_【Linux】消息队列MQ的实际应用(POSIX和 System V)

1 消息队列(Message Queue)在应用开发中,生产者,消费者的模型非常常见,一方产生数据并把数据放入队列中,而另一方从队列中取数据,先进先出。同样,在操作系统内核中,也实现了类似的功能,队列中存放的是“消息”。称之为消息队列,消息也可理解为数据。主要用途是进程间通信(IPC),所谓通信,就是进行数据交互。2 接口标准(POSIX和 System V)关于标准接口,Linux系统中提供了P...

weixin_33232907的博客 1171

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Linux进程间通信

进程间通信 初识进程间通信 什么是进程间通信 进程间通信简称IPC(Interprocess communication),进程间通信就是在不同进程之间传播或交换信息。 进程间通信的目的 数据传输: 一个进程需要将它的数据发送给另一个进程。 资源共享: 多个进程之间共享同样的资源。 通知事件: 一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件,比如进程终止时需要通知其父进程。 进程控制: 有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有

weixin_48565202的博客 890

Linux进程间通信之消息队列实践指南

无需配置Linux环境就能在线运行消息队列示例。平台自动生成的项目结构清晰,还能一键分享给协作伙伴。想快速验证这些概念?

SilvermistOwl67的博客 757

《网络编程卷2:进程间通信》第五章:POSIX消息队列深度解析与高性能实践

POSIX消息队列凭借其高效的优先级管理和灵活的异步机制,成为高实时性系统的首选IPC方案。本文从内核实现到应用实践,详细剖析了其核心机制,并给出了多场景代码示例。容错设计:处理进程崩溃导致的半开队列。访问控制:结合Linux Capabilities限制队列访问权限。监控工具:使用查看队列状态,调整内核参数。扩展阅读ZeroMQ:基于消息队列的分布式通信框架。RDMA:绕过内核的高性能网络通信技术。掌握POSIX消息队列,不仅能够构建高效进程间通信系统,更为理解现代分布式消息中间件奠定基础。

SEU123WW的博客 1187

Linux ipc System V 消息队列

Linux ipc System V 消息队列 消息队列提供了从一个进程向另一个进程发送有类型数据块的方法。 消息队列由操作系统内核提供,可以同时被两个进程同时看到。 每个数据块都被认为是有一个类型值的,接受者可以接受不同种类型的值。 每个消息对列的最大长度是有上限的(MSGMAX),每个消息队列的总字节数是有上限的(MSGMNB),系统上消息队列的总数也是有上限的(MSGMNI)。 I P C ...

M_jianjianjiao 283

【Linux】进程间通信>管道&&共享内存&&消息队列&&信号量详解

【Linux】进程间通信>管道&&共享内存&&消息队列&&信号量详解

m0_74722801的博客 1361

【Linux】进程间通信 —— System V

一旦这样的内存映射到共享它的进程的地址空间,这些进程间数据传递不再设计到内核,换句话说进程不再通过执行进入内核的系统调用来传递数据了。SHM_RND标志,则连接的地址会自动向下调整为SHMLBA的整数倍。注意:共享内存段不是必须手动删除,这里只是演示,应该由进程删除IPC资源。shmflg:它的两个可能取值是SHM_RND和SHM_RDONLY。返回值:成功返回一个非负整数,即该共享内存段的标识码;注意:将共享内存段与当前进程脱离并不表示删除该共享内存段。shmid:由shmget返回的共享内存标识码。

2401_82759674的博客 634

【Linux篇】第十二篇——进程间通信(管道+system V共享内存)

进程间通信介绍概念目的本质分类管道什么是管道匿名管道匿名管道的原理pipe函数匿名管道使用步骤管道读写规则管道的特点管道的大小命名管道命名管道的原理使用命令创建命名管道创建一个命名管道命令管道的打开规则 用命名管道实现serve&client通信用命名管道实现进程遥控用命名管道实现文件拷贝命名管道和匿名管道的区别命令行当中的管道system V进程间通信system V共享内存共享内存的基本原理共享内存数据结构共享内存的建立与释放共享内存的创建共享内存的释放共享内存的关联共享内存的去关联用共享内存实习ser

m0_58367586的博客 5917
上一篇: 程序员常去的14个顶级开发社区
下一篇: linux进程和线程之间通信方法和同步方法总结
readyao
博客等级 码龄11年 243粉丝 163原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值