임베디드 리눅스 RTOS - imbedideu linugseu RTOS

제가 개발자로서 처음 해본 일은 임베디드 시스템 개발 이었습니다.

임베디드 시스템이 뭐인지는 대략 나중에 잘 정리해서 설명 드리겠습니다.

어쨌든 임베디드 시스템이란 그런 것 같습니다. 일단 하드웨어적인 와꾸를 잘 잡아 놓고 이제 거기에 들어가는 소프트웨어를 개발해야 합니다.

하드웨어적인 와꾸야.. 사실 큰 문제는 아닌 것 같습니다. 물론 하드웨어적인 와꾸가 핵심인 분야도 있어서 그런 분야는 제외합니다.

일단 어느 정도 제품을 팔아본 회사라면 대충 와꾸 잡는 프로세스는 다 구축이 되어 있을 것이고 설계적인 면에서 이상 없고 부품들이 다 멀쩡하면 더 이상 왈가불가 할 것이 없어 보입니다.

문제는 소프트웨어입니다.ㅋ

끝까지 속 썩이고 맘대로 안 되는게 바로 소프트웨어 같습니다.

운영체제 자체도 특이합니다. 저는 vxWorks 라는 리얼타임OS를 썼었는데.. 요새는 이거 안 쓰고 임베디드 리눅스가 대세인 것 같습니다.

vxWorks는 가격이 굉장히 비쌉니다. 한 Copy에 5천만원인가 하는 것으로 알고 있습니다. 거기다가 뭔가 한 제품을 개발 해서 팔아먹을 때마다 라이선스를 지불해야 합니다.

안정성 면에서는 타의 추종을 불허하는 운영체제이지만.. 결코 대중적이지는 않습니다.

리눅스 시스템 프로그래밍도 좀 해보면서 vxWorks RTOS랑 비교해본 결과 둘 다 기본은 C언어 입니다. C++도 사용되긴 합니다만 보통 임베디드 시스템에서는 C++ 보다는 C를 선호하는 것 같습니다.

아무래도 복잡한 기교보다는 안정성을 중시하기 때문에 제 생각에도 막 이리저리 복잡해질 수 있는 C++ 보다는 C가 임베디드 시스템 개발에 맞는 것 같습니다.

vxWorks는 몇 가지 장점이 있는데 우선 태스크 관리가 굉장히 심플하고 편합니다. 리눅스에서는 프로세스 개념(vxWorks 태스크 = 리눅스 프로세스)인데 리눅스에서 프로세스를 다룰려고 하면 부모 넣고 자식 넣고 포크 쓰고 어쩌구저쩌구 약간 복잡합니다.ㅋ

vxWorks는 태스크스폰이란 함수 하나로 한 큐에 다 끝내버릴 수 있습니다.ㅋ

그 외에 시간 관리 기능 또한 굉장히 좋습니다. 아마도 이게 RTOS의 핵심이 아닐까 싶습니다. 시스템 클럭을 설정해놓고 그 설정한 클럭에 맞춰서 모든 동작을 정확한 시간을 두고 동작시킬 수 있습니다.

시간 간격이 1초, 2초 뭐 이런 것이 아니라 0.001초 0.0001초 단위로 가능합니다. 예를 들어 메시지 큐에서 데이터 하나 받는데 0.001초 마다 한 바이트씩 받고 싶다면 아주 간단하게 메시지리시브 할 때 파라미터 변수로 0.001이라는 인자를 넣어주면 땡입니다.

리눅스에서 이 짓 하려면 아주 짜증스럽습니다. ㅋㅋ 시간관련 라이브러리 참조해서 막 이 짓 저 짓 해야 되는데 제가 리눅스 시스템 프로그래밍을 깊게 안 해봐서 그렇겠지만 어쨌든 vxWorks가 훨씬 편리하고 강력한 것이 사실입니다.

그러나저러나 그렇다고 해도 임베디드 소프트웨어 개발… 어렵습니다. 개발자 라는 직무 자체가 어렵긴 한데 임베디드 시스템 같은 경우 쓰이는 곳이 거의 인간의 생명과 관계된 분야가 많아서 조그마한 버그 하나가 무지막지한 대형사고 및 회생불가능의 손해로 직결될 수 있습니다.

예를 들면, 비행기 개발자가 비행기 제어 시스템을 만들었는데 거기에 아주 사소한 버그가 하나 있었고 그게 잡히지 않은 채 비행기가 납품되었습니다. 비행기는 승객 200명을 태우고 가다가 그 버그로 인해 추락해버립니다.

여러 가지 분석 끝에 그 버그가 밝혀집니다. 그러면 뭐.. 비행기 개발 회사고 뭐고 끝장나는 것 입니다. 당장 물어줘야 되는 손해는 물론 앞으로 비즈니스 길도 콱 막혀버리겠지요.

그래서 임베디드 시스템 개발이란 어쩌면 생명을 담보로 한 무시무시한 후덜덜한 직무일 수도 있습니다.ㅋ

반대로 보면 그만큼 아무나 할 수 없기에 보람 있고 대략 연봉도 좀 받고 그럴 수 있겠습니다.


by Tirichlabs

Embedded Linux utilizes Linux kernel for an embedded device, but it is quite different from the standard Linux OS. Its application to embedded systems is motivated by the availability of device support, file-systems, network connectivity, and UI support. It is a customized version of Linux for embedded systems, consequently having a much smaller size and minimal features and requires less processing power. Based on embedded system requirements, the Linux kernel is modified and optimized. Such embedded Linux can only run device-specific purpose-built applications.

The Real-Time Operating System (RTOS) with minimal code is used for such applications where least and fix processing time is required. RTOS is a time-sharing system based on clock interrupts that implement priority sequences to execute a process. In the event of a high priority, interrupt is generated by the system, the running low priority processes are stopped and the interrupt is served. The real-time operating system requires less operational memory and synchronizes the processes in such a way they can communicate with each other hence resources can be used efficiently without wastage of time.

COMPARISON

Size

The major difference between Embedded Linux and RTOS is in their sizes. RTOS running on an AVR requires approximately 4.4 kilobytes of ROM. Embedded Linux, on the other hand, is relatively larger. The kernel can be stripped of which are not required and even with that, the footprint is generally measured in megabytes.

Embedded Linux RAM requirement is in order of few megabytes. In practical applications, it requires more than that because some other tasks run under these Linux kernels. RTOS has much smaller memory requirements than Linux. A very simple setup, running two tasks, a scheduler, a queue for communication and a semaphore on an 8-bit architecture would use in the vicinity of 200 bytes.

Scheduler

The scheduler in an RT-system is important to ensure that tasks complete in a fixed time. Compared to a regular scheduler for a general-purpose system, it is not the main task of the scheduler to ensure ’fair’ distribution of CPU-time. A common technique is simply to let the task with the highest priority run before all tasks with lower priority. It works fine for a soft real-time system but for hard real-time, the system must provide a better guarantee.

RTOS scheduler

RTOS uses the highest priority first scheduler. It means that the task having the highest priority is always running. This is achieved by having a preemptive scheduler that at a tick-interrupt decides if the currently running task is allowed to continue executing or it needs to be switched for another task based on priority. The scheduler uses the priority to schedule the task with the highest priority. Tasks having the same priority are given a “fair” process time. This schedular allows us to achieve soft real-time but it is difficult to achieve hard real-time by not having any kind of deadline-based scheduling.

For this purpose, there are choices of having a preemptive or a cooperative scheduler. In preemptive mode, a task can be preempted unlike in cooperative mode where it’s up to all tasks to give away the CPU “often” enough so higher priority tasks get to run. Typical RTOS real-time kernel achieves scheduler latencies from zero to a few microseconds.

Embedded Linux scheduler

In Embedded Linux, there are more choices to choose the scheduler. The modular of Embedded Linux allows to change different parts of the system. A simple insmod gives the possibility to change the scheduler. There are a couple of schedulers designed for different things.

First of all, it has a basic highest priority first scheduler that uses the priority of a task and schedules it first. Embedded Linux also implements the Earliest deadline first which uses the periodic feature of Embedded Linux. Assuming that the deadline for every task is when it is next to be run again one can implement a fast EDF. In theory, it is optimal since it can schedule tasks to 100% CPU-usages. In practice, it is not the same due to some overheads. As in idle process Embedded Linux runs a usual Linux kernel and when there are no rt-tasks that can run, Linux gets to run. which can lead to starvation of Linux and thus effectively disabling Linux. But the importance of a real-time system is to run the real-time tasks this is not a big problem for the system. Typical latencies in real-time Linux schedular are in the order of tens to hundreds of microseconds.

CPU resource

Embedded Linux requires a significant amount of CPU resources, perhaps >200MIPS, 32bit processor, ideally with an MMU, 4Mb of ROM and 16MB of RAM and boot may take several seconds.

An RTOS, on the other hand, runs in less than 10Kb, on microcontrollers from 8-bit up and boot in milliseconds.

IoT Implementation of OS

Embedded Linux is often preferred for extremely low-power applications, such as sensors, run for months on batteries. The low-power nature often precludes direct IP connectivity which serves as a gateway for Internet connectivity. The gateway communicates the low-power protocol to the sensors and would translate them to IP. Linux may have an existing protocol to fulfill the requirements.

The basic requirement of an IoT device is network connectivity, typically in the form of IP via a web server. An RTOS can offer IP connectivity but have a risk to be buggy unless you examine it. For example, usually, RTOSs do not isolate the IP stack user from the IP stack itself. Network connectivity requires potentially dealing with low speed or congested links which can lead to obscure and hard-to-debug buffer handling issues when the stack is intermingled with other code. On the other hand, an embedded Linux leverages hardware separation and a widely utilized IP stack that probably has been exposed to corner cases.

Security is essential in IoT devices, which are often exposed to open Internet. A system compromise on the Internet interface is prone to intruders and information or control of the device can be hijacked. Developers can leverage native, embedded Linux features—multiuser, SELinux, and containers—to contain and limit the damage.

Linux certainly is a robust and secure OS and the system has matured in an embedded operating system. Yet one of the drawbacks is its Memory footprint when compared to a real-time operating system even though it can be trimmed down by removing tools and system services that are not required in embedded systems, it still is a large software. It simply cannot run on 8 or 16-bit MCUs and requires more onboard RAM for the Linux kernel. For example, ARM Cortex-M architecture based MCUs, which typically have only a few hundred kilobytes of RAM, and Linux cannot run on these chips.

A common engineering solution for networked systems is to use two processors in the device. In this arrangement, an 8 or 16-bit MCU is used for the sensor or actuator, while a 32-bit processor is used for the network interface which runs an RTOS. Sales of 32-bit MCUs have exploded in the last several years, and have become the largest segment of the MCU market.

ORIGINALLY POSTED HERE ON TIRICH LABS