Book Image

Mastering Embedded Linux Programming - Second Edition

By : Chris Simmonds
Book Image

Mastering Embedded Linux Programming - Second Edition

By: Chris Simmonds

Overview of this book

Embedded Linux runs many of the devices we use every day, from smart TVs to WiFi routers, test equipment to industrial controllers - all of them have Linux at their heart. Linux is a core technology in the implementation of the inter-connected world of the Internet of Things. The comprehensive guide shows you the technologies and techniques required to build Linux into embedded systems. You will begin by learning about the fundamental elements that underpin all embedded Linux projects: the toolchain, the bootloader, the kernel, and the root filesystem. You’ll see how to create each of these elements from scratch, and how to automate the process using Buildroot and the Yocto Project. Moving on, you’ll find out how to implement an effective storage strategy for flash memory chips, and how to install updates to the device remotely once it is deployed. You’ll also get to know the key aspects of writing code for embedded Linux, such as how to access hardware from applications, the implications of writing multi-threaded code, and techniques to manage memory in an efficient way. The final chapters show you how to debug your code, both in applications and in the Linux kernel, and how to profile the system so that you can look out for performance bottlenecks. By the end of the book, you will have a complete overview of the steps required to create a successful embedded Linux system.
Table of Contents (17 chapters)

Avoiding page faults

A page fault occurs when an application reads or writes to memory that is not committed to physical memory. It is impossible (or very hard) to predict when a page fault will happen, so they are another source of non-determinism in computers.

Fortunately, there is a function that allows you to commit all the memory used by the process and lock it down so that it cannot cause a page fault. It is mlockall(2). These are its two flags:

  • MCL_CURRENT: This locks all pages currently mapped
  • MCL_FUTURE: This locks pages that are mapped in later

You usually call mlockall during the startup of the application with both flags set to lock all current and future memory mappings.

MCL_FUTURE is not magic, in that there will still be non-deterministic delay when allocating or freeing heap memory using malloc()/free() or mmap(). Such operations are best done at startup and...