Windows进程间通信-共享内存

Windows进程间通信之共享内存 之前自己做的一个项目涉及到进程间通信问题,我采用的是SOCKET方式。面试的时候有问过为什么不采用其他方式。好吧,其实发现共享内存更方便一点。于是自己写了一下,并且做了个测试界面。 写的程序可以在这里下载下载程序   程序启动会获得自身的窗口句柄,另外可以输入一个窗口句柄,用于进程间Windows消息通信。File Mapping Name实际上就是内核对象名,进程间依据这个来访 阅读详情

Windows进程间通信-命名管道

一、介绍

Windows平台上有很多种进程间通信(IPC:Inter-Process Communication)的方法。我将把他们列举出来,并谈谈它们的优缺点。
IPC定义:
  • 进程间通信(IPC)主要是处理的技术和机制是方便进程间通信。
  • 操作系统支持的一种进程和进程通信。这些进程可以是运行在同一台机器上,也可以是通过网络互连的不同计算机上的进程。

二、背景

通常一个一定大小软件或者工程都需要两个或者多个进程相互通信。通信的方式有多种。比如:如果进程使用消息队列,那么你可以使用消息WM_COPYDATA在进程间传递信息。Windows中的剪切板也可以利用。

COM/DCOM/RPC 这些技术也能用于进程间通信,这些技术超出了本文。

还有一些技术可能会提到:

  • 共享内存
  • 命名管道(Named Pipe)
  • Socket通信(WinSock)
  • 油槽(Mailslot)
理解Windows同步时学习IPC的基础。几乎每个IPC机制的使用都伴随着进程间同步。
一下是一些进程间用来同步的同步对象:
  • Mutex
  • Semaphore
  • Events
还有一个同步对象--临界区。然而,临界区是用来控制一个进程、多个线程间的同步的。它不能用于进程间同步。Windows同步的详细讨论就不深入了。

三、代码实现

3、1共享内存

下面的一些API是共享内存(内存映射对象)工作时使用的:

  • CreateFileMapping()
  • MapViewOfFile()
  • UnMapViewOfFile()
  • CloseHandle()
共享内存特点:
  • 代码具有平台依赖性
  • 只能在一台计算机中的进程中使用
  • 一旦进程映射对象映射到进程区域,用起来非常容易
  • 也许是所有IPC中最快的方式了
共享内存实现进程间通信的源码在另一篇文章中:

3、2命名管道(Named Pipe)

命名一个对象有助于进程间共享对象的处理。名字区分大小写。已命名对象是内核对象,内核对象是特定进程相关的,这个进程使用它们之前需要先创建。

管道遵循先进先出原则,有两种类型的管道:匿名管道,命名管道

创建匿名管道使用函数CreatePipe(),匿名管道存在与本地,不能用于在网络上传输。匿名管道没有名字,单向的,通常用来从父进程向子进程传递数据。

命名管道可以单向或者多项传输数据。命名管道可以在一台计算机或者通过网络连接的多台计算机中相互通信。

下面是一些Win32命名管道使用的API:
  • CreateNamedPipe()
  • ConnectNamedPipe()
  • WaitNamedPipe()
  • DisconnectNamedPipe()
  • ReadFile()
  • WriteFile()
  • CloseHandle()

命名管道进程通信服务端的代码:
#include "stdafx.h"
#include "windows.h"

//Name given to the pipe
#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"
//Pipe name format - \\.\pipe\pipename

#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"

int main(int argc, char* argv[])
{
     HANDLE hPipe;
     
     hPipe = CreateNamedPipe( 
          g_szPipeName,             // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFFER_SIZE,              // output buffer size 
          BUFFER_SIZE,              // input buffer size 
          NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
          NULL);                    // default security attribute 
     
     if (INVALID_HANDLE_VALUE == hPipe) 
     {
          printf("\nError occurred while " 
                 "creating the pipe: %d", GetLastError()); 
          return 1;  //Error
     }
     else
     {
          printf("\nCreateNamedPipe() was successful.");
     }
     
     printf("\nWaiting for client connection...");
     
     //Wait for the client to connect
     BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);
     
     if (FALSE == bClientConnected)
     {
          printf("\nError occurred while connecting" 
                 " to the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  //Error
     }
     else
     {
          printf("\nConnectNamedPipe() was successful.");
     }
     
     char szBuffer[BUFFER_SIZE];
     DWORD cbBytes;
     
     //We are connected to the client.
     //To communicate with the client 
     //we will use ReadFile()/WriteFile() 
     //on the pipe handle - hPipe
     
     //Read client message
     BOOL bResult = ReadFile( 
          hPipe,                // handle to pipe 
          szBuffer,             // buffer to receive data 
          sizeof(szBuffer),     // size of buffer 
          &cbBytes,             // number of bytes read 
          NULL);                // not overlapped I/O 
     
     if ( (!bResult) || (0 == cbBytes)) 
     {
          printf("\nError occurred while reading " 
                 "from the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  //Error
     }
     else
     {
          printf("\nReadFile() was successful.");
     }
     
     printf("\nClient sent the following message: %s", szBuffer);
     
     strcpy(szBuffer, ACK_MESG_RECV);
     
     //Reply to client
     bResult = WriteFile( 
          hPipe,                // handle to pipe 
          szBuffer,             // buffer to write from 
          strlen(szBuffer)+1,   // number of bytes to write, include the NULL 
          &cbBytes,             // number of bytes written 
          NULL);                // not overlapped I/O 
     
     if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
     {
          printf("\nError occurred while writing" 
                 " to the client: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  //Error
     }
     else
     {
          printf("\nWriteFile() was successful.");
     }
     
     CloseHandle(hPipe);
     return 0; //Success
}

命名管道客户端:
#include "stdafx.h"
#include "windows.h"

//Name given to the pipe
#define g_szPipeName "\\\\.\\Pipe\\MyNamedPipe"
//Pipe name format - \\servername\pipe\pipename
//This pipe is for server on the same computer, 
//however, pipes can be used to
//connect to a remote server

#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"

int main(int argc, char* argv[])
{
     HANDLE hPipe;
     
     //Connect to the server pipe using CreateFile()
     hPipe = CreateFile( 
          g_szPipeName,   // pipe name 
          GENERIC_READ |  // read and write access 
          GENERIC_WRITE, 
          0,              // no sharing 
          NULL,           // default security attributes
          OPEN_EXISTING,  // opens existing pipe 
          0,              // default attributes 
          NULL);          // no template file 
     
     if (INVALID_HANDLE_VALUE == hPipe) 
     {
          printf("\nError occurred while connecting" 
                 " to the server: %d", GetLastError()); 
          //One might want to check whether the server pipe is busy
          //This sample will error out if the server pipe is busy
          //Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
          return 1;  //Error
     }
     else
     {
          printf("\nCreateFile() was successful.");
     }
     
     //We are done connecting to the server pipe, 
     //we can start communicating with 
     //the server using ReadFile()/WriteFile() 
     //on handle - hPipe
     
     char szBuffer[BUFFER_SIZE];
     
     printf("\nEnter a message to be sent to the server: ");
     gets(szBuffer);
     
     DWORD cbBytes;
     
     //Send the message to server
     BOOL bResult = WriteFile( 
          hPipe,                // handle to pipe 
          szBuffer,             // buffer to write from 
          strlen(szBuffer)+1,   // number of bytes to write, include the NULL
          &cbBytes,             // number of bytes written 
          NULL);                // not overlapped I/O 
     
     if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
     {
          printf("\nError occurred while writing" 
                 " to the server: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  //Error
     }
     else
     {
          printf("\nWriteFile() was successful.");
     }
     
     //Read server response
     bResult = ReadFile( 
          hPipe,                // handle to pipe 
          szBuffer,             // buffer to receive data 
          sizeof(szBuffer),     // size of buffer 
          &cbBytes,             // number of bytes read 
          NULL);                // not overlapped I/O 
     
     if ( (!bResult) || (0 == cbBytes)) 
     {
          printf("\nError occurred while reading" 
                 " from the server: %d", GetLastError()); 
          CloseHandle(hPipe);
          return 1;  //Error
     }
     else
     {
          printf("\nReadFile() was successful.");
     }
     
     printf("\nServer sent the following message: %s", szBuffer);
     
     CloseHandle(hPipe);
     return 0; //Success
}

特点:
  • 源码具有平台相关性
  • 命名管道用起来简单
  • 能通过网络通信

3、3Socket通信(WinSock)

WinSock提供了很高的网络能力。支持TCP/IP协议,同时还有其他协议---AppleTalk, DECNet, IPX/SPX等

WinSock使用的函数:

  • socket()
  • bind()
  • listen()
  • accept()
  • connect()
  • send()
  • recv()
特点:
  • 使用广泛,本机或者网络都能运行。而且能跨平台和协议
  • 使用WinSock需要扎实的网络基础知识。

3、4油槽(Mailslot)

油槽用于进程间的单向通信。油槽服务器是只读的,它从客户端读取数据并发送。客户端是只写的,然后发送信息给服务端。油槽信息只能传输大约400字节。

油槽能在一定的域范围内广播消息。如果一个域中的进程创建了相同名字的油槽,那么消息就会发送给所有进程。

油槽的函数:

  • CreateMailSlot()
  • GetMailslotInfo()
  • SetMailslotInfo()
  • ReadFile()
  • WriteFile()
  • CloseHandle()
油槽服务端代码:

#include "stdafx.h"
#include "windows.h"

//Name given to the Mailslot
#define g_szMailslot "\\\\.\\mailslot\\MyMailSlot"
//Mailslot name format - \\.\mailslot\mailslotname

#define BUFFER_SIZE 1024 //1k

int main(int argc, char* argv[])
{
     HANDLE hMailslot;
     
     hMailslot = CreateMailslot( 
          g_szMailslot,             // mailslot name
          BUFFER_SIZE,              // input buffer size 
          MAILSLOT_WAIT_FOREVER,    // no timeout
          NULL);                    // default security attribute 
     
     if (INVALID_HANDLE_VALUE == hMailslot) 
     {
          printf("\nError occurred while" 
                 " creating the mailslot: %d", GetLastError()); 
          return 1;  //Error
     }
     else
     {
          printf("\nCreateMailslot() was successful.");
     }
     
     //Mailslot is one-way communication
     //Server will only read
     //Using ReadFile()
     char szBuffer[BUFFER_SIZE];
     DWORD cbBytes;
     BOOL bResult;
     
     printf("\nWaiting for client connection...");
     
     while(1) //Infinite, till user terminates the console app
     {
          //Read client message
          bResult = ReadFile( 
               hMailslot,            // handle to mailslot 
               szBuffer,             // buffer to receive data 
               sizeof(szBuffer),     // size of buffer 
               &cbBytes,             // number of bytes read 
               NULL);                // not overlapped I/O 
          
          if ( (!bResult) || (0 == cbBytes)) 
          {
               printf("\nError occurred while reading" 
                      " from the client: %d", GetLastError()); 
               CloseHandle(hMailslot);
               return 1;  //Error
          }
          else
          {
               printf("\nReadFile() was successful.");
          }
          
          printf("\nClient sent the following message: %s", szBuffer);
     }
     
     CloseHandle(hMailslot);
     return 0; //Success
}

油槽客户端代码:
#include "stdafx.h"
#include "windows.h"

//Name given to the Mailslot
#define g_szMailslot "\\\\.\\mailslot\\MyMailSlot"
//Mailslot name format - \\.\mailslot\mailslotname
//This mailslot is for server on the same computer, 
//however, mailslots can be used to
//connect to a remote server

#define BUFFER_SIZE 1024 //1k

int main(int argc, char* argv[])
{
     HANDLE hMailslot;
     
     //Connect to the server mailslot using CreateFile()
     hMailslot = CreateFile( 
          g_szMailslot,          // mailslot name 
          GENERIC_WRITE,         // mailslot write only 
          FILE_SHARE_READ,       // required for mailslots
          NULL,                  // default security attributes
          OPEN_EXISTING,         // opens existing mailslot 
          FILE_ATTRIBUTE_NORMAL, // normal attributes 
          NULL);                 // no template file 
     
     if (INVALID_HANDLE_VALUE == hMailslot) 
     {
          printf("\nError occurred while connecting" 
                 " to the server: %d", GetLastError()); 
          return 1;  //Error
     }
     else
     {
          printf("\nCreateFile() was successful.");
     }
     
     //We are done connecting to the mailslot, 
     //Mailslot communication is one-way, 
     //client will just write to mailslot
     //Using WriteFile()
     
     char szBuffer[BUFFER_SIZE];
     
     printf("\nEnter a message to be sent to the server: ");
     gets(szBuffer);
     
     DWORD cbBytes;
     
     //Send the message to server
     BOOL bResult = WriteFile( 
          hMailslot,            // handle to mailslot 
          szBuffer,             // buffer to write from 
          strlen(szBuffer)+1,   // number of bytes to write, include the NULL
          &cbBytes,             // number of bytes written 
          NULL);                // not overlapped I/O 
     
     if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
     {
          printf("\nError occurred while writing" 
                 " to the server: %d", GetLastError()); 
          CloseHandle(hMailslot);
          return 1;  //Error
     }
     else
     {
          printf("\nWriteFile() was successful.");
     }
     
     CloseHandle(hMailslot);
     return 0; //Success
}

特点:
  • 代码具有平台相关性
  • 油槽只能提供单向的数据传送
  • 消息的大小限制在400字节左右
  • 油槽支持广播
  • 能通过网络通信
  • 使用简单

四、总结

本文简要介绍Windows IPC机制。试图可用选项列表,并提供了一些代码来帮助读者了解如何使用Windows IPC机制。这篇文章不是很详尽的或详细的。读者需要引用其他详细的关于这一主题的资料了解Windows IPC在一个更好的方式。






Windows C++/Qt进程间通信:共享内存与消息机制实战详解 进程间通信(IPC)是操作系统与分布式系统的基础概念,用于实现不同进程间的数据交换与协同工作。其核心原理在于突破进程地址空间的隔离,通过操作系统提供的共享资源或通信机制传递信息。在技术价值上,高效的IPC能显著提升应用性能与模块化程度,是实现多进程协作、微服务架构及高性能计算的关键。在Windows桌面应用开发中,共享内存配合Windows消息机制是一种经典的高性能IPC方案,尤其适用于C++/Qt项目中的本地进程间实时数据同步与事件通知场景。该方案利用共享内存实现高速数据交换,并通过Windows消息机制 阅读详情

相关推荐

视频教程-Ps教程之神奇的Photoshop自动化技术-Photoshop

Ps教程之神奇的Photoshop自动化技术 10年以上IT行业工作经验、三...

weixin_28812857的博客 1481

windows上共享内存操作

windows共享内存 共享内存主要是通过映射机制实现的。windows下进程的地址空间在逻辑上是相互隔离的,但是在物理上确实相互重叠的。所谓的重叠是指同一块内存区域可能被多个进程同时使用。 在windows程序开发过程中,当多个进程之间需要访问共同的数据的时候,最好的方式就是使用共享内存进行处理。 编写共享内存的程序模式上基本上是一样的,大致为下面几个过程: 调用CreateFileMappi...

llfjcmx的博客 7221

TI AWR1642实战指南:CCS调试mmWave Demo Visualizer全流程解析

本文详细解析了TI AWR1642开发板的CCS调试流程,重点介绍了mmWave Demo Visualizer的使用方法。从环境准备、版本确认到bin文件烧录、CCS工程配置,再到调试启动与结果验证,提供了全流程的实战指南,帮助开发者快速掌握AWR1642的调试技巧,避免常见错误。

weixin_30790841的博客 117

深入理解和实现Windows进程间通信(共享内存)

Windows共享内存(Shared Memory in Windows)是一种操作系统机制,允许不同的进程(程序)共享一段内存空间。这意味着多个进程可以同时访问同一个内存区域,用以交换数据或进行通信,这是进程间通信(IPC)的一种形式。使用共享内存通常可以提高应用程序之间的数据交换效率,因为它避免了数据的复制过程,直接在内存中进行读写。基于实际的磁盘文件或系统分页文件来创建内存映射文件对象。

LeoLei8060的博客 6564

kali破解WIFI密码

今天我们来学习一下通过kali和一个好的字典来破解WIFI密码。 声明:此教程仅供学习研究使用,严禁使用此技术进行非法行为 首先我们需要一个具有监听功能的无线网卡(这里我给大家推荐:rt3070) 本章实验通过虚拟机VMware进行: 先通过services.msc来打开VMware USB Arbitration Service 插上监听网卡,连接USB到虚...

#的博客 5847

【汽车主动降噪】【ANC算法优化】基于深度学习的汽车主动噪声控制技术研究及Matlab实现

本文探讨了基于深度学习的汽车主动降噪(ANC)算法优化方案。针对传统FXLMS算法在应对复杂、非线性车内噪声时的局限性,文章详细解析了如何利用神经网络作为非线性建模器、深度滤波器或智能步长调节器来增强ANC系统性能,并提供了完整的Matlab仿真实战流程,展示了深度学习在提升降噪效果和收敛速度方面的潜力。

seed的博客 547

Windows进程通信之共享内存通信

本文章先介绍共享内存是啥,再介绍使用共享内存要涉及的4个函数,最后将实例代码附上,实例代码用于加深理解,读者也可以模仿用例改编使用。 1. 共享内存的简单介绍 共享内存就是允许两个不相关的进程访问同一个虚拟内存。共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式。不同进程之间共享的内存通常安排为同一段物理内存。进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都...

Quellaaa的博客 2098

ISP 芯片作为独立设备节点的驱动实现要点:多平台结构差异与工程部署策略解析

随着 ISP(Image Signal Processor)从 SoC 集成形态向独立芯片模块演进,在多摄系统、高动态场景、AI 图像前处理等需求下,ISP 作为单独设备节点进行驱动设计已成为主流趋势。尤其在多 SoC 平台(如高通、联发科、展锐、Rockchip)中,不同厂商对 ISP 的抽象方式与驱动接口处理存在显著差异。本文以实际工程为基础,从 Linux 内核下的设备注册流程、media\_entity 配置、VideoDevice 分配、数据 DMA 管理、与 sensor 的流同步控制等关键点展

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 1478

共享内存和内存映射-同步机制和严格数据一致

特性共享内存内存映射文件性能更高,数据直接在内存共享略低,涉及文件系统操作数据持久化不支持(除非显式实现)支持,数据自动持久化到磁盘跨语言支持需要严格的数据布局和同步机制较为方便,通过文件接口实现跨语言共享实现复杂度较高,需管理同步机制和内存布局较低,相对简单,只需处理文件映射和同步安全性与权限管理需要显式控制共享内存的权限基于文件系统的权限控制,更加直观和灵活在仅写入,Python仍需确保数据布局一致,定义清晰的数据结构和格式,确保Go和Python对数据的解释一致。

远方雪的专栏 1105

C#上位机入门全指南:从零搞懂工业自动化的「大脑中枢」,看完就能写第一个采集程序

C#上位机的入门门槛真的不高,它难的从来不是语言本身,是对工业场景的理解,是工程化的稳定落地。很多人学了一堆语法、背了一堆协议,还是做不好项目,就是因为忽略了最朴素的道理:工业软件的核心价值,是稳、是好用、是能解决现场的实际问题。如果你刚入行,不用焦虑,从第一个采集程序开始写,从第一个项目开始踩坑,慢慢积累,很快就能上手。工控这个领域,经验永远比新技术重要,沉下心做两年,你也能成为独当一面的工程师。

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 404

第4.4节:保护轴承与安全系统

飞轮储能系统的保护轴承是磁悬浮失效时的最后安全屏障,其设计需满足非干涉性、高可靠性和有限寿命承载原则。转子跌落过程包括自由下落、碰撞、进动和滑行减速等复杂非线性阶段,产生远超常规工况的冲击载荷。保护轴承需具备极限冲击容量、高温耐受性和抗卡死能力。系统采用多层级安全架构,结合故障诊断与健康管理,通过监测振动、电流等参数实现预测性维护。安全停机序列有序结合主动制动与被动摩擦制动,确保系统在极端工况下的安全。保护轴承类型多样,需根据系统规模与可靠性要求合理选择。

技术工作者 244

windows下共享内存和数据同步

windows下共享内存和数据同步 前言 当在windows系统下,有多个exe程序同时频繁处理大量文件操作时,而且还要保证效率,假设一个简单的场景:一个程序负责产生数据,另一个程序负责读取数据,那么问题来了,两个进程都是彼此独立的,彼此都不知道各自的存在,如何读取数据呢?我们首先能想到的是使用共享内存。但是,在多核情况下,读写如何同步呢?试想以下场景: 场景1:出现A进程数据写了一部分数据在共享内存中(没写完),而这时B进程读这块内存,那么读取的这块内存数据就不完整。 场景2

aoxuestudy的专栏 2007

2万字Mplus最细入门指南,3小时从小白到跑通毕业论文模型!Mplus完全指南第一篇

这是我被问得最多的问题。「语法是什么?」「那个黑底白字的东西怎么写?」「是不是要会编程才能用?」 别怕。真的别怕。语法就像你给Mplus发微信,你打字告诉它要做什么,它收到后执行,直接把结果吐给你。它不说「好的亲」,直接给你输出结果。就这么简单。你平时用SPSS是用鼠标点来点去对吧?点菜单、选变量、点确定。Mplus的语法做的是完全一样的事,只不过你不用鼠标了,用打字代替了点击。比如你想做一个线性回归,自变量是X,因变量是Y,控制变量是gender、age:用SPSS?

Codewar的博客 88

Windows下进程间通信——共享内存

window API 共享内存 文件映射 CreateFileMapping

ypy_datou的专栏 2764

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Windows下复制粘贴的进程间通信模型——共享内存

Windows下复制粘贴的进程间通信模型——共享内存 最近在学习操作系统时遇到的一道思考题:Windows下复制粘贴操作属于哪种进程间通信方式?出于直觉,那必然是共享内存的方式,但是苦于没有直接的证据,一通搜索之后没有答案?最后还是在StackOverflow上找到了答案,最后还是在StackOverflow上有所收获。 附上讨论的链接https://stackoverflow.com/questions/52887507/how-is-copy-paste-possible/53177697#53177

M010K的博客 1869

深度学习工具包 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")

Windows进程间通信之命名共享内存

Windows进程间通信之命名共享内存概述消息服务端服务端程序工作过程消息消息端消费端程序工作过程 概述   命名共享内存,在windows下主要是使用windows提供的几个API函数,创建、打开、读取、销毁命名共享内存,共享内存比较适合有消息服务者和消息消费者的场景,优点是效率高,适合较多信息的共享,缺点是不太适合双方需要频繁沟通信息的场景。   以下程序分为两部分,一个是负责生产消息的消息服务进程,一个是负责消费消息的消息消费进程;前者创建指定名称的命名共享内存,并将服务消息写入共享内存中,后者打开指

find_the_ferry的博客 495

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

实践用“共享内存”(Named Shared Memory)实现Windows进程间通信

目标 参考资料:《Windows共享内存示例 - 可笑痴狂 - 博客园》 《Creating Named Shared Memory - Win32 apps | Microsoft Docs》 接口 代码实践 效果

主要记录了工作之余胡乱学习的知识,大部分是入门级别的内容。 主要是为了自己未来有用到时能快速上手,相当于是个备忘录。 当然,也很开心其他人可以从中找到对自己有用的信息。^_^ 2999

Windows进程间通信实例--共享内存

共享内存--是允许两个不相关的进程访问同一个逻辑内存,共享内存是两个正在运行的进程之间共享和传递数据的一种非常有效的方式。不同进程之间共享的内存通常为同一段物理内存。进程可以将同一段物理内存连接到他们自己的地址空间中,所有的进程都可以访问共享内存中的地址。如果某个进程向共享内存写入数据,所做的改动将立即影响到可以访问同一段共享内存的任何其他进程。 采用共享内...

Alisa_xf的博客 963

windows 进程间通信共享内存

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34235135的博客 164

Windows IPC 进程间通信之共享内存

1. 必须先创建共享内存,才能够打开 2. 关闭共享内存后,就无法打开 3. 使用Global则必须是管理员权限才能创建或打开 相当于要读写一个文件,必须先创建文件才能够打开,删掉文件后就无法打开。权限不够的话就无法读写 // share_memory.cpp : 定义控制台应用程序的入口点。 //

在线笔记 685

java web 进程通信_Windows进程间通信之共享内存

之前自己做的一个项目涉及到进程间通信问题,我采用的是SOCKET方式。面试的时候有问过为什么不采用其他方式。好吧,其实发现共享内存更方便一点。于是自己写了一下,并且做了个测试界面。程序启动会获得自身的窗口句柄,另外可以输入一个窗口句柄,用于进程间Windows消息通信。File Mapping Name实际上就是内核对象名,进程间依据这个来访问共享内存。Windows下的共享内存实际是用文件映射实...

weixin_28998761的博客 322

Windows Mobile使用Shared Memory(共享内存)进行IPC(进程间通信)的开发

背景 在Unix-like系统进行IPC(Inter-process communication)通信,Shared memory是效率最高的,我称之为IPC的王中王。 简介 本文讲述在Windows Mobile和Windows Embedded CE下如何使用Shared Memory(共享内存)进行IPC(进程间通信)。演示如何使用Shared Memory共享数据,使用Name...

weixin_30467087的博客 228

Windows进程间通信--共享内存

共享内存主要是通过映射机制实现的。   Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的。所谓的重叠是指同一块内存区域可能被多个进程同时使用。当调用 CreateFileMapping 创建命名的内存映射文件对象时,Windows 即在物理内存申请一块指定大小的内存区域,返回文件映射对象的句柄 hMap。为了能够访问这块内存区域必须调用 MapViewOfFile 函数

chenxiao88957478的博客 723

Windows环境下利用 共享内存 实现进程间通信的C/C++代码---利用CreateFileMapping和MapV

Windows环境下利用 共享内存 实现进程间通信的C/C++代码---利用CreateFileMapping和MapV

ugfffj的博客 888
上一篇: 调试技术--Windows平台调试技术(一)
下一篇: 多数据库支持类库
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址