实时操作系统要求在一个触发信号到来之后能够在一个确定的时间点之前进行响应处理。它与普通的非实时操作系统区别在于:普通操作系统的响应延时受系统负载影响较大,轻负载时可能会满足响应的要求,但是在负载变大的时候响应会变慢,并且这个响应时间是不确定的。因此一个足够快的系统不代表是一个实时操作系统,实时系统必须要求在负载变化的情况下依然能够满足确定的响应时间,如果无法在确定的时候下完成响应,那么会导致系统错误。
低延迟,高可靠性。
常规意义上来讲,Linux并不算是一个实时操作系统,它并没有很强的实时处理能力,但是如果加入了PREEMTP_RT补丁后,它可以成为一个实时操作系统。PREEMTP_RT补丁并不属于内核主线,不同内核版本都有对应的PREEMTP_RT补丁,它是由单独项目维护的(Real-Time Linux Project)。我们需要先下载并打上PREEMTP_RT补丁,然后重新配置内核的实时抢占模型,重新编译后才能成为一个实时系统。经过打上补丁,PREEMTP_RT Linux是可以达到硬实时需求的。
实时操作系统分为两种,硬实时和软实时。这两种可以根据实时需求应用在不同的环境中:
硬实时要求极低的响应延迟,否则会导致严重的后果,比如航空航天和工业控制、汽车制动控制系统等,要求特别高的确定性和可靠性。代表产品:vxwork
软实时也要求低延迟和确定的响应时间,但可靠性要求没有硬实时那么高,即使违反截止期限并不会带来严重的后果,比如实时视频流的处理,最多的后果就是视频帧丢失和卡顿。
Linux内核主线中的关于Preemption Model(抢占模型)的配置有三种:
Preemption Model:
The traditional Linux preemption model, geared towards throughput . System call returns and interrupts are the only preemption points.
This option reduces the latency of the kernel by adding more “explicit preemption points” to the kernel code [. . . ] at the cost of slightly lower throughput . In addition to explicit preemption points, system call returns and interrupt returns are implicit preemption points.
This option reduces the latency of the kernel by making all kernel code (that is not executing in a critical section) preemptible 3). An implicit preemption point is located after each preemption disable section.
以上三种是内核中自带的配置,但是这三种都达不到作为一个实时操作系统的低延迟要求,这也就是为什么前面说常规Linux不是一个实时操作系统。
RT Linux中定义了两种更加注重实时性的抢占模型,需要内核打上PREEMPT_RT patch才能看到:
This preemption model resembles the “Preemptible Kernel (Low-Latency Desktop)” model. Besides the properties mentioned above, threaded interrupt handlers are forced (as when using the kernel command line parameter threadirqs). This model is mainly used for testing and debugging of substitution mechanisms implemented by the PREEMPT_RT patch.
All kernel code is preemptible except for a few selected critical sections. Threaded interrupt handlers are forced. Furthermore several substitution mechanisms like sleeping spinlocks and rt_mutex are implemented to reduce preemption disabled sections. Additionally, large preemption disabled sections are substituted by separate locking constructs. This preemption model has to be selected in order to obtain real-time behavior.
下载
wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.12.tar.xz
wget https://www.kernel.org/pub/linux/kernel/projects/rt/4.4/patch-4.4.12-rt19.patch.xz
配置内核
配置内核为完全实时模型(Fully Preemptible Kernel )
CONFIG_PREEMPT_RT_FULL
编译运行
编译和运行和普通的内核一样。
参考链接:
Real-Time Linux Wiki:https://rt.wiki.kernel.org/index.php/Main_Page
Real-Time Linux Project:https://wiki.linuxfoundation.org/realtime/start