QNX® Neutrino® Realtime Operating System
Total Page:16
File Type:pdf, Size:1020Kb
QNX Neutrino Realtime Operating System Programmer’s Guide For QNX Neutrino 6.3 2004, QNX Software Systems Ltd. QNX Software Systems Ltd. 175 Terence Matthews Crescent Kanata, Ontario K2M 1W8 Canada Voice: 613-591-0931 Fax: 613-591-3579 Email: [email protected] Web: http://www.qnx.com/ QNX Software Systems Ltd. 2004. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise without the prior written permission of QNX Software Systems Ltd. Although every precaution has been taken in the preparation of this book, we assume no responsibility for any errors or omissions, nor do we assume liability for damages resulting from the use of the information contained in this book. Technical support options To obtain technical support for any QNX product, visit the Technical Support section in the Support area on our website (www.qnx.com). You’ll find a wide range of support options, including our free web-based QNX Developer’s Network. QNX, Momentics, Neutrino, and Photon are registered trademarks of QNX Software Systems Ltd. All other trademarks and registered trademarks belong to their respective owners. Contents About This Book xvii Note to Windows users xx Recommended reading xx 1 Compiling and Debugging 1 Choosing the version of the OS 3 Conforming to standards 4 Header files in include 7 Self-hosted or cross-development 7 A simple example 8 Self-hosted 10 Cross-development with network filesystem 10 Cross-development with debugger 11 Cross-development, deeply embedded 11 Using libraries 14 Static linking 15 Dynamic linking 15 Runtime loading 15 Static and dynamic libraries 15 Platform-specific library locations 17 Linking your modules 18 Creating shared objects 19 Debugging 20 Debugging in a self-hosted environment 20 Debugging in a cross-development environment 21 May 31, 2004 Contents iii 2004, QNX Software Systems Ltd. The GNU debugger (gdb)22 The process-level debug agent 22 A simple debug session 29 Configure the target 30 Compile for debugging 30 Start the debug session 30 Get help 31 Sample boot image 33 2 Programming Overview 35 Process model 37 An application as a set of processes 38 Processes and threads 39 Some definitions 39 Priorities and scheduling 41 Priority range 41 BLOCKED and READY states 42 The ready queue 43 Suspending a running thread 45 When the thread is blocked 45 When the thread is preempted 45 When the thread yields 46 Scheduling algorithms 46 FIFO scheduling 47 Round-robin scheduling 48 Why threads? 49 Summary 50 3 Processes 51 Starting processes — two methods 53 Process creation 53 Concurrency 54 Using fork() and forkpty() 55 iv Contents May 31, 2004 2004, QNX Software Systems Ltd. Inheriting file descriptors 55 Process termination 56 Normal process termination 57 Abnormal process termination 57 Affect of parent termination 59 Detecting process termination 59 4 Writing a Resource Manager 71 What is a resource manager? 73 Why write a resource manager? 74 Under the covers 77 The types of resource managers 82 Components of a resource manager 83 iofunc layer 83 resmgr layer 84 dispatch layer 85 thread pool layer 87 Simple device resource manager examples 87 Single-threaded device resource manager example 88 Multi-threaded device resource manager example 94 Data carrying structures 97 The Open Control Block (OCB) structure 98 The attribute structure 99 The mount structure 105 Handling the IO READ message 107 Sample code for handling IO READ messages 108 Ways of adding functionality to the resource manager 112 Handling the IO WRITE message 116 Sample code for handling IO WRITE messages 117 Methods of returning and replying 119 Returning with an error 120 Returning using an IOV array that points to your data 120 Returning with a single buffer containing data 121 May 31, 2004 Contents v 2004, QNX Software Systems Ltd. Returning success but with no data 121 Getting the resource manager library to do the reply 122 Performing the reply in the server 122 Returning and telling the library to do the default action 124 Handling other read/write details 125 Handling the xtype member 125 Handling pread*() and pwrite*() 127 Handling readcond() 129 Attribute handling 129 Updating the time for reads and writes 130 Combine messages 131 Where combine messages are used 131 The library’s combine-message handling 133 Extending Data Control Structures (DCS) 139 Extending the OCB and attribute structures 139 Extending the mount structure 142 Handling devctl() messages 142 Sample code for handling IO DEVCTL messages 145 Handling ionotify() and select() 149 Sample code for handling IO NOTIFY messages 153 Handling private messages and pulses 160 Handling open(), dup(), and close() messages 163 Handling client unblocking due to signals or timeouts 164 Handling interrupts 166 Sample code for handling interrupts 167 Multi-threaded resource managers 169 Multi-threaded resource manager example 169 Thread pool attributes 171 Thread pool functions 174 Filesystem resource managers 175 Considerations for filesystem resource managers 175 Taking over more than one device 175 vi Contents May 31, 2004 2004, QNX Software Systems Ltd. Handling directories 177 Message types 183 Connect messages 183 I/O messages 183 Resource manager data structures 184 5 Transparent Distributed Processing Using Qnet 187 What is Qnet? 189 Benefits of Qnet 189 What works best 190 What type of application is well-suited for Qnet? 191 Qnet drivers 191 How does it work? 192 Locating services using GNS 196 Quality of Service (QoS) and multiple paths 205 Designing a system using Qnet 208 The product 208 Developing your distributed system 209 Configuring the data cards 209 Configuring the controller card 210 Enhancing reliability via multiple transport buses 211 Redundancy and scalability using multiple controller cards 213 Autodiscovery vs static 214 When should you use Qnet, TCP/IP or NFS? 215 Writing a driver for Qnet 218 6 Writing an Interrupt Handler 223 Overview 225 Attaching and detaching interrupts 225 The Interrupt Service Routine (ISR) 226 Advanced topics 236 May 31, 2004 Contents vii 2004, QNX Software Systems Ltd. Interrupt environment 236 Ordering of shared interrupts 237 Interrupt latency 237 Atomic Operations 237 7 Heap Analysis: Making Memory Errors a Thing of the Past 239 Introduction 241 Dynamic Memory Management 241 Heap Corruption 242 Common sources 244 Detecting and Reporting Errors 246 Using the malloc debug library 247 What’s checked? 250 Controlling the level of checking 251 Manual Checking (Bounds Checking) 255 Getting pointer information 256 Getting the heap buffer size 257 Memory Leaks 258 Tracing 258 Causing a trace and giving results 259 Analyzing dumps 260 Compiler Support 261 C++ issues 261 Bounds checking GCC 263 Summary 264 A Freedom from Hardware and Platform Dependencies 265 Common problems 267 I/O space vs memory-mapped 267 Big-endian vs little-endian 268 Alignment and structure packing 269 viii Contents May 31, 2004 2004, QNX Software Systems Ltd. Atomic operations 270 Solutions 270 Determining endianness 270 Swapping data if required 271 Accessing unaligned data 272 Examples 273 Accessing I/O ports 276 B Conventions for Makefiles and Directories 279 Structure 281 Makefile structure 283 The recurse.mk file 283 Macros 284 Directory structure 286 The project level 286 The section level (optional) 286 The OS level 286 The CPU level 287 The variant level 287 Specifying options 287 The common.mk file 287 The variant-level makefile 288 Recognized variant names 288 Using the standard macros and include files 290 The qconfig.mk include file 291 The qrules.mk include file 294 The qtargets.mk include file 298 Advanced topics 299 Collapsing unnecessary directory levels 300 Performing partial builds 301 More uses for LIST 302 GNU configure 303 May 31, 2004 Contents ix 2004, QNX Software Systems Ltd. C Developing SMP Systems 309 Introduction 311 Building an SMP image 311 The impact of SMP 312 To SMP or not to SMP 312 Processor affinity 312 SMP and synchronization primitives 313 SMP and FIFO scheduling 313 SMP and interrupts 313 SMP and atomic operations 314 Designing with SMP in mind 315 Use the SMP primitives 315 Assume that threads really do run concurrently 316 Break the problem down 316 D Using GDB 319 GDB commands 321 Command syntax 321 Command completion 322 Getting help 324 Running programs under GDB 327 Compiling for debugging 328 Setting the target 328 Starting your program 329 Your program’s arguments 330 Your program’s environment 331 Your program’s input and output 332 Debugging an already-running process 333 Killing the child process 334 Debugging programs with multiple threads 334 Debugging programs with multiple processes 336 Stopping and continuing 337 Breakpoints, watchpoints, and exceptions 337 x Contents May 31, 2004 2004, QNX Software Systems Ltd. Continuing and stepping 352 Signals 357 Stopping and starting multithreaded programs 359 Examining the stack 360 Stack frames 361 Backtraces 362 Selecting a frame 363 Information about a frame 365 MIPS machines and the function stack 366 Examining source files 367 Printing source lines 367 Searching source files 369 Specifying source directories 370 Source and machine code 371 Shared libraries 373 Examining data 374 Expressions 375 Program variables 376 Artificial arrays 378 Output formats 379 Examining memory 381 Automatic display 383 Print settings 385 Value history 392 Convenience variables 394 Registers 396 Floating point hardware 398 Examining the symbol