马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
Introduction_To_Real-Time_Operating_Systems.pdf
(3.55 MB, 下载次数: 168 )
Chapter 1 INTRODUCTION TO OPERATING SYSTEMS .... 1-1 References ......................................................................................................................... 1-1 What is an Operating system? ........................................................................................... 1-1 What is Multi-programming? ........................................................................................... 1-2 Why were Multi-programming Systems Developed? ....................................................... 1-2 Definition of an Operating System ................................................................................... 1-2 Types of Operating Systems ............................................................................................. 1-3 Some more definitions – Processors, Programs and Tasks ............................................... 1-4 Hierarchical Structure of Operating Systems. .................................................................. 1-5 A digression – the role of interrupts in Operating Systems. ............................................. 1-6 What is an interrupt? .................................................................................................... 1-6 Why are interrupts crucial to Operating Systems? ....................................................... 1-6 Example: Producer-Consumer Problem ....................................................................... 1-8 Chapter 2 PRINCIPLES OF SOFTWARE DESIGN ................... 2-1 Problems with Real Time Design ..................................................................................... 2-2 Some Suggestions on Program Structure .......................................................................... 2-3 The efficiency trap ............................................................................................................ 2-4 Chapter 3 OVERVIEW OF MULTI-TASKING ................................. 3-1 Coroutines ......................................................................................................................... 3-1 Tasks ................................................................................................................................ 3-3 Anatomy of an Operating System Kernel ......................................................................... 3-4 The Dispatcher ............................................................................................................. 3-4 Synchronization Primitives .......................................................................................... 3-5 Protected and Non-protected Operating Systems ............................................................. 3-5 Brief Discussion of Memory Management .................................................................. 3-6 Brief Discussion of Supervisor Mode .......................................................................... 3-6 Chapter 4 SEMAPHORES ............................................................................ 4-1 Basic Concepts .................................................................................................................. 4-1 Wait and Signal ................................................................................................................. 4-2 Semaphores as Resource Counters ................................................................................... 4-5 Task Synchronization ........................................................................................................ 4-6 Consumer-Producer Problem Revisited ............................................................................ 4-7 More Complex Synchronization Problems ..................................................................... 4-10 Multiple unit resource allocation ............................................................................... 4-10 Dining philosophers problem ..................................................................................... 4-11 Readers and Writers problem ..................................................................................... 4-12 Variations on the semaphore ........................................................................................... 4-15 Logic semaphores ....................................................................................................... 4-15 Timed semaphores, sleeping tasks and simulated task suspension ............................ 4-16 Semaphores and priorities .......................................................................................... 4-17 Other synchronization primitives .................................................................................... 4-18 Event primitives ......................................................................................................... 4-18 Monitors ..................................................................................................................... 4-18 A practical semaphore structure ................................................................................. 4-20 Deadlock and Starvation ................................................................................................. 4-21 Resource types ........................................................................................................... 4-21 Reusable resources ................................................................................................ 4-21 Consumable resources ........................................................................................... 4-23 The conditions for deadlock ....................................................................................... 4-23 Techniques to handle deadlock .................................................................................. 4-24 Deadlock prevention .............................................................................................. 4-24 Deadlock detection ................................................................................................ 4-25 Deadlock avoidance ............................................................................................... 4-26 Summarizing .............................................................................................................. 4-28 Chapter 5 TASK MANAGEMENT ........................................................... 5-1 Task descriptors ................................................................................................................ 5-1 Kernel Queues ................................................................................................................... 5-1 Description of UNOS-V1.5 .............................................................................................. 5-2 Unos-V1.5 Task Control Block (TCB) and Semaphore Structures ............................. 5-3 Queue Operation in UNOS-V1.5 ................................................................................. 5-6 UNOS-V1.5 task creation ............................................................................................ 5-8 The Dispatcher ........................................................................................................... 5-10 The UNOS-V1.5 Dispatcher ................................................................................. 5-10 The task switch operation ...................................................................................... 5-12 The UNOS-V1.5 task switch ................................................................................. 5-13 Subtasks and Interrupt Tasks ...................................................................................... 5-14 Time-slicing ............................................................................................................... 5-15 Unos time slicing ................................................................................................... 5-16 Task suspension .......................................................................................................... 5-16 Dynamic Priorities .......................................................................................................... 5-18 Definitions Useful for Priority Inheritance ................................................................ 5-20 A General Priority Inheritance Algorithm ................................................................. 5-24 An Implementation of Priority Inheritance ........................................................... 5-25 Some Explicit Implementations ................................................................................. 5-29 The Inherit1 Algorithm .......................................................................................... 5-29 The Inherit2 Algorithm .......................................................................................... 5-29 The Inherit3 Algorithm .......................................................................................... 5-29 Interesting Benefit ...................................................................................................... 5-33 Higher Order Scheduling ................................................................................................ 5-33 Some Definitions and Background ................................................................................. 5-33 Basic Rate-Monotonic Analysis ..................................................................................... 5-35 Fundamental Results .................................................................................................. 5-36 Rate-Monotonic Scheduling ....................................................................................... 5-36 Utilisation Bound ................................................................................................... 5-36 Completion Time Test ........................................................................................... 5-37 Deadline-Monotonic Scheduling ........................................................................... 5-39 Relaxing the Assumptions – Generalised Rate-Monotonic Analysis ............................. 5-40 Task Synchronisation ................................................................................................. 5-40 Release Jitters ............................................................................................................. 5-44 Arbitrary Deadlines .................................................................................................... 5-44 Chapter 6 TASK COMMUNICATION AND TIMING .................. 6-1 Intertask Communications ................................................................................................ 6-1 Common Data Buffer Technique ................................................................................. 6-1 Circular Buffer Technique ............................................................................................ 6-2 Mail Box Technique ..................................................................................................... 6-3 Pointer Transfer Technique .......................................................................................... 6-3 Examples of Intertask Communication in Some Real Operating Systems .................. 6-4 Unix ......................................................................................................................... 6-4 UNOS ...................................................................................................................... 6-4 Timing in Real-time Systems ............................................................................................ 6-6 Time Delays ................................................................................................................. 6-7 UNOS timers ........................................................................................................... 6-8 UNOS timer creation ............................................................................................... 6-9 UNOS timer queues ................................................................................................. 6-9 UNOS timer maintenance routines .......................................................................... 6-9 UNOS user interface routines ................................................................................ 6-10 UNOS timed semaphore wait ................................................................................ 6-11 Applications of UNOS timers ............................................................................... 6-11 UNOS Heap Management .............................................................................................. 6-12 Chapter 7 INTERRUPTS ................................................................................ 7-1 Basic interrupt mechanism ................................................................................................ 7-1 Finer details of the interrupt mechanism .......................................................................... 7-2 Hardware structure of interrupts ....................................................................................... 7-3 Other types of interrupts ................................................................................................... 7-4 Interrupt priorities ............................................................................................................. 7-6 Interrupt tasks .................................................................................................................... 7-6 Interrupt handlers and the dispatcher ................................................................................ 7-7 Relaxing the “no task switch” condition ........................................................................... 7-9 Chapter 8 DEVICE DRIVERS ..................................................................... 8-1 What is a device driver? .................................................................................................... 8-1 Structure of a device driver ............................................................................................... 8-1 Design objectives and implications .................................................................................. 8-2 Anatomy of an I/O request ................................................................................................ 8-3 The device handler ............................................................................................................ 8-4 Buffering ........................................................................................................................... 8-6 Issues related to efficiency ................................................................................................ 8-7 Chapter 9 AN INTRODUCTION TO FILE SYSTEMS ............... 9-1 Logical Structure of File Systems ..................................................................................... 9-1 Basic Implementation ................................................................................................... 9-2 Sequential Files ....................................................................................................... 9-3 Indexed Sequential Access Method (ISAM) ........................................................... 9-4 Indexed file .............................................................................................................. 9-5 Directories .................................................................................................................... 9-6 Symbolic Naming .................................................................................................... 9-7 Access Rights .......................................................................................................... 9-7 Physical Structure of File Systems ................................................................................... 9-9 Physical Structure of Disks .......................................................................................... 9-9 Disk allocation table ................................................................................................... 9-10 Record blocking ......................................................................................................... 9-11 File allocation table .................................................................................................... 9-11 Some examples ........................................................................................................... 9-12 MS-DOS file allocation ......................................................................................... 9-12 FAT32 File System ..................................................................................................... 9-15 UNIX file allocation .............................................................................................. 9-16 Miscellaneous File System Issues ................................................................................... 9-17 Links ........................................................................................................................... 9-17 File System Consistency ............................................................................................ 9-18 Real-time issues ......................................................................................................... 9-19 Chapter 10 VIRTUAL MEMORY AND PROTECTION .............. 10-1 Virtual addressing - basic concepts. ................................................................................ 10-2 Virtual memory models ................................................................................................... 10-3 Segmentation - basic principles ...................................................................................... 10-3 Paging - basic principles ................................................................................................. 10-4 Some implementation issues ........................................................................................... 10-6 The best of both worlds - combined paging and segmentation ...................................... 10-9 Chapter 11 CASE STUDY - THE 80386-DX PROCESSOR . 11-1 Application Programmer’s Register Model .................................................................... 11-1 System Programmer’s Register Model ........................................................................... 11-2 System Flags .............................................................................................................. 11-3 Memory-Management Registers ................................................................................ 11-4 Control Registers ........................................................................................................ 11-5 Segmented Address Models in the 80386 ....................................................................... 11-6 Flat Model ............................................................................................................. 11-6 Protected Flat Model ............................................................................................. 11-6 Multi-Segment Model ........................................................................................... 11-6 Segment Translation in the 80386 .............................................................................. 11-6 Segment Selectors ................................................................................................. 11-7 Segment Descriptors .............................................................................................. 11-8 Paging in the 80386 ...................................................................................................... 11-10 Translation Lookaside Buffer. ............................................................................. 11-12 Combined Segment and Page Translation .................................................................... 11-12 The Flat Paging Model – Getting rid of Segmentation ............................................ 11-12 Segmentation and Paging Together .......................................................................... 11-12 Protection in the 80386 Processor ................................................................................. 11-14 Segment-level Protection .............................................................................................. 11-15 Segment Descriptors and Protection ........................................................................ 11-15 Type Checking ..................................................................................................... 11-15 Limit Checking .................................................................................................... 11-17 Privilege Levels ................................................................................................... 11-17 Restricting Access .................................................................................................... 11-19 Restricting Access to Data ................................................................................... 11-19 Restricting Control Transfers .............................................................................. 11-20 Gate Descriptors ....................................................................................................... 11-21 Stack Switching ................................................................................................... 11-24 Returning from a Procedure ................................................................................ 11-26 Pointer Validation .......................................................................................................... 11-27 Descriptor Validation ............................................................................................... 11-27 Pointer Integrity and RPL ........................................................................................ 11-27 Page Level Protection ................................................................................................... 11-28 Restriction of Addressable Domain ......................................................................... 11-29 Multitasking Support in the 80386 ............................................................................... 11-29 Task State Segment .................................................................................................. 11-31 TSS Descriptor ......................................................................................................... 11-32 Task Register ............................................................................................................ 11-33 Task Gate Descriptor ................................................................................................ 11-33 Task Switching ......................................................................................................... 11-33 Task Linking ............................................................................................................. 11-35 Busy Bit ............................................................................................................... 11-35 Task Address Space .................................................................................................. 11-35 Protected Input/Output .................................................................................................. 11-36 I/O Addressing in the 80386 .................................................................................... 11-36 Protection and I/O .................................................................................................... 11-37 I/O Privilege Level .............................................................................................. 11-37 I/O Permission Bit Map ....................................................................................... 11-37 Conclusion .................................................................................................................... 11-37 Appendix A UNOS-V2.0 TUTORIAL .................................................... A-1 Introduction ...................................................................................................................... A-1 General Layout of UNOS Based Software ...................................................................... A-2 Detailed Example of the Use of UNOS ........................................................................... A-3 The Main Module ........................................................................................................ A-3 The Initunos Module ................................................................................................... A-6 Usertask Module ....................................................................................................... A-14 Task Modules ............................................................................................................ A-18 Task0 Module ....................................................................................................... A-18 Task1 Module ....................................................................................................... A-29 Task 2 Module ...................................................................................................... A-31 Header Files .............................................................................................................. A-34 unos.h .................................................................................................................... A-34 general.h ............................................................................................................... A-41 Common Header Files for User Tasks ...................................................................... A-42 comdec.h ............................................................................................................... A-42 pcscrn.h ................................................................................................................. A-42 tasks.h ................................................................................................................... A-42 time.h .................................................................................................................... A-42 Other important header files ...................................................................................... A-42 hwpc.h .................................................................................................................. A-42 config_os.h ........................................................................................................... A-42 Appendix B A SIMPLE WINDOW SYSTEM ................................... B-1 Introduction ...................................................................................................................... B-1 User Functions ................................................................................................................. B-2 How to Use the Windowing System ................................................................................ B-9 A Note on Setting Character Attributes .................................................................... B-11 Reference Section .......................................................................................................... B-14 The Point Class .............................................................................................................. B-14 POINT:OINT DESCRIPTION .............................................................................. B-14 POINT::OPERATOR= DESCRIPTION ................................................................... B-15 POINT::MOVETO DESCRIPTION ......................................................................... B-16 POINT::SET_ATTRIBUTES DESCRIPTION ......................................................... B-17 POINT::WRITE_TO_PT DESCRIPTION ................................................................ B-18 POINT::RETURN_ATTRIBUTES ........................................................................... B-19 The Screen Class ............................................................................................................ B-20 SCREEN::SCREEN DESCRIPTION ....................................................................... B-21 SCREEN:RINT DESCRIPTION ........................................................................... B-22 SCREEN::CLR_LINE DESCRIPTION .................................................................... B-23 SCREEN::CLR_EOL DESCRIPTION ..................................................................... B-24 SCREEN::CLR_BOL DESCRIPTION ..................................................................... B-25 SCREEN::CLR_SCR DESCRIPTION ..................................................................... B-26 SCREEN::SET_SCRN_BACKGRD DESCRIPTION ............................................. B-27 SCREEN:RAW_BOX DESCRIPTION ................................................................ B-28 SCREEN:RAW_HORIZ_LINE DESCRIPTION ................................................. B-29 SCREEN:RAW_VERT_LINE DESCRIPTION ................................................... B-30 SCREEN::SCROLL DESCRIPTION ....................................................................... B-31 Window Class ................................................................................................................ B-32 WINDOW::WINDOW DESCRIPTION ................................................................... B-33 WINDOW::CLEAR_WINDOW DESCRIPTION .................................................... B-34 WINDOW::SET_WINDOW_BACKGRD_COLOUR DESCRIPTION .................. B-35 WINDOW:AINT_WINDOW_BACKGRD DESCRIPTION ................................ B-36 WINDOW::GO_WINDOW_XY DESCRIPTION ................................................... B-37 WINDOW::PRINT DESCRIPTION ......................................................................... B-38 WINDOW::PRINT DESCRIPTION ......................................................................... B-39 WINDOW::SET_STRING_COLOUR DESCRIPTION .......................................... B-40 WINDOW::SET_STRING_BACKGRD_COLOUR DESCRIPTION ..................... B-41 WINDOW::SCROLL_WINDOW DESCRIPTION .................................................. B-42 WINDOW::WINDOW_CLEAR_LINE DESCRIPTION ......................................... B-43 WINDOW::GET_TOP/BOT_LEFT/RIGHT/XY DESCRIPTIONS ........................ B-44 WINDOW::DRAW_HORIZ_LINE DESCRIPTION ............................................... B-45 WINDOW::DRAW_VERT_LINE DESCRIPTION ................................................. B-46 Appendix C UNOS-V2.0 REFERENCE MANUAL ...................... C-1 INTRODUCTION ........................................................................................................... C-1 KEY FEATURES ............................................................................................................. C-2 REAL-TIME SERVICES ................................................................................................ C-2 HARDWARE REQUIREMENTS ................................................................................... C-4 INTRODUCTION TO UNOS-V2.0 KERNEL PRIMITIVES ........................................ C-4 Kernel Initialisation ..................................................................................................... C-4 setup_os_data_structures ........................................................................................ C-4 Task Management ....................................................................................................... C-5 create_task .............................................................................................................. C-5 change_task_priority .............................................................................................. C-5 rtn_task_priority ..................................................................................................... C-5 start_tasks ............................................................................................................... C-5 rtn_current_task_num ............................................................................................. C-5 rtn_current_task_name_ptr ..................................................................................... C-5 chg_task_tick_delta ................................................................................................ C-5 Task scheduling management ...................................................................................... C-5 preemptive_schedule .............................................................................................. C-5 reschedule ............................................................................................................... C-5 start_time_slice ....................................................................................................... C-5 stop_time_slice ....................................................................................................... C-5 chg_base_ticks_per_time_slice .............................................................................. C-5 Time management ....................................................................................................... C-5 create_timer ............................................................................................................ C-5 start_timer ............................................................................................................... C-6 reset_timer .............................................................................................................. C-6 stop_timer ............................................................................................................... C-6 Intertask communication and synchronization ............................................................ C-6 create_semaphore ................................................................................................... C-6 init_semaphore ....................................................................................................... C-6 wait ......................................................................................................................... C-6 timed_wait .............................................................................................................. C-6 usignal ..................................................................................................................... C-6 return_semaphore_value ......................................................................................... C-6 create_lock .............................................................................................................. C-6 lock ......................................................................................................................... C-6 unlock ..................................................................................................................... C-6 send_mess ............................................................................................................... C-6 send_qik_mess ........................................................................................................ C-6 rcv_mess ................................................................................................................. C-7 size_mbx ................................................................................................................. C-7 size_mbx_mess ....................................................................................................... C-7 free_mbx ................................................................................................................. C-7 used_mbx ................................................................................................................ C-7 flush_mbx ............................................................................................................... C-7 Memory Management ................................................................................................. C-7 umalloc ................................................................................................................... C-7 ucalloc ..................................................................................................................... C-7 ufree ........................................................................................................................ C-7 ret_free_mem .......................................................................................................... C-7 Miscellaneous .............................................................................................................. C-7 return_interrupt_status ............................................................................................ C-7 DETAILED DESCRIPTION OF USER INTERFACE ................................................... C-7 Kernel Initialisation Functions ......................................................................................... C-8 SETUP_OS_DATA_STRUCTURES DESCRIPTION ............................................... C-8 Task Management Functions ........................................................................................... C-9 CREATE_TASK DESCRIPTION ............................................................................... C-9 CHANGE_TASK_PRIORITY DESCRIPTION ....................................................... C-11 RTN_TASK_PRIORITY DESCRIPTION ................................................................ C-12 START_TASKS DESCRIPTION .............................................................................. C-13 RTN_CURRENT_TASK_NUM DESCRIPTION .................................................... C-14 RTN_CURRENT_TASK_NAME_PTR DESCRIPTION ......................................... C-15 CHG_TASK_TICK_DELTA DESCRIPTION .......................................................... C-16 Task Scheduling Management Functions ...................................................................... C-17 PREEMPTIVE_SCHEDULE DESCRIPTION ......................................................... C-17 RESCHEDULE DESCRIPTION .............................................................................. C-18 START_TIME_SLICE DESCRIPTION ................................................................... C-19 STOP_TIME_SLICE DESCRIPTION ..................................................................... C-20 CHG_BASE_TICKS_PER_TIME_SLICE DESCRIPTION .................................... C-21 Time Management Functions ......................................................................................... C-22 CREATE_TIMER DESCRIPTION .......................................................................... C-22 START_TIMER DESCRIPTION ............................................................................. C-23 RESET_TIMER DESCRIPTION ............................................................................. C-24 STOP_TIMER DESCRIPTION ................................................................................ C-25 Intertask Communication and Synchronization Functions ............................................ C-26 CREATE_SEMAPHORE DESCRIPTION ............................................................... C-26 INIT_SEMAPHORE DESCRIPTION ...................................................................... C-27 WAIT DESCRIPTION .............................................................................................. C-28 TIMED_WAIT DESCRIPTION ............................................................................... C-29 USIGNAL DESCRIPTION ...................................................................................... C-30 RETURN_SEMAPHORE_VALUE DESCRIPTION ............................................... C-31 CREATE_LOCK DESCRIPTION ............................................................................ C-32 LOCK DESCRIPTION ............................................................................................. C-33 UNLOCK DESCRIPTION ....................................................................................... C-34 DESTROY_LOCK DESCRIPTION ......................................................................... C-35 SEND_MESS DESCRIPTION ................................................................................. C-36 SEND_MESS MACRO DESCRIPTION ................................................................. C-37 SEND_MESS_NB MACRO DESCRIPTION .......................................................... C-38 SEND_QIK_MESS DESCRIPTION ........................................................................ C-39 RCV_MESS DESCRIPTION ................................................................................... C-40 SIZE_MBX DESCRIPTION .................................................................................... C-41 SIZE_MBX_MESS DESCRIPTION ........................................................................ C-42 FREE_MBX DESCRIPTION ................................................................................... C-43 USED_MBX DESCRIPTION .................................................................................. C-44 FLUSH_MBX DESCRIPTION ................................................................................ C-45 Memory Management Functions ................................................................................... C-46 UMALLOC DESCRIPTION .................................................................................... C-46 UCALLOC DESCRIPTION ..................................................................................... C-47 UFREE DESCRIPTION ........................................................................................... C-48 RET_FREE_MEM DESCRIPTION ......................................................................... C-49 Miscellaneous Functions ................................................................................................ C-50 RETURN_INTERRUPT_STATUS DESCRIPTION ................................................ C-50 Appendix D THE KEYBOARD SYSTEM ........................................... D-1 Introduction ...................................................................................................................... D-1 Organisation and Use ....................................................................................................... D-1 Appendix E UNOS SERIAL HANDLERS ..........................................E-1 INTRODUCTION ............................................................................................................E-1 USER INTERFACE ..........................................................................................................E-1 SETTING UP AND USING THE SERIAL CHANNELS ...............................................E-2 The header files ............................................................................................................E-2 USER INTERFACE DESCRIPTION ...............................................................................E-4 CREATE_SERIAL DESCRIPTION ............................................................................E-4 Appendix F COMMAND DECODER .....................................................F-1 Introduction .......................................................................................................................F-1 Basic Data Structures ........................................................................................................F-1 Decoder Tables - How to use them ..............................................................................F-3 Example ...................................................................................................................F-6 Appendix G UNOS ALARM HANDLING ........................................... G-1 Features ............................................................................................................................ G-1 Data structures used in the Alarm System ....................................................................... G-1 The Alarm Number Array ........................................................................................... G-2 The Alarm Structure. ................................................................................................... G-2 Ancillary Data Structures ............................................................................................ G-3 Low Level User Interface to the Alarm System .............................................................. G-5 finCreateAlarm Description ........................................................................................ G-5 fulgAlarm Description ................................................................................................ G-6 fulgRetNumUnresetAlarms Description ..................................................................... G-7 fulgResetAlarm Description ........................................................................................ G-8 fvdStopConseqSuppr Description ............................................................................... G-9 fulgResetAllAlarmsClearOnce Description .............................................................. G-10 fulgRetCumulAlarms Description ............................................................................ G-11 finRetCumulStartTimeDate Description ................................................................... G-12 finRetFirstUnresetAlarmTimeDate Description ....................................................... G-13 finRetLastUnrestAlarmTimeDate Description ......................................................... G-14 finClearCumulAlarms Description ........................................................................... G-15 Some Notes About Usage .............................................................................................. G-16 Appendix H UNOS-V2.0 LISTING .......................................................... H-1 CONDITIONS OF USE OF THE UNOS KERNEL ....................................................... H-1 MISCELLANEOUS INFORMATION ............................................................................ H-1 FULL KERNEL LISTING .............................................................................................. H-3 BIBLIOGRAPHY........................................................................................................ b-i |