1.44k likes | 1.69k Views
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads. 主講人:虞台文. Content. Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads Process and Thread Descriptors
E N D
Operating Systems PrinciplesProcess Management and CoordinationLecture 4:The Operating System Kernel:Implementing Processes and Threads 主講人:虞台文
Content • Kernel Definitions and Objects • Queue Structures • Threads • Implementing Processes and Threads • Process and Thread Descriptors • Implementing the Operations • Implementing Sync/Comm Mechanisms • Semaphores and Locks • Building Monitor Primitives • Clock and Time Management • Communications Kernel • Interrupt Handling
Operating Systems PrinciplesProcess Management and CoordinationLecture 4:The Operating System Kernel:Implementing Processes and Threads Kernel Definitions and Objects
Windows Kernel Hardware dependent functions are placed in the kernel.
OS Kernel • A basic set of objects, primitives, data structures, processes from which the remainder of the system may be built on its top. • In other words, the kernel transforms the hardware into an OS’s machine.
OS’s Machine I am staying on the top of an OS machine.
Kernel Objects • Kernel defines/provides mechanisms to implement various policies. • Four classes of possible functions and objects in a kernel: • Process and thread management • Interrupt and trap handling • Resource management • Input/output
Process and thread management • Interrupt and trap handling • Resource management • Input/output Kernel Objects • Process and thread management • Process Creation • Process Destruction • Process Communication/Synchronization
Process and thread management • Interrupt and trap handling • Resource management • Input/output Kernel Objects • Interrupt and trap handling • Responding to signals triggered by various system events. • Some system events: • Process termination • I/O completion • Time-out of clock • Error • Hardware malfunction
CPU I/O Processor Done Interrupt • Process and thread management • Interrupt and trap handling • Resource management • Input/output Kernel Objects • Interrupt and trap handling • Responding to signals triggered by various system events. ...... ...... ...... ...... Do_I/O ...... Start I/O Interrupt Service Routine
Process and thread management • Interrupt and trap handling • Resource management • Input/output Kernel Objects • Resource management • Primitives for maintaining, allocating, and releasing system resources. • Some system resources: • CPUs • Timers • Main memory • Secondary storage • I/O devices • Files
Process and thread management • Interrupt and trap handling • Resource management • Input/output Kernel Objects • Input/output • Read, write, and control operations for initiating and supervising the transfer of data between I/O devices and main memory or registers.
Main Topics in the Lecture • Process and thread management • Interrupt and trap handling • Resource management • Input/output Main topics
p1 pj pn qm q1 child processes Process Creation Hierarchy Kernel ps OS process user 1 login user j login user n login user processes . . . . . . application processes. . . . Interaction with kernel objects
Operating Systems PrinciplesProcess Management and CoordinationLecture 4:The Operating System Kernel:Implementing Processes and Threads Queue Structures
Queues • OS needs many different queues • e.g., ready queues, wait queues. • Single-level queues • Implemented as array • Fixed size • Efficient for simple FIFO operations • Implemented as linked list • Unbounded size • More overhead, but more flexible operations
Single-Level Queues Circular Array Implementation Link List Implementation
Priority Queues Array indexed by priority
Priority Queues Binary heap of priority
Priority Queues Binary heap of priority Array implementation of binary heap
Operating Systems PrinciplesProcess Management and CoordinationLecture 4:The Operating System Kernel:Implementing Processes and Threads Threads
Lowest Address, e.g., 00000000 Highest Address, e.g., FFFFFFFF Virtual Memory Address Spaces Memory
Lowest Address, e.g., 00000000 Starting Address of all processes Highest Address, e.g., FFFFFFFF Virtual Memory Address Spaces Memory OS User Programs
OS OS OS OS User Programs Process 1 Process 2 Process n Only one process can be activated at a time. Each process thinks that it owns all memory. Processes Their address spaces are different.
OS OS OS OS User Programs Process 1 Process 2 Process n Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching Context Switching Context Switching
OS OS OS OS User Programs Process 1 Process 2 Process n Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching The context switching among processes, i.e., to change address space, is very time consuming.
OS OS OS OS User Programs Process 1 Process 2 Process n Threads • Each process can have multiple threads. • They share the same address space. • The context switching among threads in the process is efficient. • Lightweight process Mesa
Processes and Threads • Process has one or morethreads • All threads in a process share: • Memory space • Other resources • Each thread has its own: • CPU state(registers, program counter) • Stack • Threads are efficient, but lackprotection from each other
Microsoft Windows Process & Thread Functions OS Support for Processes/Threads • Create a new Process/thread • Initiate or make a thread ready • Destroy or terminate a thread • Delay or put a thread to sleep for a given amount of time • Synchronize threads through semaphore, events, or condition variables • Perform lower-level operations, such as blocking, suspending, or scheduling a thread.
Operating Systems PrinciplesProcess Management and CoordinationLecture 4:The Operating System Kernel:Implementing Processes and Threads Implementing Processes and Threads
Process and Thread Descriptors • System needs some data structures to keep track the state and miscellaneous information, e.g., identification, resources used, accounting information, of processes and treads. • In the following, we are dealing with a system composed solely of processes, much of concept will also apply to threads.
Process Identification A system-wide unique identifier.
CPU’s State Contain necessary data, e.g., program counter, data register, and flag register, to restart the process at the point of last interruption.
Processor ID To identify the processor that is executing the process. Make sense only for multiprocessor system.
Memory Memory map information. Physical Memory Virtual Memory
blocked running ready Point to the list, e.g., ready list or wait list, on which the process may reside. Status
running Request Scheduler ready blocked Create Release More on Status • Basic process status • running, ready, and blocked • State transition diagram
Suspend Thread Process Activation and Suspension Resume Thread • Some applications require a process (or thread) can be suspended by programs. • For examples • Suspension of a debugging program • Needed by the internal, e.g., to detect or prevent a deadlock.
The Finer State Transition Diagram Active Processes Suspended Processes
Creation Tree A link list of PCBs of the child processes Point to the PCB of the parent process.
Priority Two methods: • Single-integer value • Two-leveled valued • Base priority + Changeable part Used by scheduler to decide which process should be running next.
Priority Two methods: • Single-integer value • Two-leveled valued • Base priority + Changeable part Windows NT priority classes
Others • CPU time used • Time remaining • Resource used • Resource claimed • Resource quotas • Number of I/O requests since creation • . . .