Sys Arch
Sys Arch
Sys Arch
System Architecture
19962009, QNX Software Systems GmbH & Co. KG. All rights reserved. Published under license by: QNX Software Systems International Corporation 175 Terence Matthews Crescent Kanata, Ontario K2M 1W8 Canada Voice: +1 613 591-0931 Fax: +1 613 591-3579 Email: [email protected] Web: http://www.qnx.com/ Electronic edition published 2009 QNX, Neutrino, Photon, Photon microGUI, Momentics, and Aviage are trademarks, registered in certain jurisdictions, of QNX Software Systems GmbH & Co. KG. and are used under license by QNX Software Systems International Corporation. All other trademarks belong to their respective owners.
Contents
About This Guide xiii
What youll nd in this guide xv Typographical conventions xvi Note to Windows users xvii Technical support xvii
Design goals 3 An embeddable POSIX OS? 3 Product scaling 3 Why POSIX for embedded systems? 4 Why QNX Neutrino for embedded systems? 5 Microkernel architecture 6 The OS as a team of processes 8 A true kernel 8 System processes 9 Interprocess communication 10 QNX Neutrino as a message-passing operating system Network distribution of kernels 11 Single-computer model 11 Flexible networking 11
10
13
Introduction 15 The implementation of QNX Neutrino 16 POSIX realtime and thread extensions 16 System services 16 Threads and processes 17 Thread attributes 20 Thread scheduling 24 When are scheduling decisions are made? 24 Scheduling priority 24 Scheduling algorithms 26
May 8, 2009
Contents
iii
IPC issues 31 Thread complexity issues 32 Synchronization services 33 Mutexes: mutual exclusion locks 34 Condvars: condition variables 35 Barriers 36 Sleepon locks 38 Reader/writer locks 38 Semaphores 39 Synchronization via scheduling algorithm 40 Synchronization via message passing 40 Synchronization via atomic operations 40 Synchronization services implementation 41 Clock and timer services 41 Time correction 43 Timers 43 Interrupt handling 45 Interrupt latency 45 Scheduling latency 46 Nested interrupts 47 Interrupt calls 48
53
64
iv
Contents
May 8, 2009
Shared memory 74 Shared memory with message passing 75 Creating a shared-memory object 76 mmap() 77 Initializing allocated memory 80 Typed memory 81 Implementation-dened behavior 82 Practical examples 85 Pipes and FIFOs 87 Pipes 87 FIFOs 87
89
94
Multicore Processing
97
Introduction 99 Asymmetric multiprocessing (AMP) 99 Symmetric multiprocessing (SMP) 100 Neutrinos microkernel approach 101 Booting an x86 SMP system 102 Booting a PowerPC or MIPS SMP system 102 How the SMP microkernel works 103 Critical sections 104 Bound multiprocessing (BMP) 105 A viable migration strategy 106 Choosing between AMP, SMP, and BMP 106
May 8, 2009
Contents
Memory Management Units (MMUs) 117 Memory protection at run time 118 Quality control 119 Full-protection model 120 Variable page size 120 Defragmenting physical memory 121 Pathname management 124 Domains of authority 124 Resolving pathnames 124 Symbolic prexes 128 File descriptor namespace 130
141
Filesystems 155
Introduction 157 Filesystems and pathname resolution Filesystem classes 158 Filesystems as shared libraries 158 157
vi
Contents
May 8, 2009
159 Filesystem limitations 162 Image lesystem 163 RAM lesystem 163 Embedded transaction lesystem (ETFS) 163 Inside a transaction 164 Types of storage media 165 Reliability features 165 QNX 4 lesystem 167 Power-Safe lesystem 167 Problems with existing disk lesystems 168 Copy-on-write lesystem 168 Performance 171 DOS Filesystem 172 CD-ROM lesystem 174 FFS3 lesystem 175 Customization 175 Organization 175 Features 176 Utilities 178 System calls 178 NFS lesystem 178 CIFS lesystem 179 Linux Ext2 lesystem 179 Universal Disk Format (UDF) lesystem 180 Apple Macintosh HFS and HFS Plus 180 Windows NT lesystem 180 Virtual lesystems 180
io-blk
10
May 8, 2009
Contents
vii
11
12
208
13
viii
Contents
May 8, 2009
14
15
16
May 8, 2009
Contents
ix
Widget library 261 Fundamental widgets 262 Container widgets 264 Advanced widgets 266 Convenience functions 271 Driver development kits 273 Summary 274
17
Multimedia 275
Overview 277 MME functional areas 278 The MME interface 279 Component-based architecture 280 The MME resource managers 280
283
Contents
May 8, 2009
List of Figures
Conventional executives offer no memory protection. 7 In a monolithic OS, system processes have no protection. 7 A microkernel provides complete memory protection. 7 The QNX Neutrino architecture. 8 The QNX Neutrino microkernel. 15 QNX Neutrino preemption details. 17 21 Sparse matrix (tid,key) to value mapping. Possible thread states. 22 The ready queue. 25 Thread A blocks; Thread B runs. 27 FIFO scheduling. 27 Round-robin scheduling. 28 A threads budget is replenished periodically. 29 A thread drops in priority until its budget is replenished. 30 A thread oscillates between high and low priority. 30 Interrupt handler simply terminates. 46 Interrupt handler terminates, returning an event. 47 Stacked interrupts. 48 Changes of state for a client thread in a send-receive-reply transaction. Changes of state for a server thread in a send-receive-reply transaction. A multipart transfer. 58 Scatter/gather of a read of 1454 bytes. 59 Connections map elegantly into le descriptors. 62 Pulses pack a small payload. 63 Threads should always send up to higher-level threads. 65 A higher-level thread can send a pulse event. 66 67 The client sends a sigevent to the server. Signal delivery. 69 Mapping memory with mmap(). 78 Instrumentation at a glance. 92 The IDE helps you visualize system activity. 95 Virtual address mapping (on an x86). 117 Full protection VM (on an x86). 120 The SCOID and FD map to an OCB of an I/O Manager. 131
56 57
May 8, 2009
List of Figures
xi
Two processes open the same le. 132 A process opens a le twice. 133 Object le format: linking view and execution view. 139 Process memory layout on an x86. 140 A resource manager is responsible for three data structures. 152 Multiple clients opening various devices. 153 Encapsulation. 154 QNX Neutrino lesystem layering. 159 ETFS is a lesystem composed entirely of transactions. 164 The io-char module is implemented as a library. 183 Device I/O in QNX Neutrino. 184 Conditions for satisfying an input request. 186 Pseudo-ttys. 190 A detailed view of the io-pkt architecture. 196 A simple network where the client and server reside on separate machines. 204 A client-server message pass across the network. 206 Qnet and a single network. 209 Qnet and physically separate networks. 210 The io-pkt suite and its dependents. 218 Embedded web server. 222 Static partitions guarantee that processes get the resources specied by the system designer. 243 Without adaptive partitioning, a DOS attack on one process can starve other critical functions. 247 With scheduler partitions, a DOS attack is contained. 248 Photon regions. 254 Opaque regions are clipped out. 257 High-level view of the MME components. 277 MME architecture showing mme and qdb resource managers. 281
xii
List of Figures
May 8, 2009
May 8, 2009
xiii
continued. . .
May 8, 2009
xv
Typographical conventions
To nd out about: Graphical environment Multimedia Engine (MME) Terms used in QNX documentation
For information about programming in Neutrino, see Getting Started with QNX Neutrino: A Guide for Realtime Programmers and the Neutrino Programmers Guide.
Typographical conventions
Throughout this manual, we use certain typographical conventions to distinguish technical terms. In general, the conventions we use conform to those found in IEEE POSIX publications. The following table summarizes our conventions: Reference Code examples Command options Commands Environment variables File and pathnames Function names Keyboard chords Keyboard input Keyboard keys Program output Programming constants Programming data types Programming literals Variable names User-interface components Example
if( stream == NULL ) -lR make
PATH
/dev/null
exit()
Ctrl-Alt-Delete something you type Enter login: NULL unsigned short 0xFF, "message string"
stdin Cancel
We use an arrow () in directions for accessing menu items, like this: Youll nd the Other... menu item under PerspectiveShow View. We use notes, cautions, and warnings to highlight important messages:
xvi
May 8, 2009
Technical support
CAUTION: Cautions tell you about commands or procedures that may have unwanted or undesirable side effects.
WARNING: Warnings tell you about commands or procedures that could be dangerous to your les, your hardware, or even yourself.
Technical support
To obtain technical support for any QNX product, visit the Support + Services area on our website (www.qnx.com). Youll nd a wide range of support options, including community forums.
May 8, 2009
xvii
May 8, 2009
Design goals
Design goals
The primary goal of QNX Neutrino is to deliver the open systems POSIX API in a robust, scalable form suitable for a wide range of systems from tiny, resource-constrained embedded systems to high-end distributed computing environments. The OS supports several processor families, including x86, ARM, XScale, PowerPC, MIPS, and SH-4. For mission-critical applications, a robust architecture is also fundamental, so the OS makes exible and complete use of MMU hardware. Of course, simply setting out these goals doesnt guarantee results. We invite you to read through this System Architecture guide to get a feel for our implementation approach and the design trade-offs chosen to achieve these goals. When you reach the end of this guide, we think youll agree that QNX Neutrino is the rst OS product of its kind to truly deliver open systems standards, wide scalability, and high reliability.
Product scaling
Since you can readily scale a microkernel OS simply by including or omitting the particular processes that provide the functionality required, you can use a single microkernel OS for a much wider range of applications than a realtime executive. Product development often takes the form of creating a product line, with successive models providing greater functionality. Rather than be forced to change operating systems for each version of the product, developers using a microkernel OS can easily scale the system as needed by adding lesystems, networking, graphical user interfaces, and other technologies. Some of the advantages to this scalable approach include:
May 8, 2009
Design goals
portable application code (between product-line members) common tools used to develop the entire product line portable skill sets of development staff reduced time-to-market.
May 8, 2009
Multiple OS sources
Hardware manufacturers are loath to choose a single-sourced hardware component because of the risks implied if that source discontinues production. For the same reason, manufacturers shouldnt be tied to a single-sourced, proprietary OS simply because their application source code isnt portable to other OSs. By building applications to the POSIX standards, developers can use OSs from multiple vendors. Application source code can be readily ported from platform to platform and from OS to OS, provided that developers avoid using OS-specic extensions.
May 8, 2009
Microkernel architecture
QNX Neutrino is also remarkably exible. Developers can easily customize the OS to meet the needs of their applications. From a bare-bones conguration of a microkernel with a few small modules to a full-blown network-wide system equipped to serve hundreds of users, youre free to set up your system to use only those resources you require to tackle the job at hand. QNX Neutrino achieves its unique degree of efciency, modularity, and simplicity through two fundamental principles: microkernel architecture message-based interprocess communication
Microkernel architecture
Buzzwords often fall in and out of fashion. Vendors tend to enthusiastically apply the buzzwords of the day to their products, whether the terms actually t or not. The term microkernel has become fashionable. Although many new operating systems are said to be microkernels (or even nanokernels), the term may not mean very much without a clear denition. Lets try to dene the term. A microkernel OS is structured as a tiny kernel that provides the minimal services used by a team of optional cooperating processes, which in turn provide the higher-level OS functionality. The microkernel itself lacks lesystems and many other services normally expected of an OS those services are provided by optional processes. The real goal in designing a microkernel OS is not simply to make it small. A microkernel OS embodies a fundamental change in the approach to delivering OS functionality. Modularity is the key, size is but a side effect. To call any kernel a microkernel simply because it happens to be small would miss the point entirely. Since the IPC services provided by the microkernel are used to glue the OS itself together, the performance and exibility of those services govern the performance of the resulting OS. With the exception of those IPC services, a microkernel is roughly comparable to a realtime executive, both in terms of the services provided and in their realtime performance. The microkernel differs from an executive in how the IPC services are used to extend the functionality of the kernel with additional, service-providing processes. Since the OS is implemented as a team of cooperating processes managed by the microkernel, user-written processes can serve both as applications and as processes that extend the underlying OS functionality for industry-specic applications. The OS itself becomes open and easily extensible. Moreover, user-written extensions to the OS wont affect the fundamental reliability of the core OS. A difculty for many realtime executives implementing the POSIX 1003.1 standard is that their runtime environment is typically a single-process, multiple-threaded model, with unprotected memory between threads. Such an environment is only a subset of the multi-process model that POSIX assumes; it cannot support the fork() function. In
May 8, 2009
Microkernel architecture
contrast, QNX Neutrino fully utilizes an MMU to deliver the complete POSIX process model in a protected environment. As the following diagrams show, a true microkernel offers complete memory protection, not only for user applications, but also for OS components (device drivers, lesystems, etc.):
Kernel space TCP/IP stack Applications Device drivers File system
The rst version of the QNX OS was shipped in 1981. With each successive product revision, we have applied the experience from previous product generations to the latest incarnation: QNX Neutrino, our most capable, scalable OS to date. We believe that this time-tested experience is what enables the QNX Neutrino OS to deliver the functionality it does using the limited resources it consumes.
May 8, 2009
Microkernel architecture
Software bus
Character manager
Application
Mqueue manager
QNX Neutrino acts as a kind of software bus that lets you dynamically plug in/out OS modules whenever theyre needed.
A true kernel
The kernel is the heart of any operating system. In some systems, the kernel comprises so many functions that for all intents and purposes it is the entire operating system! But our microkernel is truly a kernel. First of all, like the kernel of a realtime executive, its very small. Secondly, its dedicated to only a few fundamental services: thread services via POSIX thread-creation primitives signal services via POSIX signal primitives message-passing services the microkernel handles the routing of all messages between all threads throughout the entire system. synchronization services via POSIX thread-synchronization primitives.
May 8, 2009
Microkernel architecture
scheduling services the microkernel schedules threads for execution using the various POSIX realtime scheduling algorithms. timer services the microkernel provides the rich set of POSIX timer services. process management services the microkernel and the process manager together form a unit (called procnto). The process manager portion is responsible for managing processes, memory, and the pathname space. Unlike threads, the microkernel itself is never scheduled for execution. The processor executes code in the microkernel only as the result of an explicit kernel call, an exception, or in response to a hardware interrupt.
System processes
All OS services, except those provided by the mandatory microkernel/process manager module (procnto), are handled via standard processes. A richly congured system could include the following: lesystem managers character device managers graphical user interface (Photon) native network manager TCP/IP
May 8, 2009
Interprocess communication
allows such processes to be implemented cleanly, with no need for modications to the standard components of the OS itself. For developers creating custom embedded systems, this provides the exibility to extend the OS in directions that are uniquely useful to their applications, without needing access to OS source code.
Device drivers
Device drivers allow the OS and application programs to make use of the underlying hardware in a generic way (e.g. a disk drive, a network interface). While most OSs require device drivers to be tightly bound into the OS itself, device drivers for QNX Neutrino can be started and stopped as standard processes. As a result, adding device drivers doesnt affect any other part of the OS drivers can be developed and debugged like any other application.
Interprocess communication
When several threads run concurrently, as in typical realtime multitasking environments, the OS must provide mechanisms to allow them to communicate with each other. Interprocess communication (IPC) is the key to designing an application as a set of cooperating processes in which each process handles one well-dened part of the whole. The OS provides a simple but powerful set of IPC capabilities that greatly simplify the job of developing applications made up of cooperating processes. For more information, see the Interprocess Communication (IPC) chapter.
10
May 8, 2009
interrelated. The discipline imposed by QNX Neutrinos message-passing design helps bring order and greater reliability to applications.
Single-computer model
QNX Neutrino is designed from the ground up as a network-wide operating system. In some ways, a native QNX Neutrino network feels more like a mainframe computer than a set of individual micros. Users are simply aware of a large set of resources available for use by any application. But unlike a mainframe, QNX Neutrino provides a highly responsive environment, since the appropriate amount of computing power can be made available at each node to meet the needs of each user. In a mission-critical environment, for example, applications that control realtime I/O devices may require more performance than other, less critical, applications, such as a web browser. The network is responsive enough to support both types of applications at the same time the OS lets you focus computing power on the devices in your hard realtime system where and when its needed, without sacricing concurrent connectivity to the desktop. Moreover, critical aspects of realtime computing, such as priority inheritance, function seamlessly across a QNX Neutrino network, regardless of the physical media employed (switch fabric, serial, etc.).
Flexible networking
QNX Neutrino networks can be put together using various hardware and industry-standard protocols. Since these are completely transparent to application programs and users, new network architectures can be introduced at any time without disturbing the OS. Each node in the network is assigned a unique name that becomes its identier. This name is the only visible means to determine whether the OS is running as a network or as a standalone operating system.
May 8, 2009
11
This degree of transparency is yet another example of the distinctive power of QNX Neutrinos message-passing architecture. In many systems, important functions such as networking, IPC, or even message passing are built on top of the OS, rather than integrated directly into its core. The result is often an awkward, inefcient double standard interface, whereby communication between processes is one thing, while penetrating the private interface of a mysterious monolithic kernel is another matter altogether. In contrast to monolithic systems, QNX Neutrino is grounded on the principle that effective communication is the key to effective operation. Message passing thus forms the cornerstone of our microkernel architecture and enhances the efciency of all transactions among all processes throughout the entire system, whether across a PC backplane or across a mile of twisted pair.
12
May 8, 2009
May 8, 2009
13
Introduction
Introduction
The QNX Neutrino microkernel, procnto, implements the core POSIX features used in embedded realtime systems, along with the fundamental QNX Neutrino message-passing services. The POSIX features that arent implemented in the microkernel (le and device I/O, for example) are provided by optional processes and shared libraries. To determine the release version of the kernel on your system, use the uname -a command. For more information, see its entry in the Utilities Reference. Successive QNX microkernels have seen a reduction in the code required to implement a given kernel call. The object denitions at the lowest layer in the kernel code have become more specic, allowing greater code reuse (such as folding various forms of POSIX signals, realtime signals, and QNX pulses into common data structures and code to manipulate those structures). At its lowest level, the microkernel contains a few fundamental objects and the highly tuned routines that manipulate them. The OS is built from this foundation.
Interface Microkernel Objects
Thread Sched
Thread
Connection Synch Signal Message Channel Timer Clock Interrupt Channel Pulse Timer Vector Dispatch
Some developers have assumed that our microkernel is implemented entirely in assembly code for size or performance reasons. In fact, our implementation is coded primarily in C; size and performance goals are achieved through successively rened algorithms and data structures, rather than via assembly-level peep-hole optimizations.
May 8, 2009
15
System services
The QNX Neutrino microkernel has kernel calls to support the following: threads message passing signals clocks timers interrupt handlers semaphores mutual exclusion locks (mutexes) condition variables (condvars) barriers The entire OS is built upon these calls. The OS is fully preemptible, even while passing messages between processes; it resumes the message pass where it left off before preemption.
16
May 8, 2009
The minimal complexity of the microkernel helps place an upper bound on the longest nonpreemptible code path through the kernel, while the small code size makes addressing complex multiprocessor issues a tractable problem. Services were chosen for inclusion in the microkernel on the basis of having a short execution path. Operations requiring signicant work (e.g. process loading) were assigned to external processes/threads, where the effort to enter the context of that thread would be insignicant compared to the work done within the thread to service the request. Rigorous application of this rule to dividing the functionality between the kernel and external processes destroys the myth that a microkernel OS must incur higher runtime overhead than a monolithic kernel OS. Given the work done between context switches (implicit in a message pass), and the very quick context-switch times that result from the simplied kernel, the time spent performing context switches becomes lost in the noise of the work done to service the requests communicated by the message passing between the processes that make up the OS. The following diagram shows the preemption details for the non-SMP kernel (x86 implementation).
SYSCALL Entry usecs Interrupts off
usecs to msecs
usecs usecs
Interrupts are disabled, or preemption is held off, for only very brief intervals (typically in the order of hundreds of nanoseconds).
May 8, 2009
17
container for threads, dening the address space within which threads will execute. A process will always contain at least one thread. Depending on the nature of the application, threads might execute independently with no need to communicate between the algorithms (unlikely), or they may need to be tightly coupled, with high-bandwidth communications and tight synchronization. To assist in this communication and synchronization, QNX Neutrino provides a rich variety of IPC and synchronization services. The following pthread_* (POSIX Threads) library calls dont involve any microkernel thread calls: pthread_attr_destroy() pthread_attr_getdetachstate() pthread_attr_getinheritsched() pthread_attr_getschedparam() pthread_attr_getschedpolicy() pthread_attr_getscope() pthread_attr_getstackaddr() pthread_attr_getstacksize() pthread_attr_init() pthread_attr_setdetachstate() pthread_attr_setinheritsched() pthread_attr_setschedparam() pthread_attr_setschedpolicy() pthread_attr_setscope() pthread_attr_setstackaddr() pthread_attr_setstacksize() pthread_cleanup_pop() pthread_cleanup_push() pthread_equal() pthread_getspecic() pthread_setspecic() pthread_key_create() pthread_key_delete() pthread_self() The following table lists the POSIX thread calls that have a corresponding microkernel thread call, allowing you to choose either interface:
18
May 8, 2009
POSIX call pthread_create() pthread_exit() pthread_detach() pthread_join() pthread_cancel() N/A pthread_mutex_init() pthread_mutex_destroy() pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock() pthread_cond_init() pthread_cond_destroy() pthread_cond_wait() pthread_cond_signal() pthread_cond_broadcast() pthread_getschedparam() pthread_setschedparam() pthread_sigmask() pthread_kill()
Microkernel call ThreadCreate() ThreadDestroy() ThreadDetach() ThreadJoin() ThreadCancel() ThreadCtl() SyncTypeCreate() SyncDestroy() SyncMutexLock() SyncMutexLock() SyncMutexUnlock() SyncTypeCreate() SyncDestroy() SyncCondvarWait() SyncCondvarSignal() SyncCondvarSignal() SchedGet() SchedSet() SignalProcmask() SignalKill()
Description Create a new thread of execution. Destroy a thread. Detach a thread so it doesnt need to be joined. Join a thread waiting for its exit status. Cancel a thread at the next cancellation point. Change a threads Neutrino-specic thread characteristics. Create a mutex. Destroy a mutex. Lock a mutex. Conditionally lock a mutex. Unlock a mutex. Create a condition variable. Destroy a condition variable. Wait on a condition variable. Signal a condition variable. Broadcast a condition variable. Get the scheduling parameters and policy of a thread. Set the scheduling parameters and policy of a thread. Examine or set a threads signal mask. Send a signal to a specic thread.
The OS can be congured to provide a mix of threads and processes (as dened by POSIX). Each process is MMU-protected from each other, and each process may contain one or more threads that share the processs address space. The environment you choose affects not only the concurrency capabilities of the application, but also the IPC and synchronization services the application might make use of. Even though the common term IPC refers to communicating processes, we use it here to describe the communication between threads, whether theyre within the same process or separate processes. For information about processes and threads from the programming point of view, see the Processes and Threads chapter of Getting Started with QNX Neutrino, and the Programming Overview and Processes chapters of the Neutrino Programmers Guide.
May 8, 2009
19
Thread attributes
Although threads within a process share everything within the processs address space, each thread still has some private data. In some cases, this private data is protected within the kernel (e.g. the tid or thread ID), while other private data resides unprotected in the processs address space (e.g. each thread has a stack for its own use). Some of the more noteworthy thread-private resources are: tid Priority Each thread is identied by an integer thread ID, starting at 1. The tid is unique within the threads process. Each thread has a priority that helps determine when it runs. A thread inherits its initial priority from its parent, but the priority can change, depending on the scheduling algorithm, explicit changes that the thread makes, or messages sent to the thread.
In Neutrino, processes dont have priorities; their threads do. For more information, see Thread scheduling, later in this chapter. Name Starting with the QNX Neutrino Core OS 6.3.2, you can assign a name to a thread; see the entries for pthread_getname_np() and pthread_setname_np() in the Neutrino Library Reference. Utilities such as dumper and pidin support thread names. Thread names are a Neutrino extension. Each thread has its own instruction pointer (IP), stack pointer (SP), and other processor-specic register context. Each thread executes on its own stack, stored within the address space of its process. Each thread has its own signal mask.
Thread local storage A thread has a system-dened data area called thread local storage (TLS). The TLS is used to store per-thread information (such as tid, pid, stack base, errno, and thread-specic key/data bindings). The TLS doesnt need to be accessed directly by a user application. A thread can have user-dened data associated with a thread-specic data key. Cancellation handlers Callback functions that are executed when the thread terminates. Thread-specic data, implemented in the pthread library and stored in the TLS, provides a mechanism for associating a process global integer key with a unique per-thread data value. To use thread-specic data, you rst create a new key and then
20
May 8, 2009
bind a unique data value to the key (per thread). The data value may, for example, be an integer or a pointer to a dynamically allocated data structure. Subsequently, the key can return the bound data value per thread. A typical application of thread-specic data is for a thread-safe function that needs to maintain a context for each calling thread.
key tid
You use the following functions to create and manipulate this data: Function pthread_key_create() pthread_key_delete() pthread_setspecic() pthread_getspecic() Description Create a data key with destructor function Destroy a data key Bind a data value to a data key Return the data value bound to a data key
May 8, 2009
21
SEND
SIG WAITINFO
RECEIVE
INTERRUPT
NET_REPLY
READY
NANO SLEEP
NET_SEND
WAITPAGE
SEM
WAITCTX
WAIT THREAD
The thread is blocked on a condition variable (e.g. it called pthread_cond_wait()). The thread has terminated and is waiting for a join by another thread. The thread is blocked waiting for an interrupt (i.e. it called InterruptWait()). The thread is blocked waiting to join another thread (e.g. it called pthread_join()). The thread is blocked on a mutual exclusion lock (e.g. it called pthread_mutex_lock()).
22
May 8, 2009
The thread is sleeping for a short time interval (e.g. it called nanosleep()). The thread is waiting for a reply to be delivered across the network (i.e. it called MsgReply*()). The thread is waiting for a pulse or signal to be delivered across the network (i.e. it called MsgSendPulse(), MsgDeliverEvent(), or SignalKill()). The thread is waiting to be executed while the processor executes another thread of equal or higher priority. The thread is blocked on a message receive (e.g. it called MsgReceive()). The thread is blocked on a message reply (i.e. it called MsgSend(), and the server received the message). The thread is being executed by a processor. The kernel uses an array (with one entry per processor in the system) to keep track of the running threads. The thread is waiting for a semaphore to be posted (i.e. it called SyncSemWait()). The thread is blocked on a message send (e.g. it called MsgSend(), but the server hasnt yet received the message). The thread is blocked waiting for a signal (i.e. it called sigsuspend()). The thread is blocked waiting for a signal (i.e. it called sigwaitinfo()). The thread is waiting for the virtual address space to be allocated for the threads stack (parent will have called ThreadCreate()). The thread is blocked waiting for a SIGCONT signal. The thread is waiting for a noninteger (e.g. oating point) context to become available for use. The thread is waiting for physical memory to be allocated for a virtual address. The thread is waiting for a child thread to nish creating itself (i.e. it called ThreadCreate()).
May 8, 2009
23
Thread scheduling
Thread scheduling
When are scheduling decisions are made?
The execution of a running thread is temporarily suspended whenever the microkernel is entered as the result of a kernel call, exception, or hardware interrupt. A scheduling decision is made whenever the execution state of any thread changes it doesnt matter which processes the threads might reside within. Threads are scheduled globally across all processes. Normally, the execution of the suspended thread will resume, but the thread scheduler will perform a context switch from one thread to another whenever the running thread: is blocked is preempted yields
Scheduling priority
Every thread is assigned a priority. The thread scheduler selects the next thread to run by looking at the priority assigned to every thread that is READY (i.e. capable of using the CPU). The thread with the highest priority is selected to run. The following diagram shows the ready queue for ve threads (BF) that are READY. Thread A is currently running. All other threads (GZ) are BLOCKED. Thread A, B, and C are at the highest priority, so theyll share the processor based on the running threads scheduling algorithm.
24
May 8, 2009
Thread scheduling
Running
10 B C
Priority
5 D
Blocked
E 0
Idle F
The OS supports a total of 256 scheduling priority levels. A non-root thread can set its priority to a level from 1 to 63 (the highest priority), independent of the scheduling policy. Only root threads (i.e. those whose effective uid is 0) are allowed to set priorities above 63. The special idle thread (in the process manager) has priority 0 and is always ready to run. A thread inherits the priority of its parent thread by default. You can change the allowed priority range for non-root processes with the procnto -P option:
procnto -P priority
Heres a summary of the ranges: Priority level 0 1 through priority 1 priority through 255 Owner Idle thread Non-root or root
root
Note that in order to prevent priority inversion, the kernel may temporarily boost a threads priority. For more information, see Priority inheritance and mutexes later in this chapter, and Priority inheritance and messages in the Interprocess Communication (IPC) chapter.
May 8, 2009
25
Thread scheduling
The threads on the ready queue are ordered by priority. The ready queue is actually implemented as 256 separate queues, one for each priority. The rst thread in the highest-priority queue is selected to run. Most of the time, threads are queued in FIFO order in the queue of their priority, but there are some exceptions: A server thread thats coming out of a RECEIVE-blocked state with a message from a client is inserted at the head of the queue for that priority that is, the order is LIFO, not FIFO. If a thread sends a message with an nc (non-cancellation point) variant of MsgSend*(), then when the server replies, the thread is placed at the front of the ready queue, rather than at the end. If the scheduling policy is round-robin, the threads timeslice isnt replenished; for example, if the thread had already used half its timeslice before sending, then it still has only half a timeslice left before being eligible for preemption.
Scheduling algorithms
To meet the needs of various applications, QNX Neutrino provides these scheduling algorithms: FIFO scheduling round-robin scheduling sporadic scheduling Each thread in the system may run using any method. The methods are effective on a per-thread basis, not on a global basis for all threads and processes on a node. Remember that the FIFO and round-robin scheduling algorithms apply only when two or more threads that share the same priority are READY (i.e. the threads are directly competing with each other). The sporadic method, however, employs a budget for a threads execution. In all cases, if a higher-priority thread becomes READY, it immediately preempts all lower-priority threads. In the following diagram, three threads of equal priority are READY. If Thread A blocks, Thread B will run.
26
May 8, 2009
Thread scheduling
10 Priority B C
Blocked
Although a thread inherits its scheduling algorithm from its parent process, the thread can request to change the algorithm applied by the kernel.
FIFO scheduling
In FIFO scheduling, a thread selected to run continues executing until it: voluntarily relinquishes control (e.g. it blocks) is preempted by a higher-priority thread
Ready queue Running
Priority
10 B C
FIFO scheduling.
Round-robin scheduling
In round-robin scheduling, a thread selected to run continues executing until it: voluntarily relinquishes control is preempted by a higher-priority thread
May 8, 2009
27
Thread scheduling
consumes its timeslice As the following diagram shows, Thread A ran until it consumed its timeslice; the next READY thread (Thread B) now runs:
Ready queue Running A
Priority
10
Round-robin scheduling.
A timeslice is the unit of time assigned to every process. Once it consumes its timeslice, a thread is preempted and the next READY thread at the same priority level is given control. A timeslice is 4 the clock period. (For more information, see the entry for ClockPeriod() in the Neutrino Library Reference.) Apart from time slicing, round-robin scheduling is identical to FIFO scheduling.
Sporadic scheduling
The sporadic scheduling algorithm is generally used to provide a capped limit on the execution time of a thread within a given period of time. This behavior is essential when Rate Monotonic Analysis (RMA) is being performed on a system that services both periodic and aperiodic events. Essentially, this algorithm allows a thread to service aperiodic events without jeopardizing the hard deadlines of other threads or processes in the system. As in FIFO scheduling, a thread using sporadic scheduling continues executing until it blocks or is preempted by a higher-priority thread. And as in adaptive scheduling, a thread using sporadic scheduling will drop in priority, but with sporadic scheduling you have much more precise control over the threads behavior. Under sporadic scheduling, a threads priority can oscillate dynamically between a foreground or normal priority and a background or low priority. Using the following parameters, you can control the conditions of this sporadic shift: Initial budget (C) The amount of time a thread is allowed to execute at its normal priority (N) before being dropped to its low priority (L).
28
May 8, 2009
Thread scheduling
The priority level to which the thread will drop. The thread executes at this lower priority (L) while in the background, and runs at normal priority (N) while in the foreground.
Replenishment period (T) The period of time during which a thread is allowed to consume its execution budget. To schedule replenishment operations, the POSIX implementation also uses this value as the offset from the time the thread becomes READY. Max number of pending replenishments This value limits the number of replenishment operations that can take place, thereby bounding the amount of system overhead consumed by the sporadic scheduling policy. In a poorly congured system, a threads execution budget may become eroded because of too much blocking i.e. it wont receive enough replenishments. As the following diagram shows, the sporadic scheduling policy establishes a threads initial execution budget (C), which is consumed by the thread as it runs and is replenished periodically (for the amount T). When a thread blocks, the amount of the execution budget thats been consumed (R) is arranged to be replenished at some later time (e.g. at 40 msec) after the thread rst became ready to run.
Replenished at this point
R C T C
0 msec
40 msec
80 msec
At its normal priority N, a thread will execute for the amount of time dened by its initial execution budget C. As soon as this time is exhausted, the priority of the thread will drop to its low priority L until the replenishment operation occurs. Assume, for example, a system where the thread never blocks or is never preempted:
May 8, 2009
29
Thread scheduling
Priority N Exhausts budget; drops to lower priority Priority L May or may not run T 80 msec Replenished at this point; priority is restored
0 msec
40 msec
Here the thread will drop to its low-priority (background) level, where it may or may not get a chance to run depending on the priority of other threads in the system. Once the replenishment occurs, the threads priority is raised to its original level. This guarantees that within a properly congured system, the thread will be given the opportunity every period T to run for a maximum execution time C. This ensures that a thread running at priority N will consume only C/T percent of the systems resources. When a thread blocks multiple times, then several replenishment operations may be started and occur at different times. This could mean that the threads execution budget will total C within a period T; however, the execution budget may not be contiguous during that period.
1 Priority N 3 4 5 6 2
Priority L T T
0 msec
40 msec
80 msec
In the diagram above, the thread has a budget (C) of 10 msec to be consumed within each 40-msec replenishment period (T). 1 The initial run of the thread is blocked after 3 msec, so a replenishment operation of 3 msec is scheduled to begin at the 40-msec mark, i.e. when its rst replenishment period has elapsed.
30
May 8, 2009
Thread scheduling
The thread gets an opportunity to run again at 6 msec, which marks the start of another replenishment period (T). The thread still has 7 msec remaining in its budget. The thread runs without blocking for 7 msec, thereby exhausting its budget, and then drops to low priority L, where it may or may not be able to execute. A replenishment of 7 msec is scheduled to occur at 46 msec (40 + 6), i.e. when T has elapsed. The thread has 3 msec of its budget replenished at 40 msec (see Step 1) and is therefore boosted back to its normal priority. The thread consumes the 3 msec of its budget and then is dropped back to the low priority. The thread has 7 msec of its budget replenished at 46 msec (see Step 3) and is boosted back to its normal priority.
4 5 6
And so on. The thread will continue to oscillate between its two priority levels, servicing aperiodic events in your system in a controlled, predictable manner.
IPC issues
Since all the threads in a process have unhindered access to the shared data space, wouldnt this execution model trivially solve all of our IPC problems? Cant we just communicate the data through shared memory and dispense with any other execution models and IPC mechanisms? If only it were that simple! One issue is that the access of individual threads to common data must be synchronized. Having one thread read inconsistent data because another thread is part way through modifying it is a recipe for disaster. For example, if one thread is updating a linked list, no other threads can be allowed to traverse or modify the list until the rst thread has nished. A code passage that must execute serially (i.e. by only one thread at a time) in this manner is termed a critical section. The program would fail (intermittently, depending on how frequently a collision occurred) with
May 8, 2009
31
Thread scheduling
irreparably damaged links unless some synchronization mechanism ensured serial access. Mutexes, semaphores, and condvars are examples of synchronization tools that can be used to address this problem. These tools are described later in this section. Although synchronization services can be used to allow threads to cooperate, shared memory per se cant address a number of IPC issues. For example, although threads can communicate through the common data space, this works only if all the threads communicating are within a single process. What if our application needs to communicate a query to a database server? We need to pass the details of our query to the database server, but the thread we need to communicate with lies within a database server process and the address space of that server isnt addressable to us. The OS takes care of the network-distributed IPC issue because the one interface message passing operates in both the local and network-remote cases, and can be used to access all OS services. Since messages can be exactly sized, and since most messages tend to be quite tiny (e.g. the error status on a write request, or a tiny read request), the data moved around the network can be far less with message passing than with network-distributed shared memory, which would tend to copy 4K pages around.
32
May 8, 2009
Synchronization services
Nevertheless, threads can also provide concurrency advantages that a pure process model cannot address. For example, a lesystem server process that executes requests on behalf of many clients (where each request takes signicant time to complete), denitely benets from having multiple threads of execution. If one client process requests a block from disk, while another client requests a block already in cache, the lesystem process can utilize a pool of threads to concurrently service client requests, rather than remain busy until the disk block is read for the rst request. As requests arrive, each thread is able to respond directly from the buffer cache or to block and wait for disk I/O without increasing the response latency seen by other client processes. The lesystem server can precreate a team of threads, ready to respond in turn to client requests as they arrive. Although this complicates the architecture of the lesystem manager, the gains in concurrency are signicant.
Synchronization services
QNX Neutrino provides the POSIX-standard thread-level synchronization primitives, some of which are useful even between threads in different processes. The synchronization services include at least the following: Synchronization service Mutexes Condvars Barriers Sleepon locks Reader/writer locks Semaphores FIFO scheduling Send/Receive/Reply Atomic operations Supported between processes Yes Yes No No Yes Yes Yes Yes Yes Supported across a QNX LAN No No No No No Yes (named only) No Yes No
The above synchronization primitives are implemented directly by the kernel, except for: barriers, sleepon locks, and reader/writer locks (which are built from mutexes and condvars) atomic operations (which are either implemented directly by the processor or emulated in the kernel)
May 8, 2009
33
Synchronization services
CAUTION: You should allocate mutexes, condvars, barriers, reader/writer locks, and semaphores, as well as objects you plan to use atomic operations on, only in normal memory mappings. On certain processors (e.g. some PPC ones), atomic operations and calls such as pthread_mutex_lock() will cause a fault if the object is allocated in uncached memory.
34
May 8, 2009
Synchronization services
You can also modify the attributes of the mutex (using pthread_mutexattr_setrecursive()) to allow a mutex to be recursively locked by the same thread. This can be useful to allow a thread to call a routine that might attempt to lock a mutex that the thread already happens to have locked. Recursive mutexes are non-POSIX services they dont work with condvars.
In this code sample, the mutex is acquired before the condition is tested. This ensures that only this thread has access to the arbitrary condition being examined. While the condition is true, the code sample will block on the wait call until some other thread performs a signal or broadcast on the condvar. The while loop is required for two reasons. First of all, POSIX cannot guarantee that false wakeups will not occur (e.g. multiprocessor systems). Second, when another thread has made a modication to the condition, we need to retest to ensure that the modication matches our criteria. The associated mutex is unlocked atomically by pthread_cond_wait() when the waiting thread is blocked to allow another thread to enter the critical section. A thread that performs a signal will unblock the highest-priority thread queued on the condvar, while a broadcast will unblock all threads queued on the condvar. The associated mutex is locked atomically by the highest-priority unblocked thread; the thread must then unlock the mutex after proceeding through the critical section.
May 8, 2009
35
Synchronization services
A version of the condvar wait operation allows a timeout to be specied (pthread_cond_timedwait()). The waiting thread can then be unblocked when the timeout expires.
Barriers
A barrier is a synchronization mechanism that lets you corral several cooperating threads (e.g. in a matrix computation), forcing them to wait at a specic point until all have nished before any one thread can continue. Unlike the pthread_join() function, where youd wait for the threads to terminate, in the case of a barrier youre waiting for the threads to rendezvous at a certain point. When the specied number of threads arrive at the barrier, we unblock all of them so they can continue to run. You rst create a barrier with pthread_barrier_init():
#include <pthread.h> int pthread_barrier_init (pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count);
This creates a barrier object at the passed address (a pointer to the barrier object is in barrier), with the attributes as specied by attr. The count member holds the number of threads that must call pthread_barrier_wait(). Once the barrier is created, each thread will call pthread_barrier_wait() to indicate that it has completed:
#include <pthread.h> int pthread_barrier_wait (pthread_barrier_t *barrier);
When a thread calls pthread_barrier_wait(), it blocks until the number of threads specied initially in the pthread_barrier_init() function have called pthread_barrier_wait() (and blocked also). When the correct number of threads have called pthread_barrier_wait(), all those threads will unblock at the same time. Heres an example:
/* * barrier1.c */ #include #include #include #include #include #include <stdio.h> <unistd.h> <stdlib.h> <time.h> <pthread.h> <sys/neutrino.h> barrier; // barrier synchronization object
pthread_barrier_t
36
May 8, 2009
Synchronization services
void * thread1 (void *not_used) { time_t now; time (&now); printf ("thread1 starting at %s", ctime (&now)); // do the computation // lets just do a sleep here... sleep (20); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread1() done at %s", ctime (&now)); } void * thread2 (void *not_used) { time_t now; time (&now); printf ("thread2 starting at %s", ctime (&now)); // do the computation // lets just do a sleep here... sleep (40); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in thread2() done at %s", ctime (&now)); } int main () // ignore arguments { time_t now; // create a barrier object with a count of 3 pthread_barrier_init (&barrier, NULL, 3); // start up two threads, thread1 and thread2 pthread_create (NULL, NULL, thread1, NULL); pthread_create (NULL, NULL, thread2, NULL); // at this point, thread1 and thread2 are running // now wait for completion time (&now); printf ("main() waiting for barrier at %s", ctime (&now)); pthread_barrier_wait (&barrier); // after this point, all three threads have completed. time (&now); printf ("barrier in main() done at %s", ctime (&now)); pthread_exit( NULL ); return (EXIT_SUCCESS); }
The main thread created the barrier object and initialized it with a count of the total number of threads that must be synchronized to the barrier before the threads may
May 8, 2009
37
Synchronization services
carry on. In the example above, we used a count of 3: one for the main() thread, one for thread1(), and one for thread2(). Then we start thread1() and thread2(). To simplify this example, we have the threads sleep to cause a delay, as if computations were occurring. To synchronize, the main thread simply blocks itself on the barrier, knowing that the barrier will unblock only after the two worker threads have joined it as well. In this release, the following barrier functions are included: Function pthread_barrierattr_getpshared() pthread_barrierattr_destroy() pthread_barrierattr_init() pthread_barrierattr_setpshared() pthread_barrier_destroy() pthread_barrier_init() pthread_barrier_wait() Description Get the value of a barriers process-shared attribute Destroy a barriers attributes object Initialize a barriers attributes object Set the value of a barriers process-shared attribute Destroy a barrier Initialize a barrier Synchronize participating threads at the barrier
Sleepon locks
Sleepon locks are very similar to condvars, with a few subtle differences. Like condvars, sleepon locks (pthread_sleepon_lock()) can be used to block until a condition becomes true (like a memory location changing value). But unlike condvars, which must be allocated for each condition to be checked, sleepon locks multiplex their functionality over a single mutex and dynamically allocated condvar, regardless of the number of conditions being checked. The maximum number of condvars ends up being equal to the maximum number of blocked threads. These locks are patterned after the sleepon locks commonly used within the UNIX kernel.
Reader/writer locks
More formally known as Multiple readers, single writer locks, these locks are used when the access pattern for a data structure consists of many threads reading the data, and (at most) one thread writing the data. These locks are more expensive than mutexes, but can be useful for this data access pattern. This lock works by allowing all the threads that request a read-access lock (pthread_rwlock_rdlock()) to succeed in their request. But when a thread wishing to write asks for the lock (pthread_rwlock_wrlock()), the request is denied until all the current reading threads release their reading locks (pthread_rwlock_unlock()).
38
May 8, 2009
Synchronization services
Multiple writing threads can queue (in priority order) waiting for their chance to write the protected data structure, and all the blocked writer-threads will get to run before reading threads are allowed access again. The priorities of the reading threads are not considered. There are also calls (pthread_rwlock_tryrdlock() and pthread_rwlock_trywrlock()) to allow a thread to test the attempt to achieve the requested lock, without blocking. These calls return with a successful lock or a status indicating that the lock couldnt be granted immediately. Reader/writer locks arent implemented directly within the kernel, but are instead built from the mutex and condvar services provided by the kernel.
Semaphores
Semaphores are another common form of synchronization that allows threads to post (sem_post()) and wait (sem_wait()) on a semaphore to control when threads wake or sleep. The post operation increments the semaphore; the wait operation decrements it. If you wait on a semaphore that is positive, you will not block. Waiting on a nonpositive semaphore will block until some other thread executes a post. It is valid to post one or more times before a wait. This use will allow one or more threads to execute the wait without blocking. A signicant difference between semaphores and other synchronization primitives is that semaphores are async safe and can be manipulated by signal handlers. If the desired effect is to have a signal handler wake a thread, semaphores are the right choice. Note that in general, mutexes are much faster than semaphores, which always require a kernel entry. Semaphores dont affect a threads effective priority; if you need priority inheritance, use a mutex. For more information, see Mutexes: mutual exclusion locks, earlier in this chapter. Another useful property of semaphores is that they were dened to operate between processes. Although our mutexes work between processes, the POSIX thread standard considers this an optional capability and as such may not be portable across systems. For synchronization between threads in a single process, mutexes will be more efcient than semaphores. As a useful variation, a named semaphore service is also available. It lets you use semaphores between processes on different machines connected by a network. Note that named semaphores are slower than the unnamed variety. Since semaphores, like condition variables, can legally return a nonzero value because of a false wake-up, correct usage requires a loop:
while (sem_wait(&s) && (errno == EINTR)) { do_nothing(); } do_critical_region(); /* Semaphore was decremented */
May 8, 2009
39
Synchronization services
CAUTION: This exclusive-access relationship doesnt apply in multiprocessor systems, since each CPU could run a thread simultaneously through the region that would otherwise be serially scheduled on a single-processor machine.
40
May 8, 2009
These atomic operations are available by including the C header le <atomic.h>. Although you can use these atomic operations just about anywhere, youll nd them particularly useful in these two cases: between an ISR and a thread between two threads (SMP or single-processor) Since an ISR can preempt a thread at any given point, the only way that the thread would be able to protect itself would be to disable interrupts. Since you should avoid disabling interrupts in a realtime system, we recommend that you use the atomic operations provided with QNX Neutrino. On an SMP system, multiple threads can and do run concurrently. Again, we run into the same situation as with interrupts above you should use the atomic operations where applicable to eliminate the need to disable and reenable interrupts.
Block on a condvar. Wake up condvar-blocked threads. Lock a mutex. Unlock a mutex. Post a semaphore. Wait on a semaphore.
May 8, 2009
41
Valid dates on a QNX Neutrino system range from January 1970 to at least 2038. The time_t data type is an unsigned 32-bit number, which extends this range for many applications through 2106. The kernel itself uses unsigned 64-bit numbers to count the nanoseconds since January 1970, and so can handle dates through 2554. If your system must operate past 2554 and theres no way for the system to be upgraded or modied in the eld, youll have to take special care with system dates (contact us for help with this). The ClockTime() kernel call allows you to get or set the system clock specied by an ID (CLOCK_REALTIME), which maintains the system time. Once set, the system time increments by some number of nanoseconds based on the resolution of the system clock. This resolution can be queried or changed using the ClockPeriod() call. Within the system page, an in-memory data structure, theres a 64-bit eld (nsec) that holds the number of nanoseconds since the system was booted. The nsec eld is always monotonically increasing and is never affected by setting the current time of day via ClockTime() or ClockAdjust(). The ClockCycles() function returns the current value of a free-running 64-bit cycle counter. This is implemented on each processor as a high-performance mechanism for timing short intervals. For example, on Intel x86 processors, an opcode that reads the processors time-stamp counter is used. On a Pentium processor, this counter increments on each clock cycle. A 100 MHz Pentium would have a cycle time of 1/100,000,000 seconds (10 nanoseconds). Other CPU architectures have similar instructions. On processors that dont implement such an instruction in hardware (e.g. a 386), the kernel will emulate one. This will provide a lower time resolution than if the instruction is provided (838.095345 nanoseconds on an IBM PC-compatible system). In all cases, the SYSPAGE_ENTRY(qtime)->cycles_per_sec eld gives the number of ClockCycles() increments in one second. The ClockPeriod() function allows a thread to set the system timer to some multiple of nanoseconds; the OS kernel will do the best it can to satisfy the precision of the request with the hardware available. The interval selected is always rounded down to an integral of the precision of the underlying hardware timer. Of course, setting it to an extremely low value can result in a signicant portion of CPU performance being consumed servicing timer interrupts. Microkernel call ClockTime() POSIX call clock_gettime(), clock_settime() Description Get or set the time of day (using a 64-bit value in nanoseconds ranging from 1970 to 2554).
continued. . .
42
May 8, 2009
Description Apply small time adjustments to synchronize clocks. Read a 64-bit free-running high-precision counter. Get or set the period of the clock. Return an integer thats passed to ClockTime() as a clockid_t.
Time correction
In order to facilitate applying time corrections without having the system experience abrupt steps in time (or even having time jump backwards), the ClockAdjust() call provides the option to specify an interval over which the time correction is to be applied. This has the effect of speeding or retarding time over a specied interval until the system has synchronized to the indicated current time. This service can be used to implement network-coordinated time averaging between multiple nodes on a network.
Timers
QNX Neutrino directly provides the full set of POSIX timer functionality. Since these timers are quick to create and manipulate, theyre an inexpensive resource in the kernel. The POSIX timer model is quite rich, providing the ability to have the timer expire on: an absolute date a relative date (i.e. n nanoseconds from now) cyclical (i.e. every n nanoseconds) The cyclical mode is very signicant, because the most common use of timers tends to be as a periodic source of events to kick a thread into life to do some processing and then go back to sleep until the next event. If the thread had to re-program the timer for every event, there would be the danger that time would slip unless the thread was programming an absolute date. Worse, if the thread doesnt get to run on the timer event because a higher-priority thread is running, the date next programmed into the timer could be one that has already elapsed! The cyclical mode circumvents these problems by requiring that the thread set the timer once and then simply respond to the resulting periodic source of events. Since timers are another source of events in the OS, they also make use of its event-delivery system. As a result, the application can request that any of the Neutrino-supported events be delivered to the application upon occurrence of a timeout.
May 8, 2009
43
An often-needed timeout service provided by the OS is the ability to specify the maximum time the application is prepared to wait for any given kernel call or request to complete. A problem with using generic OS timer services in a preemptive realtime OS is that in the interval between the specication of the timeout and the request for the service, a higher-priority process might have been scheduled to run and preempted long enough that the specied timeout will have expired before the service is even requested. The application will then end up requesting the service with an already lapsed timeout in effect (i.e. no timeout). This timing window can result in hung processes, inexplicable delays in data transmission protocols, and other problems.
alarm(...); . . . . . . Alarm fires here . . . blocking_call();
Our solution is a form of timeout request atomic to the service request itself. One approach might have been to provide an optional timeout parameter on every available service request, but this would overly complicate service requests with a passed parameter that would often go unused. QNX Neutrino provides a TimerTimeout() kernel call that allows an application to specify a list of blocking states for which to start a specied timeout. Later, when the application makes a request of the kernel, the kernel will atomically enable the previously congured timeout if the application is about to block on one of the specied states. Since the OS has a very small number of blocking states, this mechanism works very concisely. At the conclusion of either the service request or the timeout, the timer will be disabled and control will be given back to the application.
TimerTimeout(...); . . . . . . . . . blocking_call(); . . . Timer atomically armed within kernel
Description Set a process alarm. Create an interval timer. Destroy an interval timer.
continued. . .
44
May 8, 2009
Interrupt handling
POSIX call timer_gettime() timer_getoverrun() timer_settime() sleep(), nanosleep(), sigtimedwait(), pthread_cond_timedwait(), pthread_mutex_trylock()
Description Get time remaining on an interval timer. Get number of overruns on an interval timer. Start an interval timer. Arm a kernel timeout for any blocking state.
For more information, see the Clocks, Timers, and Getting a Kick Every So Often chapter of Getting Started with QNX Neutrino.
Interrupt handling
No matter how much we wish it were so, computers are not innitely fast. In a realtime system, its absolutely crucial that CPU cycles arent unnecessarily spent. Its also crucial to minimize the time from the occurrence of an external event to the actual execution of code within the thread responsible for reacting to that event. This time is referred to as latency. The two forms of latency that most concern us are interrupt latency and scheduling latency. Latency times can vary signicantly, depending on the speed of the processor and other factors. For more information, visit our website (www.qnx.com).
Interrupt latency
Interrupt latency is the time from the assertion of a hardware interrupt until the rst instruction of the device drivers interrupt handler is executed. The OS leaves interrupts fully enabled almost all the time, so that interrupt latency is typically insignicant. But certain critical sections of code do require that interrupts be temporarily disabled. The maximum such disable time usually denes the worst-case interrupt latency in QNX Neutrino this is very small. The following diagrams illustrate the case where a hardware interrupt is processed by an established interrupt handler. The interrupt handler either will simply return, or it will return and cause an event to be delivered.
May 8, 2009
45
Interrupt handling
T il
Tint
T iret
T il
interrupt latency
The interrupt latency (Til) in the above diagram represents the minimum latency that which occurs when interrupts were fully enabled at the time the interrupt occurred. Worst-case interrupt latency will be this time plus the longest time in which the OS, or the running system process, disables CPU interrupts.
Scheduling latency
In some cases, the low-level hardware interrupt handler must schedule a higher-level thread to run. In this scenario, the interrupt handler will return and indicate that an event is to be delivered. This introduces a second form of latency scheduling latency which must be accounted for. Scheduling latency is the time between the last instruction of the users interrupt handler and the execution of the rst instruction of a driver thread. This usually means the time it takes to save the context of the currently executing thread and restore the context of the required driver thread. Although larger than interrupt latency, this time is also kept small in a QNX Neutrino system.
46
May 8, 2009
Interrupt handling
Interrupt occurs
T il
interrupt latency
Its important to note that most interrupts terminate without delivering an event. In a large number of cases, the interrupt handler can take care of all hardware-related issues. Delivering an event to wake up a higher-level driver thread occurs only when a signicant event occurs. For example, the interrupt handler for a serial device driver would feed one byte of data to the hardware upon each received transmit interrupt, and would trigger the higher-level thread within (devc-ser*) only when the output buffer is nearly empty.
Nested interrupts
QNX Neutrino fully supports nested interrupts. The previous scenarios describe the simplest and most common situation where only one interrupt occurs. Worst-case timing considerations for unmasked interrupts must take into account the time for all interrupts currently being processed, because a higher priority, unmasked interrupt will preempt an existing interrupt. In the following diagram, Thread A is running. Interrupt IRQx causes interrupt handler Intx to run, which is preempted by IRQy and its handler Inty . Inty returns an event causing Thread B to run; Intx returns an event causing Thread C to run.
May 8, 2009
47
Interrupt handling
Time
IRQ x
Stacked interrupts.
Interrupt calls
The interrupt-handling API includes the following kernel calls: Function InterruptAttach() InterruptAttachEvent() Description Attach a local function to an interrupt vector. Generate an event on an interrupt, which will ready a thread. No user interrupt handler runs. This is the preferred call. Detach from an interrupt using the ID returned by InterruptAttach() or InterruptAttachEvent(). Wait for an interrupt. Enable hardware interrupts. Disable hardware interrupts. Mask a hardware interrupt. Unmask a hardware interrupt. Guard a critical section of code between an interrupt handler and a thread. A spinlock is used to make this code SMP-safe. This function is a superset of InterruptDisable() and should be used in its place. Remove an SMP-safe lock on a critical section of code.
InterruptUnlock()
Using this API, a suitably privileged user-level thread can call InterruptAttach() or InterruptAttachEvent(), passing a hardware interrupt number and the address of a function in the threads address space to be called when the interrupt occurs. QNX Neutrino allows multiple ISRs to be attached to each hardware interrupt number
48
May 8, 2009
Interrupt handling
unmasked interrupts can be serviced during the execution of running interrupt handlers. The startup code is responsible for making sure that all interrupt sources are masked during system initialization. When the rst call to InterruptAttach() or InterruptAttachEvent() is done for an interrupt vector, the kernel unmasks it. Similarly, when the last InterruptDetach() is done for an interrupt vector, the kernel remasks the level. For more information on InterruptLock() and InterruptUnlock(), see Critical sections in the chapter on Multicore Processing in this guide. The following code sample shows how to attach an ISR to the hardware timer interrupt on the PC (which the OS also uses for the system clock). Since the kernels timer ISR is already dealing with clearing the source of the interrupt, this ISR can simply increment a counter variable in the threads data space and return to the kernel:
#include <stdio.h> #include <sys/neutrino.h> #include <sys/syspage.h> struct sigevent event; volatile unsigned counter; const struct sigevent *handler( void *area, int id ) { // Wake up the thread every 100th interrupt if ( ++counter == 100 ) { counter = 0; return( &event ); } else return( NULL ); } int main() { int i; int id; // Request I/O privileges ThreadCtl( _NTO_TCTL_IO, 0 ); // Initialize event structure event.sigev_notify = SIGEV_INTR; // Attach ISR vector id=InterruptAttach( SYSPAGE_ENTRY(qtime)->intr, &handler, NULL, 0, 0 ); for( i = 0; i < 10; ++i ) { // Wait for ISR to wake us up InterruptWait( 0, NULL ); printf( "100 events\n" ); } // Disconnect the ISR handler InterruptDetach(id); return 0;
May 8, 2009
49
Interrupt handling
With this approach, appropriately privileged user-level threads can dynamically attach (and detach) interrupt handlers to (and from) hardware interrupt vectors at run time. These threads can be debugged using regular source-level debug tools; the ISR itself can be debugged by calling it at the thread level and source-level stepping through it or by using the InterruptAttachEvent() call. When the hardware interrupt occurs, the processor will enter the interrupt redirector in the microkernel. This code pushes the registers for the context of the currently running thread into the appropriate thread table entry and sets the processor context such that the ISR has access to the code and data that are part of the thread the ISR is contained within. This allows the ISR to use the buffers and code in the user-level thread to resolve the interrupt and, if higher-level work by the thread is required, to queue an event to the thread the ISR is part of, which can then work on the data the ISR has placed into thread-owned buffers. Since it runs with the memory-mapping of the thread containing it, the ISR can directly manipulate devices mapped into the threads address space, or directly perform I/O instructions. As a result, device drivers that manipulate hardware dont need to be linked into the kernel. The interrupt redirector code in the microkernel will call each ISR attached to that hardware interrupt. If the value returned indicates that a process is to be passed an event of some sort, the kernel will queue the event. When the last ISR has been called for that vector, the kernel interrupt handler will nish manipulating the interrupt control hardware and then return from interrupt. This interrupt return wont necessarily be into the context of the thread that was interrupted. If the queued event caused a higher-priority thread to become READY, the microkernel will then interrupt-return into the context of the now-READY thread instead. This approach provides a well-bounded interval from the occurrence of the interrupt to the execution of the rst instruction of the user-level ISR (measured as interrupt latency), and from the last instruction of the ISR to the rst instruction of the thread readied by the ISR (measured as thread or process scheduling latency). The worst-case interrupt latency is well-bounded, because the OS disables interrupts only for a couple opcodes in a few critical regions. Those intervals when interrupts are disabled have deterministic runtimes, because theyre not data dependent. The microkernels interrupt redirector executes only a few instructions before calling the users ISR. As a result, process preemption for hardware interrupts or kernel calls is equally quick and exercises essentially the same code path. While the ISR is executing, it has full hardware access (since its part of a privileged thread), but cant issue other kernel calls. The ISR is intended to respond to the hardware interrupt in as few microseconds as possible, do the minimum amount of work to satisfy the interrupt (read the byte from the UART, etc.), and if necessary, cause a thread to be scheduled at some user-specied priority to do further work.
50
May 8, 2009
Interrupt handling
Worst-case interrupt latency is directly computable for a given hardware priority from the kernel-imposed interrupt latency and the maximum ISR runtime for each interrupt higher in hardware priority than the ISR in question. Since hardware interrupt priorities can be reassigned, the most important interrupt in the system can be made the highest priority. Note also that by using the InterruptAttachEvent() call, no user ISR is run. Instead, a user-specied event is generated on each and every interrupt; the event will typically cause a waiting thread to be scheduled to run and do the work. The interrupt is automatically masked when the event is generated and then explicitly unmasked by the thread that handles the device at the appropriate time. Both InterruptMask() and InterruptUnmask() are counting functions. For example, if InterruptMask() is called ten times, then InterruptUnmask() must also be called ten times. Thus the priority of the work generated by hardware interrupts can be performed at OS-scheduled priorities rather than hardware-dened priorities. Since the interrupt source wont re-interrupt until serviced, the effect of interrupts on the runtime of critical code regions for hard-deadline scheduling can be controlled. In addition to hardware interrupts, various events within the microkernel can also be hooked by user processes and threads. When one of these events occurs, the kernel can upcall into the indicated function in the user thread to perform some specic processing for this event. For example, whenever the idle thread in the system is called, a user thread can have the kernel upcall into the thread so that hardware-specic low-power modes can be readily implemented. Microkernel call InterruptHookIdle() Description When the kernel has no active thread to schedule, it will run the idle thread, which can upcall to a user handler. This handler can perform hardware-specic power-management operations. This function attaches a pseudo interrupt handler that can receive trace events from the instrumented kernel.
InterruptHookTrace()
For more information about interrupts, see the Interrupts chapter of Getting Started with QNX Neutrino, and the Writing an Interrupt Handler chapter of the Neutrino Programmers Guide.
May 8, 2009
51
64
May 8, 2009
53
Interprocess Communication plays a fundamental role in the transformation of QNX Neutrino from an embedded realtime kernel into a full-scale POSIX operating system. As various service-providing processes are added to the microkernel, IPC is the glue that connects those components into a cohesive whole. Although message passing is the primary form of IPC in QNX Neutrino, several other forms are available as well. Unless otherwise noted, those other forms of IPC are built over our native message passing. The strategy is to create a simple, robust IPC service that can be tuned for performance through a simplied code path in the microkernel; more feature cluttered IPC services can then be implemented from these. Benchmarks comparing higher-level IPC services (like pipes and FIFOs implemented over our messaging) with their monolithic kernel counterparts show comparable performance. QNX Neutrino offers at least the following forms of IPC: Service: Message-passing Signals POSIX message queues Shared memory Pipes FIFOs Implemented in: Kernel Kernel External process Process manager External process External process
The designer can select these services on the basis of bandwidth requirements, the need for queuing, network transparency, etc. The trade-off can be complex, but the exibility is useful. As part of the engineering effort that went into dening the QNX Neutrino microkernel, the focus on message passing as the fundamental IPC primitive was deliberate. As a form of IPC, message passing (as implemented in MsgSend(), MsgReceive(), and MsgReply()), is synchronous and copies data. Lets explore these two attributes in more detail.
May 8, 2009
55
READY state if it can use the CPU. It might not actually be getting any CPU time because of its and other threads priority and scheduling algorithm, but the thread isnt blocked. Lets look rst at the client thread:
SEND blocked
Client does a MsgSend() Client does a MsgSend() READY Server does a MsgReply() or MsgError()
REPLY blocked
Legend:
This thread Other thread
If the client thread calls MsgSend(), and the server thread hasnt yet called MsgReceive(), then the client thread becomes SEND blocked. Once the server thread calls MsgReceive(), the kernel changes the client threads state to be REPLY blocked, which means that server thread has received the message and now must reply. When the server thread calls MsgReply(), the client thread becomes READY. If the client thread calls MsgSend(), and the server thread is already blocked on the MsgReceive(), then the client thread immediately becomes REPLY blocked, skipping the SEND-blocked state completely. If the server thread fails, exits, or disappears, the client thread becomes READY, with MsgSend() indicating an error. Next, lets consider the server thread:
56
May 8, 2009
Server does a MsgReply() or MsgError() Client does a MsgSend() RECEIVE blocked Server does a MsgReceive() READY
Legend:
This thread
Other thread
If the server thread calls MsgReceive(), and no other thread has sent to it, then the server thread becomes RECEIVE blocked. When another thread sends to it, the server thread becomes READY. If the server thread calls MsgReceive(), and another thread has already sent to it, then MsgReceive() returns immediately with the message. In this case, the server thread doesnt block. If the server thread calls MsgReply(), it doesnt become blocked. This inherent blocking synchronizes the execution of the sending thread, since the act of requesting that the data be sent also causes the sending thread to be blocked and the receiving thread to be scheduled for execution. This happens without requiring explicit work by the kernel to determine which thread to run next (as would be the case with most other forms of IPC). Execution and data move directly from one context to another. Data-queuing capabilities are omitted from these messaging primitives because queueing could be implemented when needed within the receiving thread. The sending thread is often prepared to wait for a response; queueing is unnecessary overhead and complexity (i.e. it slows down the nonqueued case). As a result, the sending thread doesnt need to make a separate, explicit blocking call to wait for a response (as it would if some other IPC form had been used). While the send and receive operations are blocking and synchronous, MsgReply() (or MsgError()) doesnt block. Since the client thread is already blocked waiting for the reply, no additional synchronization is required, so a blocking MsgReply() isnt needed. This allows a server to reply to a client and continue processing while the kernel and/or networking code asynchronously passes the reply data to the sending thread and marks it ready for execution. Since most servers will tend to do some processing to prepare to receive the next request (at which point they block again), this works out well.
May 8, 2009
57
Message copying
Note that in a network, a reply may not complete as immediately as in a local message pass. For more information on network message passing, see the chapter on Qnet networking in this book.
MsgReply() vs MsgError()
The MsgReply() function is used to return a status and zero or more bytes to the client. MsgError(), on the other hand, is used to return only a status to the client. Both functions will unblock the client from its MsgSend().
Message copying
Since our messaging services copy a message directly from the address space of one thread to another without intermediate buffering, the message-delivery performance approaches the memory bandwidth of the underlying hardware. The kernel attaches no special meaning to the content of a message the data in a message has meaning only as mutually dened by sender and receiver. However, well-dened message types are also provided so that user-written processes or threads can augment or substitute for system-supplied services. The messaging primitives support multipart transfers, so that a message delivered from the address space of one thread to another neednt pre-exist in a single, contiguous buffer. Instead, both the sending and receiving threads can specify a vector table that indicates where the sending and receiving message fragments reside in memory. Note that the size of the various parts can be different for the sender and receiver. Multipart transfers allow messages that have a header block separate from the data block to be sent without performance-consuming copying of the data to create a contiguous message. In addition, if the underlying data structure is a ring buffer, specifying a three-part message will allow a header and two disjoint ranges within the ring buffer to be sent as a single atomic message. A hardware equivalent of this concept would be that of a scatter/gather DMA facility.
IOV
Len Each 0 IOV may have any number of parts 2 Addr Part 1 Part 2 Part 3 Each part may be 0 to 4 GB
Message Data
A multipart transfer.
The multipart transfers are also used extensively by lesystems. On a read, the data is copied directly from the lesystem cache into the application using a message with
58
May 8, 2009
Message copying
n parts for the data. Each part points into the cache and compensates for the fact that cache blocks arent contiguous in memory with a read starting or ending within a block. For example, with a cache block size of 512 bytes, a read of 1454 bytes can be satised with a 5-part message:
Five-part IOV
Len 0 16 400 1 512 512 4 30 4 Addr Header
2 3
Since message data is explicitly copied between address spaces (rather than by doing page table manipulations), messages can be easily allocated on the stack instead of from a special pool of page-aligned memory for MMU page ipping. As a result, many of the library routines that implement the API between client and server processes can be trivially expressed, without elaborate IPC-specic memory allocation calls. For example, the code used by a client thread to request that the lesystem manager execute lseek on its behalf is implemented as follows:
#include <unistd.h> #include <errno.h> #include <sys/iomsg.h> off64_t lseek64(int fd, off64_t offset, int whence) { io_lseek_t msg; off64_t off; msg.i.type = _IO_LSEEK; msg.i.combine_len = sizeof msg.i; msg.i.offset = offset; msg.i.whence = whence; msg.i.zero = 0;
May 8, 2009
59
Simple messages
if(MsgSend(fd, &msg.i, sizeof msg.i, &off, sizeof off) == -1) { return -1; } return off; } off64_t tell64(int fd) { return lseek64(fd, 0, SEEK_CUR); } off_t lseek(int fd, off_t offset, int whence) { return lseek64(fd, offset, whence); } off_t tell(int fd) { return lseek64(fd, 0, SEEK_CUR); }
This code essentially builds a message structure on the stack, populates it with various constants and passed parameters from the calling thread, and sends it to the lesystem manager associated with fd. The reply indicates the success or failure of the operation. This implementation doesnt prevent the kernel from detecting large message transfers and choosing to implement page ipping for those cases. Since most messages passed are quite tiny, copying messages is often faster than manipulating MMU page tables. For bulk data transfer, shared memory between processes (with message-passing or the other synchronization primitives for notication) is also a viable option.
Simple messages
For simple single-part messages, the OS provides functions that take a pointer directly to a buffer without the need for an IOV (input/output vector). In this case, the number of parts is replaced by the size of the message directly pointed to. In the case of the message send primitive which takes a send and a reply buffer this introduces four variations: Function MsgSend() MsgSendsv() MsgSendvs() MsgSendv() Send message Simple Simple IOV IOV Reply message Simple IOV Simple IOV
The other messaging primitives that take a direct message simply drop the trailing v in their names:
60
May 8, 2009
May 8, 2009
61
Server Channel
Connections
A process acting as a server would implement an event loop to receive and process messages as follows:
chid = ChannelCreate(flags); SETIOV(&iov, &msg, sizeof(msg)); for(;;) { rcv_id = MsgReceivev( chid, &iov, parts, &info ); switch( msg.type ) { /* Perform message processing here */ } MsgReplyv( rcv_id, &iov, rparts ); }
This loop allows the thread to receive messages from any thread that had a connection to the channel. The server can also use name_attach() to create a channel and associate a name with it. The sender process can then use name_open() to locate that name and create a connection to it. The channel has several lists of messages associated with it: Receive Send Reply A LIFO queue of threads waiting for messages. A priority FIFO queue of threads that have sent messages that havent yet been received. An unordered list of threads that have sent messages that have been received, but not yet replied to.
62
May 8, 2009
While in any of these lists, the waiting thread is blocked (i.e. RECEIVE-, SEND-, or REPLY-blocked). Multiple threads and multiple clients may wait on one channel.
Pulses
In addition to the synchronous Send/Receive/Reply services, the OS also supports xed-size, nonblocking messages. These are referred to as pulses and carry a small payload (four bytes of data plus a single byte code). Pulses pack a relatively small payload eight bits of code and 32 bits of data. Pulses are often used as a notication mechanism within interrupt handlers. They also allow servers to signal clients without blocking on them.
Code Value 8 bits 32 bits
May 8, 2009
63
The change happens before the server receives the message to avoid another case of priority inversion. If the servers priority remains unchanged at 10, and another thread, T3, starts to run at priority 11, the server has to wait until T3 lets it have some CPU time so that it can eventually receive T1s message. So, T1 would would be delayed by a lower-priority thread, T3. You can turn off priority inheritance by specifying the _NTO_CHF_FIXED_PRIORITY ag when you call ChannelCreate(). If youre using adaptive partitioning, this ag also causes the receiving threads not to run in the sending threads partitions.
Message-passing API
The message-passing API consists of the following functions: Function MsgSend() MsgReceive() MsgReceivePulse() MsgReply() MsgError() MsgRead() MsgWrite() MsgInfo() MsgSendPulse() MsgDeliverEvent() MsgKeyData() Description Send a message and block until reply. Wait for a message. Wait for a tiny, nonblocking message (pulse). Reply to a message. Reply only with an error status. No message bytes are transferred. Read additional data from a received message. Write additional data to a reply message. Obtain info on a received message. Send a tiny, nonblocking message (pulse). Deliver an event to a client. Key a message to allow security checks.
For information about messages from the programming point of view, see the Message Passing chapter of Getting Started with QNX Neutrino.
64
May 8, 2009
by relying on a window explicitly opened and shut, during which signals will be tolerated. With a synchronous, nonqueued system architecture built around Send/Receive/Reply, robust application architectures can be very readily implemented and delivered. Avoiding deadlock situations is another difcult problem when constructing applications from various combinations of queued IPC, shared memory, and miscellaneous synchronization primitives. For example, suppose thread A doesnt release mutex 1 until thread B releases mutex 2. Unfortunately, if thread B is in the state of not releasing mutex 2 until thread A releases mutex 1, a standoff results. Simulation tools are often invoked in order to ensure that deadlock wont occur as the system runs. The Send/Receive/Reply IPC primitives allow the construction of deadlock-free systems with the observation of only these simple rules: 1 2 Never have two threads send to each other. Always arrange your threads in a hierarchy, with sends going up the tree.
The rst rule is an obvious avoidance of the standoff situation, but the second rule requires further explanation. The team of cooperating threads and processes is arranged as follows:
A MsgSend()
B MsgSend()
C MsgSend()
Here the threads at any given level in the hierarchy never send to each other, but send only upwards instead. One example of this might be a client application that sends to a database server process, which in turn sends to a lesystem process. Since the sending threads block and wait for the target thread to reply, and since the target thread isnt send-blocked on the sending thread, deadlock cant happen. But how does a higher-level thread notify a lower-level thread that it has the results of a previously requested operation? (Assume the lower-level thread didnt want to wait for the replied results when it last sent.)
May 8, 2009
65
Events
QNX Neutrino provides a very exible architecture with the MsgDeliverEvent() kernel call to deliver nonblocking events. All of the common asynchronous services can be implemented with this. For example, the server-side of the select() call is an API that an application can use to allow a thread to wait for an I/O event to complete on a set of le descriptors. In addition to an asynchronous notication mechanism being needed as a back channel for notications from higher-level threads to lower-level threads, we can also build a reliable notication system for timers, hardware interrupts, and other event sources around this.
A MsgSendPulse() or MsgDeliverEvent() B
MsgSend()
A related issue is the problem of how a higher-level thread can request work of a lower-level thread without sending to it, risking deadlock. The lower-level thread is present only to serve as a worker thread for the higher-level thread, doing work on request. The lower-level thread would send in order to report for work, but the higher-level thread wouldnt reply then. It would defer the reply until the higher-level thread had work to be done, and it would reply (which is a nonblocking operation) with the data describing the work. In effect, the reply is being used to initiate work, not the send, which neatly side-steps rule #1.
Events
A signicant advance in the kernel design for QNX Neutrino is the event-handling subsystem. POSIX and its realtime extensions dene a number of asynchronous notication methods (e.g. UNIX signals that dont queue or pass data, POSIX realtime signals that may queue and pass data, etc.). The kernel also denes additional, QNX-specic notication techniques such as pulses. Implementing all of these event mechanisms could have consumed signicant code space, so our implementation strategy was to build all of these notication methods over a single, rich, event subsystem. A benet of this approach is that capabilities exclusive to one notication technique can become available to others. For example, an application can apply the same queueing services of POSIX realtime signals to UNIX signals. This can simplify the robust implementation of signal handlers within applications. The events encountered by an executing thread can come from any of three sources: a MsgDeliverEvent() kernel call invoked by a thread an interrupt handler
66
May 8, 2009
Events
the expiry of a timer The event itself can be any of a number of different types: QNX Neutrino pulses, interrupts, various forms of signals, and forced unblock events. Unblock is a means by which a thread can be released from a deliberately blocked state without any explicit event actually being delivered. Given this multiplicity of event types, and applications needing the ability to request whichever asynchronous notication technique best suits their needs, it would be awkward to require that server processes (the higher-level threads from the previous section) carry code to support all these options. Instead, the client thread can give a data structure, or cookie, to the server to hang on to until later. When the server needs to notify the client thread, it will invoke MsgDeliverEvent() and the microkernel will set the event type encoded within the cookie upon the client thread.
Server sigevent Client
MsgSend() MsgReply()
MsgDeliverEvent()
I/O notication
The ionotify() function is a means by which a client thread can request asynchronous event delivery. Many of the POSIX asynchronous services (e.g. mq_notify() and the client-side of the select()) are built on top of it. When performing I/O on a le descriptor (fd), the thread may choose to wait for an I/O event to complete (for the write() case), or for data to arrive (for the read() case). Rather than have the thread block on the resource manager process thats servicing the read/write request, ionotify() can allow the client thread to post an event to the resource manager that the client thread would like to receive when the indicated I/O condition occurs. Waiting in this manner allows the thread to continue executing and responding to event sources other than just the single I/O request. The select() call is implemented using I/O notication and allows a thread to block and wait for a mix of I/O events on multiple fds while continuing to respond to other forms of IPC. Here are the conditions upon which the requested event can be delivered:
_NOTIFY_COND_OUTPUT theres room in the output buffer for more data.
May 8, 2009
67
Signals
available to read.
_NOTIFY_COND_OBAND resource-manager-dened out of band data is
available.
Signals
The OS supports the 32 standard POSIX signals (as in UNIX) as well as the POSIX realtime signals, both numbered from a kernel-implemented set of 64 signals with uniform functionality. While the POSIX standard denes realtime signals as differing from UNIX-style signals (in that they may contain four bytes of data and a byte code and may be queued for delivery), this functionality can be explicitly selected or deselected on a per-signal basis, allowing this converged implementation to still comply with the standard. Incidentally, the UNIX-style signals can select POSIX realtime signal queuing, if the application wants it. QNX Neutrino also extends the signal-delivery mechanisms of POSIX by allowing signals to be targeted at specic threads, rather than simply at the process containing the threads. Since signals are an asynchronous event, theyre also implemented with the event-delivery mechanisms. Microkernel call SignalKill() SignalAction() SignalProcmask() SignalSuspend() SignalWaitinfo() POSIX call kill(), pthread_kill(), raise(), sigqueue() sigaction() sigprocmask(), pthread_sigmask() sigsuspend(), pause() sigwaitinfo() Description Set a signal on a process group, process, or thread. Dene action to take on receipt of a signal. Change signal blocked mask of a thread. Block until a signal invokes a signal handler. Wait for signal and return info on it.
The original POSIX specication dened signal operation on processes only. In a multithreaded process, the following rules are followed: The signal actions are maintained at the process level. If a thread ignores or catches a signal, it affects all threads within the process. The signal mask is maintained at the thread level. If a thread blocks a signal, it affects only that thread. An unignored signal targeted at a thread will be delivered to that thread alone. An unignored signal targeted at a process is delivered to the rst thread that doesnt have the signal blocked. If all threads have the signal blocked, the signal will be
68
May 8, 2009
Signals
queued on the process until any thread ignores or unblocks the signal. If ignored, the signal on the process will be removed. If unblocked, the signal will be moved from the process to the thread that unblocked it. When a signal is targeted at a process with a large number of threads, the thread table must be scanned, looking for a thread with the signal unblocked. Standard practice for most multithreaded processes is to mask the signal in all threads but one, which is dedicated to handling them. To increase the efciency of process-signal delivery, the kernel will cache the last thread that accepted a signal and will always attempt to deliver the signal to it rst.
Process Signal ignore Signal queue 64...1
64
Thread 64...1 33 47
Signals queued to this thread. Thread Signal blocked Signal queue Signals delivered to this thread. 64...1 33
Signal delivery.
The POSIX standard includes the concept of queued realtime signals. QNX Neutrino supports optional queuing of any signal, not just realtime signals. The queuing can be specied on a signal-by-signal basis within a process. Each signal can have an associated 8-bit code and a 32-bit value. This is very similar to message pulses described earlier. The kernel takes advantage of this similarity and uses common code for managing both signals and pulses. The signal number is mapped to a pulse priority using _SIGMAX signo. As a result, signals are delivered in priority order with lower signal numbers having higher priority. This conforms with the POSIX standard, which states that existing signals have priority over the new realtime signals.
May 8, 2009
69
Signals
Special signals
As mentioned earlier, the OS denes a total of 64 signals. Their range is as follows: Signal range 1 ... 57 41 ... 56 57 ... 64 Description 57 POSIX signals (including traditional UNIX signals) 16 POSIX realtime signals (SIGRTMIN to SIGRTMAX) Eight special-purpose QNX Neutrino signals
The eight special signals cannot be ignored or caught. An attempt to call the signal() or sigaction() functions or the SignalAction() kernel call to change them will fail with an error of EINVAL. In addition, these signals are always blocked and have signal queuing enabled. An attempt to unblock these signals via the sigprocmask() function or SignalProcmask() kernel call will be quietly ignored. A regular signal can be programmed to this behavior using the following standard signal calls. The special signals save the programmer from writing this code and protect the signal from accidental changes to this behavior.
sigset_t *set; struct sigaction action; sigemptyset(&set); sigaddset(&set, signo); sigprocmask(SIG_BLOCK, &set, NULL); action.sa_handler = SIG_DFL; action.sa_flags = SA_SIGINFO; sigaction(signo, &action, NULL);
This conguration makes these signals suitable for synchronous notication using the sigwaitinfo() function or SignalWaitinfo() kernel call. The following code will block until the eighth special signal is received:
sigset_t *set; siginfo_t info; sigemptyset(&set); sigaddset(&set, SIGRTMAX + 8); sigwaitinfo(&set, &info); printf("Received signal %d with code %d and value %d\n", info.si_signo, info.si_code, info.si_value.sival_int);
Since the signals are always blocked, the program cannot be interrupted or killed if the special signal is delivered outside of the sigwaitinfo() function. Since signal queuing is always enabled, signals wont be lost theyll be queued for the next sigwaitinfo() call.
70
May 8, 2009
Signals
These signals were designed to solve a common IPC requirement where a server wishes to notify a client that it has information available for the client. The server will use the MsgDeliverEvent() call to notify the client. There are two reasonable choices for the event within the notication: pulses or signals. A pulse is the preferred method for a client that may also be a server to other clients. In this case, the client will have created a channel for receiving messages and can also receive the pulse. This wont be true for most simple clients. In order to receive a pulse, a simple client would be forced to create a channel for this express purpose. A signal can be used in place of a pulse if the signal is congured to be synchronous (i.e. the signal is blocked) and queued this is exactly how the special signals are congured. The client would replace the MsgReceive() call used to wait for a pulse on a channel with a simple sigwaitinfo() call to wait for the signal. This signal mechanism is used by Photon to wait for events and by the select() function to wait for I/O from multiple servers. Of the eight special signals, the rst two have been given special names for this use.
#define SIGSELECT #define SIGPHOTON (SIGRTMAX + 1) (SIGRTMAX + 2)
Summary of signals
Signal
SIGABRT SIGALRM SIGBUS
Description Abnormal termination signal such as issued by the abort() function. Timeout signal such as issued by the alarm() function. Indicates a memory parity error (QNX-specic interpretation). Note that if a second fault occurs while your process is in a signal handler for this fault, the process will be terminated. Child process terminated. The default action is to ignore the signal. Continue if HELD. The default action is to ignore the signal if the process isnt HELD. Mutex deadlock occurred. If you havent called SyncMutexEvent(), and if the conditions that would cause the kernel to deliver the event occur, then the kernel delivers a SIGDEADLK instead. EMT instruction (emulator trap). Erroneous arithmetic operation (integer or oating point), such as division by zero or an operation resulting in overow. Note that if a second fault occurs while your process is in a signal handler for this fault, the process will be terminated.
SIGEMT SIGFPE
continued. . .
May 8, 2009
71
Signal
SIGHUP SIGILL
Description Death of session leader, or hangup detected on controlling terminal. Detection of an invalid hardware instruction. Note that if a second fault occurs while your process is in a signal handler for this fault, the process will be terminated. Interactive attention signal (Break). IOT instruction (not generated on x86 hardware). Termination signal should be used only for emergency situations. This signal cannot be caught or ignored. Attempt to write on a pipe with no readers. Pollable event occurred. Interactive termination signal. Detection of an invalid memory reference. Note that if a second fault occurs while your process is in a signal handler for this fault, the process will be terminated. Stop process (the default). This signal cannot be caught or ignored. Bad argument to system call. Termination signal. Unsupported software interrupt. Stop signal generated from keyboard. Background read attempted from control terminal. Background write attempted to control terminal. Urgent condition present on socket. Reserved as application-dened signal 1. Reserved as application-dened signal 2. Window size changed.
SIGSTOP SIGSYS SIGTERM SIGTRAP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGUSR1 SIGUSR2 SIGWINCH
72
May 8, 2009
To use POSIX message queues in QNX Neutrino, the message queue server must be running. QNX Neutrino has two implementations of message queues: a traditional implementation that uses the mqueue resource manager (see the Resource Managers chapter in this book) an alternate implementation that uses the mq server and asynchronous messages For more information about these implementations, see the Utilities Reference. Unlike our inherent message-passing primitives, the POSIX message queues reside outside the kernel.
File-like interface
Message queues resemble les, at least as far as their interface is concerned. You open a message queue with mq_open(), close it with mq_close(), and destroy it with mq_unlink(). And to put data into (write) and take it out of (read) a message queue, you use mq_send() and mq_receive(). For strict POSIX conformance, you should create message queues that start with a single slash (/) and contain no other slashes. But note that we extend the POSIX standard by supporting pathnames that may contain multiple slashes. This allows, for example, a company to place all its message queues under its company name and distribute a product with increased condence that a queue name will not conict with that of another company. In QNX Neutrino, all message queues created will appear in the lename space under the directory: /dev/mqueue if youre using the traditional (mqueue) implementation /dev/mq if youre using the alternate (mq) implementation
May 8, 2009
73
Shared memory
You can display all message queues in the system using the ls command as follows:
ls -Rl /dev/mqueue
Message-queue functions
POSIX message queues are managed via the following functions: Function mq_open() mq_close() mq_unlink() mq_send() mq_receive() mq_notify() mq_setattr() mq_getattr() Description Open a message queue. Close a message queue. Remove a message queue. Add a message to the message queue. Receive a message from the message queue. Tell the calling process that a message is available on a message queue. Set message queue attributes. Get message queue attributes.
Shared memory
Shared memory offers the highest bandwidth IPC available. Once a shared-memory object is created, processes with access to the object can use pointers to directly read and write into it. This means that access to shared memory is in itself unsynchronized. If a process is updating an area of shared memory, care must be taken to prevent another process from reading or updating the same area. Even in the simple case of a read, the other process may get information that is in ux and inconsistent. To solve these problems, shared memory is often used in conjunction with one of the synchronization primitives to make updates atomic between processes. If the granularity of updates is small, then the synchronization primitives themselves will
74
May 8, 2009
Shared memory
limit the inherently high bandwidth of using shared memory. Shared memory is therefore most efcient when used for updating large amounts of data as a block. Both semaphores and mutexes are suitable synchronization primitives for use with shared memory. Semaphores were introduced with the POSIX realtime standard for interprocess synchronization. Mutexes were introduced with the POSIX threads standard for thread synchronization. Mutexes may also be used between threads in different processes. POSIX considers this an optional capability; we support it. In general, mutexes are more efcient than semaphores.
May 8, 2009
75
Shared memory
Simple shared memory cant be used between processes on different computers connected via a network. Message passing, on the other hand, is network transparent. A server could use shared memory for local clients and full message passing of the data for remote clients. This allows you to provide a high-performance server that is also network transparent. In practice, the message-passing primitives are more than fast enough for the majority of IPC needs. The added complexity of a combined approach need only be considered for special applications with very high bandwidth.
POSIX shared memory is implemented in QNX Neutrino via the process manager (procnto). The above calls are implemented as messages to procnto (see the Process Manager chapter in this book). The shm_open() function takes the same arguments as open() and returns a le descriptor to the object. As with a regular le, this function lets you create a new shared-memory object or open an existing shared-memory object.
76
May 8, 2009
Shared memory
You must open the le descriptor for reading; if you want to write in the memory object, you also need write access, unless you specify a private (MAP_PRIVATE) mapping. When a new shared-memory object is created, the size of the object is set to zero. To set the size, you use ftruncate() the very same function used to set the size of a le or shm_ctl().
mmap()
Once you have a le descriptor to a shared-memory object, you use the mmap() function to map the object, or part of it, into your processs address space. The mmap() function is the cornerstone of memory management within QNX Neutrino and deserves a detailed discussion of its capabilities. You can also use mmap() to map les and typed memory objects into your processs address space. The mmap() function is dened as follows:
void * mmap( void *where_i_want_it, size_t length, int memory_protections, int mapping_ags, int fd, off_t offset_within_shared_memory );
In simple terms this says: Map in length bytes of shared memory at offset_within_shared_memory in the shared-memory object associated with fd. The mmap() function will try to place the memory at the address where_i_want_it in your address space. The memory will be given the protections specied by memory_protections and the mapping will be done according to the mapping_ags. The three arguments fd, offset_within_shared_memory, and length dene a portion of a particular shared object to be mapped in. Its common to map in an entire shared object, in which case the offset will be zero and the length will be the size of the shared object in bytes. On an Intel processor, the length will be a multiple of the page size, which is 4096 bytes.
May 8, 2009
77
Shared memory
Process address space mmap ( addr, len, prot, flags, fd, offset );
addr len
The return value of mmap() will be the address in your processs address space where the object was mapped. The argument where_i_want_it is used as a hint by the system to where you want the object placed. If possible, the object will be placed at the address requested. Most applications specify an address of zero, which gives the system free reign to place the object where it wishes. The following protection types may be specied for memory_protections: Manifest
PROT_EXEC PROT_NOCACHE PROT_NONE PROT_READ PROT_WRITE
Description Memory may be executed. Memory should not be cached. No access allowed. Memory may be read. Memory may be written.
You should use the PROT_NOCACHE manifest when youre using a shared-memory region to gain access to dual-ported memory that may be modied by hardware (e.g. a video frame buffer or a memory-mapped network or communications board). Without this manifest, the processor may return stale data from a previously cached read. The mapping_ags determine how the memory is mapped. These ags are broken down into two parts the rst part is a type and must be specied as one of the following:
78
May 8, 2009
Shared memory
Map type
MAP_SHARED MAP_PRIVATE
Description The mapping may be shared by many processes; changes are propagated back to the underlying object. The mapping is private to the calling process; changes arent propagated back to the underlying object. The mmap() function allocates system RAM and makes a copy of the object.
The MAP_SHARED type is the one to use for setting up shared memory between processes; MAP_PRIVATE has more specialized uses. You can OR a number of ags into the above type to further dene the mapping. These are described in detail in the mmap() entry in the Library Reference. A few of the more interesting ags are:
MAP_ANON
Map anonymous memory that isnt associated with any le descriptor; you must set the fd parameter to NOFD. The mmap() function allocates the memory, and by default, lls the allocated memory with zeros; see Initializing allocated memory, below. You commonly use MAP_ANON with MAP_PRIVATE, but you can use it with MAP_SHARED to create a shared memory area for forked applications. You can use MAP_ANON as the basis for a page-level memory allocator.
MAP_FIXED
Map the object to the address specied by where_i_want_it. If a shared-memory region contains pointers within it, then you may need to force the region at the same address in all processes that map it. This can be avoided by using offsets within the region in place of direct pointers. This ag indicates that you wish to deal with physical memory. The fd parameter should be set to NOFD. When used without MAP_ANON, the offset_within_shared_memory species the exact physical address to map (e.g. for video frame buffers). If used with MAP_ANON, then physically contiguous memory is allocated (e.g. for a DMA buffer). You can use MAP_NOX64K and MAP_BELOW16M to further dene the MAP_ANON allocated memory and address limitations present in some forms of DMA.
MAP_PHYS
You should use mmap_device_memory() instead of MAP_PHYS, unless youre allocating physically contiguous memory.
MAP_NOX64K
Used with MAP_PHYS | MAP_ANON. The allocated memory area will not cross a 64-KB boundary. This is required for the old 16-bit PC DMA.
May 8, 2009
79
Shared memory
MAP_BELOW16M
Used with MAP_PHYS | MAP_ANON. The allocated memory area will reside in physical memory below 16 MB. This is necessary when using DMA with ISA bus devices. Relax the POSIX requirement to zero the allocated memory; see Initializing allocated memory, below.
MAP_NOINIT
Using the mapping ags described above, a process can easily share memory between processes:
/* Map in a shared memory region */ fd = shm_open("datapoints", O_RDWR); addr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
You can unmap all or part of a shared-memory object from your address space using munmap(). This primitive isnt restricted to unmapping shared memory it can be used to unmap any region of memory within your process. When used in conjunction with the MAP_ANON ag to mmap(), you can easily implement a private page-level allocator/deallocator. You can change the protections on a mapped region of memory using mprotect(). Like munmap(), mprotect() isnt restricted to shared-memory regions it can change the protection on any region of memory within your process.
Pass one of the following for the ags argument: 0 Behave like munmap().
80
May 8, 2009
Typed memory
UNMAP_INIT_REQUIRED
POSIX initialization of the page to all zeroes is required the next time the underlying physical memory is allocated.
UNMAP_INIT_OPTIONAL
Initialization of the underlying physical memory to zeroes on its next allocation is optional. If you specify the MAP_NOINIT ag to mmap(), and the physical memory being mapped was previously unmapped with UNMAP_INIT_OPTIONAL, the POSIX requirement that the memory be zeroed is relaxed. By default, the kernel initializes the memory, but you can control this by using the -m option to procnto. The argument to this option is a string that lets you enable or disable aspects of the memory manager:
i i
munmap() acts as if UNMAP_INIT_REQUIRED were specied. munmap() acts as if UNMAP_INIT_OPTIONAL were specied.
Note again that munmap_ags() with a ags argument of 0 behaves the same as munmap() does.
Typed memory
Typed memory is POSIX functionality dened in the 1003.1 specication. Its part of the advanced realtime extensions, and the manifests are located in the <sys/mman.h> header le. Typed memory adds the following functions to the C library: posix_typed_mem_open() Open a typed memory object. This function returns a le descriptor, which you can then pass to mmap() to establish a memory mapping of the typed memory object. posix_typed_mem_get_info() Get information (currently the amount of available memory) about a typed memory object. POSIX typed memory provides an interface to open memory objects (which are dened in an OS-specic fashion) and perform mapping operations on them. Its useful in providing an abstraction between BSP- or board-specic address layouts and device drivers or user code.
May 8, 2009
81
Typed memory
Implementation-dened behavior
POSIX species that typed memory pools (or objects) are created and dened in an implementation-specic fashion. This section describes the following for Neutrino: Seeding of typed memory regions Naming of typed memory regions Pathname space and typed memory mmap() allocation ags and typed memory objects Permissions and typed memory objects Object length and offset denitions Interaction with other POSIX APIs
Physical addressability of the processor, typically 4 GB on a 32-bit CPU (more with physical addressing extensions). All of the RAM on the system. This may consist of multiple entries. System RAM, i.e. memory that has been given to the OS to manage. This may also consist of multiple entries.
ram sysram
Since by convention sysram is the memory that has been given to the OS, this pool is the same as that used by the OS to satisfy anonymous mmap() and malloc() requests. You can create additional entries, but only in startup, using the as_add() function.
82
May 8, 2009
Typed memory
Name
/memory /memory/ram /memory/ram/sysram /memory/isa/ram/dma /memory/ram/dma
Range (start, end) 0, 0xFFFFFFFF 0, 0x1FFFFFF 0x1000, 0x1FFFFFF 0x1000, 0xFFFFFF 0x1000, 0x1FFFFFF
The name you pass to posix_typed_mem_open() follows the above naming convention. POSIX allows an implementation to dene what happens when the name doesnt start with a leading slash (/). The resolution rules on opening are as follows: 1 2 If the name starts with a leading /, an exact match is done. The name may contain intermediate / characters. These are considered as path component separators. If multiple path components are specied, theyre matched from the bottom up (the opposite of the way lenames are resolved). If the name doesnt start with a leading /, a tail match is done on the pathname components specied.
Here are some examples of how posix_typed_mem_open() resolves names, using the above sample conguration: This name:
/memory /memory/ram /sysram sysram
Resolves to:
/memory /memory/ram
Fails
/memory/ram/sysram
Rule 3
May 8, 2009
83
Typed memory
The memory is allocated and not available for other allocations, but if you fork the process, the child processes can access it as well. The memory is released when the last mapping to it is removed. Note that like somebody doing mem_offset() and then a MAP_PHYS to gain access to previously allocated memory, somebody else could open the typed memory object with POSIX_TYPED_MEMORY_ALLOCATABLE (or with no ags) and gain access to the same physical memory that way.
POSIX_TYPED_MEM_ALLOC_CONTIG is like MAP_ANON | MAP_SHARED, in
that it causes a contiguous allocation. The POSIX_TYPED_MEM_ALLOCATABLE case, which is used to create a mapping to an object without allocation or deallocation. This is equivalent to a shared mapping to physical memory. You should use only MAP_SHARED mappings, since a write to a MAP_PRIVATE mapping will (as normal) create a private copy for the process in normal anonymous memory. If you specify no ag, or you specify POSIX_TYPED_MEM_MAP_ALLOCATABLE, the offset parameter to mmap() species the starting physical address in the typed memory region; if the typed memory region is discontiguous (multiple asinfo entries), the allowed offset values are also discontiguous and dont start at zero as they do for shared memory objects. If you specify a [paddr, paddr + size) region that falls outside the allowed addresses for the typed memory object, mmap() fails with ENXIO.
84
May 8, 2009
Typed memory
The POSIX setrlimit() APIs provide the ability to set limits on the virtual and physical memory that a process can consume. Since typed memory operations may operate on normal RAM (sysram) and will create mappings in the processs address space, they need to be taken into account when doing the rlimit accounting. In particular, the following rules apply: Any mapping created by mmap() for typed memory objects is counted in the processs RLIMIT_VMEM or RLIMIT_AS limit. Typed memory never counts against RLIMIT_DATA.
POSIX le-descriptor functions You can use the le descriptor that posix_typed_memory_open() returns with selected POSIX fd-based calls, as follows: fstat(fd,..), which lls in the stat structure as it does for a shared memory object, except that the size eld doesnt hold the size of the typed memory object. close(fd) closes the le descriptor. dup() and dup2() duplicate the le handle. posix_mem_offset() behaves as documented in the POSIX specication.
Practical examples
May 8, 2009
85
Typed memory
where phys_addr is the physical address of the SRAM, size is the SRAM size, and mem_id is the ID of the parent (typically memory, which is returned by as_default()). This code creates an asinfo entry for packet_memory, which you can then use as POSIX typed memory. The following code allows different applications to allocate pages from packet_memory:
int fd = posix_typed_mem_open( "packet_memory", O_RDWR, POSIX_TYPED_MEM_ALLOCATE); unsigned vaddr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alternatively, you may want to use the packet memory as direct shared, physical buffers. In this case, applications would use it as follows:
int fd = posix_typed_mem_open( "packet_memory", O_RDWR, POSIX_TYPED_MEM_ALLOCATABLE); unsigned vaddr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
86
May 8, 2009
where dma_addr is the start of the DMA-safe RAM, and size is the size of the DMA-safe region. This code creates an asinfo entry for dma, which is a child of ram. Drivers can then use it to allocate DMA-safe buffers:
int fd = posix_typed_mem_open( "ram/dma", O_RDWR, POSIX_TYPED_MEM_ALLOCATE_CONTIG); unsigned vaddr = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Pipes
A pipe is an unnamed le that serves as an I/O channel between two or more cooperating processesone process writes into the pipe, the other reads from the pipe. The pipe manager takes care of buffering the data. The buffer size is dened as PIPE_BUF in the <limits.h> le. A pipe is removed once both of its ends have closed. The function pathconf() returns the value of the limit. Pipes are normally used when two processes want to run in parallel, with data moving from one process to the other in a single direction. (If bidirectional communication is required, messages should be used instead.) A typical application for a pipe is connecting the output of one program to the input of another program. This connection is often made by the shell. For example:
ls | more
directs the standard output from the ls utility through a pipe to the standard input of the more utility. If you want to: Create pipes from within the shell Create pipes from within programs Use the: pipe symbol (|) pipe() or popen() functions
FIFOs
FIFOs are essentially the same as pipes, except that FIFOs are named permanent les that are stored in lesystem directories.
May 8, 2009
87
If you want to: Create FIFOs from within the shell Create FIFOs from within programs Remove FIFOs from within the shell Remove FIFOs from within programs
Use the:
mkfifo utility
mkfo() function
rm utility
88
May 8, 2009
May 8, 2009
89
Introduction
Introduction
An instrumented version of the QNX Neutrino microkernel (procnto-instr) is equipped with a sophisticated tracing and proling mechanism that lets you monitor your systems execution in real time. The procnto-instr module works on both single-CPU and SMP systems. The procnto-instr module uses very little overhead and gives exceptionally good performance its typically about 98% as fast as the noninstrumented kernel (when it isnt logging). The additional amount of code (about 30 KB on an x86 system) in the instrumented kernel is a relatively small price to pay for the added power and exibility of this useful tool. Depending on the footprint requirements of your nal system, you may choose to use this special kernel as a development/prototyping tool or as the actual kernel in your nal product. The instrumented module is nonintrusive you dont have to modify a programs source code in order to monitor how that program interacts with the kernel. You can trace as many or as few interactions (e.g. kernel calls, state changes, and other system activities) as you want between the kernel and any running thread or process in your system. You can even monitor interrupts. In this context, all such activities are known as events. For more details, see the System Analysis Toolkit Users Guide.
Instrumentation at a glance
Here are the essential tasks involved in kernel instrumentation: 1 The instrumented microkernel (procnto-instr) emits trace events as a result of various system activities. These events are automatically copied to a set of buffers grouped into a circular linked list. As soon as the number of events inside a buffer reaches the high-water mark, the kernel noties a data-capture utility. The data-capture utility then writes the trace events from the buffer to an output device (e.g. a serial port, an event le, etc.). A data-interpretation facility then interprets the events and presents this data to the user.
2 3 4
May 8, 2009
91
Event control
Instrumentation at a glance.
Event control
Given the large number of activities occurring in a live system, the number of events that the kernel emits can be overwhelming (in terms of the amount of data, the processing requirements, and the resources needed to store it). But you can easily control the amount of data emitted. Specically, you can: control the initial conditions that trigger event emissions apply predened kernel lters to dynamically control emissions implement your own event handlers for even more ltering. Once the data has been collected by the data-capture utility (tracelogger), it can then be analyzed. You can analyze the data in real time or ofine after the relevant events have been gathered. The System Analysis tool within the IDE presents this data graphically so you can see whats going on in your system.
Modes of emission
Apart from applying the various lters to control the event stream, you can also specify one of two modes the kernel can use to emit events: fast mode wide mode Emits only the most pertinent information (e.g. only two kernel call arguments) about an event. Generates more information (e.g. all kernel call arguments) for the same event.
The trade-off here is one of speed vs knowledge: fast mode delivers less data, while wide mode packs much more information for each event. Either way, you can easily tune your system, because these modes work on a per-event basis.
92
May 8, 2009
Data interpretation
As an example of the difference between the fast and wide emission modes, lets look at the kinds of information we might see for a MsgSendv() call entry: Fast mode data Connection ID Message data Number of bytes for the event 4 bytes 4 bytes (the rst 4 bytes usually comprise the header) Total emitted: 8 bytes
Wide mode data Connection ID # of parts to send # of parts to receive Message data Message data Message data
Number of bytes for the event 4 bytes 4 bytes 4 bytes 4 bytes (the rst 4 bytes usually comprise the header) 4 bytes 4 bytes Total emitted: 24 bytes
Ring buffer
Rather than always emit events to an external device, the kernel can keep all of the trace events in an internal circular buffer. This buffer can be programmatically dumped to an external device on demand when a certain triggering condition is met, making this a very powerful tool for identifying elusive bugs that crop up under certain runtime conditions.
Data interpretation
The data of an event includes a high-precision timestamp as well as the ID number of the CPU on which the event was generated. This information helps you easily diagnose difcult timing problems, which are more likely to occur on multiprocessor systems. The event format also includes the CPU platform (e.g. x86, PowerPC, etc.) and endian type, which facilitates remote analysis (whether in real time or ofine). Using a data interpreter, you can view the data output in various ways, such as: a timestamp-based linear presentation of the entire system a running view of only the active threads/processes a state-based view of events per process/thread.
May 8, 2009
93
Data interpretation
The linear output from the data interpreter might look something like this:
TRACEPRINTER version 0.94 -- HEADER FILE INFORMATION -TRACE_FILE_NAME:: /dev/shmem/tracebuffer TRACE_DATE:: Fri Jun 8 13:14:40 2001 TRACE_VER_MAJOR:: 0 TRACE_VER_MINOR:: 96 TRACE_LITTLE_ENDIAN:: TRUE TRACE_ENCODING:: 16 byte events TRACE_BOOT_DATE:: Fri Jun 8 04:31:05 2001 TRACE_CYCLES_PER_SEC:: 400181900 TRACE_CPU_NUM:: 4 TRACE_SYSNAME:: QNX TRACE_NODENAME:: x86quad.gp.qa TRACE_SYS_RELEASE:: 6.1.0 TRACE_SYS_VERSION:: 2001/06/04-14:07:56 TRACE_MACHINE:: x86pc TRACE_SYSPAGE_LEN:: 2440 -- KERNEL EVENTS -t:0x1310da15 CPU:01 CONTROL :TIME msb:0x0000000f, lsb(offset):0x1310d81c t:0x1310e89d CPU:01 PROCESS :PROCCREATE_NAME ppid:0 pid:1 name:./procnto-smp-instr t:0x1310eee4 CPU:00 THREAD :THCREATE pid:1 tid:1 t:0x1310f052 CPU:00 THREAD :THRUNNING pid:1 tid:1 t:0x1310f144 CPU:01 THREAD :THCREATE pid:1 tid:2 t:0x1310f201 CPU:01 THREAD :THREADY pid:1 tid:2 t:0x1310f32f CPU:02 THREAD :THCREATE pid:1 tid:3 t:0x1310f3ec CPU:02 THREAD :THREADY pid:1 tid:3 t:0x1310f52d CPU:03 THREAD :THCREATE pid:1 tid:4 t:0x1310f5ea CPU:03 THREAD :THRUNNING pid:1 tid:4 t:0x1310f731 CPU:02 THREAD :THCREATE pid:1 tid:5 . . .
To help you ne-tune your interpretation of the event data stream, we provide a library (traceparser) so you can write your own custom event interpreters.
94
May 8, 2009
Proactive tracing
Proactive tracing
While the instrumented kernel provides an excellent unobtrusive method for instrumenting and monitoring processes, threads, and the state of your system in general, you can also have your applications proactively inuence the event-collection process. Using the TraceEvent() library call, applications themselves can inject custom events into the trace stream. This facility is especially useful when building large, tightly coupled, multicomponent systems. For example, the following simple call would inject the integer values of eventcode, rst, and second into the event stream:
TraceEvent(_NTO_TRACE_INSERTSUSEREVENT, eventcode, first, second);
You can also inject a string (e.g. My Event) into the event stream, as shown in the following code:
#include <stdio.h>
May 8, 2009
95
Proactive tracing
#include <sys/trace.h> /* Code to associate with emitted events */ #define MYEVENTCODE 12 int main(int argc, char **argv) { printf("My pid is %d \n", getpid()); /* Inject two integer events (26, 1975) */ TraceEvent(_NTO_TRACE_INSERTSUSEREVENT, MYEVENTCODE, 26, 1975); /* Inject a string event (My Event) */ TraceEvent(_NTO_TRACE_INSERTUSRSTREVENT, MYEVENTCODE, "My Event"); return 0; }
The output, as gathered by the traceprinter data interpreter, would then look something like this:
. . . t:0x38ea737e CPU:00 USREVENT:EVENT:12, d0:26 d1:1975 . . . t:0x38ea7cb0 CPU:00 USREVENT:EVENT:12 STR:"My Event"
Note that 12 was specied as the trace user eventcode for these events.
96
May 8, 2009
May 8, 2009
97
Introduction
Introduction
Two heads are better than one goes the old saying, and the same is true for computer systems, where twoor moreprocessors can greatly improve performance. Multiprocessing systems can be in these forms: Discrete or traditional A system that has separate physical processors hooked up in multiprocessing mode over a board-level bus. Multicore A chip that has one physical processor with multiple CPUs interconnected over a chip-level bus. Multicore processors deliver greater computing power through concurrency, offer greater system density, and run at lower clock speeds than uniprocessor chips. Multicore processors also reduce thermal dissipation, power consumption, and board area (and hence the cost of the system). Multiprocessing includes several operating modes: Asymmetric multiprocessing (AMP) A separate OS, or a separate instantiation of the same OS, runs on each CPU. Symmetric multiprocessing (SMP) A single instantiation of an OS manages all CPUs simultaneously, and applications can oat to any of them. Bound multiprocessing (BMP) A single instantiation of an OS manages all CPUs simultaneously, but each application is locked to a specic CPU.
To determine how many processors there are on your system, look at the num_cpu entry of the system page. For more information, see Structure of the system page in the Customizing Image Startup Programs chapter of Building Embedded Systems.
May 8, 2009
99
heterogeneous each CPU runs either a different OS or a different version of the same OS Neutrinos distributed programming model lets you make the best use of the multiple CPUs in a homogeneous environment. Applications running on one CPU can communicate transparently with applications and system services (e.g. device drivers, protocol stacks) on other CPUs, without the high CPU utilization imposed by traditional forms of interprocessor communication. In heterogeneous systems, you must either implement a proprietary communications scheme or choose two OSs that share a common infrastructure (likely IP based) for interprocessor communications. To help avoid resource conicts, the OSs should also provide standardized mechanisms for accessing shared hardware components. With AMP, you decide how the shared hardware resources used by applications are divided up between the CPUs. Normally, this resource allocation occurs statically during boot time and includes physical memory allocation, peripheral usage, and interrupt handling. While the system could allocate the resources dynamically, doing so would entail complex coordination between the CPUs. In an AMP system, a process always runs on the same CPU, even when other CPUs run idle. As a result, one CPU can end up being under- or overutilized. To address the problem, the system could allow applications to migrate dynamically from CPU to another. Doing so, however, can involve complex checkpointing of state information or a possible service interruption as the application is stopped on one CPU and restarted on another. Also, such migration is difcult, if not impossible, if the CPUs run different OSs.
100
May 8, 2009
messaging, and other events, all with high-resolution timestamping. Application synchronization also becomes much easier since you use standard OS primitives rather than complex IPC mechanisms. Neutrino lets the threads of execution within an application run concurrently on any CPU, making the entire computing power of the chip available to applications at all times. Neutrinos preemption and thread prioritization capabilities help you ensure that CPU cycles go to the application that needs them the most.
May 8, 2009
101
hardware. The PPC version supports any SMP system with 7xx or 74xx series processors, as in such reference design platforms as the Motorola MVP or the Marvell EV-64260-2XMPC7450 SMP Development System. The MIPS version supports such systems as the dual-core Broadcom BCM1250 processor.
102
May 8, 2009
initialize the caches set the processor spinning with interrupts disabled, waiting to be released by the kernel
Scheduling
The scheduling algorithm follows the same rules as on a uniprocessor system. That is, the highest-priority thread will be running on the available processor. If a new thread becomes ready to run as the highest-priority thread in the system, it will be dispatched to the appropriate processor. If more than one processor is selected as a potential target, then the microkernel will try to dispatch the thread to the processor where it last ran. This afnity is used as an attempt to reduce thread migration from one processor to another, which can affect cache performance. In an SMP system, the scheduler has some exibility in deciding exactly how to schedule low-priority threads, with an eye towards optimizing cache usage and minimizing thread migration. In any case, the realtime scheduling rules that were in place on a uniprocessor system are guaranteed to be upheld on an SMP system.
Kernel locking
In a uniprocessor system, only one thread is allowed to execute within the microkernel at a time. Most kernel operations are short in duration (typically a few microseconds on a Pentium-class processor). The microkernel is also designed to be completely preemptible and restartable for those operations that take more time. This design keeps the microkernel lean and fast without the need for large numbers of ne-grained locks. It is interesting to note that placing many locks in the main code path through a kernel will noticeably slow the kernel down. Each lock typically involves processor bus transactions, which can cause processor stalls. In an SMP system, QNX Neutrino maintains this philosophy of only one thread in a preemptible and restartable kernel. The microkernel may be entered on any processor, but only one processor will be granted access at a time. For most systems, the time spent in the microkernel represents only a small fraction of the processors workload. Therefore, while conicts will occur, they should be more the exception than the norm. This is especially true for a microkernel where traditional OS services like lesystems are separate processes and not part of the kernel itself.
May 8, 2009
103
a thread running on another processor is hit with a signal a thread running on another processor is canceled a thread running on another processor is destroyed
Critical sections
To control access to data structures that are shared between them, threads and processes use the standard POSIX primitives of mutexes, condvars, and semaphores. These work without change in an SMP system. Many realtime systems also need to protect access to shared data structures between an interrupt handler and the thread that owns the handler. The traditional POSIX primitives used between threads arent available for use by an interrupt handler. There are two solutions here: One is to remove all work from the interrupt handler and do all the work at thread time instead. Given our fast thread scheduling, this is a very viable solution. In a uniprocessor system running QNX Neutrino, an interrupt handler may preempt a thread, but a thread will never preempt an interrupt handler. This allows the thread to protect itself from the interrupt handler by disabling and enabling interrupts for very brief periods of time. The thread on a non-SMP system protects itself with code of the form:
InterruptDisable() // critical section InterruptEnable()
Or:
InterruptMask(intr) // critical section InterruptUnmask(intr)
Unfortunately, this code will fail on an SMP system since the thread may be running on one processor while the interrupt handler is concurrently running on another processor! One solution would be to lock the thread to a particular processor (see Bound Multiprocessing (BMP), later in this chapter). A better solution would be to use a new exclusion lock available to both the thread and the interrupt handler. This is provided by the following primitives, which work on both uniprocessor and SMP machines:
InterruptLock(intrspin_t* spinlock)
Attempt to acquire a spinlock, a variable shared between the interrupt handler and thread. The code will spin in a tight loop until the lock is acquired. After disabling interrupts, the code will acquire the lock (if it was acquired by a thread). The lock must be released as soon as possible (typically within a few lines of C code without any loops).
104
May 8, 2009
InterruptUnlock(intrspin_t* spinlock)
Release a lock and reenable interrupts. On a non-SMP system, theres no need for a spinlock. For more information, see the Multicore Processing Users Guide.
May 8, 2009
105
You can specify the runmask for a new thread or process by: setting the runmask member of the inheritance structure and specifying the SPAWN_EXPLICIT_CPU ag when you call spawn() Or: using the -C or -R option to the on utility when you launch a program. This also sets the processs inherit mask to the same value. You can change the runmask for an existing thread or process by: using the _NTO_TCTL_RUNMASK or
_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT command to the ThreadCtl()
kernel call Or: using the -C or -R option to the slay utility. If you also use the -i option, slay sets the inherit mask to the same value. For more information, see the Multicore Processing Users Guide.
106
May 8, 2009
As the following table illustrates, the exibility to choose from any of these models lets you strike the optimal balance between performance, scalability, and ease of migration. Feature Seamless resource sharing Scalable beyond dual CPU Legacy application operation Mixed OS environment (e.g. Neutrino and Linux) Dedicated processor by function Intercore messaging Thread synchronization between CPUs Load balancing System-wide debugging and optimization SMP Yes Yes In most cases BMP Yes Yes Yes AMP Limited Yes Yes
May 8, 2009
107
May 8, 2009
109
Introduction
Introduction
In QNX Neutrino, the microkernel is paired with the Process Manager in a single module (procnto). This module is required for all runtime systems. The process manager is capable of creating multiple POSIX processes (each of which may contain multiple POSIX threads). Its main areas of responsibility include: process management manages process creation, destruction, and process attributes such as user ID (uid) and group ID (gid). memory management manages a range of memory-protection capabilities, shared libraries, and interprocess POSIX shared-memory primitives. pathname management manages the pathname space into which resource managers may attach. User processes can access microkernel functions directly via kernel calls and process manager functions by sending messages to procnto. Note that a user process sends a message by invoking the MsgSend*() kernel call. Its important to note that threads executing within procnto invoke the microkernel in exactly the same way as threads in other processes. The fact that the process manager code and the microkernel share the same process address space doesnt imply a special or private interface. All threads in the system share the same consistent kernel interface and all perform a privilege switch when invoking the microkernel.
Process management
The rst responsibility of procnto is to dynamically create new processes. These processes will then depend on procntos other responsibilities of memory management and pathname management. Process management consists of both process creation and destruction as well as the management of process attributes such as process IDs, process groups, user IDs, etc.
Process primitives
The process primitives include: posix_spawn() spawn() fork() vfork() exec*() POSIX QNX Neutrino POSIX UNIX BSD extension POSIX
May 8, 2009
111
Process management
posix_spawn()
The posix_spawn() function creates a child process by directly specifying an executable to load. To those familiar with UNIX systems, the call is modeled after a fork() followed by an exec*(). However, it operates much more efciently in that theres no need to duplicate address spaces as in a fork(), only to destroy and replace it when the exec*() is called. In a UNIX system, one of the main advantages of using the fork()-then-exec*() method of creating a child process is the exibility in changing the default environment inherited by the new child process. This is done in the forked child just before the exec*(). For example, the following simple shell command would close and reopen the standard output before exec*()ing:
ls >file
You can do the same with posix_spawn(); it gives you control over the following classes of environment inheritance, which are often adjusted when creating a new child process: le descriptors process user and group IDs signal mask ignored signals adaptive partitioning (scheduler ) attributes Theres also a companion function, posix_spawnp(), that doesnt require the absolute path to the program to spawn, but instead searches for the executable using the callers PATH. Using the posix_spawn() functions is the preferred way to create a new child process.
spawn()
The QNX Neutrino spawn() function is similar to posix_spawn(). The spawn() function gives you control over the following: le descriptors process group ID signal mask ignored signals the node to create the process on scheduling policy scheduling parameters (priority)
112
May 8, 2009
Process management
maximum stack size runmask (for SMP systems) The basic forms of the spawn() function are: spawn() spawnp() Spawn with the explicitly specied path. Search the current PATH and invoke spawn() with the rst matching executable.
Theres also a set of convenience functions that are built on top of spawn() and spawnp() as follows: spawnl() spawnle() spawnlp() spawnlpe() spawnv() spawnve() spawnvp() spawnvpe() Spawn with the command line provided as inline arguments. spawnl() with explicitly passed environment variables. spawnp() that follows the command search path. spawnlp() with explicitly passed environment variables. Spawn with the command line pointed to by an array of pointers. spawnv() with explicitly passed environment variables. spawnv() that follows the command search path. spawnvp() with explicitly passed environment variables.
When a process is spawn()ed, the child process inherits the following attributes of its parent: process group ID (unless SPAWN_SETGROUP is set in inherit.ags) session membership real user ID and real group ID supplementary group IDs priority and scheduling policy current working directory and root directory le creation mask signal mask (unless SPAWN_SETSIGMASK is set in inherit.ags) signal actions specied as SIG_DFL signal actions specied as SIG_IGN (except the ones modied by inherit.sigdefault when SPAWN_SETSIGDEF is set in inherit.ags)
May 8, 2009
113
Process management
The child process has several differences from the parent process: Signals set to be caught by the parent process are set to the default action (SIG_DFL). The child processs tms_utime, tms_stime, tms_cutime, and tms_cstime are tracked separately from the parents. The number of seconds left until a SIGALRM signal would be generated is set to zero for the child process. The set of pending signals for the child process is empty. File locks set by the parent arent inherited. Per-process timers created by the parent arent inherited. Memory locks and mappings set by the parent arent inherited. If the child process is spawned on a remote node, the process group ID and the session membership arent set; the child process is put into a new session and a new process group. The child process can access the parent processs environment by using the environ global variable (found in <unistd.h>). For more information, see the spawn() function in the QNX Neutrino Library Reference.
fork()
The fork() function creates a new child process by sharing the same code as the calling process and duplicating the calling processs data to give the child process an exact copy. Most process resources are inherited. The following lists some resources that are explicitly not inherited: process ID parent process ID le locks pending signals and alarms timers The fork() function is typically used for one of two reasons: to create a new instance of the current execution environment to create a new process running a different program
114
May 8, 2009
Process management
When creating a new thread, common data is placed in an explicitly created shared memory region. Prior to the POSIX thread standard, this was the only way to accomplish this. With POSIX threads, this use of fork() is better accomplished by creating threads within a single process using pthread_create(). When creating a new process running a different program, the call to fork() is soon followed by a call to one of the exec*() functions. This too is better accomplished by a single call to the posix_spawn() function or the QNX Neutrino spawn() function, which combine both operations with far greater efciency. Since QNX Neutrino provides better POSIX solutions than using fork(), its use is probably best suited for porting existing code and for writing portable code that must run on a UNIX system that doesnt support the POSIX pthread_create() or posix_spawn() API. Note that fork() should be called from a process containing only a single thread.
vfork()
The vfork() function (which should also be called only from a single-threaded process) is useful when the purpose of fork() would have been to create a new system context for a call to one of the exec*() functions. The vfork() function differs from fork() in that the child doesnt get a copy of the calling processs data. Instead, it borrows the calling processs memory and thread of control until a call to one of the exec*() functions is made. The calling process is suspended while the child is using its resources. The vfork() child cant return from the procedure that called vfork(), since the eventual return from the parent vfork() would then return to a stack frame that no longer existed.
exec*()
The exec*() family of functions replaces the current process with a new process, loaded from an executable le. Since the calling process is replaced, there can be no successful return. The following exec*() functions are dened: execl() execle() execlp() execlpe() execv() execve() execvp() Exec with the command line provided as inline arguments. execl() with explicitly passed environment variables. execl() that follows the command search path. execlp()with explicitly passed environment variables. execl() with the command line pointed to by an array of pointers. execv() with explicitly passed environment variables. execv() that follows the command search path.
May 8, 2009
115
Memory management
execvpe()
The exec*() functions usually follow a fork() or vfork() in order to load a new child process. This is better achieved by using the posix_spawn() call.
Process loading
Processes loaded from a lesystem using the exec*(), posix_spawn() or spawn() calls are in ELF format. If the lesystem is on a block-oriented device, the code and data are loaded into main memory. If the lesystem is memory mapped (e.g. ROM/ash image), the code neednt be loaded into RAM, but may be executed in place. This approach makes all RAM available for data and stack, leaving the code in ROM or ash. In all cases, if the same process is loaded more than once, its code will be shared.
Memory management
While some realtime kernels or executives provide support for memory protection in the development environment, few provide protected memory support for the runtime conguration, citing penalties in memory and performance as reasons. But with memory protection becoming common on many embedded processors, the benets of memory protection far outweigh the very small penalties in performance for enabling it. The key advantage gained by adding memory protection to embedded applications, especially for mission-critical systems, is improved robustness. With memory protection, if one of the processes executing in a multitasking environment attempts to access memory that hasnt been explicitly declared or allocated for the type of access attempted, the MMU hardware can notify the OS, which can then abort the thread (at the failing/offending instruction). This protects process address spaces from each other, preventing coding errors in a thread in one process from damaging memory used by threads in other processes or even in the OS. This protection is useful both for development and for the installed runtime system, because it makes postmortem analysis possible. During development, common coding errors (e.g. stray pointers and indexing beyond array bounds) can result in one process/thread accidentally overwriting the data space of another process. If the overwriting touches memory that isnt referenced again until much later, you can spend hours of debugging often using in-circuit emulators and logic analyzers in an attempt to nd the guilty party. With an MMU enabled, the OS can abort the process the instant the memory-access violation occurs, providing immediate feedback to the programmer instead of mysteriously crashing the system some time later. The OS can then provide the location of the errant instruction in the failed process, or position a symbolic debugger directly on this instruction.
116
May 8, 2009
Memory management
0 1023 0
For a large address space with many processes and threads, the number of page-table entries needed to describe these mappings can be signicant more than can be stored within the processor. To maintain performance, the processor caches frequently used portions of the external page tables within a TLB (translation look-aside buffer). The servicing of misses on the TLB cache is part of the overhead imposed by enabling the MMU. Our OS uses various clever page-table arrangements to minimize this overhead. Associated with these page tables are bits that dene the attributes of each page of memory. Pages can be marked as read-only, read-write, etc. Typically, the memory of an executing process would be described with read-only pages for code, and read-write pages for the data and stack. When the OS performs a context switch (i.e. suspends the execution of one thread and resumes another), it will manipulate the MMU to use a potentially different set of page
May 8, 2009
117
Memory management
tables for the newly resumed thread. If the OS is switching between threads within a single process, no MMU manipulations are necessary. When the new thread resumes execution, any addresses generated as the thread runs are mapped to physical memory through the assigned page tables. If the thread tries to use an address not mapped to it, or it tries to use an address in a way that violates the dened attributes (e.g. writing to a read-only page), the CPU will receive a fault (similar to a divide-by-zero error), typically implemented as a special type of interrupt. By examining the instruction pointer pushed on the stack by the interrupt, the OS can determine the address of the instruction that caused the memory-access fault within the thread/process and can act accordingly.
Software watchdog
When an intermittent software error occurs in a memory-protected system, the OS can catch the event and pass control to a user-written thread instead of the memory dump facilities. This thread can make an intelligent decision about how best to recover from the failure, instead of forcing a full reset as the hardware watchdog timer would do. The software watchdog could: Abort the process that failed due to a memory access violation and simply restart that process without shutting down the rest of the system. Abort the failed process and any related processes, initialize the hardware to a safe state, and then restart the related processes in a coordinated manner. If the failure is very critical, perform a coordinated shutdown of the entire system and sound an audible alarm. The important distinction here is that we retain intelligent, programmed control of the embedded system, even though various processes and threads within the control software may have failed for various reasons. A hardware watchdog timer is still of use to recover from hardware latch-ups, but for software failures we now have much better control.
118
May 8, 2009
Memory management
While performing some variation of these recovery strategies, the system can also collect information about the nature of the software failure. For example, if the embedded system contains or has access to some mass storage (ash memory, hard drive, a network link to another computer with disk storage), the software watchdog can generate a chronologically archived sequence of dump les. These dump les could then be used for postmortem diagnostics. Embedded control systems often employ these partial restart approaches to surviving intermittent software failures without the operators experiencing any system downtime or even being aware of these quick-recovery software failures. Since the dump les are available, the developers of the software can detect and correct software problems without having to deal with the emergencies that result when critical systems fail at inconvenient times. If we compare this to the hardware watchdog timer approach and the prolonged interruptions in service that result, its obvious what our preference is! Postmortem dump-le analysis is especially important for mission-critical embedded systems. Whenever a critical system fails in the eld, signicant effort should be made to identify the cause of the failure so that a x can be engineered and applied to other systems before they experience similar failures. Dump les give programmers the information they need to x the problem without them, programmers may have little more to go on than a customers cryptic complaint that the system crashed.
Quality control
By dividing embedded software into a team of cooperating, memory-protected processes (containing threads), we can readily treat these processes as components to be used again in new projects. Because of the explicitly dened (and hardware-enforced) interfaces, these processes can be integrated into applications with condence that they wont disrupt the systems overall reliability. In addition, because the exact binary image (not just the source code) of the process is being reused, we can better control changes and instabilities that might have resulted from recompilation of source code, relinking, new versions of development tools, header les, library routines, etc. Since the binary image of the process is reused (with its behavior perhaps modied by command-line options), the condence we have in that binary module from acquired experience in the eld more easily carries over to new applications than if the binary image of the process were changed. As much as we strive to produce error-free code for the systems we deploy, the reality of software-intensive embedded systems is that programming errors will end up in released products. Rather than pretend these bugs dont exist (until the customer calls to report them), we should adopt a mission-critical mindset. Systems should be designed to be tolerant of, and able to recover from, software faults. Making use of the memory protection delivered by integrated MMUs in the embedded systems we build is a good step in that direction.
May 8, 2009
119
Memory management
Full-protection model
Our full-protection model relocates all code in the image into a new virtual space, enabling the MMU hardware and setting up the initial page-table mappings. This allows procnto to start in a correct, MMU-enabled environment. The process manager will then take over this environment, changing the mapping tables as needed by the processes it starts.
procnto
3.5G 0
3.5G 0
3.5G
3.5G
4G
The memory cost per process may increase by 4 KB to 8 KB for each processs page tables. Note that this memory model supports the POSIX fork() call.
120
May 8, 2009
Memory management
May 8, 2009
121
Memory management
If free memory is fragmented, it prevents an application from allocating contiguous memory, which in turn might lead to complete failure of the application. To defragment free memory, the memory manager swaps memory thats in use for memory thats free, in such a way that the free memory blocks coalesce into larger blocks that are sufcient to satisfy a request for contiguous memory. When an application allocates memory, its provided by the operating system in quantums, 4-KB blocks of memory that exist on 4-KB boundaries. The operating system programs the MMU so that the application can reference the physical block of memory through a virtual address; during operation, the MMU translates a virtual address into a physical address. For example, a request for 16 KB of memory is satised by allocating four 4-KB quantums. The operating system sets aside the four physical blocks for the application and congures the MMU to ensure that the application can reference them through a 16-KB contiguous virtual address. However, these blocks might not be physically contiguous; the operating system can arrange the MMU conguration (the virtual to physical mapping) so that non-contiguous physical addresses are accessed through contiguous virtual addresses. The task of defragmentation consists of changing existing memory allocations and mappings to use different underlying physical pages. By swapping around the underlying physical quantums, the OS can consolidate the fragmented free blocks into contiguous runs. However, its careful to avoid moving certain types of memory where the virtual-to-physical mapping cant safely be changed: Memory allocated by the kernel and addressed through the one-to-one mapping area cant be moved, because the one-to-one mapping area denes the mapping of virtual to physical addresses, and the OS cant change the physical address without also changing the virtual address. Memory thats locked by the application (see mlock()) cant be moved: by locking the memory, the application is indicating that moving the memory isnt acceptable. An application that runs with I/O privileges (see the _NTO_TCTL_IO ag for ThreadCtl()) has all pages locked by default, because device drivers often require physical addresses. Pages of memory that have mutex objects on them arent currently moved. While its possible to move these pages, mutex objects are registered with the kernel through their physical addresses, so moving a page with a mutex on it would require rehashing the mutex object in the kernel. There are other times when memory cant be moved; see Automatically marking memory as unmovable, below. Defragmentation is done, if necessary, when an application allocates a piece of contiguous memory. The application does this through the mmap() call, providing MAP_PHYS | MAP_ANON ags. If it isnt possible to satisfy a MAP_PHYS allocation
122
May 8, 2009
Memory management
with contiguous memory, what happens depends on whether defragmentation is disabled or enabled: If its disabled, mmap() fails. If its enabled, the memory manager runs a memory-defragmentation algorithm that attempts to rearrange memory mappings across the system in order to allow the MAP_PHYS allocation to be satised. During the memory defragmentation, the thread calling mmap() is blocked. Compaction can take a signicant amount of time (particularly on systems with large amounts of memory), but other system activities are mostly unaffected. Since other system tasks are running simultaneously, the defragmentation algorithm takes into account that memory mappings can change while the algorithm is running. Defragmenting is enabled by default. You can disable it by using the procnto command-line option -md, and enable it by using the -md option.
May 8, 2009
123
Pathname management
Pathname management
Domains of authority
I/O resources are not built into the microkernel, but are instead provided by resource manager processes that may be started dynamically at runtime. The procnto manager allows resource managers, through a standard API, to adopt a subset of the pathname space as a domain of authority to administer. As other resource managers adopt their respective domains of authority, procnto becomes responsible for maintaining a pathname tree to track the processes that own portions of the pathname space. An adopted pathname is sometimes referred to as a prex because it prexes any pathnames that lie beneath it; prexes can be arranged in a hierarchy called a prex tree. The adopted pathname is also called a mountpoint, because thats where a server mounts into the pathname. This approach to pathname space management is what allows QNX Neutrino to preserve the POSIX semantics for device and le access, while making the presence of those services optional for small embedded systems. At startup, procnto populates the pathname space with the following pathname prexes: Prex
/ /proc/boot/ /proc/pid
Description Root of the lesystem. Some of the les from the boot image presented as a at lesystem. The running processes, each represented by its process ID (PID). For more information, see Controlling processes via the /proc lesystem in the Processes chapter of the QNX Neutrino Programmers Guide. A device that always returns zero. Used for allocating zero-lled pages using the mmap() function. A device that represents all physical memory.
/dev/zero /dev/mem
Resolving pathnames
When a process opens a le, the POSIX-compliant open() library routine rst sends the pathname to procnto, where the pathname is compared against the prex tree to determine which resource managers should be sent the open() message. The prex tree may contain identical or partially overlapping regions of authority multiple servers can register the same prex. If the regions are identical, the order of resolution can be specied (see Ordering mountpoints, below). If the regions are overlapping, the responses from the path manager are ordered with the longest prexes rst; for prexes of equal length, the same specied order of resolution applies as for identical regions.
124
May 8, 2009
Pathname management
Description QNX 4 disk-based lesystem (fs-qnx4.so) Serial device manager (devc-ser*) Serial device manager (devc-ser*) Raw disk volume (devb-eide)
The lesystem manager has registered a prex for a mounted QNX 4 lesystem (i.e.
/). The block device driver has registered a prex for a block special le that represents an entire physical hard drive (i.e. /dev/hd0). The serial device manager
has registered two prexes for the two PC serial ports. The following table illustrates the longest-match rule for pathname resolution: This pathname:
/dev/ser1 /dev/ser2 /dev/ser /dev/hd0 /usr/jhsmith/test
matches:
/dev/ser1 /dev/ser2 / /dev/hd0 /
Ordering mountpoints
Generally the order of resolving a lename is the order in which you mounted the lesystems at the same mountpoint (i.e. new mounts go on top of or in front of any existing ones). You can specify the order of resolution when you mount the lesystem. For example, you can use: the before and after keywords for block I/O (devb-*) drivers, in the blk options the -Z b and -Z a options to fs-cifs, fs-nfs2, and fs-nfs3 You can also use the -o option to mount with these keywords:
before
Mount the lesystem so that its resolved before any other lesystems mounted at the same pathname (in other words, its placed in front of any existing mount). When you access a le, the system looks on this lesystem rst. Mount the lesystem so that its resolved after any other lesystems mounted at the same pathname (in other words, its placed behind any
after
May 8, 2009
125
Pathname management
existing mounts). When you access a le, the system looks on this lesystem last, and only if the le wasnt found on any other lesystems. If you specify the appropriate before option, the lesystem oats in front of any other lesystems mounted at the same mountpoint, except those that you later mount with before. If you specify after, the lesystem goes behind any any other lesystems mounted at the same mountpoint, except those that are already mounted with after. So, the search order for these lesystems is: 1 2 3 those mounted with before those mounted with no ags those mounted with after
with each list searched in order of mount requests. The rst server to claim the name gets it. You would typically use after to have a lesystem wait at the back and pick up things the no one else is handling, and before to make sure a lesystems looks rst at lenames.
Single-device mountpoints
Consider an example involving three servers: Server A Server B Server C A QNX 4 lesystem. Its mountpoint is /. It contains the les bin/true and bin/false. A ash lesystem. Its mountpoint is /bin. It contains the les ls and
echo.
At this point, the process managers internal mount table would look like this: Mountpoint
/ /bin /dev/random
Of course, each Server name is actually an abbreviation for the nd,pid,chid for that particular server channel. Now suppose a client wants to send a message to Server C. The clients code might look like this:
int fd; fd = open("/dev/random", ...);
126
May 8, 2009
Pathname management
In this case, the C library will ask the process manager for the servers that could potentially handle the path /dev/random. The process manager would return a list of servers: Server C (most likely; longest path match) Server A (least likely; shortest path match) From this information, the library will then contact each server in turn and send it an open message, including the component of the path that the server should validate: 1 2 Server C receives a null path, since the request came in on the same path as the mountpoint. Server A receives the path dev/random, since its mountpoint was /.
As soon as one server positively acknowledges the request, the library wont contact the remaining servers. This means Server A is contacted only if Server C denies the request. This process is fairly straightforward with single device entries, where the rst server is generally the server that will handle the request. Where it becomes interesting is in the case of unioned lesystem mountpoints.
Note that each server has a /bin directory, but with different contents. Once both servers are mounted, you would see the following due to the unioning of the mountpoints:
/ /bin /bin/echo /bin/false /bin/ls /bin/true
May 8, 2009
127
Pathname management
Whats happening here is that the resolution for the path /bin takes place as before, but rather than limit the return to just one connection ID, all the servers are contacted and asked about their handling for the path:
DIR *dirp; dirp = opendir("/bin", ...); closedir(dirp);
which results in: 1 2 Server B receives a null path, since the request came in on the same path as the mountpoint. Server A receives the path "bin", since its mountpoint was "/".
The result now is that we have a collection of le descriptors to servers who handle the path /bin (in this case two servers); the actual directory name entries are read in turn when a readdir() is called. If any of the names in the directory are accessed with a regular open, then the normal resolution procedure takes place and only one server is accessed.
Symbolic prexes
Weve discussed prexes that map to a resource manager. A second form of prex, known as a symbolic prex, is a simple string substitution for a matched prex. You create symbolic prexes using the POSIX ln (link) command. This command is typically used to create hard or symbolic links on a lesystem by using the -s option. If you also specify the -P option, then a symbolic link is created in the in-memory prex space of procnto. Command
ln -s existing_le symbolic_link ln -Ps existing_le symbolic_link
Description Create a lesystem symbolic link. Create a prex tree symbolic link.
Note that a prex tree symbolic link will always take precedence over a lesystem symbolic link. For example, assume youre running on a machine that doesnt have a local lesystem. However, theres a lesystem on another node (say neutron) that you wish to access as /bin. You accomplish this using the following symbolic prex:
ln -Ps /net/neutron/bin /bin
128
May 8, 2009
Pathname management
This will cause /bin to be mapped into /net/neutron/bin. For example, /bin/ls will be replaced with the following:
/net/neutron/bin/ls
This new pathname will again be applied against the prex tree, but this time the prex matched will be /net, which will point to lsm-qnet. The lsm-qnet resource manager will then resolve the neutron component, and redirect further resolution requests to the node called neutron. On node neutron, the rest of the pathname (i.e. /bin/ls) will be resolved against the prex space on that node. This will resolve to the lesystem manager on node neutron, where the open() request will be directed. With just a few characters, this symbolic prex has allowed us to access a remote lesystem as though it were local. Its not necessary to run a local lesystem process to perform the redirection. A diskless workstations prex tree might look something like this:
/ lsm-qnet.so dev
console
ser2 ser1
devc-con
devc-ser...
With this prex tree, local devices such as /dev/ser1 or /dev/console will be routed to the local character device manager, while requests for other pathnames will be routed to the remote lesystem.
Any request to open /dev/modem will be replaced with /dev/ser1. This mapping would allow the modem to be changed to a different serial port simply by changing the symbolic prex and without affecting any applications.
May 8, 2009
129
Pathname management
Relative pathnames
Pathnames need not start with slash. In such cases, the path is considered relative to the current working directory. The OS maintains the current working directory as a character string. Relative pathnames are always converted to full network pathnames by prepending the current working directory string to the relative pathname. Note that different behaviors result when your current working directory starts with a slash versus starting with a network root.
A note about cd
In some traditional UNIX systems, the cd (change directory) command modies the pathname given to it if that pathname contains symbolic links. As a result, the pathname of the new current working directory which you can display with pwd may differ from the one given to cd. In QNX Neutrino, however, cd doesnt modify the pathname aside from collapsing .. references. For example:
cd /usr/home/dan/test/../doc
would result in a current working directory of /usr/home/dan/doc, even if some of the elements in the pathname were symbolic links. For more information about symbolic links and .. references, see QNX 4 lesystem in the Working with Filesystems chapter of the QNX Neutrino Users Guide.
130
May 8, 2009
Pathname management
May 8, 2009
131
Pathname management
File descriptors 0
Server process
Open control blocks
Process A 1
File descriptors 0
/tmp/file
Process B
FDs are a process resource, not a thread resource. Several le descriptors in one or more processes can refer to the same OCB. This is accomplished by two means: A process may use the dup(), dup2(), or fcntl() functions to create a duplicate le descriptor that refers to the same OCB. When a new process is created via vfork(), fork(), posix_spawn(), or spawn(), all open le descriptors are by default inherited by the new process; these inherited descriptors refer to the same OCBs as the corresponding le descriptors in the parent process. When several FDs refer to the same OCB, then any change in the state of the OCB is immediately seen by all processes that have le descriptors linked to the same OCB. For example, if one process uses the lseek() function to change the position of the seek point, then reading or writing takes place from the new position no matter which linked le descriptor is used. The following diagram shows two processes in which one opens a le twice, then does a dup() to get a third FD. The process then creates a child that inherits all open les.
132
May 8, 2009
Pathname management
File descriptors 0
Server process
Open control blocks
Parent process
1 2
File descriptors 0
/tmp/file
Child process
1 2
You can prevent a le descriptor from being inherited when you posix_spawn(), spawn(), or exec*() by calling the fcntl() function and setting the FD_CLOEXEC ag.
May 8, 2009
133
May 8, 2009
135
Shared objects
Shared objects
In a typical system, a number of programs will be running. Each program relies on a number of functions, some of which will be standard C library functions, like printf(), malloc(), write(), etc. If every program uses the standard C library, it follows that each program would normally have a unique copy of this particular library present within it. Unfortunately, this results in wasted resources. Since the C library is common, it makes more sense to have each program reference the common instance of that library, instead of having each program contain a copy of the library. This approach yields several advantages, not the least of which is the savings in terms of total system memory required.
Statically linked
The term statically linked means that the program and the particular library that its linked against are combined together by the linker at linktime. This means that the binding between the program and the particular library is xed and known at linktime well in advance of the program ever running. It also means that we cant change this binding, unless we relink the program with a new version of the library. You might consider linking a program statically in cases where you werent sure whether the correct version of a library will be available at runtime, or if you were testing a new version of a library that you dont yet want to install as shared. Programs that are linked statically are linked against archives of objects (libraries) that typically have the extension of .a. An example of such a collection of objects is the standard C library, libc.a.
Dynamically linked
The term dynamically linked means that the program and the particular library it references are not combined together by the linker at linktime. Instead, the linker places information into the executable that tells the loader which shared object module the code is in and which runtime linker should be used to nd and bind the references. This means that the binding between the program and the shared object is done at runtime before the program starts, the appropriate shared objects are found and bound. This type of program is called a partially bound executable, because it isnt fully resolved the linker, at linktime, didnt cause all the referenced symbols in the program to be associated with specic code from the library. Instead, the linker simply said: This program calls some functions within a particular shared object, so Ill just make a note of which shared object these functions are in, and continue on. Effectively, this defers the binding until runtime. Programs that are linked dynamically are linked against shared objects that have the extension .so. An example of such an object is the shared object version of the standard C library, libc.so.
May 8, 2009
137
You use a command-line option to the compiler driver qcc to tell the tool chain whether youre linking statically or dynamically. This command-line option then determines the extension used (either .a or .so).
ELF format
QNX Neutrino uses the ELF (Executable and Linking Format) binary format, which is currently used in SVR4 Unix systems. ELF not only simplies the task of making shared libraries, but also enhances dynamic loading of modules at runtime. In the following diagram, we show two views of an ELF le: the linking view and the execution view. The linking view, which is used when the program or library is linked, deals with sections within an object le. Sections contain the bulk of the object le information: data, instructions, relocation information, symbols, debugging information, etc. The execution view, which is used when the program runs, deals with segments. At linktime, the program or library is built by merging together sections with similar attributes into segments. Typically, all the executable and read-only data sections are combined into a single text segment, while the data and BSSs are combined into the data segment. These segments are called load segments, because they need to be loaded in memory at process creation. Other sections such as symbol information and debugging sections are merged into other, nonload segments.
138
May 8, 2009
Linking view:
ELF header Program header table (optional) Section 1 ... Section n ... ... Section header table
Execution view:
ELF header Program header table Segment 1
The process
The diagram below shows the memory layout of a typical process. The process load segments (corresponding to text and data in the diagram) are loaded at the processs base address. The main stack is located just below and grows downwards. Any additional threads that are created will have their own stacks, located below the main stack. Each of the stacks is separated by a guard page to detect stack overows. The heap is located above the process and grows upwards.
May 8, 2009
139
Shared memory
Heap Data Text Process base address Stack Stack Guard page
In the middle of the processs address space, a large region is reserved for shared objects. Shared libraries are located at the top of the address space and grow downwards. When a new process is created, the process manager rst maps the two segments from the executable into memory. It then decodes the programs ELF header. If the program header indicates that the executable was linked against a shared library, the process manager will extract the name of the dynamic interpreter from the program header. The dynamic interpreter points to a shared library that contains the runtime linker code. The process manager will load this shared library in memory and will then pass control to the runtime linker code in this library.
Runtime linker
The runtime linker is invoked when a program that was linked against a shared object is started or when a program requests that a shared object be dynamically loaded. The runtime linker is contained within the C runtime library. The runtime linker performs several tasks when loading a shared library (.so le): 1 If the requested shared library isnt already loaded in memory, the runtime linker loads it:
140
May 8, 2009
If the shared library name is fully qualied (i.e. begins with a slash), its loaded directly from the specied location. If it cant be found there, no further searches are performed. If its not a fully qualied pathname, the runtime linker searches for it as follows: 1a If the executables dynamic section contains a DT_RPATH tag, then the path specied by DT_RPATH is searched. 1b If the shared library isnt found, the runtime linker searches for it in the directories specied by LD_LIBRARY_PATH only if the program isnt marked as setuid. 1c If the shared library still isnt found, then the runtime linker searches for the default library search path as specied by the LD_LIBRARY_PATH environment variable to procnto (i.e. the CS_LIBPATH conguration string). If none has been specied, then the default library path is set to the image lesystems path. 2 Once the requested shared library is found, its loaded into memory. For ELF shared libraries, this is a very efcient operation: the runtime linker simply needs to use the mmap() call twice to map the two load segments into memory. The shared library is then added to the internal list of all libraries that the process has loaded. The runtime linker maintains this list. The runtime linker then decodes the dynamic section of the shared object.
3 4
This dynamic section provides information to the linker about other libraries that this library was linked against. It also gives information about the relocations that need to be applied and the external symbols that need to be resolved. The runtime linker will rst load any other required shared libraries (which may themselves reference other shared libraries). It will then process the relocations for each library. Some of these relocations are local to the library, while others require the runtime linker to resolve a global symbol. In the latter case, the runtime linker will search through the list of libraries for this symbol. In ELF les, hash tables are used for the symbol lookup, so theyre very fast. The order in which libraries are searched for symbols is very important, as well see in the section on Symbol name resolution below. Once all relocations have been applied, any initialization functions that have been registered in the shared librarys init section are called. This is used in some implementations of C++ to call global constructors.
May 8, 2009
141
Remember: shared libraries are available only to processes that are dynamically linked. The program can also determine the symbol associated with a given address by using the dladdr() call. Finally, when the process no longer needs the shared library, it can call dlclose() to unload the library from memory.
3 4
The runtime linkers scoping behavior can be changed in two ways when dlopen()ing a shared library: When the program loads a new library, it may instruct the runtime linker to place the librarys symbols on the global list by passing the RTLD_GLOBAL ag to the dlopen() call. This will make the librarys symbols available to any libraries that are subsequently loaded. The list of objects that are searched when resolving the symbols within the shared library can be modied. If the RTLD_GROUP ag is passed to dlopen(), then only objects that the library directly references will be searched for symbols. If the RTLD_WORLD ag is passed, only the objects on the global list will be searched.
142
May 8, 2009
May 8, 2009
143
Introduction
Introduction
To give QNX Neutrino a great degree of exibility, to minimize the runtime memory requirements of the nal system, and to cope with the wide variety of devices that may be found in a custom embedded system, the OS allows user-written processes to act as resource managers that can be started and stopped dynamically. Resource managers are typically responsible for presenting an interface to various types of devices. This may involve managing actual hardware devices (like serial ports, parallel ports, network cards, and disk drives) or virtual devices (like /dev/null, a network lesystem, and pseudo-ttys). In other operating systems, this functionality is traditionally associated with device drivers. But unlike device drivers, resource managers dont require any special arrangements with the kernel. In fact, a resource manager looks just like any other user-level program.
May 8, 2009
145
The API for communicating with the resource manager is for the most part, POSIX. All C programmers are familiar with the open(), read(), and write() functions. Training costs are minimized, and so is the need to document the interface to your server. You can reduce the number of interface types. If you have many server processes, writing each server as a resource manager keeps the number of different interfaces that clients need to use to a minimum. For example, suppose you have a team of programmers building your overall application, and each programmer is writing one or more servers for that application. These programmers may work directly for your company, or they may belong to partner companies who are developing addon hardware for your modular platform. If the servers are resource managers, then the interface to all of those servers is the POSIX functions: open(), read(), write(), and whatever else makes sense. For control-type messages that dont t into a read/write model, theres devctl() (although devctl() isnt POSIX). Command-line utilities can communicate with resource managers. Since the API for communicating with a resource manager is the POSIX set of functions, and since standard POSIX utilities use this API, you can use the utilities for communicating with the resource managers. For instance, suppose a resource manager registers the name /proc/my_stats. If you open this name and read from it, the resource manager responds with a body of text that describes its statistics. The cat utility takes the name of a le and opens the le, reads from it, and displays whatever it reads to standard output (typically the screen). As a result, you could type:
cat /proc/my_stats
and the resource manager would respond with the appropriate statistics. You could also use command-line utilities for a robot-arm driver. The driver could register the name, /dev/robot/arm/angle, and interpret any writes to this device as the angle to set the robot arm to. To test the driver from the command line, youd type:
echo 87 >/dev/robot/arm/angle The echo utility opens /dev/robot/arm/angle and writes the string (87) to
it. The driver handles the write by setting the robot arm to 87 degrees. Note that this was accomplished without writing a special tester program. Another example would be names such as /dev/robot/registers/r1, r2,.... Reading from these names returns the contents of the corresponding registers; writing to these names sets the corresponding registers to the given values. Even if all of your other IPC is done via some non-POSIX API, its still worth having one thread written as a resource manager for responding to reads and writes for doing things as shown above.
146
May 8, 2009
Identies the mountpoint thats managed by the process manager. Identies the remaining part thats to be managed by the lesystem resource manager.
home/thomasf
Here are some examples of using lesystem resource managers: ash lesystem drivers (although the source code for ash drivers available on our Foundry27 community website takes care of these details) a tar lesystem process that presents the contents of a tar le as a lesystem that the user can cd into and ls from a mailbox-management process that registers the name /mailboxes and manages individual mailboxes that look like directories, and les that contain the actual messages.
May 8, 2009
147
the clients C library will construct an io_open message, which it then sends to the devc-ser* resource manager via IPC. Some time later, when the client program executes:
read (fd, buf, BUFSIZ);
the clients C library constructs an io_read message, which is then sent to the resource manager. A key point is that all communications between the client program and the resource manager are done through native IPC messaging. This allows for a number of unique features: A well-dened interface to application programs. In a development environment, this allows a very clean division of labor for the implementation of the client side and the resource manager side. A simple interface to the resource manager. Since all interactions with the resource manager go through native IPC, and there are no special back door hooks or arrangements with the OS, the writer of a resource manager can focus on the task at hand, rather than worry about all the special considerations needed in other operating systems. Free network transparency. Since the underlying native IPC messaging mechanism is inherently network-distributed without any additional effort required by the client or server (resource manager), programs can seamlessly access resources on other nodes in the network without even being aware that theyre going over a network. All QNX Neutrino device drivers and lesystems are implemented as resource managers. This means that everything that a native QNX Neutrino device driver or lesystem can do, a user-written resource manager can do as well. Consider FTP lesystems, for instance. Here a resource manager would take over a portion of the pathname space (e.g. /ftp) and allow users to cd into FTP sites to get les. For example, cd /ftp/rtfm.mit.edu/pub would connect to the FTP site rtfm.mit.edu and change directory to /pub. After that point, the user could open, edit, or copy les. Application-specic lesystems would be another example of a user-written resource manager. Given an application that makes extensive use of disk-based les, a custom tailored lesystem can be written that works with that application and delivers superior performance.
148
May 8, 2009
The possibilities for custom resource managers are limited only by the application developers imagination.
The architecture contains three parts: 1 2 A channel is created so that client programs can connect to the resource manager to send it messages. The pathname (or pathnames) that the resource manager is going to be responsible for is registered with the process manager, so that it can resolve open requests for that particular pathname to this resource manager. Messages are received and processed.
This message-processing structure (the switch/case, above) is required for each and every resource manager. However, we provide a set of convenient library functions to handle this functionality (and other key functionality as well).
Message types
Architecturally, there are two categories of messages that a resource manager will receive: connect messages I/O messages. A connect message is issued by the client to perform an operation based on a pathname (e.g. an io_open message). This may involve performing operations such
May 8, 2009
149
as permission checks (does the client have the correct permission to open this device?) and setting up a context for that request. An I/O message is one that relies upon this context (created between the client and the resource manager) to perform subsequent processing of I/O messages (e.g. io_read). There are good reasons for this design. It would be inefcient to pass the full pathname for each and every read() request, for example. The io_open handler can also perform tasks that we want done only once (e.g. permission checks), rather than with each I/O message. Also, when the read() has read 4096 bytes from a disk le, there may be another 20 megabytes still waiting to be read. Therefore, the read() function would need to have some context information telling it the position within the le its reading from.
150
May 8, 2009
The client would generate an io_open message for the rst open(), and then two io_dup messages for the two dup() calls. Then, when the client executed the close() calls, three io_close messages would be generated. Since the dup() functions generate duplicates of the le descriptors, new context information should not be allocated for each one. When the io_close messages arrive, because no new context has been allocated for each dup(), no release of the memory by each io_close message should occur either! (If it did, the rst close would wipe out the context.) The resource manager shared library provides default handlers that keep track of the open(), dup(), and close() messages and perform work only for the last close (i.e. the third io_close message in the example above).
Dispatch functions
The OS provides a set of dispatch_* functions that: allow a common blocking point for managers and clients that need to support multiple message types (e.g. a resource manager could handle its own private message range). provide a exible interface for message types that isnt tied to the resource manager (for clean handling of private messages and pulse codes) decouple the blocking and handler code from threads. You can implement the resource manager event loop in your main code. This decoupling also makes for easier debugging, because you can put a breakpoint between the block function and the handler function. For more information, see the Resource Managers chapter of Getting Started with QNX Neutrino, and the Writing a Resource Manager guide.
May 8, 2009
151
Combine messages
In order to conserve network bandwidth and to provide support for atomic operations, the OS supports combine messages. A combine message is constructed by the clients C library and consists of a number of I/O and/or connect messages packaged together into one. For example, the function readblock() allows a thread to atomically perform an lseek() and read() operation. This is done in the client library by combining the io_lseek and io_read messages into one. When the resource manager shared library receives the message, it will process both the io_lseek and io_read messages, effectively making that readblock() function behave atomically. Combine messages are also useful for the stat() function. A stat() call can be implemented in the clients library as an open(), fstat(), and close(). Instead of generating three separate messages (one for each of the component functions), the library puts them together into one contiguous combine message. This boosts performance, especially over a networked connection, and also simplies the resource manager, which doesnt need a connect function to handle stat(). The resource manager shared library takes care of the issues associated with breaking out the individual components of the combine message and passing them to the various handler functions supplied. Again, this minimizes the effort associated with writing a resource manager.
(optional)
152
May 8, 2009
The rst data structure, the context, has already been discussed (see the section on Message types). It holds data used on a per-open basis, such as the current position into a le (the lseek() offset). Since a resource manager may be responsible for more than one device (e.g. devc-ser* may be responsible for /dev/ser1, /dev/ser2, /dev/ser3, etc.), the attributes structure holds data on a per-device basis. The attributes structure contains such items as the user and group ID of the owner of the device, the last modication time, etc. For lesystem (block I/O device) managers, one more structure is used. This is the mount structure, which contains data items that are global to the entire mount device. When a number of client programs have opened various devices on a particular resource, the data structures may look like this:
One per name One per open One per mountpoint (optional) OCB A Clients Process A Channel Process B Resource manager threads Process C Mount structure describing Attribute structure for OCB B Attribute structure for
/dev/path1
OCB C
/dev/path*
/dev/path2
resmgr library
The iofunc_*() default functions operate on the assumption that the programmer has used the default denitions for the context block and the attributes structures. This is a safe assumption for two reasons: 1 2 The default context and attribute structures contain sufcient information for most applications. If the default structures dont hold enough information, they can be encapsulated within the structures that youve dened.
May 8, 2009
153
Summary
By denition, the default structures must be the rst members of their respective superstructures, allowing clean and simple access to the requisite base members by the iofunc_*() default functions:
Attribute superstructure
(iofunc_attr_t *)
Default members Extensions
Encapsulation.
The library contains iofunc_*() default handlers for these client functions: chmod() chown() close() devctl() fpathconf() fseek() fstat() lock() lseek() mmap() open() pathconf() stat() utime()
Summary
By supporting pathname space mapping, by having a well-dened interface to resource managers, and by providing a set of libraries for common resource manager functions, QNX Neutrino offers the developer unprecedented exibility and simplicity in developing drivers for new hardware a critical feature for many embedded systems. For more details on developing a resource manager, see the Resource Managers chapter of Getting Started with QNX Neutrino, and the Writing a Resource Manager guide.
154
May 8, 2009
May 8, 2009
Chapter 9 Filesystems
155
Introduction
Introduction
QNX Neutrino provides a rich variety of lesystems. Like most service-providing processes in the OS, these lesystems execute outside the kernel; applications use them by communicating via messages generated by the shared-library implementation of the POSIX API. Most of these lesystems are resource managers as described in this book. Each lesystem adopts a portion of the pathname space (called a mountpoint) and provides lesystem services through the standard POSIX API (open(), close(), read(), write(), lseek(), etc.). Filesystem resource managers take over a mountpoint and manage the directory structure below it. They also check the individual pathname components for permissions and for access authorizations. This implementation means that: Filesystems may be started and stopped dynamically. Multiple lesystems may run concurrently. Applications are presented with a single unied pathname space and interface, regardless of the conguration and number of underlying lesystems. A lesystem running on one node is transparently accessible from any other node.
May 8, 2009
Chapter 9 Filesystems
157
Filesystem classes
Filesystem classes
The many lesystems available can be categorized into the following classes: Image A special lesystem that presents the modules in the image and is always present. Note that the procnto process automatically provides an image lesystem and a RAM lesystem. Traditional lesystems that operate on block devices like hard disks and CD-ROM drives. This includes the Power-Safe lesystem, QNX 4, DOS, and CD-ROM lesystems. Nonblock-oriented lesystems designed explicitly for the characteristics of ash memory devices. For NOR devices, use the FFS3 lesystem; for NAND, use ETFS. Filesystems that provide network le access to the lesystems on remote host computers. This includes the NFS and CIFS (SMB) lesystems. QNX Neutrino provides an Inator virtual lesystem, a resource manager that sits in front of other lesystems and uncompresses les that were previously compressed (using the deflate utility).
Block
Flash
Network Virtual
158
Chapter 9 Filesystems
May 8, 2009
Filesystem classes
fs-nfs2
fs-cifs
io_blk.so
devb-* devn-*.so
As shown in this diagram, the lesystems and io-blk are implemented as shared libraries (essentially passive blocks of code resident in memory), while the devb-* driver is the executing process that calls into the libraries. In operation, the driver process starts rst and invokes the block-level shared library (io-blk.so). The lesystem shared libraries may be dynamically loaded later to provide lesystem interfaces and services. A lesystem shared library implements a lesystem protocol or personality on a set of blocks on a physical disk device. The lesystems arent built into the OS kernel; rather, theyre dynamic entities that can be loaded or unloaded on demand. For example, a removable storage device (PCCard ash card, oppy disk, removable cartridge disk, etc.) may be inserted at any time, with any of a number of lesystems stored on it. While the hardware the driver interfaces to is unlikely to change dynamically, the on-disk data structure could vary widely. The dynamic nature of the lesystem copes with this very naturally.
io-blk
Most of the lesystem shared libraries ride on top of the Block I/O module (io-blk.so). This module also acts as a resource manager and exports a block-special le for each physical device. For a system with two hard disks the default les would be:
May 8, 2009
Chapter 9 Filesystems
159
Filesystem classes
/dev/hd0 /dev/hd1
These les represent each raw disk and may be accessed using all the normal POSIX le primitives (open(), close(), read(), write(), lseek(), etc.). Although the io-blk module can support a 64-bit offset on seek, the driver interface is 32-bit, allowing access to 2-terabyte disks.
Partitions
QNX Neutrino complies with the de facto industry standard for partitioning a disk. This allows a number of lesystems to share the same physical disk. Each partition is also represented as a block-special le, with the partition type appended to the lename of the disk its located on. In the above two-disk example, if the rst disk had a QNX partition and a DOS partition, while the second disk had only a QNX partition, then the default les would be:
/dev/hd0 /dev/hd0t6 /dev/hd0t79 /dev/hd1 /dev/hd1t79
First hard disk DOS partition on rst hard disk QNX partition on rst hard disk Second hard disk QNX partition on second hard disk
160
Chapter 9 Filesystems
May 8, 2009
Filesystem classes
Filesystem DOS (12-bit FAT) DOS (16-bit FAT; partitions <32M) DOS Extended Partition (enumerated but not presented) DOS 4.0 (16-bit FAT; partitions 32M) OS/2 HPFS Previous QNX version 2 (pre-1988) Windows NT QNX 1.x and 2.x (qny) QNX 1.x and 2.x (qnz) DOS 32-bit FAT; partitions up to 2047G Same as Type 11, but uses Logical Block Address Int 13h extensions Same as Type 6, but uses Logical Block Address Int 13h extensions Same as Type 5, but uses Logical Block Address Int 13h extensions QNX POSIX partition (secondary) QNX POSIX partition (secondary) QNX POSIX partition UNIX Linux (Ext2) Apple Macintosh HFS or HFS Plus QNX Power-Safe POSIX partition (secondary) QNX Power-Safe POSIX partition (secondary) QNX Power-Safe POSIX partition
Buffer cache
The io-blk shared library implements a buffer cache that all lesystems inherit. The buffer cache attempts to store frequently accessed lesystem blocks in order to minimize the number of times a system has to perform a physical I/O to the disk. Read operations are synchronous; write operations are usually asynchronous. When an application writes to a le, the data enters the cache, and the lesystem manager immediately replies to the client process to indicate that the data has been written. The data is then written to the disk. Critical lesystem blocks such as bitmap blocks, directory blocks, extent blocks, and inode blocks are written immediately and synchronously to disk.
May 8, 2009
Chapter 9 Filesystems
161
Filesystem classes
Applications can modify write behavior on a le-by-le basis. For example, a database application can cause all writes for a given le to be performed synchronously. This would ensure a high level of le integrity in the face of potential hardware or power problems that might otherwise leave a database in an inconsistent state.
Filesystem limitations
POSIX denes the set of services a lesystem must provide. However, not all lesystems are capable of delivering all those services:
Filesystem Access date Modication date Status change date Filename lengtha Permissions Directories Hard links Soft links Decompression on read
Image RAM ETFS QNX 4 Power-Safe DOS NTFS CD-ROM UDF HFS FFS3 NFS CIFS Ext2
No Yes Yes Yes Yes Yesc Yes Yese Yes Yes No Yes No Yes
a
No Yes Yes Yes Yes Yes Yes Yese Yes Yes Yes Yes Yes Yes
No Yes Yes Yes Yes No No Yese Yes Yes Yes Yes No Yes
255 255 91 48b 510 8.3d 255 207f 254 255g 255 h h 255
Yes Yes Yes Yes Yes No No Yese Yes Yes Yes Yesh Yesh Yes
No No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes
No No No No No No Yes No No No Yes No No No
Our internal representation for le names is UTF-8, which uses a variable number of bytes per character. Many on-disk formats instead use UCS2, which is a xed number (2 bytes). Thus a length limit in characters may be 1, 2, or 3 times that number in bytes, as we convert from on-disk to OS representation. The lengths for the QNX 4, Power-Safe, and EXT2 lesystems are in bytes; those for NTFS, HFS, UDF, CD/Joliet and DOS/VFAT are in characters.
b c d e
505 if .longfilenames is enabled; otherwise, 48. VFAT or FAT32 (e.g. Windows 95). 255-character lename lengths used by VFAT or FAT32 (e.g. Windows 95). With Rock Ridge extensions.
162
Chapter 9 Filesystems
May 8, 2009
Image lesystem
f g h
103 characters with Joliet extensions; 255 with Rock Ridge extensions. 31 on HFS. Limited by the remote lesystem.
Image lesystem
Every QNX Neutrino system image provides a simple read-only lesystem that presents the set of les built into the OS image. Since this image may include both executables and data les, this lesystem is sufcient for many embedded systems. If additional lesystems are required, they would be placed as modules within the image where they can be started as needed.
RAM lesystem
Every QNX system also provides a simple RAM-based lesystem that allows read/write les to be placed under /dev/shmem. This RAM lesystem nds the most use in tiny embedded systems where persistent storage across reboots isnt required, yet where a small, fast, temporary-storage lesystem with limited features is called for. The lesystem comes for free with procnto and doesnt require any setup. You can simply create les under /dev/shmem and grow them to any size (depending on RAM resources). Although the RAM lesystem itself doesnt support hard or soft links or directories, you can create a link to it by using process-manager links. For example, you could create a link to a RAM-based /tmp directory:
ln -sP /dev/shmem /tmp
This tells procnto to create a process manager link to /dev/shmem known as /tmp. Application programs can then open les under /tmp as if it were a normal lesystem. In order to minimize the size of the RAM lesystem code inside the process manager, this lesystem specically doesnt include big lesystem features such as le locking and directory creation.
May 8, 2009
Chapter 9 Filesystems
163
Transactions never overwrite live data. A write in the middle of a le or a directory update always writes to a new unused area. In this way, if the operation fails part way through (due to a crash or power failure), the old data is still intact. Some log-based lesystems also operate under the principle that live data is never overwritten. But ETFS takes this to the extreme by turning everything into a log of transactions. The lesystem hierarchy is built on the y by processing the log of transactions in the device. This scan occurs at startup, but is designed such that only a small subset of the data is read and CRC-checked, resulting in faster startup times without sacricing reliability. Transactions are position-independent in the device and may occur in any order. You could read the transactions from one device and write them in a different order to another device. This is important because it allows bulk programming of devices containing bad blocks that may be at arbitrary locations. This design is well-suited for NAND ash memory. NAND ash is shipped with factory-marked bad blocks that may occur in any location.
Transaction Header Data Header Data Header ...
Required; device-independent
Optional; device-dependent
Inside a transaction
Each transaction consists of a header followed by data. The header contains the following: FID Offset Size Sequence CRCs A unique le ID that identies which le the transaction belongs to. The offset of the data portion within the le. The size of the data portion. A monotonically increasing number (to enable time ordering). Data integrity checks (for NAND, NOR, SRAM).
164
Chapter 9 Filesystems
May 8, 2009
ECCs Other
Cluster size 1K 2K 1K 1K 1K
Yes Yes No No No
Although ETFS can support NOR ash, we recommend instead the FFS3 lesystem (devf-*), which is designed explicitly for NOR ash devices.
Reliability features
ETFS is designed to survive across a power failure, even during an active ash write or block erase. The following features contribute to its reliability: dynamic wear-leveling static wear-leveling CRC error detection ECC error correction read degradation monitoring with automatic refresh transaction rollback atomic le operations automatic le defragmentation.
Dynamic wear-leveling
Flash memory allows a limited number of erase cycles on a ash block before the block will fail. This number can be as low as 100,000. ETFS tracks the number of erases on each block. When selecting a block to use, ETFS attempts to spread the erase cycles evenly over the device, dramatically increasing its life. The difference can be extreme: from usage scenarios of failure within a few days without wear-leveling to over 40 years with wear-leveling.
May 8, 2009
Chapter 9 Filesystems
165
Static wear-leveling
Filesystems often consist of a large number of static les that are read but not written. These les will occupy ash blocks that have no reason to be erased. If the majority of the les in ash are static, this will cause the remaining blocks containing dynamic data to wear at a dramatically increased rate. ETFS notices these under-worked static blocks and forces them into service by copying their data to an over-worked block. This solves two problems: It gives the over-worked block a rest, since it now contains static data, and it forces the under-worked static block into the dynamic pool of blocks.
Transaction rollback
When ETFS starts, it processes all transactions and rolls back (discards) the last partial or damaged transaction. The rollback code is designed to handle a power failure during a rollback operation, thus allowing the system to recover from multiple nested faults. The validity of a transaction is protected by CRC codes on each transaction.
Atomic le operations
ETFS implements a very simple directory structure on the device, allowing signicant modications with a single ash write. For example, the move of a le or directory to another directory is often a multistage operation in most lesystems. In ETFS, a move is accomplished with a single ash write.
166
Chapter 9 Filesystems
May 8, 2009
QNX 4 lesystem
Automatic le defragmentation
Log-based lesystems often suffer from fragmentation, since each update or write to an existing le causes a new transaction to be created. ETFS uses write-buffering to combine small writes into larger write transactions in an attempt to minimize fragmentation caused by lots of very small transactions. ETFS also monitors the fragmentation level of each le and will do a background defrag operation on les that do become badly fragmented. Note that this background activity will always be preempted by a user data request in order to ensure immediate access to the le being defragmented.
QNX 4 lesystem
The QNX 4 lesystem (fs-qnx4.so) is a high-performance lesystem that shares the same on-disk structure as in the QNX 4 RTOS. The QNX 4 lesystem implements an extremely robust design, utilizing an extent-based, bitmap allocation scheme with ngerprint control structures to safeguard against data loss and to provide easy recovery. Features include: extent-based POSIX lesystem robustness: all sensitive lesystem info is written through to disk on-disk signatures and special key information to allow fast data recovery in the event of disk damage 505-character lenames multi-threaded design client-driven priority same disk format as the lesystem under QNX 4 Since the release of 6.2.1, the 48-character lename limit has increased to 505 characters via a backwards-compatible extension. The same on-disk format is retained, but new systems will see the longer name, old ones will see a truncated 48-character name. For more information, see QNX 4 lesystem in the Working with Filesystems chapter of the QNX Neutrino Users Guide.
Power-Safe lesystem
The Power-Safe lesystem, supported by the fs-qnx6.so shared object, is a reliable disk lesystem that can withstand power failures without losing or corrupting data. It was designed for and is intended for traditional rotating hard disk drive media.
May 8, 2009
Chapter 9 Filesystems
167
Power-Safe lesystem
Copy-on-write lesystem
To address the problems associated with existing disk lesystems, the Power-Safe lesystem never overwrites live data; it does all updates using copy-on-write (COW), assembling a new view of the lesystem in unused blocks on the disk. The new view of the lesystem becomes live only when all the updates are safely written on the disk. Everything is COW: both metadata and user data are protected. To see how this works, lets consider how the data is stored. A Power-Safe lesystem is divided into logical blocks, the size of which you can specify when you use mkqnx6fs to format the lesystem. Each inode includes 16 pointers to blocks. If the le is smaller than 16 blocks, the inode points to the data blocks directly. If the le is any bigger, those 16 blocks become pointers to more blocks, and so on. The nal block pointers to the real data are all in the leaves and are all at the same level. In some other lesystems such as EXT2 a le always has some direct blocks, some indirect ones, and some double indirect, so you go to different levels to
168
Chapter 9 Filesystems
May 8, 2009
Power-Safe lesystem
get to different parts of the le. With the Power-Safe lesystem, all the user data for a le is at the same level.
Inode
...
...
...
User data
...
If you change some data, its written in one or more unused blocks, and the original data remains unchanged. The list of indirect block pointers must be modied to refer to the newly used blocks, but again the lesystem copies the existing block of pointers and modies the copy. The lesystem then updates the inode once again by modifying a copy to refer to the new block of indirect pointers. When the operation is complete, the original data and the pointers to it remain intact, but theres a new set of blocks, indirect pointers, and inode for the modied data:
Inode
......
......
...
User data
...
This has several implications for the COW lesystem: The bitmap and inodes are treated in the same way as user les. Any lesystem block can be relocated, so there arent any xed locations, such as those for the root block or bitmap in the QNX 4 lesystem The lesystem must be completely self-referential. A superblock is a global root block that contains the inodes for the system bitmap and inodes les. A Power-Safe lesystem maintains two superblocks: a stable superblock that reects the original version of all the blocks a working superblock that reects the modied data The working superblock can include pointers to blocks in the stable superblock. These blocks contain data that hasnt yet been modied. The inodes and bitmap for the working superblock grow from it.
May 8, 2009
Chapter 9 Filesystems
169
Power-Safe lesystem
Stable superblock
Working superblock
...
...
...
...
...
...
A snapshot is a consistent view of the lesystem (simply a committed superblock). To take a snapshot, the lesystem: 1 2 3 4 5 6 Locks the lesystem to make sure that its in a stable state; all client activity is suspended, and there must be no active operations. Writes all the copied blocks to disk. The order isnt important (as it is for the QNX 4 lesystem), so it can be optimized. Forces the data to be synchronized to disk, including ushing any hardware track cache. Constructs the superblock, recording the new location of the bitmap and inodes, incrementing its sequence number, and calculating a CRC. Writes the superblock to disk. Switches between the working and committed views. The old versions of the copied blocks are freed and become available for use.
To mount the disk at startup, the lesystem simply reads the superblocks from disk, validates their CRCs, and then chooses the one with the higher sequence number. Theres no need to run chkfsys or replay a transaction log. The time it takes to mount the lesystem is the time it takes to read a couple of blocks.
CAUTION: If the drive doesnt support synchronizing, fs-qnx6.so cant guarantee that the lesystem is power-safe. Before using this lesystem on devices such as USB/Flash devices other than traditional rotating hard disk drive media, check to make sure that your device meets the lesystems requirements. For more information, see Required properties of the device in the entry for fs-qnx6.so in the Utilities Reference.
170
Chapter 9 Filesystems
May 8, 2009
Power-Safe lesystem
Performance
The COW method has some drawbacks: Each change to user data can cause up to a dozen blocks to be copied and modied, because the lesystem never modies the inode and indirect block pointers in place; it has to copy the blocks to a new location and modify the copies. Thus, write operations are longer. When taking a snapshot, the lesystem must force all blocks fully to disk before it commits the superblock. However: Theres no constraint on the order in which the blocks (aside from the superblock) can be written. The new blocks can be allocated from any free, contiguous space. The performance of the lesystem depends on how much buffer cache is available, and on the frequency of the snapshots. Snapshots occur periodically (every 10 seconds, or as specied by the snapshot option to fs-qnx6.so), and also when you call sync() for the entire lesystem, or fsync() for a single le. Synchronization is at the lesystem level, not at that of individual les, so fsync() is potentially an expensive operation; the Power-Safe lesystem ignores the O_SYNC ag. You can also turn snapshots off if youre doing some long operation, and the intermediate states arent useful to you. For example, suppose youre copying a very large le into a Power-Safe lesystem. The cp utility is really just a sequence of basic operations: an open(O_CREAT|O_TRUNC) to make the le a bunch of write() operations to copy the data a close(), chmod(), and chown() to copy the metadata If the le is big enough so that copying it spans snapshots, you have on-disk views that include the le not existing, the le existing at a variety of sizes, and nally the complete le copied and its IDs and permissions set:
Snapshot Snapshot ... Snapshot Snapshot
Time ...
open()
write()
write()
write()
May 8, 2009
Chapter 9 Filesystems
171
DOS Filesystem
Each snapshot is a valid point-in-time view of the lesystem (i.e. if youve copied 50 MB, the size is 50 MB, and all data up to 50 MB is also correctly copied and available). If theres a power failure, the lesystem is restored to the most recent snapshot. But the lesystem has no concept that the sequence of open(), write(), and close() operations is really one higher-level operation, cp. If you want the higher-level semantics, disable the snapshots around the cp, and then the middle snapshots wont happen, and if a power failure occurs, the le will either be complete, or not there at all. For information about using this lesystem, see Power-Safe lesystem in the Working with Filesystems chapter of the QNX Neutrino Users Guide.
DOS Filesystem
The DOS Filesystem, fs-dos.so, provides transparent access to DOS disks, so you can treat DOS lesystems as though they were POSIX lesystems. This transparency allows processes to operate on DOS les without any special knowledge or work on their part. The structure of the DOS lesystem on disk is old and inefcient, and lacks many desirable features. Its only major virtue is its portability to DOS and Windows environments. You should choose this lesystem only if you need to transport DOS les to other machines that require it. Consider using the QNX lesystem alone if DOS le portability isnt an issue or in conjunction with the DOS lesystem if it is. If theres no DOS equivalent to a POSIX feature, fs-dos.so will either return an error or a reasonable default. For example, an attempt to create a link() will result in the appropriate errno being returned. On the other hand, if theres an attempt to read the POSIX times on a le, fs-dos.so will treat any of the unsupported times the same as the last write time.
172
Chapter 9 Filesystems
May 8, 2009
DOS Filesystem
An attempt to create a le that contains one of these invalid characters will return an error. DOS (8.3 format) also expects all alphabetical characters to be uppercase, so fs-dos.so maps these characters to uppercase when creating a lename on disk. But it maps a lename to lowercase by default when returning a lename to a QNX Neutrino application, so that QNX Neutrino users and programs can always see and type lowercase (via the sfn=sfn_mode option).
Handling lenames
You can specify how you want fs-dos.so to handle long lenames (via the lfn=lfn_mode option): Ignore them display/create only 8.3 lenames. Show them if lenames are longer than 8.3 or if mixed case is used. Always create both short and long lenames. If you use the ignore option, you can specify whether or not to silently truncate lename characters beyond the 8.3 limit.
International lenames
The DOS lesystem supports DOS code pages (international character sets) for locale lenames. Short 8.3 names are stored using a particular character set (typically the most common extended characters for a locale are encoded in the 8th-bit character range). All the common American as well as Western and Eastern European code pages (437, 850, 852, 866, 1250, 1251, 1252) are supported. If you produce software that must access a variety of DOS/Windows hard disks, or operate in non-US-English countries, this feature offers important portability lenames will be created with both a Unicode and locale name and are accessible via either name. The DOS lesystem supports international text in lenames only. No attempt is made to be aware of data contents, with the sole exception of Windows shortcut (.LNK) les, which will be parsed and translated into symbolic links if youve specied that option (lnk=lnk_mode).
May 8, 2009
Chapter 9 Filesystems
173
CD-ROM lesystem
File ownership
Although the DOS le structure doesnt support user IDs and group IDs, fs-dos.so (by default) wont return an error code if an attempt is made to change them. An error isnt returned because a number of utilities attempt to do this and failure would result in unexpected errors. The approach taken is you can change anything to anything since it isnt written to disk anyway. The posix= options let you set stricter POSIX checks and enable POSIX emulation. For example, in POSIX mode, an error of EINVAL is agged for attempts to do any of the following: Set the user ID or group ID to something other than the default (root). Remove an r (read) permission. Set an s (set ID on execution) permission. If you set the posix option to emulate (the default) or strict, you get the following benets: The . and .. directory entries are created in the root directory. The directory size is calculated. The number of links in a directory is calculated, based on its subdirectories.
CD-ROM lesystem
The CD-ROM lesystem provides transparent access to CD-ROM media, so you can treat CD-ROM lesystems as though they were POSIX lesystems. This transparency allows processes to operate on CD-ROM les without any special knowledge or work on their part. The fs-cd.so manager implements the ISO 9660 standard as well as a number of extensions, including Rock Ridge (RRIP), Joliet (Microsoft), and multisession (Kodak Photo CD, enhanced audio).
174
Chapter 9 Filesystems
May 8, 2009
FFS3 lesystem
Weve deprecated fs-cd.so in favor of fs-udf.so, which now supports ISO-9660 lesystems in addition to UDF. For information about UDF, see Universal Disk Format (UDF) lesystem, later in this chapter.
FFS3 lesystem
The FFS3 lesystem drivers implement a POSIX-like lesystem on NOR ash memory devices. The drivers are standalone executables that contain both the ash lesystem code and the ash device code. There are versions of the FFS3 lesystem driver for different embedded systems hardware as well as PCMCIA memory cards. The naming convention for the drivers is devf-system, where system describes the embedded system. For example, the devf-800fads driver is for the 800FADS PowerPC evaluation board. To nd out what ash devices we currently support, please refer to the following sources: the boards and mtd-flash directories under
bsp_working_dir/src/hardware/flash
QNX Neutrino OS docs (devf-* entries in the Utilities Reference) the QNX Software Systems website (www.qnx.com)
Customization
Along with the prebuilt ash lesystem drivers, including the generic driver (devf-generic), we provide the libraries and source code that youll need to build custom ash lesystem drivers for different embedded systems. For information on how to do this, see the Customizing the Flash Filesystem chapter in the Building Embedded Systems book.
Organization
The FFS3 lesystem drivers support one or more logical ash drives. Each logical drive is called a socket, which consists of a contiguous and homogeneous region of ash memory. For example, in a system containing two different types of ash device at different addresses, where one ash device is used for the boot image and the other for the ash lesystem, each ash device would appear in a different socket. Each socket may be divided into one or more partitions. Two types of partitions are supported: raw partitions and ash lesystem partitions.
Raw partitions
A raw partition in the socket is any partition that doesnt contain a ash lesystem. The driver doesnt recognize any lesystem types other than the ash lesystem. A raw partition may contain an image lesystem or some application-specic data.
May 8, 2009
Chapter 9 Filesystems
175
FFS3 lesystem
The lesystem will make accessible through a raw mountpoint (see below) any partitions on the ash that arent ash lesystem partitions. Note that the ash lesystem partitions are available as raw partitions as well. Filesystem partitions A ash lesystem partition contains the POSIX-like ash lesystem, which uses a QNX proprietary format to store the lesystem data on the ash devices. This format isnt compatible with either the Microsoft FFS2 or PCMCIA FTL specication. The lesystem allows les and directories to be freely created and deleted. It recovers space from deleted les using a reclaim mechanism similar to garbage collection.
Mountpoints
When you start the ash lesystem driver, it will by default mount any partitions it nds in the socket. Note that you can specify the mountpoint using mkefs or flashctl (e.g. /flash). Mountpoint
/dev/fsX /dev/fsXpY /fsXpY /fsXpY/.cmp
Description raw mountpoint socket X raw mountpoint socket X partition Y lesystem mountpoint socket X partition Y lesystem compressed mountpoint socket X partition Y
Features
The FFS3 lesystem supports many advanced features, such as POSIX compatibility, multiple threads, background reclaim, fault recovery, transparent decompression, endian-awareness, wear-leveling, and error-handling.
POSIX
The lesystem supports the standard POSIX functionality (including long lenames, access privileges, random writes, truncation, and symbolic links) with the following exceptions: You cant create hard links. Access times arent supported (but le modication times and attribute change times are). These design compromises allow this lesystem to remain small and simple, yet include most features normally found with block device lesystems.
176
Chapter 9 Filesystems
May 8, 2009
FFS3 lesystem
Background reclaim
The FFS3 lesystem stores les and directories as a linked list of extents, which are marked for deletion as theyre deleted or updated. Blocks to be reclaimed are chosen using a simple algorithm that nds the block with the most space to be reclaimed while keeping level the amount of wear of each individual block. This wear-leveling increases the MTBF (mean time between failures) of the ash devices, thus increasing their longevity. The background reclaim process is performed when there isnt enough free space. The reclaim process rst copies the contents of the reclaim block to an empty spare block, which then replaces the reclaim block. The reclaim block is then erased. Unlike rotating media with a mechanical head, proximity of data isnt a factor with a ash lesystem, so data can be scattered on the media without loss of performance.
Fault recovery
The lesystem has been designed to minimize corruption due to accidental loss-of-power faults. Updates to extent headers and erase block headers are always executed in carefully scheduled sequences. These sequences allow the recovery of the lesystems integrity in the case of data corruption. Note that properly designed ash hardware is essential for effective fault-recovery systems. In particular, special reset circuitry must be in place to hold the system in reset before power levels drop below critical. Otherwise, spurious or random bus activity can form write/erase commands and corrupt the ash beyond recovery. Rename operations are guaranteed atomic, even through loss-of-power faults. This means, for example, that if you lost power while giving an image or executable a new name, you would still be able to access the le via its old name upon recovery. When the FFS3 lesystem driver is started, it scans the state of every extent header on the media (in order to validate its integrity) and takes appropriate action, ranging from a simple block reclamation to the erasure of dangling extent links. This process is merged with the lesystems normal mount procedure in order to achieve optimal bootstrap timings.
Compression/decompression
For fast and efcient compression/decompression, you can use the deflate and inflator utilities, which rely on popular deate/inate algorithms. The deate algorithm combines two algorithms. The rst takes care of removing data duplication in les; the second algorithm handles data sequences that appear the most often by giving them shorter symbols. Those two algorithms provide excellent lossless compression of data and executable les. The inate algorithm simply reverses what the deate algorithm does. The deflate utility is intended for use with the filter attribute for mkefs. You can also use it to precompress les intended for a ash lesystem.
May 8, 2009
Chapter 9 Filesystems
177
NFS lesystem
The inflator resource manager sits in front of the other lesystems that were previously compressed using the deflate utility. It can almost double the effective size of the ash memory. Compressed les can be manipulated with standard utilities such as cp or ftp they can display their compressed and uncompressed size with the ls utility if used with the proper mountpoint. These features make the management of a compressed ash lesystem seamless to a systems designer.
Flash errors
As ash hardware wears out, its write state-machine may nd that it cant write or erase a particular bit cell. When this happens, the error status is propagated to the ash driver so it can take proper action (i.e. mark the bad area and try to write/erase in another place). This error-handling mechanism is transparent. Note that after several ash errors, all writes and erases that fail will eventually render the ash read-only. Fortunately, this situation shouldnt happen before several years of ash operation. Check your ash specication and analyze your applications data ow to ash in order to calculate its potential longevity or MTBF.
Endian awareness
The FFS3 lesystem is endian-aware, making it portable across different platforms. The optimal approach is to use the mkefs utility to select the targets endian-ness.
Utilities
The lesystem supports all the standard POSIX utilities such as ls, mkdir, rm, ln, mv, and cp. There are also some QNX Neutrino utilities for managing the ash:
flashctl deflate mkefs
Erase, format, and mount ash partitions. Compress les for ash lesystems. Create ash lesystem image les.
System calls
The lesystem supports all the standard POSIX I/O functions such as open(), close(), read(), and write(). Special functions such as erasing are supported using the devctl() function.
NFS lesystem
The Network File System (NFS) allows a client workstation to perform transparent le access over a network. It allows a client workstation to operate on les that reside on a server across a variety of operating systems. Client le access calls are converted to NFS protocol requests, and are sent to the server over the network. The server receives
178
Chapter 9 Filesystems
May 8, 2009
CIFS lesystem
the request, performs the actual lesystem operation, and sends a response back to the client. The Network File System operates in a stateless fashion by using remote procedure calls (RPC) and TCP/IP for its transport. Therefore, to use fs-nfs2 or fs-nfs3, youll also need to run the TCP/IP client for Neutrino. Any POSIX limitations in the remote server lesystem will be passed through to the client. For example, the length of lenames may vary across servers from different operating systems. NFS (versions 2 and 3) limits lenames to 255 characters; mountd (versions 1 and 3) limits pathnames to 1024 characters. Although NFS (version 2) is older than POSIX, it was designed to emulate UNIX lesystem semantics and happens to be relatively close to POSIX. If possible, you should use fs-nfs3 instead of fs-nfs2.
CIFS lesystem
Formerly known as SMB, the Common Internet File System (CIFS) allows a client workstation to perform transparent le access over a network to a Windows 98 or NT system, or a UNIX system running an SMB server. Client le access calls are converted to CIFS protocol requests and are sent to the server over the network. The server receives the request, performs the actual lesystem operation, and sends a response back to the client. The CIFS protocol makes no attempt to conform to POSIX. The fs-cifs manager uses TCP/IP for its transport. Therefore, to use fs-cifs (SMBfsys in QNX 4), youll also need to run the TCP/IP client for Neutrino.
May 8, 2009
Chapter 9 Filesystems
179
Windows NT lesystem
The NT lesystem is used on Microsoft Windows NT and later. The fs-nt.so shared object provides read-only access to NTFS disks on a QNX Neutrino system.
Virtual lesystems
QNX Neutrino provides an Inator virtual lesystem, which is a resource manager that sits in front of other lesystems and inates les that were previously deated (using the deflate utility). The inflator utility is typically used when the underlying lesystem is a ash lesystem. Using it can almost double the effective size of the ash memory. If a le is being opened for a read, inflator attempts to open the le itself on an underlying lesystem. It reads the rst 16 bytes and checks for the signature of a deated le. If the le was deated, inflator places itself between the application and the underlying lesystem. All reads return the original le data before it was deated. From the applications point of view, the le appears to be uncompressed. Random seeks are also supported. If the application does a stat() on the le, the size of the inated le (the original size before it was deated) is returned.
180
Chapter 9 Filesystems
May 8, 2009
190
May 8, 2009
181
Introduction
Introduction
A key requirement of any realtime operating system is high-performance character I/O. Character devices can be described as devices to which I/O consists of a sequence of bytes transferred serially, as opposed to block-oriented devices (e.g. disk drives). As in the POSIX and UNIX tradition, these character devices are located in the OS pathname space under the /dev directory. For example, a serial port to which a modem or terminal could be connected might appear in the system as:
/dev/ser1
Typical character devices found on PC hardware include: serial ports parallel ports text-mode consoles pseudo terminals (ptys) Programs access character devices using the standard open(), close(), read(), and write() API functions. Additional functions are available for manipulating other aspects of the character device, such as baud rate, parity, ow control, etc. Since its common to run multiple character devices, they have been designed as a family of drivers and a library called io-char to maximize code reuse.
io-char
Serial driver
Parallel driver
Console driver
Pty driver
As shown in this diagram, io-char is implemented as a library. The io-char module contains all the code to support POSIX semantics on the device. It also contains a signicant amount of code to implement character I/O features beyond POSIX but desirable in a realtime system. Since this code is in the common library, all drivers inherit these capabilities. The driver is the executing process that calls into the library. In operation, the driver starts rst and invokes io-char. The drivers themselves are just like any other QNX Neutrino process and can run at different priorities according to the nature of the hardware being controlled and the clients requesting service.
May 8, 2009
183
Introduction
Once a single character device is running, the memory cost of adding additional devices is minimal, since only the code to implement the new driver structure would be new.
Driver/io-char communication
The io-char library manages the ow of data between an application and the device driver. Data ows between io-char and the driver through a set of memory queues associated with each character device. Three queues are used for each device. Each queue is implemented using a rst-in, rst-out (FIFO) mechanism.
Process B
Application processes
Process A
Process C
io-char
Console driver
Serial driver
Parallel driver
Received data is placed into the raw input queue by the driver and is consumed by io-char only when application processes request data. (For details on raw versus edited or canonical input, see the section Input modes later in this chapter.)
184
May 8, 2009
Introduction
Interrupt handlers within drivers typically call a trusted library routine within io-char to add data to this queue this ensures a consistent input discipline and minimizes the responsibility of the driver (and effort required to create new drivers). The io-char module places output data into the output queue to be consumed by the driver as characters are physically transmitted to the device. The module calls a trusted routine within the driver each time new data is added so it can kick the driver into operation (in the event that it was idle). Since output queues are used, io-char implements write-behind for all character devices. Only when the output buffers are full will io-char cause a process to block while writing. The canonical queue is managed entirely by io-char and is used while processing input data in edited mode. The size of this queue determines the maximum edited input line that can be processed for a particular device. The sizes of these queues are congurable using command-line options. Default values are usually more than adequate to handle most hardware congurations, but you can tune these to reduce overall system memory requirements, to accommodate unusual hardware situations, or to handle unique protocol requirements. Device drivers simply add received data to the raw input queue or consume and transmit data from the output queue. The io-char module decides when (and if) output transmission is to be suspended, how (and if) received data is echoed, etc.
Device control
Low-level device control is implemented using the devctl() call. The POSIX terminal control functions are layered on top of devctl() as follows: tcgetattr() tcsetattr() tcgetpgrp() tcsetpgrp() tcsendbreak() tcow() Get terminal attributes. Set terminal attributes. Get ID of process group leader for a terminal. Set ID of process group leader for a terminal. Send a break condition. Suspend or restart data transmission/reception.
QNX extensions
The QNX extensions to the terminal control API are as follows: tcdropline() tcinject() Initiate a disconnect. For a serial device, this will pulse the DTR line. Inject characters into the canonical buffer.
The io-char module acts directly on a common set of devctl() commands supported by most drivers. Applications send device-specic devctl() commands through io-char to the drivers.
May 8, 2009
185
Introduction
Input modes
Each device can be in a raw or edited input mode.
when at least this number of characters arrives. if a pause in the character stream occurs. if an overall amount of time passes. if a framing character arrives.
In the case where multiple conditions are specied, the read will be satised when any one of them is satised.
MIN
The qualier MIN is useful when an application has knowledge of the number of characters it expects to receive. Any protocol that knows the character count for a frame of data can use MIN to wait for the entire frame to arrive. This signicantly reduces IPC and process scheduling. MIN is often used in conjunction with TIME or TIMEOUT. MIN is part of the POSIX standard.
TIME
The qualier TIME is useful when an application is receiving streaming data and wishes to be notied when the data stops or pauses. The pause time is specied in 1/10ths of a second. TIME is part of the POSIX standard.
186
May 8, 2009
Introduction
TIMEOUT
The qualier TIMEOUT is useful when an application has knowledge of how long it should wait for data before timing out. The timeout is specied in 1/10ths of a second. Any protocol that knows the character count for a frame of data it expects to receive can use TIMEOUT. This in combination with the baud rate allows a reasonable guess to be made when data should be available. It acts as a deadman timer to detect dropped characters. It can also be used in interactive programs with user input to timeout a read if no response is available within a given time. TIMEOUT is a QNX extension and is not part of the POSIX standard.
FORWARD
The qualier FORWARD is useful when a protocol is delimited by a special framing character. For example, the PPP protocol used for TCP/IP over a serial link starts and ends its packets with a framing character. When used in conjunction with TIMEOUT, the FORWARD character can greatly improve the efciency of a protocol implementation. The protocol process will receive complete frames, rather than character by character. In the case of a dropped framing character, TIMEOUT or TIME can be used to quickly recover. This greatly minimizes the amount of IPC work for the OS and results in a much lower processor utilization for a given TCP/IP data rate. Its interesting to note that PPP doesnt contain a character count for its frames. Without the data-forwarding character, an implementation might be forced to read the data one character at a time. FORWARD is a QNX extension and is not part of the POSIX standard. The ability to push the processing for application notication into the service-providing components of the OS reduces the frequency with which user-level processing must occur. This minimizes the IPC work to be done in the system and frees CPU cycles for application processing. In addition, if the application implementing the protocol is executing on a different network node than the communications port, the number of network transactions is also minimized. For intelligent, multiport serial cards, the data-forwarding character recognition can also be implemented within the intelligent serial card itself, thereby signicantly reducing the number of times the card must interrupt the host processor for interrupt servicing.
May 8, 2009
187
Introduction
In edited mode, each character is received into the raw input buffer by the interrupt handler. Unlike raw mode where the driver is scheduled to run only when some input conditions are met, the interrupt handler will schedule the driver on every received character. There are two reasons for this. First, edited input mode is rarely used for high-performance communication protocols. Second, the work of editing is signicant and not suitable for an interrupt handler. When the driver runs, code in io-char will examine the character and apply it to the canonical buffer in which its building a line. When a line is complete and an application requests input, the line will be transferred from the canonical buffer to the application the transfer is direct from the canonical buffer to the application buffer without any intervening copies. The editing code correctly handles multiple pending input lines in the canonical buffer and allows partial lines to be read. This can happen, for example, if an application asked only for 1 character when a 10-character line was available. In this case, the next read will continue where the last one left off. The io-char module provides a rich set of editing capabilities, including full support for moving over the line with cursor keys and for changing, inserting, or deleting characters. Here are some of the more common capabilities:
LEFT RIGHT HOME END ERASE DEL KILL UP DOWN INS
Move the cursor one character to the left. Move the cursor one character to the right. Move the cursor to the beginning of the line. Move the cursor to the end of the line. Erase the character to the left of the cursor. Erase the character at the current cursor position. Erase the entire input line. Erase the current line and recall a previous line. Erase the current line and recall the next line. Toggle between insert mode and typeover mode (every new line starts in insert mode).
Line-editing characters vary from terminal to terminal. The console always starts out with a full set of editing keys dened. If a terminal is connected via a serial channel, you need to dene the editing characters that apply to that particular terminal. To do this, you can use the stty utility. For example, if you have an ANSI terminal connected to a serial port (called /dev/ser1), you would use the following command to extract the appropriate editing keys from the terminfo database and apply them to /dev/ser1:
188
May 8, 2009
Console devices
Console devices
System consoles (with VGA-compatible graphics chips in text mode) are managed by the devc-con or devc-con-hid driver. The video display card/screen and the system keyboard are collectively referred to as the physical console. The devc-con permits multiple sessions to be run concurrently on a physical console by means of virtual consoles. The devc-con console driver process typically manages more than one set of I/O queues to io-char, which are made available to user processes as a set of character devices with names like /dev/con1, /dev/con2, etc. From the applications point of view, there really are multiple consoles available to be used. Of course, theres only one physical console (screen and keyboard), so only one of these virtual consoles is actually displayed at any one time. The keyboard is attached to whichever virtual console is currently visible.
Terminal emulation
The console drivers emulate an ANSI terminal.
Serial devices
Serial communication channels are managed by the devc-ser* family of driver processes. These drivers can manage more than one physical channel and provide character devices with names such as /dev/ser1, /dev/ser2, etc.
May 8, 2009
189
Parallel devices
When devc-ser* is started, command-line arguments can specify which and how many serial ports are installed. On a PC-compatible system, this will typically be the two standard serial ports often referred to as com1 and com2. The devc-ser* driver directly supports most nonintelligent multiport serial cards. QNX Neutrino includes various serial drivers (e.g. devc-ser8250, devc-serppc800, etc.). For details, see the devc-ser* entries in Utilities Reference. The devc-ser* drivers support hardware ow control (except under edited mode) provided that the hardware supports it. Loss of carrier on a modem can be programmed to deliver a SIGHUP signal to an application process (as dened by POSIX).
Parallel devices
Parallel printer ports are managed by the devc-par driver. When devc-par is started, command-line arguments can specify which parallel port is installed. The devc-par driver is an output-only driver, so it has no raw input or canonical input queues. The size of the output buffer can be congured with a command-line argument. If congured to a large size, this creates the effect of a software print buffer.
Serial line
devc-ser*
Process
devc-pty
Application process
Pseudo-ttys.
190
May 8, 2009
Ptys are routinely used to create pseudo-terminal interfaces for programs like pterm, a terminal emulator that runs under the Photon microGUI and telnet, which uses TCP/IP to provide a terminal session to a remote system.
May 8, 2009
191
May 8, 2009
193
Introduction
Introduction
As with other service-providing processes in QNX Neutrino, the networking services execute outside the kernel. Developers are presented with a single unied interface, regardless of the conguration and number of networks involved. This architecture allows: Network drivers to be started and stopped dynamically. Qnet and other protocols to run together in any combination. Our native network subsystem consists of the network manager executable (io-pkt-v4, io-pkt-v4-hc, or io-pkt-v6-hc), plus one or more shared library modules. These modules can include protocols (e.g. lsm-qnet.so) and drivers (e.g. devnp-speedo.so).
May 8, 2009
195
io-pkt
Net application Stack utilities
libsocket libc
BPF IP input Application Resource Manager A Protocols (.so) Stack Packet Filtering Ether input
libprotocol A libc
802.11 framework Drivers (.so) Legend: Message-passing API Function calls WiFi drivers (.so)
At the driver layer, there are interfaces for Ethernet trafc (used by all Ethernet drivers), and an interface into the stack for 802.11 management frames from wireless drivers. The hc variants of the stack also include a separate hardware crypto API that allows the stack to use a crypto ofoad engine when its encrypting or decrypting data for secure links. You can load drivers (built as DLLs for dynamic linking and prexed with devnp- for new-style drivers, and devn- for legacy drivers) into the stack using the -d option to io-pkt. APIs providing connection into the data ow at either the Ethernet or IP layer allow protocols to coexist within the stack process. Protocols (such as Qnet) are also built as DLLs. A protocol links directly into either the IP or Ethernet layer and runs within the stack context. Theyre prexed with lsm (loadable shared module) and you load them into the stack using the -p option. The tcpip protocol (-ptcpip) is a special option that the stack recognizes, but doesnt link a protocol module for (since the IP stack is already built in). You still use the -ptcpip option to pass additional parameters to the stack that apply to the IP protocol layer (e.g. -ptcpip prefix=/alt to get the IP stack to register /alt/dev/socket as the name of its resource manager). A protocol requiring interaction from an application sitting outside of the stack process may include its own resource manager infrastructure (this is what Qnet does) to allow communication and conguration to occur.
196
May 8, 2009
Threading model
In addition to drivers and protocols, the stack also includes hooks for packet ltering. The main interfaces supported for ltering are: Berkeley Packet Filter (BPF) interface A socket-level interface that lets you read and write, but not modify or block, packets, and that you access by using a socket interface at the application layer (see http://en.wikipedia.org/wiki/Berkeley_Packet_Filter). This is the interface of choice for basic, raw packet interception and transmission and gives applications outside of the stack process domain access to raw data streams. Packet Filter (PF) interface A read/write/modify/block interface that gives complete control over which packets are received by or transmitted from the upper layers and is more closely related to the io-net lter API. For more information, see the Packet Filtering and Firewalling chapter of the Neutrino Core Networking Users Guide.
Threading model
The default mode of operation is for io-pkt to create one thread per CPU. The io-pkt stack is fully multi-threaded at layer 2. However, only one thread may acquire the stack context for upper-layer packet processing. If multiple interrupt sources require servicing at the same time, these may be serviced by multiple threads. Only one thread will be servicing a particular interrupt source at any point in time. Typically an interrupt on a network device indicates that there are packets to be received. The same thread that handles the receive processing may later transmit the received packets out another interface. Examples of this are layer-2 bridging and the ipow fastforwarding of IP packets. The stack uses a thread pool to service events that are generated from other parts of the system. These events may be: time outs ISR events other things generated by the stack or protocol modules You can use a command-line option to the driver to control the priority at which the thread is run to receive packets. Client connection requests are handled in a oating priority mode (i.e. the thread priority matches that of the client application thread accessing the stack resource manager). Once a thread receives an event, it examines the event type to see if its a hardware event, stack event, or other event:
May 8, 2009
197
Protocol module
If the event is a hardware event, the hardware is serviced and, for a receive packet, the thread determines whether bridging or fast-forwarding is required. If so, the thread performs the appropriate lookup to determine which interface the packet should be queued for, and then takes care of transmitting it, after which it goes back to check and see if the hardware needs to be serviced again. If the packet is meant for the local stack, the thread queues the packet on the stack queue. The thread then goes back and continues checking and servicing hardware events until there are no more events. Once a thread has completed servicing the hardware, it checks to see if theres currently a stack thread running to service stack events that may have been generated as a result of its actions. If theres no stack thread running, the thread becomes the stack thread and loops, processing stack events until there are none remaining. It then returns to the wait for event state in the thread pool. This capability of having a thread change directly from being a hardware-servicing thread to being the stack thread eliminates context switching and greatly improves the receive performance for locally terminated IP ows.
Protocol module
The networking protocol module is responsible for implementing the details of a particular protocol (e.g. Qnet). Each protocol component is packaged as a shared object (e.g. lsm-qnet.so). One or more protocol components may run concurrently. For example, the following line from a buildle shows io-pkt-v4 loading the Qnet protocol via its -p protocol command-line option:
io-pkt-v4 -dne2000 -pqnet
The io-pkt* managers include the TCP/IP stack. Qnet is the QNX Neutrino native networking protocol. Its main purpose is to extend the OSs powerful message-passing IPC transparently over a network of microkernels. Qnet also provides Quality of Service policies to help ensure reliable network transactions. For more information on the Qnet and TCP/IP protocols, see the following chapters in this book: Native Networking (Qnet) TCP/IP Networking
198
May 8, 2009
Driver module
Driver module
The network driver module is responsible for managing the details of a particular network adaptor (e.g. an NE-2000 compatible Ethernet controller). Each driver is packaged as a shared object and installs into the io-pkt* component.
All network device drivers are shared objects whose names are of the form devnp-driver.so. The io-pkt* manager can also load legacy io-net drivers. The names of these drivers start with devn-. Once the shared object is loaded, io-pkt* will then initialize it. The driver and io-pkt* are then effectively bound together the driver will call into io-pkt* (for example when packets arrive from the interface) and io-pkt* will call into the driver (for example when packets need to be sent from an application to the interface). To unload a legacy io-net driver, you can use the umount command. For example:
umount /dev/io-net/en0
To unload a new-style driver or a legacy io-net driver, use the ifconfig destroy command:
ifconfig bge0 destroy
For more information on network device drivers, see their individual utility pages (devn-*, devnp-*) in the Utilities Reference.
May 8, 2009
199
May 8, 2009
201
May 8, 2009
203
Now consider the case of a simple network with two machines one contains the client process, the other contains the server process.
lab1 lab2
Client
Server
A simple network where the client and server reside on separate machines.
The code required for client-server communication is identical to the code in the single-node case, but with one important exception: the pathname. The pathname will contain a prex that species the node that the service (/dev/ser1) resides on. As well see later, this prex will be translated into a node descriptor for the lower-level ConnectAttach() kernel call that will take place. Each node in the network is assigned a node descriptor, which serves as the only visible means to determine whether the OS is running as a network or standalone. For more information on node descriptors, see the Transparent Distributed Processing with Qnet chapter of the Neutrino Programmers Guide.
So with Qnet running, you can now open pathnames (les or managers) on other remote Qnet nodes, just as you open les locally on your own node. This means you
204
May 8, 2009
can access regular les or manager processes on other Qnet nodes as if they were executing on your local node. Recall our open() example above. If you wanted to open a serial device on node1 instead of on your local machine, you simply specify the path:
fd = open("/net/node1/dev/ser1",O_RDWR...); /*Open a serial device on node1*/
For client-server communications, how does the client know what node descriptor to use for the server? The client uses the lesystems pathname space to look up the servers address. In the single-machine case, the result of that lookup will be a node descriptor, a process ID, and a channel ID. In the networked case, the results are the same the only difference will be the value of the node descriptor. If node descriptor is: 0 (or ND_LOCAL_NODE) Nonzero Then the server is: Local (i.e. this node) Remote
May 8, 2009
205
lab1
lab2
Process manager
Serial driver
Here are the interactions: 1 A message is sent from the client to its local process manager, effectively asking who should be contacted to resolve the pathname /net/lab2/dev/ser1. Since the native network manager (lsm-qnet.so) has taken over the entire /net namespace, the process manager returns a redirect message, saying that the client should contact the local network manager for more information. 2 The client then sends a message to the local network manager, again asking who should be contacted to resolve the pathname. The local network manager then replies with another redirect message, giving the node descriptor, process ID, and channel ID of the process manager on node lab2 effectively deferring the resolution of the request to node lab2. 3 The client then creates a connection to the process manager on node lab2, once again asking who should be contacted to resolve the pathname. The process manager on node lab2 returns another redirect, this time with the node descriptor, channel ID, and process ID of the serial driver on its own node. 4 The client creates a connection to the serial driver on node lab2, and nally gets a connection ID that it can then use for subsequent message-passing operations. After this point, from the clients perspective, message passing to the connection ID is identical to the local case. Note that all further message operations are now direct between the client and server. The key thing to keep in mind here is that the client isnt aware of the operations taking place; these are all handled by the POSIX open() call. As far as the client is concerned, it performs an open() and gets back a le descriptor (or an error indication).
206
May 8, 2009
In each subsequent name-resolution step, the request from the client is stripped of already-resolved name components; this occurs automagically within the resource manager framework. This means that in step 2 above, the relevant part of the request is lab2/dev/ser1 from the perspective of the local network manager. In step 3, the relevant part of the request has been stripped to just dev/ser1, because thats all that lab2s process manager needs to know. Finally, in step 4, the relevant part of the request is simply ser1, because thats all the serial driver needs to know.
Network naming
As mentioned earlier, the pathname prex /net is the most common name that lsm-qnet.so uses. In resolving names in a network-wide pathname space, the following terms come into play: node name A character string that identies the node youre talking to. Note that a node name cant contain slashes or dots. In the example above, we used lab2 as one of our node names. The default is fetched via confstr() with the _CS_HOSTNAME parameter. A character string thats tacked onto the node name by lsm-qnet.so. Together the node name and node domain must form a string thats unique for all nodes that are talking to each other. The default is fetched via confstr() with the _CS_DOMAIN parameter.
node domain
fully qualied node name (FQNN) The string formed by tacking the node name and node domain together. For example, if the node name is lab2 and the node domain name is qnx.com, the resulting FQNN would be: lab2.qnx.com. network directory A directory in the pathname space implemented by lsm-qnet.so. Each network directory (there can be more than
May 8, 2009
207
one on a node) has an associated node domain. The default is /net, as used in the examples in this chapter. name resolution The process by which lsm-qnet.so converts an FQNN to a list of destination addresses that the transport layer knows how to get to. A piece of code that implements one method of converting an FQNN to a list of destination addresses. Each network directory has a list of name resolvers that are applied in turn to attempt to resolve the FQNN. The default is en_ionet (see the next section).
name resolver
Quality of Service (QoS) A denition of connectivity between two nodes. The default QoS is loadbalance (see the section on QoS later in this chapter.)
Resolvers
The following resolvers are built into the network manager: en_ionet Broadcast requests for name resolution on the LAN (similar to the TCP/IP ARP protocol). This is the default. dns Take the node name, add a dot (.) followed by the node domain, and send the result to the TCP/IP gethostbyname() function. file Search for accessible nodes, including the relevant network address, in a static le.
QoS policies
Qnet supports transmission over multiple networks and provides the following policies for specifying how Qnet should select a network interface for transmission:
208
May 8, 2009
Qnet is free to use all available network links, and will share transmission equally among them.
preferred
Qnet uses one specied link, ignoring all other networks (unless the preferred one fails). Qnet uses one and only one link, ignoring all others, even if the exclusive link fails.
exclusive
To fully benet from Qnets QoS, you need to have physically separate networks. For example, consider a network with two nodes and a hub, where each node has two connections to the hub:
Hub
Node 1
Node 2
If the link thats currently in use fails, Qnet detects the failure, but doesnt switch to the other link because both links go to the same hub. Its up to the application to recover from the error; when the application reestablishes the connection, Qnet switches to the working link. Now, consider the same network, but with two hubs:
May 8, 2009
209
Hub
Node 1
Node 2
Hub
If the networks are physically separate and a link fails, Qnet automatically switches to another link, depending on the QoS that you chose. The application isnt aware that the rst link failed. You can use the tx_retries option to lsm-qnet.so to limit the number of times that Qnet retries a transmission, and hence control how long Qnet waits before deciding that a link has failed. Note that if the number of retries is too low, Qnet wont tolerate any lost packets and may prematurely decide that a link is down. Lets look in more detail at the QoS policies.
loadbalance
Qnet decides which links to use for sending packets, depending on current load and link speeds as determined by io-pkt*. A packet is queued on the link that can deliver the packet the soonest to the remote end. This effectively provides greater bandwidth between nodes when the links are up (the bandwidth is the sum of the bandwidths of all available links), and allows a graceful degradation of service when links fail. If a link does fail, Qnet will switch to the next available link. This switch takes a few seconds the rst time, because the network driver on the bad link will have timed out, retried, and nally died. But once Qnet knows that a link is down, it will not send user data over that link. (This is a signicant improvement over the QNX 4 implementation.) While load-balancing among the live links, Qnet will send periodic maintenance packets on the failed link in order to detect recovery. When the link recovers, Qnet places it back into the pool of available links.
210
May 8, 2009
preferred
With this policy, you specify a preferred link to use for transmissions. Qnet will use only that one link until it fails. If your preferred link fails, Qnet will then turn to the other available links and resume transmission, using the loadbalance policy. Once your preferred link is available again, Qnet will again use only that link, ignoring all others (unless the preferred link fails).
exclusive
You use this policy when you want to lock transmissions to only one link. Regardless of how many other links are available, Qnet will latch onto the one interface you specify. And if that exclusive link fails, Qnet will NOT use any other link. Why would you want to use the exclusive policy? Suppose you have two networks, one much faster than the other, and you have an application that moves large amounts of data. You might want to restrict transmissions to only the fast network in order to avoid swamping the slow network under failure conditions.
The QoS parameter always begins with a tilde () character. Here were telling Qnet to lock onto the en0 interface exclusively, even if it fails.
Symbolic links
You can set up symbolic links to the various QoS-qualied pathnames:
ln -sP /net/lab2preferred:en1 /remote/sql_server
This assigns an abstracted name of /remote/sql_server to the node lab2 with a preferred QoS (i.e. over the en1 link). You cant create symbolic links inside /net because Qnet takes over that namespace. Abstracting the pathnames by one level of indirection gives you multiple servers available in a network, all providing the same service. When one server fails, the abstract pathname can be remapped to point to the pathname of a different server. For example, if lab2 failed, then a monitoring program could detect this and effectively issue:
May 8, 2009
211
Examples
This would remove lab2 and reassign the service to lab1. The real advantage here is that applications can be coded based on the abstract service name rather than be bound to a specic node name.
Examples
Lets look at a few examples of how youd use the network manager. The QNX Neutrino native network manager lsm-qnet.so is actually a shared object that installs into the executable io-pkt*.
Local networks
If youre using QNX Neutrino on a small LAN, you can use just the default en_ionet resolver. When a node name thats currently unknown is being resolved, the resolver will broadcast the name request over the LAN, and the node that has the name will respond with an identication message. Once the names been resolved, its cached for future reference. Since en_ionet is the default resolver when you start lsm-qnet.so, you can simply issue commands like:
ls /net/lab2/
If you have a machine called lab2 on your LAN, youll see the contents of its root directory.
Remote networks
CAUTION: For security reasons, you should have a rewall set up on your network before connecting to the Internet. For more information, see ftp://ftp3.usa.openbsd.org/pub/OpenBSD/doc/pf-faq.pdf in the OpenBSD documentation. Qnet uses DNS (Domain Name System) when resolving remote names. To use lsm-qnet.so with DNS, you specify this resolver on mounts command line:
mount -Tio-pkt -o"mount=:,resolve=dns,mount=.com:.net:.edu" /lib/dll/lsm-qnet.so
In this example, Qnet will use both its native en_ionet resolver (indicated by the rst mount= command) and DNS for resolving remote names. Note that weve specied several types of domain names (mount=.com:.net:.edu) as mountpoints, simply to ensure better remote name resolution. Now you could enter a command such as:
ls /net/qnet.qnx.com/repository
and youd get a listing of the repository directory at the qnet.qnx.com site.
212
May 8, 2009
May 8, 2009
213
222 222
May 8, 2009
215
Introduction
Introduction
As the Internet has grown to become more and more visible in our daily lives, the protocol its based on IP (Internet Protocol) has become increasingly important. The IP protocol and tools that go with it are ubiquitous, making IP the de facto choice for many private networks. IP is used for everything from simple tasks (e.g. remote login) to more complicated tasks (e.g. delivering realtime stock quotes). Most businesses are turning to the World Wide Web, which commonly rides on IP, for communication with their customers, advertising, and other business connectivity. QNX Neutrino is well-suited for a variety of roles in this global network, from embedded devices connected to the Internet, to the routers that are used to implement the Internet itself. Given these and many other user requirements, weve made our TCP/IP stack (included in io-pkt*) relatively light on resources, while using the common BSD API.
Stack congurations
We provide the following stack congurations: NetBSD TCP/IP stack Based on the latest RFCs, including UDP, IP, and TCP. Also supports forwarding, broadcast and multicast, hardware checksum support, routing sockets, Unix domain sockets, multilink PPP, PPPoE, supernetting (CIDR), NAT/IP ltering, ARP, ICMP, and IGMP, as well as CIFS, DHCP, AutoIP, DNS, NFS (v2 and v3 server/client), NTP, RIP, RIPv2, and an embedded web server. To create applications for this stack, you use the industry-standard BSD socket API. This stack also includes optimized forwarding code for additional performance and efcient packet routing when the stack is functioning as a network gateway. Enhanced NetBSD stack with IPsec and IPv6 Includes all the features in the standard stack, plus the functionality targeted at the new generation of mobile and secure communications. This stack provides full IPv6 and IPsec (both IPv4 and IPv6) support through KAME extensions, as well as support for VPNs over IPsec tunnels. This dual-mode stack supports IPv4 and IPv6 simultaneously and includes IPv6 support for autoconguration, which allows device conguration in plug-and-play network environments. IPv6 support includes IPv6-aware utilities and RIP/RIPng to support dynamic routing. An Advanced Socket API is also provided to supplement the standard socket API to take advantage of IPv6 extended-development capabilities. IPsec support allows secure communication between hosts or networks, providing data condentiality via strong encryption algorithms and data authentication features. IPsec support also includes the IKE (ISAKMP/Oakley) key management protocol for establishing secure host associations.
May 8, 2009
217
The QNX TCP/IP suite is also modular. For example, it provides NFS as separate modules. With this kind of modularity, together with small-sized modules, embedded systems developers can more easily and quickly build small TCP/IP-capable systems.
lsm-pf-*.so
User applications
io-pkt pppd
pppoed
devn-*.so devnp-*.so
devc-ser*
PPP is implemented as part of io-pkt-*. Since io-pkt* handles the transmission of PPP packets, theres no need for a memory copy of the packet data. This approach allows for high-performance PPPoE connections. Other components of the TCP/IP suite (such as the NFS, the snmpd daemon, etc.) are implemented outside of io-pkt*. This leads to better modularity and fault-tolerance.
Socket API
The BSD Socket API was the obvious choice for QNX Neutrino. The Socket API is the standard API for TCP/IP programming in the UNIX world. In the Windows world, the Winsock API is based on and shares a lot with the BSD Socket API. This makes conversion between the two fairly easy.
218
May 8, 2009
Socket API
All the routines that application programmers would expect are available, including (but not limited to): accept() bind() bindresvport() connect() dn_comp() dn_expand() endprotoent() endservent() gethostbyaddr() gethostbyname() getpeername() getprotobyname() getprotobynumber() getprotoent() getservbyname() getservent() getsockname() getsockopt() herror() hstrerror() htonl() htons() h_errlist() h_errno() h_nerr() inet_addr() inet_aton() inet_lnaof() inet_makeaddr() inet_netof() inet_network() inet_ntoa() ioctl() listen() ntohl() ntohs() recv() recvfrom() res_init() res_mkquery() res_query() res_querydomain() res_search() res_send() select() send() sendto() setprotoent() setservent() setsockopt() shutdown() socket()
For more information, see the Neutrino Library Reference. The common daemons and utilities from the Internet will easily port or just compile in this environment. This makes it easy to leverage what already exists for your applications.
Database routines
The database routines have been modied to better suit embedded systems.
/etc/resolv.conf
You can use conguration strings (via the confstr() function) to override the data usually contained in the /etc/resolv.conf le. You can also use the RESCONF environment variable to do this. Either method lets you use a nameserver without /etc/resolv.conf. This affects gethostbyname() and other resolver routines.
May 8, 2009
219
Multiple stacks
/etc/protocols
The getprotobyname() and getprotobynumber() functions have been modied to contain a small number of builtin protocols, including IP, ICNP, UDP, and TCP. For many applications, this means that the /etc/protocols le doesnt need to exist.
/etc/services
The getservbyname() function has been modied to contain a small number of builtin services, including ftp, telnet, smtp, domain, nntp, netbios-ns, netbios-ssn, sunrpc, and nfsd. For many applications, this means that the /etc/services le doesnt need to exist.
Multiple stacks
The QNX Neutrino network manager (io-pkt) lets you load multiple protocol shared objects. This means, for example, that you can load several instances of the TCP/IP stack on the same physical interface, making it easy to create multiple virtual networks (VLANs). You can even run multiple, independent instances of the network manager (io-pkt*) itself. As with all QNX Neutrino system components, each io-pkt* naturally benets from complete memory protection thanks to our microkernel architecture.
NTP
NTP (Network Time Protocol) allows you to keep the time of day for the devices in your network synchronized with the Internet standard time servers. The QNX NTP daemon supports both server and client modes.
220
May 8, 2009
In server mode, a daemon on the local network synchronizes with the standard time servers. It will then broadcast or multicast what it learned to the clients on the local network, or wait for client requests. The client NTP systems will then be synchronized with the server NTP system. The NTP suite implements NTP v4 while maintaining compatibility with v3, v2, and v1.
AutoIP
Developed from the Zeroconf IETF draft, lsm-autoip.so is an io-pkt* module that automatically congures the IPv4 address of your interface without the need of a server (as per DHCP) by negotiating with its peers on the network. This module can also coexist with DHCP (dhcp.client), allowing your interface to be assigned both a link-local IP address and a DHCP-assigned IP address at the same time.
Networking manager. Daemon to negotiate the PPPoE session. Photon TCP/IP and dialup conguration tool. Photon modem dialer.
May 8, 2009
221
/etc/autoconnect
/etc/autoconnect
Our autoconnect feature automatically sets up a connection to your ISP whenever a TCP/IP application is started. For example, suppose you want to start a dialup connection to the Internet. When your Web browser is started, it will pause and the /etc/autoconnect script will automatically dial your ISP. The browser will resume when the PPP session is established. For more information, see the entry for /etc/autoconnect in the Utilities Reference.
Tag Get/Set
File system
Control apps
Many embedded servers force the user to relink the server in order to add pages, which compromises reliability as vendor and user code compete in a shared memory space. Despite its size, our embedded web server provides enough functionality to support accessing generated (dynamic) HTML via CGI or SSI.
CGI method
The embedded web server supports the Common Gateway Interface (CGI) 1.1, a readily available means of handling dynamic data. The downside of CGI is that its resource-heavy because an interpreted language is often involved.
222
May 8, 2009
SSI method
With SSI (Server Side Includes), a type of command language that can be embedded in HTML les, you can add dynamic content to your HTML. For example, the embedded server can: execute utilities at user-dened points in an HTML document (the output of these utilities can be optionally placed in the document). insert contents of other HTML les at a user-dened point. handle conditional statements (if, break, goto) so you can dene what parts of an HTML le are transmitted. Note that SSI tags are available to interact with a data server.
May 8, 2009
223
May 8, 2009
225
An OS for HA
If you had to design an HA-capable OS from the ground up, would you start with a single executable environment? In this simple, high-performance design, all OS components, device drivers, applications, the works, would all run without memory protection in kernel mode. On second thought, maybe such an OS wouldnt be suited for HA, simply because if a single software component were to fail, the entire system would crash. And if you wanted to add a software component or otherwise modify the HA system, youd have to take the system out of service to do so. In other words, the conventional realtime executive architecture wasnt built with HA in mind. Suppose, then, that you base your HA-enabled OS on a separation of kernel space and user space, so that all applications would run in user mode and enjoy memory protection. Youd even be able to upgrade an application without incurring any downtime. So far so good, but what would happen if a device driver, lesystem manager, or other essential OS component were to crash? Or what if you needed to add a new driver to a live system? Youd have to rebuild and restart the kernel. Based on such a monolithic kernel architecture, your HA system wouldnt be as available as it should be.
Inherent HA
A true microkernel that provides full memory protection is inherently the most stable OS architecture. Very little code is running in kernel mode that could cause the kernel itself to fail. And individual processes, whether applications or OS services, can be started and stopped dynamically, without jeopardizing system uptime. QNX Neutrino inherently provides several key features that are well-suited for HA systems: System stability through full memory protection for all OS and user processes. Dynamic loading and unloading of system components (device drivers, lesystem managers, etc.).
May 8, 2009
227
Client library
Separation of all software components for simpler development and maintenance. While any claims regarding ve nines availability on the part of an OS must be viewed only in the context of the entire hardware/software HA system, one can always ask whether an OS truly has the appropriate underlying architecture capable of supporting HA.
HA-specic modules
Apart from its inherently robust architecture, Neutrino also provides several components to help developers simplify the task of building and maintaining effective HA systems: HA client-side library cover functions that allow for automatic and transparent recovery mechanisms for failed server connections. HA Manager a smart watchdog that can perform multistage recovery whenever system services or processes fail.
Client library
The HA client-side library provides a drop-in enhancement solution for many standard C Library I/O operations. The HA librarys cover functions allow for automatic and transparent recovery mechanisms for failed connections that can be recovered from in an HA scenario. Note that the HA library is both thread-safe and cancellation-safe. The main principle of the client library is to provide drop-in replacements for all the message-delivery functions (i.e. MsgSend*). A client can select which particular connections it would like to make highly available, thereby allowing all other connections to operate as ordinary connections (i.e. in a non-HA environment). Normally, when a server that the client is talking to fails, or if theres a transient network fault, the MsgSend* functions return an error indicating that the connection ID (or le descriptor) is stale or invalid (e.g. EBADF). But in an HA-aware scenario, these transient faults are recovered from almost immediately, thus making the services available again.
228
May 8, 2009
Client library
Recovery example
The following example demonstrates a simple recovery scenario, where a client opens a le across a network le system. If the NFS server were to die, the HA Manager would restart it and remount the lesystem. Normally, any clients that previously had les open across the old connection would now have a stale connection handle. But if the client uses the ha_attach functions, it can recover from the lost connection. The ha_attach functions allow the client to provide a custom recovery function thats automatically invoked by the cover-function library. This recovery function could simply reopen the connection (thereby getting a connection to the new server), or it could perform a more complex recovery (e.g. adjusting the le position offsets and reconstructing its state with respect to the connection). This mechanism thus lets you develop arbitrarily complex recovery scenarios, while the cover-function library takes care of the details (detecting a failure, invoking recovery functions, and retransmitting state information).
#include #include #include #include #include #include #include #include <stdio.h> <string.h> <stdlib.h> <unistd.h> <sys/stat.h> <fcntl.h> <errno.h> <ha/cover.h>
#define TESTFILE "/net/machine99/home/test/testfile" typedef struct handle { int nr; int curr_offset; } Handle ; int recover_conn(int oldfd, void *hdl) { int newfd; Handle *thdl; thdl = (Handle *)hdl; newfd = ha_reopen(oldfd, TESTFILE, O_RDONLY); if (newfd >= 0) { // adjust file offset to previously known point lseek(newfd, thdl->curr_offset, SEEK_SET); // increment our count of successful recoveries (thdl->nr)++; } return(newfd); } int main(int argc, char *argv[]) { int status; int fd; int fd2; Handle hdl; char buf[80]; hdl.nr = 0; hdl.curr_offset = 0; // open a connection
May 8, 2009
229
// recovery will be using "recovery_conn", and "hdl" will // be passed to it as a parameter fd = ha_open(TESTFILE, O_RDONLY, recover_conn, (void *)&hdl, 0); if (fd < 0) { printf("could not open file\n"); exit(-1); } status = read(fd,buf,15); if (status < 0) { printf("error: %s\n",strerror(errno)); exit(-1); } else { hdl.curr_offset += status; } fd2 = ha_dup(fd); // fs-nfs3 fails, and is restarted, the network mounts // are re-instated at this point. // Our previous "fd" to the file is stale sleep(18); // reading from dup-ped fd // will fail, and will recover via recover_conn status = read(fd,buf,15); if (status < 0) { printf("error: %s\n",strerror(errno)); exit(-1); } else { hdl.curr_offset += status; } printf("total recoveries, %d\n",hdl.nr); ha_close(fd); ha_close(fd2); exit(0); }
Since the cover-function library takes over the lowest MsgSend*() calls, most standard library functions (read(), write(), printf(), scanf(), etc.) are also automatically HA-aware. The library also provides an ha-dup() function, which is semantically equivalent to the standard dup() function in the context of HA-aware connections. You can replace recovery functions during the lifetime of a connection, which greatly simplies the task of developing highly customized recovery mechanisms.
230
May 8, 2009
could be simply a single node or a collection of nodes connected via Qnet. The HAM can monitor specic processes and can control the behavior of the system when specic components fail and need to be recovered. The HAM also allows external detectors to detect and report interesting events to the system, and can associate actions with the occurrence of those events. In many HA systems, each single points of failure (SPOF) must be identied and dealt with carefully. Since the HAM maintains information about the health of the system and also provides the basic recovery framework, the HAM itself must never become a SPOF.
HAM hierarchy
HAM consists of three main components: Entities Conditions Actions
Entities
Entities are the fundamental units of observation/monitoring in the system. Essentially, an entity is a process (pid). As processes, all entities are uniquely identiable by their pids. Associated with each entity is a symbolic name that can be used to refer to that specic entity. Again, the names associated with entities are unique across the system. Managers are currently associated with a node, so uniqueness rules apply to a node. As well see later, this uniqueness requirement is very similar to the naming scheme used in a hierarchical lesystem. There are three fundamental entity types: Self-attached entities (HA-aware components) processes that choose to send heartbeats to the HAM, which will then monitor them for failure. Self-attached
May 8, 2009
231
entities can, on their own, decide at exactly what point in their lifespan they want to be monitored, what conditions they want acted upon, and when they want to stop the monitoring. In other words, this is a situation where a process says, Do the following if I die. Externally attached entities (HA-unaware components) generic processes (including legacy components) in the system that are being monitored. These could be arbitrary daemons/service providers whose health is deemed important. This method is useful for the case where Process A says, Tell me when Process B dies but Process B neednt know about this at all. Global entity a place holder for matching any entity. The global entity can be used to associate actions that will be triggered when an interesting event is detected with respect to any entity on the system. The term global refers to the set of entities being monitored in the system, and allows a process to say things like, When any process dies or misses a heartbeat, do the following. The global entity is never added or removed, but only referred to. Conditions can be added to or removed from the global entity, of course, and actions can be added to or removed from any of the conditions.
Conditions
Conditions are associated with entities; a condition represents the entitys state. Condition
CONDDEATH CONDABNORMALDEATH
Description The entity has died. The entity has died an abnormal death. Whenever an entity dies, this condition is triggered by a mechanism that results in the generation of a core dump le. The entity that was being monitored is detaching. This ends the HAMs monitoring of that entity. An entity for whom a place holder was previously created (i.e. some process has subscribed to events relating to this entity) has joined the system. This is also the start of the HAMs monitoring of the entity. The entity missed sending a heartbeat message specied for a condition of high severity. The entity missed sending a heartbeat message specied for a condition of low The entity was restarted. This condition is true after the entity is successfully restarted.
CONDDETACH CONDATTACH
continued. . .
232
May 8, 2009
Condition
CONDRAISE
Description An externally detected condition is reported to the HAM. Subscribers can associate actions with these externally detected conditions. An entity reports a state transition to the HAM. Subscribers can associate actions with specic state transitions. This condition type matches any condition type. It can be used to associate the same actions with one of many conditions.
CONDSTATE
CONDANY
For the conditions listed above (except CONDSTATE, CONDRAISE, and CONDANY), the HAM is the publisher it automatically detects and/or triggers the conditions. For the CONDSTATE and CONDRAISE conditions, external detectors publish the conditions to the HAM. For all conditions, subscribers can associate with lists of actions that will be performed in sequence when the condition is triggered. Both the CONDSTATE and CONDRAISE conditions provide ltering capabilities, so subscribers can selectively associate actions with individual conditions based on the information published. Any condition can be associated as a wild card with any entity, so a process can associate actions with any condition in a specic entity, or even in any entity. Note that conditions are also associated with symbolic names, which also need to be unique within an entity.
Actions
Actions are associated with conditions. Actions are executed when the appropriate conditions are true with respect to a specic entity. The HAM API includes several functions for different kinds of actions: Action ham_action_restart() ham_action_execute() ham_action_notify_pulse() Description This action restarts the entity. Executes an arbitrary command (e.g. to start a process). Noties some process that this condition has occurred. This notication is sent using a specic pulse with a value specied by the process that wished to receive this notify message.
continued. . .
May 8, 2009
233
Action ham_action_notify_signal()
Description Noties some process that this condition has occurred. This notication is sent using a specic realtime signal with a value specied by the process that wished to receive this notify message. This is the same as ham_action_notify_pulse() above, except that the node name specied for the recipient of the pulse can be the fully qualied node name. This is the same as ham_action_notify_signal() above, except that the node name specied for the recipient of the signal can be the fully qualied node name. Lets you insert delays between consecutive actions in a sequence. You can also wait for certain names to appear in the namespace. Resets the heartbeat mechanism for an entity that had previously missed sending heartbeats and had triggered a missed heartbeat condition, but has now recovered. Reports this condition to a logging mechanism.
ham_action_notify_pulse_node()
ham_action_notify_signal_node()
ham_action_waitfor()
ham_action_heartbeat_healthy()
ham_action_log()
Actions are also associated with symbolic names, which are unique within a specic condition.
Alternate actions
What happens if an action itself fails? You can specify an alternate list of actions to be performed to recover from that failure. These alternate actions are associated with the primary actions through several ham_action_fail* functions: ham_action_fail_execute() ham_action_fail_notify_pulse() ham_action_fail_notify_signal() ham_action_fail_notify_pulse_node() ham_action_fail_notify_signal_node() ham_action_fail_waitfor() ham_action_fail_log()
234
May 8, 2009
State transitions
An entity can report its state transitions to the HAM, which maintains every entitys current state (as reported by the entity). The HAM doesnt interpret the meaning of the state value itself, nor does it try to validate the state transitions, but it can generate events based on transitions from one state to another. Components can publish transitions that they want the external world to know about. These states neednt necessarily represent a specic state the application uses internally for decision making. To notify the HAM of a state transition, components can use the ham_entity_condition_state() function. Since the HAM is interested only in the next state in the transition, this is the only information thats transmitted to the HAM. The HAM then triggers a condition state-change event internally, which other components can subscribe to using the ham_condition_state() API call (see below).
Other conditions
In addition to the above, components on the system can also publish autonomously detected conditions by using the ham_entity_condition_raise() API call. The component raising the condition can also specify a type, class, and severity of its choice, to allow subscribers further granularity in ltering out specic conditions to subscribe to. As a result of this call, the HAM triggers a condition-raise event internally, which other components can subscribe to using the ham_condition_raise() API call (see below).
May 8, 2009
235
the subscriber customize which of several possible published conditions theyre interested in.
HAM as a lesystem
Effectively, HAMs internal state is like a hierarchical lesystem, where entities are like directories, conditions associated with those entities are like subdirectories, and actions inside those conditions are like leaf nodes of this tree structure. HAM also presents this state as a read-only lesystem under /proc/ham. As a result, arbitrary processes can also view the current state (e.g. you can do ls /proc/ham). The /proc/ham lesystem presents a lot of information about the current state of the systems entities. It also provides useful statistics on heartbeats, restarts, and deaths, giving you a snapshot in time of the systems various entities, conditions, and actions.
Multistage recovery
HAM can perform a multistage recovery, executing several actions in a certain order. This technique is useful whenever strict dependencies exist between various actions in a sequence. In most cases, recovery requires more than a single restart mechanism in order to properly restore the systems state to what it was before a failure. For example, suppose youve started fs-nfs3 (the NFS lesystem) and then mounted a few directories from multiple sources. You can instruct HAM to restart fs-nfs3 upon failure, and also to remount the appropriate directories as required after restarting the NFS process. As another example, suppose io-pkt* (the network I/O manager) were to die. We can tell HAM to restart it and also to load the appropriate network drivers (and maybe a few more services that essentially depend on network services in order to function).
236
May 8, 2009
HAM API
The basic mechanism to talk to HAM is to use its API. This API is implemented as a library that you can link against. The library is thread-safe as well as cancellation-safe. To control exactly what/how youre monitoring, the HAM API provides a collection of functions, including: Function ham_action_control() ham_action_execute() ham_action_fail_execute() Description Perform control operations on an action object. Add an execute action to a condition. Add to an action an execute action that will be executed if the corresponding action fails. Insert a log message into the activity log. Add to an action a notify pulse action that will be executed if the corresponding action fails. Add to an action a node-specic notify pulse action that will be executed if the corresponding action fails. Add to an action a notify signal action that will be executed if the corresponding action fails. Add to an action a node-specic notify signal action that will be executed if the corresponding action fails. Add to an action a waitfor action that will be executed if the corresponding action fails. Get a handle to an action in a condition in an entity. Get a handle to an action in a condition in an entity, using a nodename. Free a previously obtained handle to an action in a condition in an entity. Reset a heartbeats state to healthy.
ham_action_fail_log() ham_action_fail_notify_pulse()
ham_action_fail_notify_pulse_node()
ham_action_fail_notify_signal()
ham_action_fail_notify_signal_node()
ham_action_fail_waitfor()
continued. . .
May 8, 2009
237
Function ham_action_log() ham_action_notify_pulse() ham_action_notify_pulse_node() ham_action_notify_signal() ham_action_notify_signal_node() ham_action_remove() ham_action_restart() ham_action_waitfor() ham_attach() ham_attach_node() ham_attach_self() ham_condition() ham_condition_control() ham_condition_handle() ham_condition_handle_node() ham_condition_handle_free() ham_condition_raise()
Description Insert a log message into the activity log. Add a notify-pulse action to a condition. Add a notify-pulse action to a condition, using a nodename. Add a notify-signal action to a condition. Add a notify-signal action to a condition, using a nodename. Remove an action from a condition. Add a restart action to a condition. Add a waitfor action to a condition. Attach an entity. Attach an entity, using a nodename. Attach an application as a self-attached entity. Set up a condition to be triggered when a certain event occurs. Perform control operations on a condition object. Get a handle to a condition in an entity. Get a handle to a condition in an entity, using a nodename. Free a previously obtained handle to a condition in an entity. Attach a condition associated with a condition raise condition thats triggered by an entity raising a condition. Remove a condition from an entity. Attach a condition associated with a state transition condition thats triggered by an entity reporting a state change. Connect to a HAM. Connect to a remote HAM.
ham_condition_remove() ham_condition_state()
ham_connect() ham_connect_nd()
continued. . .
238
May 8, 2009
Function ham_connect_node() ham_detach() ham_detach_name() ham_detach_name_node() ham_detach_self() ham_disconnect() ham_disconnect_nd() ham_disconnect_node() ham_entity() ham_entity_condition_raise() ham_entity_condition_state() ham_entity_control() ham_entity_handle() ham_entity_handle_node() ham_entity_handle_free() ham_entity_node() ham_heartbeat() ham_stop() ham_stop_nd() ham_stop_node() ham_verbose()
Description Connect to a remote HAM, using a nodename. Detach an entity from a HAM. Detach an entity from a HAM, using an entity name. Detach an entity from a HAM, using an entity name and a nodename. Detach a self-attached entity from a HAM. Disconnect from a HAM. Disconnect from a remote HAM. Disconnect from a remote HAM, using a nodename. Create entity placeholder objects in a HAM. Raise a condition. Notify the HAM of a state transition. Perform control operations on an entity object in a HAM. Get a handle to an entity. Get a handle to an entity, using a nodename. Free a previously obtained handle to an entity. Create entity placeholder objects in a HAM, using a nodename. Send a heartbeat to a HAM. Stop a HAM. Stop a remote HAM. Stop a remote HAM, using a nodename. Modify the verbosity of a HAM.
May 8, 2009
239
May 8, 2009
241
50%
20%
30%
Partition scheduler
Static partitions guarantee that processes get the resources specied by the system designer.
Typically, the main objective of competing resource partitioning systems is to divide a computer into a set of smaller computers that interact as little as possible; however, this approach is not very exible. Adaptive partitioning takes a much more exible view. QNX Neutrino partitions are adaptive because: you can change congurations at run time theyre typically xed at one conguration time the partition behavior auto-adapts to conditions at run time. For example: - free time is redistributed to other scheduler partitions - lesystems can bill time to clients with a mechanism that temporarily moves threads between time partitions As a result, adaptive partitions are less restrictive and much more powerful. In addition to being adaptive, time partitions allow you to easily model the fundamentally different behavior of CPU time when viewed as a resource. The separation of scheduler partitions also allows you to create a highly tuned implementation for each.
May 8, 2009
243
Why adaptive?
Why adaptive?
To provide realtime performance with guarantees against overloading, QNX Neutrino introduced adaptive partitioning. Rigid partitions work best in fairly static systems with little or no dynamic deployment of software. In dynamic systems, static partitions can be inefcient. For example, the static division of execution time between partitions can waste CPU time and introduce delays: If most of the partitions are idle, and one is very busy, the busy partition doesnt receive any additional execution time, while background threads in the other partitions waste CPU time.
244
May 8, 2009
If an interrupt is scheduled for a partition, it has to wait until the partition runs. This can cause unacceptable latency, especially if bursts of interrupts occur. You can introduce adaptive partitioning without changing or even recompiling your application code, although you do have to rebuild your systems OS image. An adaptive partition is a set of threads that work on a common or related goal or activity. Like a static partition, an adaptive partition has a budget allocated to it that guarantees its minimum share of the CPUs resources. Unlike a static partition, an adaptive partition: isnt locked to a xed set of code in a static partition; you can dynamically add and congure adaptive partitions, as required Theres a limit to the number of partitions. For the thread scheduler, theres a maximum of eight scheduler partitions because for every scheduling operation, the thread scheduler must examine every partition before it can pick a thread on which to run. That may occur 50000 times per second on a 700 MHz x86 (i.e. a slow machine). So its important to limit the number of scheduler partitions to keep the scheduler overhead to a minimum. behaves as a global hard realtime thread scheduler under normal load, but can still provide minimal interrupt latencies even under overload conditions maximizes the usage of the CPUs resources. In the case of the thread scheduler, it distributes a partitions unused budget among partitions that require extra resources when the system isnt loaded.
May 8, 2009
245
Partition inheritance
Another important feature of adaptive partitioning is the concept of partition inheritance. For the thread scheduler, this feature lets designers develop server processes that run with no (or minimal) budget. When the server performs requests from clients, the client partition is billed for the time. Without this feature, CPU budget would be allocated to a server regardless of how much or often its used. The benets of these features include: You dont have to over-engineer the system, so the overall cost decreases. If you add an application, you dont have to re-engineer the budget of common services, such as lesystems or servers. The system is faster and more responsive to the user. The system guarantees time for important tasks. You can use priorities to specify a processs urgency, and a partitions CPU budget to specify its importance.
246
May 8, 2009
This is largely due to the fact that theres no effective way to budget CPU use across these groups. Thread priorities provide a way to ensure that critical tasks run, but dont provide guaranteed CPU time for important, noncritical tasks, which can be starved in normal operations. In addition, a common approach to establishing thread priorities is difcult to scale across a large development team. Adaptive partitioning using the thread scheduler lets architects maintain a reserve of resources for emergency purposes, such as a disaster-recovery system, or a eld-debugging shell, and dene high-level CPU budgets per subsystem, allowing development groups to implement their own priority schemes and optimizations within a given budget. This approach lets design groups develop subsystems independently and eases the integration effort. The net effect is to improve time-to-market and facilitate product scaling.
Providing security
Many systems are vulnerable to Denial of Service (DOS) attacks. For example, a malicious user could bombard a system with requests that need to be processed by one process. When under attack, this process overloads the CPU and effectively starves the rest of the system.
99%
0% 0%
DOS attack!
0% 0%
0%
0%
1%
Operating System
Without adaptive partitioning, a DOS attack on one process can starve other critical functions.
Some systems try to overcome this problem by implementing a monitor process that detects CPU utilization and invokes corrective actions when it deems that a process is using too much CPU. This approach has a number of drawbacks, including: Response time is typically slow. This approach caps the CPU usage in times when legitimate processing is required.
May 8, 2009
247
It isnt infallible or reliable; it depends on appropriate thread priorities to ensure that the monitor process obtains sufcient CPU time. The thread scheduler can solve this problem. The thread scheduler can provide separate budgets to the systems various functions. This ensures that the system always has some CPU capacity for important tasks. Threads can change their own priorities, which can be a security hole, but you can congure the thread scheduler to prevent code running in a partition from changing its own budget.
60% 20%
DOS attack!
15%
5%
Operating System
Since adaptive partitioning can allocate any unused CPU time to partitions that require it, it doesnt unnecessarily cap control-plane activity when theres a legitimate need for increased processing.
Debugging
Adaptive partitioning can even make debugging an embedded system easierduring development or deploymentby providing an emergency door into the system. Simply create a partition that you can run diagnostic tools in; if you dont need to use the partition, the thread scheduler allocates its budget among the other partitions. This provides you with access to the system without compromising its performance. For more information, see the Testing and Debugging chapter of the Adaptive Partitioning Users Guide.
248
May 8, 2009
The thread scheduler has been designed on top of the core QNX Neutrino architecture primarily to solve these problems in embedded systems design: guaranteeing a specied minimum share of CPU time when the system is overloaded preventing unimportant or untrusted applications from monopolizing the system For more information, see the Adaptive Partitioning Users Guide.
May 8, 2009
249
May 8, 2009
251
A graphical microkernel
A graphical microkernel
This chapter provides an overview of the Photon microGUI, the graphical environment for QNX Neutrino. For more information, see the Photon documentation set. Many embedded systems require a UI so that users can access and control the embedded application. For complex applications, or for maximum ease of use, a graphical windowing system is a natural choice. However, the windowing systems on desktop PCs simply require too much in the way of system resources to be practical in an embedded system where memory and cost are limiting factors. Drawing upon the successful approach of the QNX Neutrino microkernel architecture to achieve a POSIX OS environment for embedded systems, we have followed a similar course in creating the Photon microGUI windowing system. To implement an effective microkernel OS, we rst had to tune the microkernel so that the kernel calls for IPC were as lean and efcient as possible (since the performance of the whole OS rests on this message-based IPC). Using this low-overhead IPC, we were able to structure a GUI as a graphical microkernel process with a team of cooperating processes around it, communicating via that IPC. While at rst glance this might seem similar to building a graphical system around the classic client/server paradigm already used by the X Window System, the Photon architecture differentiates itself by restricting the functionality implemented within the graphical microkernel (or server) itself and distributing the bulk of the GUI functionality among the cooperating processes. The Photon microkernel runs as a tiny process, implementing only a few fundamental primitives that external, optional processes use to construct the higher-level functionality of a windowing system. Ironically, for the Photon microkernel itself, windows do not exist. Nor can the Photon microkernel draw anything, or manage a pen, mouse, or keyboard. To manage a GUI environment, Photon creates a 3-dimensional event space and connes itself only to handling regions and processing the clipping and steering of various events as they ow through the regions in this event space. This abstraction is roughly parallel to the concept of a microkernel OS being incapable of lesystem or device I/O, but relying instead on external processes to provide these high-level services. Just as this allows a microkernel OS to scale up or down in size and functionality, so also a microkernel GUI. The core microkernel abstraction implemented by the Photon microkernel is that of a graphical event space that other processes can populate with regions. Using native IPC to communicate with the Photon microkernel, these other processes manipulate their regions to provide higher-level graphical services or to act as user applications. By removing service-providing processes, Photon can be scaled down for limited-resource systems; by adding service-providing processes, Photon can be scaled up to full desktop functionality.
May 8, 2009
253
Photon regions.
We can think of these events that travel through this space as photons (from which this windowing system gets its name). Events themselves consist of a list of rectangles with some attached data. As these events ow through the event space, their rectangle lists intersect the regions placed there by various processes (applications). Events traveling away from the root region of the event space are said to be traveling outwards (or towards the user), while events from the user are said to be traveling inwards towards the root region at the back of the event space. The interaction between events and regions is the basis for the input and output facilities in Photon. Pen, mouse, and keyboard events travel away from the user towards the root plane. Draw events originate from regions and travel towards the device plane and the user.
254
May 8, 2009
Regions
Regions are managed in a hierarchy associated as a family of rectangles that dene their location in the 3-dimensional event space. A region also has attributes that dene how it interacts with various classes of events as they intersect the region. The interactions a region can have with events are dened by two bitmasks: sensitivity bitmask opaque bitmask. The sensitivity bitmask uses specic event types to dene which intersections the process owning the region wishes to be informed of. A bit in the sensitivity bitmask denes whether or not the region is sensitive to each event type. When an event intersects a region for which the bit is set, a copy of that event is enqueued to the application process that owns the region, notifying the application of events traveling through the region. This notication doesnt modify the event in any way. The opaque bitmask is used to dene which events the region can or cant pass through. For each event type, a bit in the opaque mask denes whether or not the region is opaque or transparent to that event. The optical property of opaqueness is implemented by modifying the event itself as it passes through the intersection. These two bitmasks can be combined to accomplish a variety of effects in the event space. The four possible combinations are: Bitmask combination: Not sensitive, transparent Not sensitive, opaque Description: The event passes through the region, unmodied, without the region owner being notied. The process owning the region simply isnt interested in the event. The event is clipped by the region as it passes through; the region owner isnt notied. For example, most applications would use this attribute combination for draw event clipping, so that an applications window wouldnt be overwritten by draw events coming from underlying windows. A copy of the event is sent to the region owner; the event then continues, unmodied, through the event space. A process wishing to log the ow of events through the event space could use this combination. A copy of the event is sent to the region owner; the event is also clipped by the region as it passes through. By setting this bitmask combination, an application can act as an event lter or translator. For every event received, the application can process and regenerate it, arbitrarily transformed in some manner, possibly traveling in a new direction, and perhaps sourced from a new coordinate in the event space. For example, a region could absorb pen events, perform handwriting recognition on those events, and then generate the equivalent keystroke events.
Sensitive, transparent
Sensitive, opaque
May 8, 2009
255
Events
Like regions, events also come in various classes and have various attributes. An event is dened by: an originating region a type a direction an attached list of rectangles some event-specic data (optional). Unlike most windowing systems, Photon classies both input (pen, mouse, keyboard, etc.) and output (drawing requests) as events. Events can be generated either from the regions that processes have placed in the event space or by the Photon microkernel itself. Event types are dened for: keystrokes pen and mouse button actions pen and mouse motion region boundary crossings expose and covered events draw events drag events drag-and-drop events. Application processes can either poll for these events, block and wait for them to occur, or be asynchronously notied of a pending event. The rectangle list attached to the event can describe one or more rectangular regions, or it can be a point-source a single rectangle where the upper-left corner is the same as the lower-right corner. When an event intersects a region that is opaque to it, that regions rectangle is clipped out of the events list of rectangles such that the list describes only the portion of the event that would ultimately be visible. The best way to illustrate how this clipping is performed is to examine the changes in the rectangle list of a draw event as it passes through various intersecting regions. When the draw event is rst generated, the rectangle list consists of only a single, simple rectangle describing the region that the event originated from. If the event goes through a region that clips the upper-left corner out of the draw event, the rectangle list is modied to contain only the two rectangles that would dene the area remaining to be drawn. These resulting rectangles are called tiles.
256
May 8, 2009
Graphics drivers
Likewise, every time the draw event intersects a region opaque to draw events, the rectangle list will be modied to represent what will remain visible of the draw event after the opaque region has been clipped out. Ultimately, when the draw event arrives at a graphics driver ready to be drawn, the rectangle list will precisely dene only the portion of the draw event that is to be visible.
Region C Region D Region A
Tile 1
Tile 2
Tile 3 Tile 4
Region B
If the event is entirely clipped by the intersection of a region, the draw event will cease to exist. This mechanism of opaque windows modifying the rectangle list of a draw event is how draw events from an underlying region (and its attached process) are properly clipped for display as they travel towards the user.
Graphics drivers
Graphics drivers are implemented as processes that place a region at the front of the event space. Rather than inject pen, mouse, or keyboard events (as would an input driver), a graphics drivers region is sensitive to draw events coming out of the event space. As draw events intersect the region, those events are received by the graphics driver process. In effect, the region can be imagined to be coated in phosphor, which is illuminated by the impact of photons. Since the Photon drawing API accumulates draw requests into batches emitted as single draw events, each draw event received by the driver contains a list of individual graphical primitives to be rendered. By the time the draw event intersects the graphics driver region, its rectangle list will also contain a clip list describing exactly which portions of the draw list are to be rendered to the display. The drivers job is to transform this clipped draw list into a visual representation on whatever graphics hardware the driver is controlling. One advantage of delivering a clip list within the event passed to the driver is that each draw request then represents a signicant batch of work. As graphics hardware advances, more and more of this batch of work can be pushed directly into the graphics hardware. Many display controller chips already handle a single clip rectangle; some handle multiple clip rectangles.
May 8, 2009
257
Graphics drivers
Although using the OS IPC services to pass draw requests from the application to the graphics driver may appear to be an unacceptable overhead, our performance measurements demonstrate that this implementation performs as well as having the application make direct calls into a graphics driver. One reason for such performance is that multiple draw calls are batched with the event mechanism, allowing the already minimal overhead of our lightweight IPC to be amortized over many draw calls.
Color model
Colors processed by the graphics drivers are dened by a 24-bit RGB quantity (8 bits for each of red, green, and blue), providing a total range of 16,777,216 colors. Depending on the actual display hardware, the driver will either invoke the 24-bit color directly from the underlying hardware or map it into the color space supported by the less-capable hardware. Since the graphics drivers use a hardware-independent color representation, applications can be displayed without modications on hardware that has varied color models. This allows applications to be dragged from screen to screen, without concern for what the underlying display hardwares color model might be.
258
May 8, 2009
Font support
Font support
Photon uses Bitstreams Font Fusion object-oriented font engine, which provides developers with full font delity and high-quality typographic output at any resolution on any device, while maintaining the integrity of the original character shapes. Photon is shipped with a limited number of TrueType fonts. These industry-standard fonts are readily available from various sources.
Stroke-based fonts
To support Asian languages (e.g. Chinese, Japanese, and Korean), Photon relies on Bitstreams stroke-based fonts. These high-speed fonts are ideal for memory-constrained environments. For example, a complete traditional Chinese font with over 13,000 characters can occupy as much as 8M in a conventional desktop system a stroke-based version of the same font occupies less than 0.5M! Apart from their compact size and fast rasterization, these fonts are also fully scalable, which makes them perfect for various nondesktop displays such as LCDs, TVs, PDAs, and so on.
UTF-8 encoding
Formerly known as UTF-2, the UTF-8 (for 8-bit form) transformation format is designed to address the use of Unicode character data in 8-bit UNIX environments. Here are some of the main features of UTF-8: Unicode characters from U+0000 to U+007E (ASCII set) map to UTF-8 bytes 00 to 7E (ASCII values). ASCII values dont otherwise occur in a UTF-8 transformation, giving complete compatibility with historical lesystems that parse for ASCII bytes. The rst byte indicates the number of bytes to follow in a multibyte sequence, allowing for efcient forward parsing.
May 8, 2009
259
Animation support
Finding the start of a character from an arbitrary location in a byte stream is efcient, because you need to search at most four bytes backwards to nd an easily recognizable initial byte. For example: isInitialByte = ((byte & 0xC0)
!= 0x80);
UTF-8 is reasonably compact in terms of the number of bytes used for encoding. UTF-8 strings are terminated with a single NULL byte, like traditional ASCII C strings.
Animation support
Photon provides icker-free animations by employing off-screen video memory where possible. For instance, a special container widget (PtOSContainer) creates a dedicated off-screen memory context for drawing images. The PtOSContainer widget uses a block of video memory large enough to hold an image the size of its canvas. (For more information about widgets, see the section Widget library in this chapter.) Photons graphics drivers also maximize the use of off-screen memory to enhance the perceptual performance of animated images. The graphics drivers support other advanced techniques, such as direct graphics mode, alpha-blending, chroma-key substitution, and more.
Video overlay
Besides the ability to superimpose a semi-transparent image on top of a background (alpha-blending) or to place a color-masked foreground image on top of a separate background (chroma-key), Photon also supports video overlay a full-motion video image is rendered within a window on the display.
Layers
Some display controllers let you transparently overlay multiple screens on a single display. Each overlay is called a layer. You can use layers to combine independent display elements. Since the graphics hardware performs the overlaying, this can be more efcient than rendering all of the display elements onto a single layer. For example, a fast navigational display can have a scrolling navigational map on a background layer and a web browser or other popup GUI element on a foreground layer. The images on all the active layers of a display are combined, using alpha-blending, chroma-keying, or both, to produce the nal image on the display.
Printing support
Photon provides built-in printing support for a variety of outputs, including: bitmap les
260
May 8, 2009
PostScript Hewlett-Packard PCL Epson ESC/P2 Epson IJS Canon Lexmark Photon also comes with a print-selection widget/convenience dialog to make printing simpler within developers own applications.
Widget library
Photon provides a library of components known as widgets objects that can manage much of their on-screen behavior automatically, without explicit programming effort. As a result, a complete application can be quickly assembled by combining widgets in various ways and then attaching C code to the appropriate callbacks the widgets provide. The Photon Application Builder (PhAB), which is included as part of the Photon development system, provides an extensive widget palette in its visual development environment. Photon provides a wide range of widgets, which can be classied as follows: fundamental widgets (e.g. a button) container widgets (e.g. a window widget) advanced widgets (e.g. an HTML display widget).
May 8, 2009
261
Widget library
Fundamental widgets
Label widget (PtLabel)
The label widget can display bitmaps, images, or textual information. The PtLabel widget is the superclass for all text-based widgets, providing many customizable attributes (e.g. font typeface, pop-up balloons, colors, borders, alignment, margins, etc.), all of which are inherited by all its subclasses.
Push buttons are a necessary component in every windowing system. They have a raised look that changes to depressed when pushed, giving a visual cue to let the user know the button has been selected. In addition to this visual behavior, push buttons automatically invoke an application-dened callback when theyre selected.
Photon provides two text-input widgets: a simple single-line input widget (PtText) commonly used in forms a powerful wordprocessor-like multi-line widget (PtMultiText) providing full editing capabilities, word wrapping, automatic scrolling, and multi-font line segments.
Toggle buttons are objects that display two states on or off. Photon provides various styles of toggle buttons, each with a different visual appearance. Toggle buttons are used to display or request state information related to a command or action about to be performed.
262
May 8, 2009
Widget library
Graphical widgets (PtArc, PtPixel, PtRect, PtLine, PtPolygon, PtEllipse, PtBezier, PtGrid)
Photon has no shortage of graphical widgets. Theres a widget to accomplish everything from simple lines and rectangles to complex multi-segmented B zier e curves. Graphical widgets provide attributes for color, lls, patterns, line thickness, joins, and much more.
A scrollbar widget is used to scroll the display of a viewable area. The scrollbar is combined with other widgets (e.g. PtList, PtScrollContainer) to allow scrolling.
The separator widget is used to separate two or more different areas, such as the menu items as shown in this example. The separator can be customized for many different styles and looks.
May 8, 2009
263
Widget library
Sliders are different from scrollbars. A scrollbar denes a range, whereas a slider denes a single value.
Photon supports every major graphics le standard, so you can import graphics and display them inside widgets. Many Photon widgets directly support displaying graphics the most common are PtButton for making push-button toolbars and PtLabel for displaying images.
If an application needs to do something that takes a fair amount of time (e.g. loading a le), it can use the progress bar widget to let the user know whats happening and, more importantly, how much longer the process is going to take. The progress bar has many attributes for customization it can be horizontal or vertical, it can display specic or indeterminate values (both are shown here), etc.
The PtNumericInteger class lets the user specify integer values between given minimum and maximum values. The PtNumericFloat class lets the user enter oating-point values.
Container widgets
A container widget is a powerful and convenient interface tool that holds other widgets and manages their layout. Containers are used extensively in most Photon applications.
264
May 8, 2009
Widget library
Windows are the main application containers. The main UI components (menu bars, toolbars, etc.) appear with the window widget. The widget automatically handles all the necessary interactions with the Photon Window Manager (PWM) all you need to specify is what should and shouldnt be rendered or managed.
The group widget is a very powerful widget that manages the geometry of all its child widgets. It can align the widgets horizontally, vertically, or in a matrix. The widget also provides attributes that let you specify whether the children should be stretched to t the group if its resized larger due to anchoring.
The panel group widget is a container for panels, a useful element for organizing data in dialogs. Besides managing the geometry and layout of panels, PtPanelGroup also
May 8, 2009
265
Widget library
provides two selection modes to switch between panels: multiple-tab selection (each panel has its own tab to select) and single-tab selection (clicking the tab pops up a menu to select other panels).
The PtScrollContainer widget is a very powerful widget that provides a viewport into a potentially larger container. You can place any number of widgets inside a scroll container and it will automatically display a scrollbar if the widgets are contained within the viewable area. PtScrollContainer widgets could be used to implement a text le viewer, wordprocessor, customized list display, and so on. To scroll child widgets quickly, the scrolling area widget uses a hardware blitter (provided the underlying graphics driver supports it).
The background widget provides a way to create fancy background displays, from simple color gradations to tiled textures. This widget can handle just about any background requirement.
Advanced widgets
Armed with Photons rich set of widgets, developers can build practically any graphical application imaginable. Here are some of the more powerful widgets at your disposal:
266
May 8, 2009
Widget library
Photon has a widget for every menu-related requirement. Theres a widget to simplify the creation of a standard menu bar. The menu widget handles the pop-up display, press-drag-release, point and click, keyboard traversal, and selection of menu items. The menu button widget is used for creating individual menu items.
This container holds buttons, labels, images, whatever widgets you wish, and aligns them either vertically or horizontally in a toolbar. The toolbar group widget lets you combine a toolbar with a menu bar to create a very exible access element for your applications.
The list widget is a very powerful widget that manages a list of items. It provides many different selection modes, including single selection, multiple selection and range selection. The list widget also supports multi-columned lists through the use of a divider widget (PtDivider).
The pulldown list widget combines the PtText widget (for text input) with a pulldown button for displaying a list widget. When the user selects from the list, the text widget is automatically updated with the current selection. The pulldown list widget is very useful for displaying a list of items using a small space. Dialogs and containers use a lot less screen real-estate, which is important in embedded environments.
May 8, 2009
267
Widget library
The tree widget is similar to the list widget in fact they both have the same ancestors. The main difference is that the tree widget displays the items in a hierarchical manner. Items, called branches, can be expanded or collapsed; any number of tree branches can be created. Each branch can dene its own unique image to display. Trees are useful because they display information in a very logical manner. Photon applications that use the tree widget include the File Manager (directory display), PhAB (widget layout), Helpviewer, and many others.
A terminal widget creates and manages an entire text-mode terminal inside a widget. Just drop it into your application and youve created your very own pterm (Photons terminal application). The terminal widget doesnt stop there it also provides complete cut-and-paste functionality and quick-launch help by highlighting any text within the widget.
268
May 8, 2009
Widget library
This powerful widget manages its children in a unique and useful way. When you place two or more widgets inside a divider widget, it automatically puts little separators in between the child widgets. Using these separators, the user can drag back and forth, causing the child widgets on either side of the separator to be resized. This is very useful for creating resizable column headings for lists. In fact, if you drop a divider widget into a list widget, it will automatically turn your simple list into a resizable multi-column list. Dividers arent limited to just labels or buttons. Any widgets can be placed inside to create side-by-side resizable trees, scroll areas, and so on.
Realtime systems often require trend graphs. Photon comes with a trend bar widget, PtTrend, that supports the display of multiple trend lines simultaneously. If your graphics hardware supports masked blits, it can even smooth-scroll the trend across grid lines. The PtMTrend widget has additional features, such as a trace line, which make it appropriate for medical applications.
May 8, 2009
269
Widget library
Photon provides several handy controls for building color-selection dialogs. This convenient set of widgets includes PtColorPanel, a compound widget that provides several ways to easily select a color.
270
May 8, 2009
Widget library
The PtWebClient widget is used to start, interact, and control a web browser. The widget also provides a user-dened area within the application for the server to format and display web pages. The application controls the server by setting widget resources. The server communicates status information and user interaction back to the application using the widget callbacks. The PtWebClient widget transparently supports the version of HTML that the server supports.
Convenience functions
Once a widget has been created, you can take advantage of Photons convenience functions to easily set up dialogs and control the widget. Here are some examples of common dialogs created using the following convenience functions in Photons widget toolkit: PtFileSelection() create a le-selector dialog PtFontSelection() display a modal dialog for selecting a font PtPrintSelection() display a modal dialog for selecting print options PtAlert() display a message and request a response from the user PtNotice() display a message and wait for acknowledgment by the user PtPrompt() display a message and get textual input from the user. For more information about these functions, see the Photon Library Reference.
The PtFileSelection() function incorporates a tree widget that displays les, directories, links to les or directories, and custom entries. Besides selecting a particular le in response to an application prompt, users can also navigate an entire lesystem and choose their own le and directory.
May 8, 2009
271
Widget library
To accommodate the wide selection of fonts available, Photon provides a handy font-selector. The dialog displays a list of all available fonts and allows the user to choose the typeface and style (bold, italic, etc.) and to indicate whether the font should be anti-aliased.
The print selector lets a user select a printer or control its properties. The user may also select a range of pages as well as the number of copies to print.
272
May 8, 2009
This modal dialog is useful for informing the user of some signicant event and waiting for a response (e.g. clicking a button).
This dialog can display a message that may or may not require a response. This type of dialog often contains an OK button for the user to acknowledge the notice.
Like the alert dialog, the prompt dialog displays a message that requires the user to respond, but it also provides a eld for inputting text within the dialog.
May 8, 2009
273
Summary
Summary
Photon represents a new approach to GUI building using a microkernel and a team of cooperating processes, rather than the monolithic approach typied by other windowing systems. As a result, Photon exhibits a unique set of capabilities: Low memory requirements enable Photon to deliver a high level of windowing functionality to environments where only a graphics library might have been allowed within the memory constraints. Photon provides a very exible, user-extensible architecture that allows developers to extend the GUI in directions unique to their applications. With exible cross-platform connectivity, Photon applications can be used from virtually any connected desktop environment.
274
May 8, 2009
May 8, 2009
Chapter 17 Multimedia
275
Overview
Overview
The QNX Aviage multimedia suite consists of several packages, including the multimedia core package, codec packages that provide WMA9, MP3, and AAC decoding and encoding, and software packages that support iPod and PlaysForSure media players. The major component of the multimedia core package is the MultiMedia Engine (MME). The MME provides the main interfaces for conguring and controlling your multimedia applications. Designed to run on the QNX Neutrino OS, which can be installed on a wide variety of hardware platforms, the MME provides consumer-product developers a component-based solution that reduces the work required to develop and deliver multimedia products to their end customers.
HMI
QDB
MME API
mme
Output devices Output drivers DSP/ hardware codecs Media output
io-media
MME DB mediastores library playing ... Mediastores Software codecs
The MME is designed to simplify and speed development of end-user applications that require device and lesystem access, content synchronization, playback control, and audio and graphics delivery. It handles multiple clients, sessions and streams, and abstracts hardware and protocol dependencies from other functional areas. It provides integration with a wide variety of media sources, including those requiring Digital Rights Management (DRM), and provides a high-level API for media transport control, device control and browsing, and media library management; and it automatically detects media devices and integrates their contents into a general database view. The applications the MME can be used to develop include: transport media systems in-seat entertainment systems
May 8, 2009
Chapter 17 Multimedia
277
Overview
medical device imaging and sound monitoring units industrial control systems The MME lets Human-Machine Interface (HMI) developers apply their talents to designing the best possible user experience instead of focusing on managing the media. When you build a client application that uses the MME, you can focus on: designing and building the best possible user interface (HMI) implementing simple playback functionality such as track session creation, play, next, pause etc. conguring audio and video output You need to know about the congurations for your systems storage devices, but you can leave a long list of responsibilities to the MME: device and mediastore insertion and removal HDD, CD, DVD, USB key with media, etc. mediastore synchronization nd, itemize, extract, and manage media content and metadata input and output media connection management extensible support for specialized consumer devices, as well as for hardware ofoad to digital signal processors (DSPs) For a more in-depth description of the MME architecture, see Introduction to the MME.
Mediastore access
Mediastores are any source for multimedia data, including hard drives, DVDs, CDs and media devices such as iPods or MP3 players. The MMEs mediastore access capabilities include: detection of devices, and integration of content from static and dynamic media sources: drives, external players, USB stores, iPods, networks media stack and protocol implementations for diverse protocols: iPod Access Protocol, MTP, etc., many with DRM requirements management of different media lesystem and stream formats: DOS FAT32, UDF 2.01, etc.
278
Chapter 17 Multimedia
May 8, 2009
Overview
May 8, 2009
Chapter 17 Multimedia
279
Component-based architecture
The MME is comprised of several independent components. Each MME component executes independently as a Neutrino resource manager process. A resource manager is a user-level server process that accepts messages from other programs and, optionally, communicates with hardware. The MMEs component-based architecture delivers: exibility developers using the MME to build multimedia products can easily congure the MME and its individual resource managers to meet their specic needs. easy deployment multimedia applications build with the MME can be deployed on a single processor, or on a distributed network of processors with no changes to their application code. reliability the MMEs resource managers all have their own failure and restart; a resource manager failure doesnt mean a system failure. portability all MME components offer standard interfaces, such as POSIX or SQL.
280
Chapter 17 Multimedia
May 8, 2009
Updates
Content synchronization
Media devices
Both the mme and the qdb resource managers support device-specic functionality within a POSIX framework. Together they make up the interface to HMI client applications, providing them with an API to control, browse, copy or rip, and play media, as well as the ability to monitor and manage multimedia processing. The mme controls the low-level resource managers that directly access and process media data. Multimedia client applications dont normally interface with the MMEs lower-level resource managers. You may nonetheless nd it useful to know about these resource managers and understand what they do, especially if you are tuning your system conguration. The MMEs low-level resource managers include: the io-fs resource managers that provide access to media devices and mediastores the io-media resource managers that are responsible for media stream processing and rendering, for managing complex media streams, and for performing tasks that include CD and DVD playback, le playback, media copying, and media recording Other low-level resource managers that are not specic to the MME, but which the MME uses, include: the devb resource managers, such as devb-eide and devb-atapi, that provide access to data on block-oriented devices and lesystems the io-usb resource manager that provides access to media on USB stores io-audio, io-display, and other resource managers that provide an abstraction of media output devices
May 8, 2009
Chapter 17 Multimedia
281
Glossary
May 8, 2009
Glossary
283
A20 gate
On x86-based systems, a hardware component that forces the A20 address line on the bus to zero, regardless of the actual setting of the A20 address line on the processor. This component is in place to support legacy systems, but the QNX Neutrino OS doesnt require any such hardware. Note that some processors, such as the 386EX, have the A20 gate hardware built right into the processor itself our IPL will disable the A20 gate as soon as possible after startup.
adaptive
Scheduling algorithm whereby a threads priority is decayed by 1. See also FIFO, round robin, and sporadic.
adaptive partitioning
A method of dividing, in a exible manner, CPU time, memory, le resources, or kernel resources with some policy of minimum guaranteed usage.
atomic
Of or relating to atoms. :-) In operating systems, this refers to the requirement that an operation, or sequence of operations, be considered indivisible. For example, a thread may need to move a le position to a given location and read data. These operations must be performed in an atomic manner; otherwise, another thread could preempt the original thread and move the le position to a different location, thus causing the original thread to read data from the second threads position.
attributes structure
Structure containing information used on a per-resource basis (as opposed to the OCB, which is used on a per-open basis). This structure is also known as a handle. The structure denition is xed (iofunc_attr_t), but may be extended. See also mount structure.
bank-switched
A term indicating that a certain memory component (usually the device holding an image) isnt entirely addressable by the processor. In this case, a hardware component manifests a small portion (or window) of the device onto the processors address bus. Special commands have to be issued to the hardware to move the window to different locations in the device. See also linearly mapped.
May 8, 2009
Glossary
285
block-integral
The requirement that data be transferred such that individual structure components are transferred in their entirety no partial structure component transfers are allowed. In a resource manager, directory data must be returned to a client as block-integral data. This means that only complete struct dirent structures can be returned its inappropriate to return partial structures, assuming that the next _IO_READ request will pick up where the previous one left off.
bootable
An image can be either bootable or nonbootable. A bootable image is one that contains the startup code that the IPL can transfer control to.
bootle
The part of an OS image that runs the startup code and the Neutrino microkernel.
budget
In sporadic scheduling, the amount of time a thread is permitted to execute at its normal priority before being dropped to its low priority.
buildle
A text le containing instructions for mkifs specifying the contents and other details of an image, or for mkefs specifying the contents and other details of an embedded lesystem image.
286
Glossary
May 8, 2009
canonical mode
Also called edited mode or cooked mode. In this mode the character device library performs line-editing operations on each received character. Only when a line is completely entered typically when a carriage return (CR) is received will the line of data be made available to application processes. Contrast raw mode.
channel
A kernel object used with message passing. In QNX Neutrino, message passing is directed towards a connection (made to a channel); threads can receive messages from channels. A thread that wishes to receive messages creates a channel (using ChannelCreate()), and then receives messages from that channel (using MsgReceive()). Another thread that wishes to send a message to the rst thread must make a connection to that channel by attaching to the channel (using ConnectAttach()) and then sending data (using MsgSend()).
chid
An abbreviation for channel ID.
CIFS
Common Internet File System (aka SMB) a protocol that allows a client workstation to perform transparent le access over a network to a Windows 95/98/NT server. Client le access calls are converted to CIFS protocol requests and are sent to the server over the network. The server receives the request, performs the actual lesystem operation, and sends a response back to the client.
CIS
Card Information Structure a data block that maintains information about ash conguration. The CIS description includes the types of memory devices in the regions, the physical geometry of these devices, and the partitions located on the ash.
coid
An abbreviation for connection ID.
combine message
A resource manager message that consists of two or more messages. The messages are constructed as combine messages by the clients C library (e.g. stat(), readblock()), and then handled as individual messages by the resource manager. The purpose of combine messages is to conserve network bandwidth and/or to provide support for atomic operations. See also connect message and I/O message.
May 8, 2009
Glossary
287
connect message
In a resource manager, a message issued by the client to perform an operation based on a pathname (e.g. an io_open message). Depending on the type of connect message sent, a context block (see OCB) may be associated with the request and will be passed to subsequent I/O messages. See also combine message and I/O message.
connection
A kernel object used with message passing. Connections are created by client threads to connect to the channels made available by servers. Once connections are established, clients can MsgSendv() messages over them. If a number of threads in a process all attach to the same channel, then the one connection is shared among all the threads. Channels and connections are identied within a process by a small integer. The key thing to note is that connections and le descriptors (FD) are one and the same object. See also channel and FD.
context
Information retained between invocations of functionality. When using a resource manager, the client sets up an association or context within the resource manager by issuing an open() call and getting back a le descriptor. The resource manager is responsible for storing the information required by the context (see OCB). When the client issues further le-descriptor based messages, the resource manager uses the OCB to determine the context for interpretation of the clients messages.
cooked mode
See canonical mode.
core dump
A le describing the state of a process that terminated abnormally.
critical section
A code passage that must be executed serially (i.e. by only one thread at a time). The simplest from of critical section enforcement is via a mutex.
deadlock
A condition in which one or more threads are unable to continue due to resource contention. A common form of deadlock can occur when one thread sends a message to another, while the other thread sends a message to the rst. Both threads are now waiting for each other to reply to the message. Deadlock can be avoided by good design practices or massive kludges we recommend the good design approach.
288
Glossary
May 8, 2009
device driver
A process that allows the OS and application programs to make use of the underlying hardware in a generic way (e.g. a disk drive, a network interface). Unlike OSs that require device drivers to be tightly bound into the OS itself, device drivers for QNX Neutrino are standard processes that can be started and stopped dynamically. As a result, adding device drivers doesnt affect any other part of the OS drivers can be developed and debugged like any other application. Also, device drivers are in their own protected address space, so a bug in a device driver wont cause the entire OS to shut down.
DNS
Domain Name Service an Internet protocol used to convert ASCII domain names into IP addresses. In QNX native networking, dns is one of Qnets builtin resolvers.
dynamic bootle
An OS image built on the y. Contrast static bootle.
dynamic linking
The process whereby you link your modules in such a way that the Process Manager will link them to the library modules before your program runs. The word dynamic here means that the association between your program and the library modules that it uses is done at load time, not at linktime. Contrast static linking. See also runtime loading.
edge-sensitive
One of two ways in which a PIC (Programmable Interrupt Controller) can be programmed to respond to interrupts. In edge-sensitive mode, the interrupt is noticed upon a transition to/from the rising/falling edge of a pulse. Contrast level-sensitive.
edited mode
See canonical mode.
EOI
End Of Interrupt a command that the OS sends to the PIC after processing all Interrupt Service Routines (ISR) for that particular interrupt source so that the PIC can reset the processors In Service Register. See also PIC and ISR.
May 8, 2009
Glossary
289
EPROM
Erasable Programmable Read-Only Memory a memory technology that allows the device to be programmed (typically with higher-than-operating voltages, e.g. 12V), with the characteristic that any bit (or bits) may be individually programmed from a 1 state to a 0 state. To change a bit from a 0 state into a 1 state can only be accomplished by erasing the entire device, setting all of the bits to a 1 state. Erasing is accomplished by shining an ultraviolet light through the erase window of the device for a xed period of time (typically 10-20 minutes). The device is further characterized by having a limited number of erase cycles (typically 10e5 - 10e6). Contrast ash and RAM.
event
A notication scheme used to inform a thread that a particular condition has occurred. Events can be signals or pulses in the general case; they can also be unblocking events or interrupt events in the case of kernel timeouts and interrupt service routines. An event is delivered by a thread, a timer, the kernel, or an interrupt service routine when appropriate to the requestor of the event.
FD
File Descriptor a client must open a le descriptor to a resource manager via the open() function call. The le descriptor then serves as a handle for the client to use in subsequent messages. Note that a le descriptor is the exact same object as a connection ID (coid, returned by ConnectAttach()).
FIFO
First In First Out a scheduling algorithm whereby a thread is able to consume CPU at its priority level without bounds. See also adaptive, round robin, and sporadic.
ash memory
A memory technology similar in characteristics to EPROM memory, with the exception that erasing is performed electrically instead of via ultraviolet light, and, depending upon the organization of the ash memory device, erasing may be accomplished in blocks (typically 64k bytes at a time) instead of the entire device. Contrast EPROM and RAM.
FQNN
Fully Qualied NodeName a unique name that identies a QNX Neutrino node on a network. The FQNN consists of the nodename plus the node domain tacked together.
garbage collection
Aka space reclamation, the process whereby a lesystem manager recovers the space occupied by deleted les and directories.
290
Glossary
May 8, 2009
HA
High Availability in telecommunications and other industries, HA describes a systems ability to remain up and running without interruption for extended periods of time.
handle
A pointer that the resource manager base library binds to the pathname registered via resmgr_attach(). This handle is typically used to associate some kind of per-device information. Note that if you use the iofunc_*() POSIX layer calls, you must use a particular type of handle in this case called an attributes structure.
image
In the context of embedded QNX Neutrino systems, an image can mean either a structure that contains les (i.e. an OS image) or a structure that can be used in a read-only, read/write, or read/write/reclaim FFS-2-compatible lesystem (i.e. a ash lesystem image).
inherit mask
A bitmask that species which processors a threads children can run on. Contrast runmask.
interrupt
An event (usually caused by hardware) that interrupts whatever the processor was doing and asks it do something else. The hardware will generate an interrupt whenever it has reached some state where software intervention is required.
interrupt handler
See ISR.
interrupt latency
The amount of elapsed time between the generation of a hardware interrupt and the rst instruction executed by the relevant interrupt service routine. Also designated as Til . Contrast scheduling latency.
May 8, 2009
Glossary
291
I/O message
A message that relies on an existing binding between the client and the resource manager. For example, an _IO_READ message depends on the clients having previously established an association (or context) with the resource manager by issuing an open() and getting back a le descriptor. See also connect message, context, combine message, and message.
I/O privileges
A particular right, that, if enabled for a given thread, allows the thread to perform I/O instructions (such as the x86 assembler in and out instructions). By default, I/O privileges are disabled, because a program with it enabled can wreak havoc on a system. To enable I/O privileges, the thread must be running as root, and call ThreadCtl().
IPC
Interprocess Communication the ability for two processes (or threads) to communicate. QNX Neutrino offers several forms of IPC, most notably native messaging (synchronous, client/server relationship), POSIX message queues and pipes (asynchronous), as well as signals.
IPL
Initial Program Loader the software component that either takes control at the processors reset vector (e.g. location 0xFFFFFFF0 on the x86), or is a BIOS extension. This component is responsible for setting up the machine into a usable state, such that the startup program can then perform further initializations. The IPL is written in assembler and C. See also BIOS extension signature and startup code.
IRQ
Interrupt Request a hardware request line asserted by a peripheral to indicate that it requires servicing by software. The IRQ is handled by the PIC, which then interrupts the processor, usually causing the processor to execute an Interrupt Service Routine (ISR).
292
Glossary
May 8, 2009
ISR
Interrupt Service Routine a routine responsible for servicing hardware (e.g. reading and/or writing some device ports), for updating some data structures shared between the ISR and the thread(s) running in the application, and for signalling the thread that some kind of event has occurred.
kernel
See microkernel.
level-sensitive
One of two ways in which a PIC (Programmable Interrupt Controller) can be programmed to respond to interrupts. If the PIC is operating in level-sensitive mode, the IRQ is considered active whenever the corresponding hardware line is active. Contrast edge-sensitive.
linearly mapped
A term indicating that a certain memory component is entirely addressable by the processor. Contrast bank-switched.
message
A parcel of bytes passed from one process to another. The OS attaches no special meaning to the content of a message the data in a message has meaning for the sender of the message and for its receiver, but for no one else. Message passing not only allows processes to pass data to each other, but also provides a means of synchronizing the execution of several processes. As they send, receive, and reply to messages, processes undergo various changes of state that affect when, and for how long, they may run.
microkernel
A part of the operating system that provides the minimal services used by a team of optional cooperating processes, which in turn provide the higher-level OS functionality. The microkernel itself lacks lesystems and many other services normally expected of an OS; those services are provided by optional processes.
mount structure
An optional, well-dened data structure (of type iofunc_mount_t) within an iofunc_*() structure, which contains information used on a per-mountpoint basis (generally used only for lesystem resource managers). See also attributes structure and OCB.
mountpoint
The location in the pathname space where a resource manager has registered itself. For example, the serial port resource manager registers mountpoints for each serial
May 8, 2009
Glossary
293
device (/dev/ser1, /dev/ser2, etc.), and a CD-ROM lesystem may register a single mountpoint of /cdrom.
multicore system
A chip that has one physical processor with multiple CPUs interconnected over a chip-level bus.
mutex
Mutual exclusion lock, a simple synchronization service used to ensure exclusive access to data shared between threads. It is typically acquired (pthread_mutex_lock()) and released (pthread_mutex_unlock()) around the code that accesses the shared data (usually a critical section). See also critical section.
name resolution
In a QNX Neutrino network, the process by which the Qnet network manager converts an FQNN to a list of destination addresses that the transport layer knows how to get to.
name resolver
Program code that attempts to convert an FQNN to a destination address.
nd
An abbreviation for node descriptor, a numerical identier for a node relative to the current node. Each nodes node descriptor for itself is 0 (ND_LOCAL_NODE).
NDP
Node Discovery Protocol proprietary QNX Software Systems protocol for broadcasting name resolution requests on a QNX Neutrino LAN.
network directory
A directory in the pathname space thats implemented by the Qnet network manager.
Neutrino
Name of an OS developed by QNX Software Systems.
NFS
Network FileSystem a TCP/IP application that lets you graft remote lesystems (or portions of them) onto your local namespace. Directories on the remote systems appear as part of your local lesystem and all the utilities you use for listing and managing les (e.g. ls, cp, mv) operate on the remote les exactly as they do on your local les.
294
Glossary
May 8, 2009
NMI
Nonmaskable Interrupt an interrupt that cant be masked by the processor. We dont recommend using an NMI!
node domain
A character string that the Qnet network manager tacks onto the nodename to form an FQNN.
nodename
A unique name consisting of a character string that identies a node on a network.
nonbootable
A nonbootable OS image is usually provided for larger embedded systems or for small embedded systems where a separate, conguration-dependent setup may be required. Think of it as a second lesystem that has some additional les on it. Since its nonbootable, it typically wont contain the OS, startup le, etc. Contrast bootable.
OCB
Open Control Block (or Open Context Block) a block of data established by a resource manager during its handling of the clients open() function. This context block is bound by the resource manager to this particular request, and is then automatically passed to all subsequent I/O functions generated by the client on the le descriptor returned by the clients open().
package lesystem
A virtual lesystem manager that presents a customized view of a set of les and directories to a client. The real les are present on some medium; the package lesystem presents a virtual view of selected les to the client.
partition
A division of CPU time, memory, le resources, or kernel resources with some policy of minimum guaranteed usage.
pathname prex
See mountpoint.
May 8, 2009
Glossary
295
persistent
When applied to storage media, the ability for the medium to retain information across a power-cycle. For example, a hard disk is a persistent storage medium, whereas a ramdisk is not, because the data is lost when power is lost.
Photon microGUI
The proprietary graphical user interface built by QNX Software Systems.
PIC
Programmable Interrupt Controller hardware component that handles IRQs. See also edge-sensitive, level-sensitive, and ISR.
PID
Process ID. Also often pid (e.g. as an argument in a function call).
POSIX
An IEEE/ISO standard. The term is an acronym (of sorts) for Portable Operating System Interface the X alludes to UNIX, on which the interface is based.
preemption
The act of suspending the execution of one thread and starting (or resuming) another. The suspended thread is said to have been preempted by the new thread. Whenever a lower-priority thread is actively consuming the CPU, and a higher-priority thread becomes READY, the lower-priority thread is immediately preempted by the higher-priority thread.
prex tree
The internal representation used by the Process Manager to store the pathname table.
priority inheritance
The characteristic of a thread that causes its priority to be raised or lowered to that of the thread that sent it a message. Also used with mutexes. Priority inheritance is a method used to prevent priority inversion.
296
Glossary
May 8, 2009
priority inversion
A condition that can occur when a low-priority thread consumes CPU at a higher priority than it should. This can be caused by not supporting priority inheritance, such that when the lower-priority thread sends a message to a higher-priority thread, the higher-priority thread consumes CPU on behalf of the lower-priority thread. This is solved by having the higher-priority thread inherit the priority of the thread on whose behalf its working.
process
A nonschedulable entity, which denes the address space and a few data areas. A process must have at least one thread running in it this thread is then called the rst thread.
process group
A collection of processes that permits the signalling of related processes. Each process in the system is a member of a process group identied by a process group ID. A newly created process joins the process group of its creator.
process group ID
The unique identier representing a process group during its lifetime. A process group ID is a positive integer. The system may reuse a process group ID after the process group dies.
process ID (PID)
The unique identier representing a process. A PID is a positive integer. The system may reuse a process ID after the process dies, provided no existing process group has the same ID. Only the Process Manager can have a process ID of 1.
pty
Pseudo-TTY a character-based device that has two ends: a master end and a slave end. Data written to the master end shows up on the slave end, and vice versa. These devices are typically used to interface between a program that expects a character device and another program that wishes to use that device (e.g. the shell and the telnet daemon process, used for logging in to a system over the Internet).
pulses
In addition to the synchronous Send/Receive/Reply services, QNX Neutrino also supports xed-size, nonblocking messages known as pulses. These carry a small payload (four bytes of data plus a single byte code). A pulse is also one form of event that can be returned from an ISR or a timer. See MsgDeliverEvent() for more information.
May 8, 2009
Glossary
297
Qnet
The native network manager in QNX Neutrino.
QoS
Quality of Service a policy (e.g. loadbalance) used to connect nodes in a network in order to ensure highly dependable transmission. QoS is an issue that often arises in high-availability (HA) networks as well as realtime control systems.
RAM
Random Access Memory a memory technology characterized by the ability to read and write any location in the device without limitation. Contrast ash and EPROM.
raw mode
In raw input mode, the character device library performs no editing on received characters. This reduces the processing done on each character to a minimum and provides the highest performance interface for reading data. Also, raw mode is used with devices that typically generate binary data you dont want any translations of the raw binary stream between the device and the application. Contrast canonical mode.
replenishment
In sporadic scheduling, the period of time during which a thread is allowed to consume its execution budget.
reset vector
The address at which the processor begins executing instructions after the processors reset line has been activated. On the x86, for example, this is the address 0xFFFFFFF0.
resource manager
A user-level server program that accepts messages from other programs and, optionally, communicates with hardware. QNX Neutrino resource managers are responsible for presenting an interface to various types of devices, whether actual (e.g. serial ports, parallel ports, network cards, disk drives) or virtual (e.g. /dev/null, a network lesystem, and pseudo-ttys). In other operating systems, this functionality is traditionally associated with device drivers. But unlike device drivers, QNX Neutrino resource managers dont require any special arrangements with the kernel. In fact, a resource manager looks just like any other user-level program. See also device driver.
RMA
Rate Monotonic Analysis a set of methods used to specify, analyze, and predict the timing behavior of realtime systems.
298
Glossary
May 8, 2009
round robin
Scheduling algorithm whereby a thread is given a certain period of time to run. Should the thread consume CPU for the entire period of its timeslice, the thread will be placed at the end of the ready queue for its priority, and the next available thread will be made READY. If a thread is the only thread READY at its priority level, it will be able to consume CPU again immediately. See also adaptive, FIFO, and sporadic.
runmask
A bitmask that indicates which processors a thread can run on. Contrast inherit mask.
runtime loading
The process whereby a program decides while its actually running that it wishes to load a particular function from a library. Contrast static linking.
scheduling latency
The amount of time that elapses between the point when one thread makes another thread READY and when the other thread actually gets some CPU time. Note that this latency is almost always at the control of the system designer. Also designated as Tsl . Contrast interrupt latency.
scoid
An abbreviation for server connection ID.
session
A collection of process groups established for job control purposes. Each process group is a member of a session. A process belongs to the session that its process group belongs to. A newly created process joins the session of its creator. A process can alter its session membership via setsid(). A session can contain multiple process groups.
session leader
A process whose death causes all processes within its process group to receive a SIGHUP signal.
software interrupts
Similar to a hardware interrupt (see interrupt), except that the source of the interrupt is software.
May 8, 2009
Glossary
299
sporadic
Scheduling algorithm whereby a threads priority can oscillate dynamically between a foreground or normal priority and a background or low priority. A thread is given an execution budget of time to be consumed within a certain replenishment period. See also adaptive, FIFO, and round robin.
startup code
The software component that gains control after the IPL code has performed the minimum necessary amount of initialization. After gathering information about the system, the startup code transfers control to the OS.
static bootle
An image created at one time and then transmitted whenever a node boots. Contrast dynamic bootle.
static linking
The process whereby you combine your modules with the modules from the library to form a single executable thats entirely self-contained. The word static implies that its not going to change all the required modules are already combined into one.
thread
The schedulable entity under QNX Neutrino. A thread is a ow of execution; it exists within the context of a process.
tid
An abbreviation for thread ID.
timer
A kernel object used in conjunction with time-based functions. A timer is created via timer_create() and armed via timer_settime(). A timer can then deliver an event, either periodically or on a one-shot basis.
300
Glossary
May 8, 2009
timeslice
A period of time assigned to a round-robin or adaptive scheduled thread. This period of time is small (on the order of tens of milliseconds); the actual value shouldnt be relied upon by any program (its considered bad design).
May 8, 2009
Glossary
301
Index
!
.longfilenames 162 / directory 124 /dev hd* 160 mem 124 mq and mqueue 73 ser* 183 shmem 163 zero 124 /net directory 204 /proc boot 124 ham 236
adaptive partitioning 243 debugging with 248 partitions 244 thread scheduler 248 afnity, processor 103, 105 alarm() 44 anonymous memory 79 Apple Macintosh HFS and HFS Plus 180 as_add_containing() 86 as_add() 86 Asymmetric Multiprocessing (AMP) 99 atomic operations 33, 40 attributes structure (resource manager) 153 autoconnect 222 AutoIP 221
pid
124
/tmp directory 163 _CS_DOMAIN 207 _CS_HOSTNAME 207 _IO_STAT 147 _NOTIFY_COND_INPUT 68 _NOTIFY_COND_OBAND 68 _NOTIFY_COND_OUTPUT 67 _NTO_CHF_FIXED_PRIORITY 64 _NTO_TCTL_IO 122 _NTO_TCTL_RUNMASK 106 _NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT
B
background priority (sporadic scheduling) barriers 16, 33 and threads 36 bind() 219 bindresvport() 219 block-oriented devices 183 boot processor 102 Bound Multiprocessing (BMP) 99, 105 budgets CPU 243, 248 le space (not implemented) 243 memory (not implemented) 243 28
106
A
abort() 71 accept() 219 actions (HA) 233
May 8, 2009
Index
303
Index
C
canonical input mode 187 cd command 130 CD-ROM lesystem 174 CGI (Common Gateway Interface) 222 ChannelCreate() 61, 64 ChannelDestroy() 61 channels 61, 62 character devices 183 chmod() 154 chown() 154 CIFS lesystem 179 clipping (Photon) 256 clock_getcpuclockid() 42 clock_getres() 42 clock_gettime() 42 clock_settime() 42 clock services 41 ClockAdjust() 42, 43 ClockCycles() 42, 43 ClockId() 42 ClockPeriod() 42 ClockTime() 42 close() 76, 85, 154 COFF (Common Object File Format) 139 color model (Photon) 258 combine messages 152 Common Gateway Interface (CGI) 222 conditions (HA entity states) 232 CONDVAR (thread state) 22 condvars 16, 32, 33, 35 example 35 operations 35 SMP 104 confstr() 207 connect messages 149 connect() 219 ConnectAttach() 61 ConnectDetach() 61 consoles physical 189 virtual 189 convenience functions (Photon widget toolkit) 271 conventions
typographical xvi cooked input mode 187 cooperating processes FIFOs 87 pipes 87 copy-on-write (COW) 168 CPU afnity 103, 105 usage, budgeting 243, 248 CRC 166 critical section 34, 35, 40, 45 dened 32 SMP 104
D
data server 223 dates, valid range of 42 DEAD (thread state) 22 deadlock-free systems, rules for 65 debugging, using adaptive partitions for 248 defragmentation of physical memory 121 design goals for QNX Neutrino 3, 16
devb
resource manager 281 devc-con, devc-con-hid 189 devctl() 154, 185 device control 185 device drivers See also resource managers no need to link into kernel 50 similarity to standard processes 10 device names, creating 129 directories, changing 130 discrete multiprocessors 99 disks corruption, avoiding 168 DOS disks, accessing 172 partitions 160 dladdr() 142 dlclose() 142 dlopen() 141, 142 dlsym() 141 DMA-safe region, dening 86 dn_comp() 219 dn_expand() 219
304
Index
May 8, 2009
Index
domains of authority 124 DOS lesystem manager 172 draw events (Photon) 254 DT_RPATH 141 dumper 20 dup() 85, 132, 150, 151 dup2() 85, 132 dynamic interpreter 140 dynamic linking 137
E
edited input mode 187 editing capabilities (io-char) 188 ELF format 116, 138 Embedded Transaction Filesystem (ETFS) 163 embedded web server 222 endprotoent() 219 endservent() 219 entities (HA process) 231 environment variables LD_LIBRARY_PATH 141 LD_PRELOAD 142 RESCONF 219 ETFS 163 event space (Photon) 253, 254 events 66 unblock 67 draw (Photon) 254 instrumented kernel 91 exclusive (QoS policy) 208 exec*() functions 111, 115 Executable and Linking Format See ELF executable, partially bound 137 Ext2 lesystem 179 extensibility of OS 9 extensions to OS user-written, wont affect reliability of core OS 6
F
fast emitting mode (instrumented kernel) 92
fcntl() 132 FIFO (scheduling method) 26, 27, 33 FIFOs 87, See also pipes creating 87 removing 87 le descriptors (FDs) duplicating 132 inheritance 132, 133 open control blocks (OCBs) 130 several FDs referring to the same OCB 132 typed memory and 85 les DOS les, operating on 172 FIFOs 87 opened by different processes 131 opened twice by same process 131 pipes 87 space, budgeting (not implemented) 243 lesystems accessing a lesystem on another node 128 Apple Macintosh HFS and HFS Plus 180 CD-ROM 174 CIFS 179 DOS 172 Embedded Transaction (ETFS) 163 HAM 236 Image 163 Linux Ext2 179 NFS 179 NTFS (fs-nt.so) 180 Power-Safe (fs-qnx6) 167 QNX 125, 167 RAM 163 seek points 131 Universal Disk Format (UDF) 180 ve nines (HA metric) 227 Flash 116 foreground priority (sporadic scheduling) 28 fork() 111, 114, 115 POSIX process model and 7 fpathconf() 154 FQNN (fully qualied node name) 208 fs-cd.so 174 fs-cifs 179 fs-dos.so 172 fs-ext2.so 179
May 8, 2009
Index
305
Index
fs-mac.so 180 fs-nfs2, fs-nfs3 179 fs-nt.so 180 fs-qnx4.so 125, 167 fs-qnx6.so 167 fs-udf.so 180
fseek() 154 fstat() 85, 154 fsync() (expensive on Power-Safe lesystems) 171 ftruncate() 77
hierarchy 231 herror() 219 HFS and HFS Plus 180 high availability See HA High Availability Manager hstrerror() 219 htonl() 219 htons() 219 Hyper-Threading 101
See HAM
I G
gethostbyaddr() 219 gethostbyname() 219 getpeername() 219 getprotobyname() 219 getprotobynumber() 219 getprotoent() 219 getservbyname() 219 getservent() 219 getsockname() 219 getsockopt() 219 global list 142 GNS (Global Name Service) 207 graphical microkernel 253 graphics drivers 257 multiple 258 Guardian (HAM stand-in) 231 I/O messages 149 I/O privileges 122 I/O resources 124 i8259 interrupt control hardware 50 idle thread 25, 51 ifconfig 199 Image lesystem 163 inet_addr() 219 inet_aton() 219 inet_lnaof() 219 inet_makeaddr() 219 inet_netof() 219 inet_network() 219 inet_ntoa() 219 inheritance structure 106 initial budget (sporadic scheduling) 28 inodes 168 input mode edited 187 raw 186 input, redirecting 87 instrumentation interrupts can be traced 91 kernel can be used in prototypes or nal products 91 works on SMP systems 91 interprocess communication See IPC interprocessor interrupts (IPIs) 103 INTERRUPT (thread state) 22 interrupt control hardware (i8259 on a PC) 50 interrupt handlers 16, 45, See also ISR SMP 104 interrupt latency 45
H
h_errlist() 219 h_errno() 219 h_nerr() 219 HA 227239 client-side library 228 microkernel architecture inherently suited for 227 recovery example 229 HAM 231 API 237
306
Index
May 8, 2009
Index
Interrupt Service Routine See ISR InterruptAttach() 48 InterruptAttachEvent() 48 InterruptDetach() 48 InterruptDisable() 48 problem on SMP systems 104 InterruptEnable() 48 problem on on SMP systems 104 InterruptHookIdle() 51 InterruptHookTrace() 51 InterruptLock() 48, 104 InterruptMask() 48 problem on SMP systems 104 interrupts masking, automatically by the kernel nested 47 priorities 47 InterruptUnlock() 48, 105 InterruptUnmask() 48 problem on SMP systems 104 InterruptWait() 22, 48 intr_timed_wait() 44
io-audio
K
kill() 68
L
languages 259 latency interrupt 45, 50 scheduling 46, 50 layers, display 260 LD_LIBRARY_PATH 141 LD_PRELOAD 142 libraries loading before others 142 link() 172 linking dynamically 137 sections 138 statically 137 Linux Ext2 lesystem 179 listen() 219 loadbalance (QoS policy) 208 lock() 154 lseek() 154 lsm-pf-*.so 220 lsm-qnet.so 204, 206208 tx_retries option 210
49
281
resource manager
io-media io-pkt* io-usb
281 281
resource manager 281 ioctl() 219 iofunc_*() shared library 152 ionotify() 67 IP ltering 220 IPC 10, 55 forms of 55 term qualied to apply to threads 19 ISR 49, 51, See also interrupt handlers attaching to PC timer interrupt 49
M
Macintosh HFS and HFS Plus malloc() 137 MAP_ANON 79, 122 MAP_BELOW16M 80 MAP_FIXED 79 MAP_NOINIT 80, 81 MAP_NOX64K 79 MAP_PHYS 79, 122 MAP_PRIVATE 78 MAP_SHARED 78 mem_offset() 123 memory 180
J
JOIN (thread state) 22
May 8, 2009
Index
307
Index
anonymous 79 DMA-safe region, dening 86 initializing 80 locking 122 mapping 77, 122 physical, defragmenting 121 protection, advantage of for embedded systems 116 quantums 122 shared 32, 40, 60, 7480 typed 81 unmovable 122, 123 Memory Management Units (MMUs) 117 message copying 58 message passing 10, 16, 55 API 64 as means of synchronization 10 network-wide 203 message queues 72 messages contents of 10 multipart 58, 59 tend to be tiny 32 metadata 168 microkernel 15 comparable to a realtime executive 6 dened 6 general responsibilities 8 instrumentation 91 instrumented 91 locking 103 managing team of cooperating processes modularity as key aspect 6 services 8 SMP 101, 103 version of, determining 15 mkfifo 87 mkfo() 87 mkqnx6fs 168 mlock() 122 mmap_device_memory() 79 mmap() 76, 77, 81, 122, 154
mme
MMU 59, 60, 117 mount structure (resource manager) 153 mountpoints 124, 157 order of resolution 125 mprotect() 76, 80 mq_close() 73, 74 mq_getattr() 74 mq_notify() 67, 74 mq_open() 73, 74 mq_receive() 73, 74 mq_send() 73, 74 mq_setattr() 74 mq_unlink() 73, 74 mq server 73 mqueue resource manager 73 MsgDeliverEvent() 23, 64, 67 MsgError() 57, 58 MsgInfo() 64 MsgKeyData() 64 MsgRead() 64 MsgReadv() 60 MsgReceive() 23, 55, 64 MsgReceivePulse() 64 MsgReceivePulsev() 60 MsgReceivev() 60 MsgReply() 55, 58, 60, 64 MsgReply*() 23 MsgReplyv() 60 MsgSend() 23, 55, 58, 60, 64 non-cancellation point variants 26 MsgSendPulse() 23, 64 MsgSendsv() 60 MsgSendv() 60, 111 MsgSendvs() 60 MsgWrite() 64 msync() 76 multicore processors 99 multimedia 277 overview 277 multiprocessing 99 munmap_ags() 76, 80 munmap() 76, 80 MUTEX (thread state) 22 mutexes 16, 3234 attributes 34
308
Index
May 8, 2009
Index
not currently moved when defragmenting physical memory 122 priority inheritance 34 SMP 104
O
O_SYNC(ignored by Power-Safe lesystems)
N
name_attach() 62 name_open() 62 name resolution network 205 name resolver 208 NAND ash 163 NANOSLEEP (thread state) 23 nanosleep() 23, 44 NAT 220 ND_LOCAL_NODE 205 nested interrupts 47 NET_REPLY (thread state) 23 NET_SEND (thread state) 23 network as homogeneous set of resources exibility 11 message passing 203 name resolution 205 pathnames 130 protocol Qnet 203 transparency 11, 148 Neutrino microkernel 6 NFS lesystem 179 NMI 51 node descriptor network 205 node domain 207 node name 207 non-Ethernet interconnect 213 NTFS (fs-nt.so) 180 ntohl() 219 ntohs() 219 NTP 220
171 object les, sections of 138 on 106 opaque bitmask (Photon) 255 open control blocks (OCBs) 130, 131 open resources active information contained in OCBs open() 76, 154 operations, atomic 40 output, redirecting 87
131
P
pages 117 parallel devices 190 partially bound executable 137 partitions adaptive 244 static 243 thread scheduler 243 partitions (disk) 160 partitions (thread scheduler) 243 pathconf() 154 pathname converting relative to network 130 pathname delimiter in QNX documentation xvii pathname space 124, 205 mapping 145 pause() 68 performance context-switch 32 realtime 45 Photon architecture differs from X Window System 253 architecture similar to that of OS microkernel 253 event space 254 event types 256 graphics drivers 257, 258 microkernel runs as tiny process 253 regions 255
11
May 8, 2009
Index
309
Index
widgets 261273 window manager 261 physical memory, defragmenting 121 pidin 20 pipe manager 87 pipe() 87 pipes 87 creating 87 Point-to-Point Protocol (PPP) 218 Point-to-Point Protocol over Ethernet (PPPoE) 221 popen() 87 POSIX denes interface, not implementation 3 message queues 72 proles 4 realtime extensions 4 standards of interest to embedded systems developers 4 suitable for embedded systems 4, 5 threads 4 library calls not involving kernel calls 18 library calls with corresponding kernel calls 18 UNIX and 3 posix_mem_offset() 85 posix_spawn() family of functions 111, 112 posix_typed_mem_get_info() 81 posix_typed_mem_open() 81 Power-Safe (fs-qnx6) lesystem 167 PPP (Point-to-Point Protocol) 218 PPPoE (Point-to-Point Protocol over Ethernet) 221 preferred (QoS policy) 208 prex 124 prex tree 124 printf() 137 priority 24 background and foreground (sporadic scheduling) 28 inheritance 114 messages 63 mutexes 34 inversion 25, 34 range 25
process groups membership, inheriting 113 remote node 114 Process Manager capabilities of 111 required when creating multiple POSIX processes 111 processes as container for threads 18 cooperating via pipes and FIFOs 87 loading 116 management 111 model, required for POSIX compliance 16 opening the same le twice 131 OS as team of cooperating 8 primitives 111 system 9 processors locking processes to 103, 105 multiple 99 number of, determining 99
procnto
enabling the defragmentation of physical memory 123 marking memory blocks as unmovable 123 procnto* See also microkernel, process manager image lesystem 158 instrumented 91 RAM lesystem 163 SMP 101 product line, using a single OS for 3 PROT_EXEC 78 PROT_NOCACHE 78 PROT_NONE 78 PROT_READ 78 PROT_WRITE 78 protocols 220 pthread_attr_destroy() 18 pthread_attr_getdetachstate() 18 pthread_attr_getinheritsched() 18 pthread_attr_getschedparam() 18 pthread_attr_getschedpolicy() 18 pthread_attr_getscope() 18 pthread_attr_getstackaddr() 18
310
Index
May 8, 2009
Index
pthread_attr_getstacksize() 18 pthread_attr_init() 18 pthread_attr_setdetachstate() 18 pthread_attr_setinheritsched() 18 pthread_attr_setschedparam() 18 pthread_attr_setschedpolicy() 18 pthread_attr_setscope() 18 pthread_attr_setstackaddr() 18 pthread_attr_setstacksize() 18 pthread_barrier_destroy() 38 pthread_barrier_init() 36 pthread_barrier_wait() 36 pthread_barrierattr_destroy() 38 pthread_barrierattr_getpshared() 38 pthread_barrierattr_init() 38 pthread_barrierattr_setpshared() 38 pthread_cancel() 18 pthread_cleanup_pop() 18 pthread_cleanup_push() 18 pthread_cond_broadcast() 18, 41 pthread_cond_destroy() 18, 41 pthread_cond_init() 18, 41 pthread_cond_signal() 18, 41 pthread_cond_timedwait() 41, 44 pthread_cond_wait() 18, 22, 41 pthread_create() 18 pthread_detach() 18 pthread_equal() 18 pthread_exit() 18 pthread_getcpuclockid() 42 pthread_getname_np() 20 pthread_getschedparam() 18, 31 pthread_getspecic() 18, 21 pthread_join() 18, 22 pthread_key_create() 18, 21 pthread_key_delete() 18 pthread_kill() 18, 68 pthread_mutex_destroy() 18, 41 pthread_mutex_init() 18, 41 pthread_mutex_lock() 18, 22, 34, 41, 100 pthread_mutex_timedlock() 34 pthread_mutex_trylock() 18, 34, 41, 44 pthread_mutex_unlock() 18, 34, 41, 100 pthread_mutexattr_init() 34 pthread_mutexattr_setprotocol() 34 pthread_mutexattr_setrecursive() 35
34 pthread_rwlock_rdlock() 38 pthread_rwlock_tryrdlock() 39 pthread_rwlock_trywrlock() 39 pthread_rwlock_unlock() 38 pthread_rwlock_wrlock() 38 pthread_self() 18 pthread_setname_np() 20 pthread_setschedparam() 18, 31 pthread_setspecic() 18 pthread_sigmask() 18 pthread_sleepon_lock() 38 pthread_spin_lock() 100 pthread_spin_unlock() 100 pty (pseudo terminal) as pair of character devices 190 pulses 63, 66
PTHREAD_PRIO_INHERIT
Q
qdb
resource manager 280 QDB introduction 280 Qnet 198, 203212 limiting transmission retries 210 redundant 208 QNX 4 lesystem 125, 167 QNX 6 lesystem See Power-Safe lesystem QNX Neutrino design goals 3, 16 extensibility 9 exibility 6 network as homogeneous set of resources 11 network exibility 11 network transparency 11 preemptible even during message pass 16 realtime applications, suitability for 5 services 16 single-computer model 11 QoS (Quality of Service) 208 policies 208 quantums 122
May 8, 2009
Index
311
Index
R
raise() 68 RAM 116 RAM lesystem 163 RAM disk 160 RapidIO 213 rate monotonic analysis (RMA) 28 raw input mode 186 conditions for input request 186 FORWARD qualier 187 MIN qualier 186 TIME qualier 186 TIMEOUT qualier 186 read() 67 readblock() 152 readdir() 128, 157 reader/writer locks 33, 38 READY (thread state) 23 realtime performance 45 interrupt latency and 45 nested interrupts and 47 scheduling latency and 46 RECEIVE (thread state) 23 rectangle list (Photon) 256 recv() 219 recvfrom() 219 redirecting input 87 output 87 redundant Qnet 208 regions (Photon) 253, 255 relative pathnames, converting to network pathnames 130 remove() 87 replenishment period (sporadic scheduling) REPLY (thread state) 23 res_init() 219 res_mkquery() 219 res_query() 219 res_querydomain() 219 res_search() 219 res_send() 219 RESCONF 219 resolv.conf 219 resource managers 157
29
atomic operations 152 attributes structure 153 can be started and stopped dynamically 145 communicate with clients via IPC 148 context for client requests 150 dened 145 devb 281 dont require any special arrangements with the kernel 145 io-audio 281 io-fs 281 io-media 281 io-usb 281 iofunc_*() shared library 152 message types 149 mme 280 mount structure 153 qdb 280 shared library 150 similarity to traditional device drivers 145 similarity to user-level servers 145 thread management 151 unique features of 148 resources accessing on other machines 11 no real difference between local and remote 11 open 131 RLIMIT_AS 85 RLIMIT_DATA 85 RLIMIT_VMEM 85 rm 87 robustness improved via memory protection 116 of application architectures via Send/Receive/Reply 65 of implementations with Send/Receive/Reply 64 of IPC 55 of signal handlers 66 ROM 116 round-robin scheduling 26, 27 RTLD_GLOBAL 142 RTLD_GROUP 142 RTLD_WORLD 142
312
Index
May 8, 2009
Index
runmask 105 inheriting 105 RUNNING (thread state) runtime linker 140
23
S
scaling advantages of 3 of applications 3 scatter/gather DMA 58 sched_yield() 24 SchedGet() 18 SchedSet() 18 scheduling FIFO 26, 27 latency 46 method determining 27 setting 27 round-robin 26, 27 SMP systems 103 sporadic 26, 28 threads 24 seek points 131, 132 segments 138 select() 67, 219 SEM (thread state) 23 sem_destroy() 41 sem_init() 41 sem_post() 41 sem_trywait() 41 sem_wait() 41 semaphores 16, 32, 33, 39 named 39 SMP 104 SEND (thread state) 23 send() 219 sendto() 219 sensitivity bitmask (Photon) 255 serial devices 189 drivers 190 Server Side Includes (SSI) 223 services 220
services, handled by system processes 9 sessions, remote node 114 setprotoent() 219 setrlimit() 85 setservent() 219 setsockopt() 219 shared libraries (.so les) 15, 111, 140 shared memory 32, 40, 60, 7480 shm_ctl_special() 76 shm_ctl() 76, 77 shm_open() 76 shm_unlink() 76 shutdown() 219 SIGABRT 71 sigaction() 68 SIGALRM 71 SIGBUS 71 SIGCHLD 71 SIGCONT 71 SIGDEADLK 71 SIGEMT 71 SIGFPE 71 SIGHUP 71 SIGILL 71 SIGINT 71 SIGIOT 71 SIGKILL 71 signal() 70 SignalAction() 68 SignalKill() 18, 23, 68 SignalProcmask() 18, 68 signals 16 POSIX and UNIX 68 queuing of 69 rules for a multithreaded process 68 similarity to pulses 69 targeted at specic threads 68 SignalSuspend() 68 SignalWaitinfo() 68 SIGPIPE 71 SIGPOLL 71 sigprocmask() 68 sigqueue() 68 SIGQUIT 71 SIGSEGV 71 SIGSTOP 71
May 8, 2009
Index
313
Index
SIGSUSPEND (thread state) 23 sigsuspend() 23, 68 SIGSYS 71 SIGTERM 71 sigtimedwait() 44 SIGTRAP 71 SIGTSTP 71 SIGTTIN 71 SIGTTOU 71 SIGURG 71 SIGUSR1 71 SIGUSR2 71 SIGWAITINFO (thread state) 23 sigwaitinfo() 23, 68 SIGWINCH 71 single-computer model 11 slay 106 sleep() 44 sleepon locks 33, 38 slinger 222 SMP (Symmetric Multiprocessing) 99, 100 snapshot (Power-Safe lesystem) 170 socket() 219 sockets (logical ash drives) 175 software interrupt See signals SPAWN_EXPLICIT_CPU 106 SPAWN_SETGROUP 113 SPAWN_SETSIGDEF 113 SPAWN_SETSIGMASK 113 spawn() 106 family of functions 111113 spinlocks 104 SPOF 231 sporadic scheduling 26, 28 SSI (Server Side Includes) 223 STACK (thread state) 23 startup code (startup-*) 102 stat() 154 states CONDVAR 22 DEAD 22 INTERRUPT 22 JOIN 22 MUTEX 22 NANOSLEEP 23 NET_REPLY 23
NET_SEND 23 READY 23 RECEIVE 23 REPLY 23 RUNNING 23 SEM 23 SEND 23 SIGSUSPEND 23 SIGWAITINFO 23 STACK 23 STOPPED 23 WAITCTX 23 WAITPAGE 23 WAITTHREAD 23 static linking 137 static partitions 243 STOPPED (thread state) 23 stty 188 superblocks 169 symbol names, resolving 142 symbolic links cd command and 130 symbolic prexes 128 Symmetric Multiprocessing See SMP SyncCondvarSignal() 18, 41 SyncCondvarWait() 18, 41 SyncDestroy() 18, 41 synchronization services 33, 41 SyncMutexEvent() 18 SyncMutexLock() 18, 41 SyncMutexUnlock() 18, 41 SyncSemPost() 41 SyncSemWait() 23, 41 SyncTypeCreate() 18, 41 system emergency access to 248 page 42 processes 9 similarity to user-written processes
T
tcdropline() 185 tcgetattr() 185 tcgetpgrp() 185
314
Index
May 8, 2009
Index
tcinject() 185 TCP/IP 217223 resource manager (io-pkt*) 218 stack congurations 217 tcsendbreak() 185 tcsetattr() 185 tcsetpgrp() 185 terminal emulation 189 textto 172 thread scheduler 243 ThreadCancel() 18 ThreadCreate() 18, 23 ThreadCtl() 18, 106, 122 ThreadDestroy() 18 ThreadDetach() 18 ThreadJoin() 18 threads 16, 151 all share same kernel interface 111 and barriers 36 attributes of 20 cancellation handlers 20 concurrency advantages 33 dened 18 life cycle 21 migration, reducing 103 names 20 priority 20, 24 priority inversion 25 process must contain one or more 18 register set 20 scheduling 24 FIFO 27 round-robin 27 sporadic 28 signal mask 20 stack 20 states 21 synchronization 32 tid 20 TLS (thread local storage) 20 time_t 42 timeout service 43 timer_create() 44 timer_delete() 44 timer_getoverrun() 44 timer_gettime() 44
timer_settime() 44 TimerAlarm() 44 TimerCreate() 44 TimerDestroy() 44 TimerInfo() 44 timers 16, 43 cyclical mode 43 TimerSettime() 44 TimerTimeout() 44 timeslice 27 TLB (translation look-aside buffer) TLS (thread local storage) 20 TraceEvent() 95 transactions 163 transparency of network 11 typed memory 81 typographical conventions xvi
117, 120
U
UART 50 UDF (Universal Disk Format) lesystem umount 199 uname 15 Unicode 259 Universal Disk Format (UDF) lesystem unlink() 87 UNMAP_INIT_OPTIONAL 81 UNMAP_INIT_REQUIRED 81 UTF-8 encoding 259 utime() 154 180
180
V
variable page size 120 vfork() 111, 115 virtual addresses 117 virtual consoles 189
W
WAITCTX (thread state) 23
May 8, 2009
Index
315
Index
WAITPAGE (thread state) 23 WAITTHREAD (thread state) 23 watchdog 118 web server 222 wide emitting mode (instrumented kernel) 92 widgets (Photon) 261273 window manager 261 Windows (Microsoft) NTFS (fs-nt.so) 180 windows, dragging across network links 258 write() 67, 137
Z
zero-copy architecture (io-pkt*) 195
316
Index
May 8, 2009