简体   繁体   English

如何在实时Linux中的进程之间进行通信?

[英]How to communicate between processes in realtime Linux?

There are a lot of examples how to write realtime code for RT-Linux by FSMLabs but this distro has been abandoned many years ago. 有许多示例由FSMLabs如何为RT-Linux编写实时代码,但是此发行版已被放弃很多年了。 Currently PREEMPT_RT patch for vanilla kernel is actively developed but there are only few code examples on official Wiki. 目前,针对香草内核的PREEMPT_RT补丁正在积极开发中,但是官方Wiki上只有很少的代码示例。 First let me introduce my issue. 首先让我介绍一下我的问题。

I'm writing a project containing 2 programs: 我正在写一个包含2个程序的项目:

  1. Virtual machine of byte code - it must work as realtime application - it has 64 KB of I/O memory and 64 KB for byte code. 字节码虚拟机-它必须作为实时应用程序使用-它具有64 KB的I / O内存和64 KB的字节码。
  2. Client program - it will read and write I/O memory, start/pause machine, load new programs, set parameters, etc. It doesn't have to be realtime. 客户端程序-它将读写I / O内存,启动/暂停机器,加载新程序,设置参数等。它不一定是实时的。

How to communicate between these processes to keep process (1) realtime and avoid page faults or other behaviors that can interfere realtime app? 如何在这些进程之间进行通信以保持进程(1)实时,并避免页面错误或其他可能干扰实时应用程序的行为?

Approach 1. Use only threads 方法1.仅使用线程

There are 2 threads: 有2个线程:

  • virtual machine thread with highest priority 具有最高优先级的虚拟机线程

  • client thread with normal priority that communicates with user and machine Both threads have access to all global variables by name. 与用户和计算机通信的具有正常优先级的客户端线程这两个线程都可以按名称访问所有全局变量。 I can create additional buffer for incoming/outcoming data after each machine cycle. 我可以在每个机器周期之后为传入/传出的数据创建额外的缓冲区。 However, if client thread causes application crash, machine thread will terminate too. 但是,如果客户端线程导致应用程序崩溃,机器线程也将终止。 It's also more difficult to implement remote access. 实现远程访问也更加困难。

Approach 2. Shared memory 方法2。共享内存

In old FSMLabs it's recommended to use shared global memory between processes. 在旧的FSMLabs中,建议在进程之间使用共享的全局内存。 Modern PREEMPT_RT's Wiki page recommends using mmap() for process data sharing but in the same article it discourages mmap() because of page faults. 现代的PREEMPT_RT的Wiki页面建议使用mmap()进行过程数据共享,但在同一篇文章中,由于页面错误,它不鼓励mmap()。

Approach 3. Named pipes 方法3。命名管道

It's more flexible way to communicate between processes. 这是进程之间进行通信的更灵活的方式。 However, I'm new to programming in Linux. 但是,我是Linux编程的新手。 We want to share memory between machine and client but it should also provide a way to load new program (file path or program code), stop/start machine, etc. Old FSMLabs RT-Linux implemented its own FIFO queues (named pipes). 我们希望在机器和客户端之间共享内存,但它还应该提供一种加载新程序(文件路径或程序代码),停止/启动机器等的方法。旧的FSMLabs RT-Linux实现了自己的FIFO队列(命名管道)。 Modern PREEMPT_RT doesn't. 现代的PREEMPT_RT则没有。 Can using names pipes break realtime behavior? 使用名称管道可以破坏实时行为吗? How to do it properly? 怎么做呢? Should I read data with O_NONBLOCK flag or create another thread for reading/writing data from/to pipe? 我应该使用O_NONBLOCK标志读取数据还是创建另一个线程用于从管道读取数据或向管道写入数据?

Do you know other ways to communicate between processes where one process must be realtime? 您是否知道在一个流程必须是实时的流程之间进行通信的其他方式? Maybe I need only threads. 也许我只需要线程。 However, consider a scenario that more clients are connected to virtual machine process. 但是,请考虑将更多客户端连接到虚拟机进程的情况。

为了在同一主机操作系统上执行的进程之间交换数据,您还可以使用UNIX域套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM