Operating Systems, Embedded Systems And Real-time Systems

7m ago
37 Views
5 Downloads
4.44 MB
177 Pages
Last View : 1m ago
Last Download : 1m ago
Upload by : Kaleb Stephen
Share:
Transcription

JANEZ PUHANOperating Systems,Embedded Systems,and Real-Time SystemsFE Publishing

University of LjubljanaFaculty of electrical engineeringOperating Systems, EmbeddedSystems, and Real-Time SystemsJanez PuhanLjubljana, 2015

CIP - Cataloging In PublicationNational and University Library, Ljubljana004.451(078.5)(0.034.2)PUHAN, Janez, 1969Operating Systems, Embedded Systems, and Real-Time Systems [Electronicsource] / Janez Puhan [editor] Faculty of Electrical Engineering. - 1st ed. El. book. - Ljubljana : FE Publishing, 2015Access method (URL): http://fides.fe.uni-lj.si/ janezp/operating systems embedded systems and real-time systems.pdfISBN 978-961-243-275-1 (pdf)278131456Copyright c 2015 FE Publishing. All rights reserved.No part of this book may be reproduced, stored, or transmitted in any mannerwithout the written permission of the publisher.URL: http://fides.fe.uni-lj.si/ janezp/operating systems embedded systemsand real-time systems.pdfJanez PuhanUniversity of Ljubljana, Faculty of Electrical EngineeringSI-1000 Ljubljana, Tržaška cesta [email protected]: FE Publishing, LjubljanaUL Faculty of Electrical Engineering, LjubljanaEditor: prof. dr. Sašo TomažičReviewers: prof. dr. Tadej Tuma, prof. dr. Patricio Bulić1st edition

ContentsPreface1 Operating system1.1 Operating-system parts . . . . . . . . . . . . . . . . . . . . . . . .1.1.1 Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.1.2 Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.1.3 Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2 File storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2.1 Directory structure . . . . . . . . . . . . . . . . . . . . . . .1.2.2 Storage devices . . . . . . . . . . . . . . . . . . . . . . . . .1.2.3 Partitions and file systems . . . . . . . . . . . . . . . . . . .1.3 Login and basic commands . . . . . . . . . . . . . . . . . . . . . .1.3.1 Naming conventions, special directory names and wildcards1.3.2 Files and directories . . . . . . . . . . . . . . . . . . . . . .(commands: ls, cd, pwd, cp, dd, mv, rm, mkdir, rmdir,clear, cat, less, head, tail, mount, umount, fdisk, mkfs)1.3.3 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . .(commands: grep, wc, find) . . . . . . . . . . . . . . . . . .1.3.4 File compressing . . . . . . . . . . . . . . . . . . . . . . . .(commands: gzip, gunzip, zcat, tar) . . . . . . . . . . . .1.3.5 Miscellaneous commands . . . . . . . . . . . . . . . . . . .(commands: date, df, du, echo, exit, history, poweroff,shutdown, sleep, sort, su, sudo, touch, tty, who, whoami)1.3.6 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . .(commands: man, whatis, info) . . . . . . . . . . . . . . . .1.4 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.4.1 Named pipes . . . . . . . . . . . . . . . . . . . . . . . . . .(command: mkfifo) . . . . . . . . . . . . . . . . . . . . . .1.5 Ownership and access rights . . . . . . . . . . . . . . . . . . . . . .(commands: groups, chmod, chown, ln) . . . . . . . . . . .1.6 Processes and jobs . . . . . . . . . . . . . . . . . . . . . . . . . . .(commands: ps, bg, jobs, fg, kill, killall, top, lsof,nice, strace) . . . . . . . . . . . . . . . . . . . . . . . . .1.7 Variables and functions . . . . . . . . . . . . . . . . . . . . . . . .(commands: set, env, export, unset) . . . . . . . . . . . .1.8 Text file editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(command: vi) . . . . . . . . . . . . . . . . . . . . . . . . .1.9 Shell scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.10 Programming in C and C . . . . . . . . . . . . . . . . . . . . .(commands: gcc, make) . . . . . . . . . . . . . . . . . . . .(command: g ) . . . . . . . . . . . . . . . . . . . . . . . .1.10.1 Source code modifications . . . . . . . . . . . . . . . . . . 30303133343636

ivCONTENTS(commands: diff, patch) . . . . . . . .1.10.2 Debugging the C and C programs .(command: gdb) . . . . . . . . . . . . .1.11 Installing a software package . . . . . . . . . .(commands: dpkg, apg-get, apt-cache).36373737382 Network2.1 Internet protocol suite . . . . . . . . . . . . . . . . . . . . . . . . .2.2 Gateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.3 Routing table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.4 Port number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.5 Private network . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.5.1 Network Address Translation (NAT) . . . . . . . . . . . . .2.6 Broadcast IP address . . . . . . . . . . . . . . . . . . . . . . . . . .2.7 Domain name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.7.1 Uniform Resource Locator (URL) . . . . . . . . . . . . . .2.8 localhost IP address . . . . . . . . . . . . . . . . . . . . . . . . . .2.9 Dynamic Host Configuration Protocol (DHCP) . . . . . . . . . . .2.10 Basic network-related commands . . . . . . . . . . . . . . . . . . .(commands: hostname, ifdown, ifup, ifconfig, route,ping, telnet, ssh, scp, ftp, sftp, wget) . . . . . . . . . .2.11 Network File System (NFS) . . . . . . . . . . . . . . . . . . . . . .2.12 HyperText Markup Language (HTML) . . . . . . . . . . . . . . . .2.13 Programming in the JavaScript and PHP Hypertext Preprocessor(PHP) languages . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.14 Firewall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(commands: iptables, iptables-save, iptables-restore)2.14.1 NAT configuration . . . . . . . . . . . . . . . . . . . . . . .(command: sysctl) . . . . . . . . . . . . . . . . . . . . . .2.14.2 Port forwarding . . . . . . . . . . . . . . . . . . . . . . . . .Port forwarding over SSH . . . . . . . . . . . . . . . . . . .394043444545464848484949513 Graphical User Interface (GUI)3.1 X window system . . . . . . . . . . . . . . . . . . . . . . . . . . . .(commands: startx, xhost; variable: DISPLAY) . . . . . . .3.1.1 X11 forwarding over SSH . . . . . . . . . . . . . . . . . . .696970724 Embedded system4.1 Installing an operating system . . . . . . . . . . . . . . . . . . . . .Erasing the NOR flash memory and uploading a boot-loaderBarebox boot-loader . . . . . . . . . . . . . . . . . . . . . .Uploading the kernel and root file system to the NOR flashUploading to the NOR flash from Linux . . . . . . . . . . .(command: flash eraseall) . . . . . . . . . . . . . . . . .4.1.1 Mounting an additional memory . . . . . . . . . . . . . . ./etc/fstab file system table . . . . . . . . . . . . . . . . .Journaled Flash File System version 2 (JFFS2) . . . . . . .SRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NAND flash . . . . . . . . . . . . . . . . . . . . . . . . . . .NOR flash . . . . . . . . . . . . . . . . . . . . . . . . . . . .SD memory card . . . . . . . . . . . . . . . . . . . . . . . .USB key . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.1.2 Booting from other devices . . . . . . . . . . . . . . . . . 36465

CONTENTS4.24.34.4vUploading to the NAND flash . . . . . . . . . . . . . . . .Creating a root file system on an external device . . . . .Boot configurations . . . . . . . . . . . . . . . . . . . . . .4.1.3 Accessing an embedded system over the network . . . . .Audio and video . . . . . . . . . . . . . . . . . . . . . . . . . . .4.2.1 Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . .Developing an embedded application . . . . . . . . . . . . . . . .Programming devices . . . . . . . . . . . . . . . . . . . . . . . . .4.4.1 System and virtual consoles . . . . . . . . . . . . . . . . .(functions: open(), close(), read(), write(), ioctl())4.4.2 Framebuffer . . . . . . . . . . . . . . . . . . . . . . . . . .(functions: mmap(), munmap()) . . . . . . . . . . . . . . .4.4.3 Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . .(functions: ts open(), ts config(), ts close(),ts read raw(), ts read()) . . . . . . . . . . . . . . . .4.4.4 Qt for the embedded Linux . . . . . . . . . . . . . . . . .4.4.5 Serial port . . . . . . . . . . . . . . . . . . . . . . . . . . .(function: memset()) . . . . . . . . . . . . . . . . . . . . .4.4.6 Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . .Connection-oriented protocol . . . . . . . . . . . . . . . .(functions: socket(), bind(), listen(), accept(),htons(), htonl()) . . . . . . . . . . . . . . . . . . . . . .(functions: shutdown(), inet addr()) . . . . . . . . . . .(functions: gethostbyname(), getservbyname()) . . . . .(function: select()) . . . . . . . . . . . . . . . . . . . . .(function: fcntl()) . . . . . . . . . . . . . . . . . . . . .Connectionless protocol . . . . . . . . . . . . . . . . . . .(functions: recvfrom(), sendto()) . . . . . . . . . . . . .5 Real-time operating system5.1 Real-time pre-emptive kernel . . . . . . . . . . . . . . . . . .(command: uname) . . . . . . . . . . . . . . . . . . . .5.2 Programming a real-time application . . . . . . . . . . . . . .5.2.1 Setting the application priority and scheduling policy(functions: sched setscheduler(),sched getscheduler(), sched getparam()) . . . . .5.2.2 Process memory . . . . . . . . . . . . . . . . . . . . .(functions: setrlimit(), getrlimit()) . . . . . . . .(functions: malloc(), free(), mallopt(), sbrk()) . .5.2.3 Preventing the memory page faults . . . . . . . . . . .(function: mlockall()) . . . . . . . . . . . . . . . . .(function: sysconf()) . . . . . . . . . . . . . . . . . .5.2.4 High-resolution timer . . . . . . . . . . . . . . . . . .(functions: clock getres(), clock gettime(),clock nanosleep()) . . . . . . . . . . . . . . . . . . .(functions: create timer(), timer settime(),delete timer()) . . . . . . . . . . . . . . . . . . . . .5.2.5 Real-time application skeleton . . . . . . . . . . . . 2124125. . . 125. . . 126. . . 1276 Inter-process communication6.1 Creating/terminating threads and processes . . . . . . . . . . . . .6.1.1 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(functions: pthread create(), pthread exit(),phtread cancel(), exit()) . . . . . . . . . . . . . . . . .129129129129

viCONTENTS6.1.26.26.36.46.56.66.7Processes . . . . . . . . . . . . . . . . . . . . . . . . .(function: fork()) . . . . . . . . . . . . . . . . . . . .(function: waitpid()) . . . . . . . . . . . . . . . . . .(function: exit()) . . . . . . . . . . . . . . . . . . .Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(function: kill()) . . . . . . . . . . . . . . . . . . . .(functions: sigemptyset(), sigaction()) . . . . . . .(functions: sigaddset(), sigprocmask()) . . . . . . .(function: sigwait()) . . . . . . . . . . . . . . . . . .Pipes and named pipes . . . . . . . . . . . . . . . . . . . . . .(functions: pipe(), close()) . . . . . . . . . . . . . .(functions: mkfifo(), open(), unlink()) . . . . . . .(functions: write(), read()) . . . . . . . . . . . . . .(function: fcntl()) . . . . . . . . . . . . . . . . . . .Message queues . . . . . . . . . . . . . . . . . . . . . . . . . .(functions: mq open(), mq close(), mq unlink()) . .(functions: mq getattr(), mq send(), mq receive())(function: mq setattr()) . . . . . . . . . . . . . . . .Shared-memory segments . . . . . . . . . . . . . . . . . . . .(functions: shm open(), ftruncate(), shm unlink())(functions: mmap(), munmap()) . . . . . . . . . . . . .Memory-mapped files . . . . . . . . . . . . . . . . . . . . . .(function: lseek()) . . . . . . . . . . . . . . . . . . .Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Connection-oriented local-domain sockets . . . . . . .Connectionless local-domain sockets . . . . . . . . . .Abstract sockets . . . . . . . . . . . . . . . . . . . . .Socket pair . . . . . . . . . . . . . . . . . . . . . . . .(function: socketpair()) . . . . . . . . . . . . . . . .7 Resource sharing and synchronization7.1 Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . .(functions: sem init(), sem destroy()) . . . . . . .(functions: sem open(), sem close(), sem unlink())(functions: sem wait(), sem post()) . . . . . . . . .7.1.1 Recursive deadlock . . . . . . . . . . . . . . . . . . . .7.1.2 Deadlock because of process termination . . . . . . . .7.1.3 Circular deadlock . . . . . . . . . . . . . . . . . . . . .7.1.4 A priority-inversion problem . . . . . . . . . . . . . .7.2 MUTual EXclusion (mutex) . . . . . . . . . . . . . . . . . . .(functions: pthread mutex init(),pthread mutex destroy()) . . . . . . . . . . . . . .(functions: pthread mutex lock(),pthread mutex unlock()) . . . . . . . . . . . . . . .7.2.1 Mutex attributes . . . . . . . . . . . . . . . . . . . . .(functions: pthread mutexattr init(),pthread mutexattr destroy()) . . . . . . . . . . . .(function: pthread mutexattr setpshared()) . . . .(function: pthread mutexattr settype()) . . . . . .(functions: pthread mutexattr setrobust np(),pthread mutex consistent np()) . . . . . . . . . .Priority ceiling . . . . . . . . . . . . . . . . . . . . . .(functions: pthread mutexattr setprotocol(),pthread mutexattr setprioceiling()) . . . . . . 7147147147147148148. . . 149. . . 149. . . 149. . . 150. . . 150. . . 150. . . 151. . . 151. . . 152

CONTENTSviiPriority inheritance . . . . . . . . . . . . . . . . . . . . . . . 1528 Loadable kernel modules(commands: lsmod, insmod, rmmod) . . . . . . . . . .8.1 Kernel module programming . . . . . . . . . . . . . . . . .(functions: init module(), cleanup module()) . .(functions: misc register(), misc deregister())(function: printk()) . . . . . . . . . . . . . . . . . .(functions: put user(), get user()) . . . . . . . .(functions: krealloc(), kfree()) . . . . . . . . . .8.2 Compiling a kernel module . . . . . . . . . . . . . . . . . .8.2.1 Cross-compiling a kernel module . . . . . . . . . . .Bibliography.155155155155157158159159159160161

PrefaceThe following text represents a real-time operating-system course textbook. Thecourse is held in the third semester of the Master’s study program in ElectricalEngineering at the Faculty of Electrical Engineering of the University of Ljubljana,Slovenia. It introduces the students of Electronics into the operating systems andreal-time concepts having the embedded systems perspective in mind.Although the covered mechanisms and principles are general, they are giventhrough Linux operating system and POSIX application programming interfaceexamples. An important part of the course is the hands-on laboratory work wherethe examples can be carried out. The Phytec’s phyCORE-i.MX27 developmentkit with the Freescale’s i.MX27 microcontroller is used as an embedded systemplatform.The textbook is a kind of a crash course. The topics are explained by examplesproviding a flying start to a beginner. The reader should consult other sources fora detailed explanation.The first three chapters describe the operating system and network configuration basic principles. In Chapter 1, the students get familiar with the operating system parts, common Linux commands and program compiling. Chapter2 describes the fundamentals of the network structure. In Chapter 3, a briefdescription of the graphical user interface with the X window system is presented.The focus of Chapter 4 is on the Phytec’s phyCORE-i.MX27 embedded system platform. It describes installation of the embedded Linux operating system,booting with the Barebox boot-loader and working with some peripheral devices(framebuffer, touchscreen, serial port, ethernet, etc.), and gives examples of agraphical application written in the Qt, cross-compilation and remote debugging.Chapter 5 deals with the real-time properties and how to achieve them inLinux. A real-time application code skeleton is drafted by shedding light onvarious aspects, such as priority, scheduling policy, stack and heap memory pagefaults, etc.In Chapters 6 and 7, the inter-process communication and simultaneous accessare discussed. Various communication techniques are presented by the POSIXcompliant C code examples. The same approach is continued with the resourceaccess techniques. The circumstances leading to deadlock situations with possiblesolutions are presented.Chapter 8 enables a glimpse into kernel programming and provides a smalltutorial of programming, compiling and cross-compiling the “Hello World!” kernelmodule.The textbook is available in pdf format on the Internet at http://fides.fe.unilj.si/ janezp/operating systems embedded systems and real-time systems.pdf . Also the source code of the examples in the textbook is available at http://fides.fe.uni-lj.si/ janezp/operating systems embedded systems and real-timesystems code.zip.

Chapter 1Operating systemAn operating system is a suite of programs and data making a computer work(e.g. managing the hardware resources, providing services for application programs, etc.). Linux [1, 2] refers to the family of the Unix-like [3] computer operating systems using the Linux kernel. The Linux operating systems are made upof three parts:-kernel,shell andprograms.1.11.1.1Operating-system partsKernelAt the computer boot, BIOS (Basic Input/Output System) performs start-uptasks to recognize and start the hardware. Then it loads and executes the partition boot code from the designated boot device (e.g. hard disk) containing thefirst stage of the bootstrap loader or shortly the boot-loader. A first-stage bootloader is a small program that loads the more complex second-stage boot-loadercode into RAM (Random-Access Memory) and starts it. A second-stage bootloader, such as GRUB (GRand Unified Bootloader) or LILO (LInux LOader),loads a kernel and transfers execution to it. The second-stage boot-loaders usually can be configured to give a user multiple booting choices. These choices caninclude different operating systems, different versions of the same operating system, different operating-system loading options or standalone programs that canrun without an operating system (e.g. memory test programs, games, etc.). Asecond-stage boot-loader configuration file (e.g. /boot/grub/grub.cfg for GRUBand /etc/lilo.conf for LILO) contains information about the kernel location,options, etc.A kernel [4] is the center of the operating system. It allocates the memory andCPU (Central Processing Unit) time to programs, handles the file storage andcommunications, responds to system calls, etc. Traditionally, the kernel image onthe Unix platforms is stored in the /unix file. The kernels that support the virtualmemory feature have the vm prefix (/vmunix). The linux kernel (/vmlinux) canusually be found in a statically linked, /vmlinuz executable file, where the letterz at the end denotes that it is compressed (zipped).The kernel initializes the hardware, mounts the root file system (see subsection1.2.3), starts the operating system scheduler and the first process called init(/sbin/init). Then it goes idle. The init process spawns all other processes.It sets up all the non-operating system services and structures in order to create

2CHAPTER 1. OPERATING SYSTEMa user environment (e.g. ftp (File Transfer Protocol) and ssh (Secure SHell)services, getty (GET TeletYpe) text login program, gdm (GNOME (GNU (GNU’sNot Unix) Object Model Environment) Display Manager) GUI (Graphical UserInterface) login program, etc.). The init process runs as a daemon

Operating Systems, Embedded Systems, and Real-Time Systems Janez Puhan Ljubljana, 2015. CIP-CatalogingInPublication NationalandUniversityLibrary,Ljubljana 004.451(078.5)(0.034.2) PUHAN,Janez,1969-Operating Systems, Embedded Systems, and Real-Time Systems [Electronic