1.67k likes | 1.86k Views
Operating Systems. CST 352 Processes and Threads. Topics. Definitions Communications Process to Process Process to Thread Thread to Thread Scheduling. Definitions - Prelims. Concurrency – The appearance that threads are running simultaneously even though there is a single CPU.
E N D
Operating Systems CST 352 Processes and Threads CST 352 - Operating Systems
Topics • Definitions • Communications • Process to Process • Process to Thread • Thread to Thread • Scheduling CST 352 - Operating Systems
Definitions - Prelims Concurrency – The appearance that threads are running simultaneously even though there is a single CPU. CST 352 - Operating Systems
Definitions - Prelims Context – The “processor” state of a block of executing code. This includes all registers required to uniquely identify this chain of execution. CST 352 - Operating Systems
Definitions - Process Process – A group of instructions along with the context defining the execution “state (s)” of those instructions. Q: How does the concept of a “process” and the code that describes the process parallel the concept of a “class” and an “object”? CST 352 - Operating Systems
Definitions - Process A process is an abstraction defining processor execution resource grouping. In the days of “batch” processing, processes executed to completion before another process was loaded, then started. CST 352 - Operating Systems
Definitions - Process Batch Processing – What had to happen? • Operator is waiting for input (reading newspaper) • User writes a program on punch cards. • Operator takes a tray of punch cards from a stack of jobs. • Operator loads the cards into the system tray – computer reads each card into memory. • Operator starts the job. • System compiles the code in the job. • System jumps to the first machine instruction of compiled code. • Job runs to completion. • Operator places output in out box. – Go to 1 CST 352 - Operating Systems
Definitions - Process Batch Processing – What had to happen? The computer is only involved in steps 4, 6,7 , and 8. Where did the OS reside? CST 352 - Operating Systems
Definitions - Process Synchronous Processing – What had to happen? • System is waiting for input. • User writes program and compiles it on the computer. • User starts execution of the written program. • System loads program into memory for execution. • System jumps to the first instruction of program. • Program runs to completion. • System jumps back to state of waiting for input. CST 352 - Operating Systems
Definitions - Process Preemptive Multitasking – What has to happen? • System is waiting for input (running SETI program). • User writes program and compiles it on the computer. • User starts execution of the written program. • System loads next program into memory for execution. • System jumps to the first instruction of program. • Program runs to till time slice time-out or program done. • System jumps back to “A”. CST 352 - Operating Systems
Definitions - Thread In an older OS, each process had: • Address space • stack • heap • Single “thread” of control. • Serial execution of instructions in the executing program. CST 352 - Operating Systems
Definitions - Thread A “thread” breaks the grouping provided by a process of “resources and execution”. A process main thread can “spawn” off threads. Each thread the process spawns will share the resources of the process. CST 352 - Operating Systems
Definitions - Thread Q: • Define Process • Define Thread • Define Context CST 352 - Operating Systems
Processes and Threads Processes: • Create primary thread • Set up run-time stack. • Set up memory segment. • Create context. • Set ready for execute. CST 352 - Operating Systems
Processes and Threads Processes: • A process has a memory segment assigned to it. • A process has a primary run-time stack assigned to it (used by the primary thread). • Processes must communicate with special mechanism. • A process has protected code segment. • A process has a protected memory segment. • The OS must provide a special address space for interprocess communication. CST 352 - Operating Systems
Processes and Threads Processes and Threads: • Create Thread • Set up run-time stack. • Create context. • Set ready for execute. • Execution is controlled by parent process. • Parent process must retain a leash on child threads. • Threads may communicate through the process memory segment. CST 352 - Operating Systems
Processes and Threads Threads: • Threads share the memory segment of a process (data and code). • A Thread is assigned it’s own run-time stack by the Operating System. • Threads can communicate with other threads contained in the same process using data structures in the process memory segment. • The data structures must be “thread-safe”. • The process and thread code must be written “thread-safe”. CST 352 - Operating Systems
Processes and Threads A preemptive multitasking OS is made up of several processes: • Foreground processes – those that require user interaction (shell, GUI, etc.). • Background processes – those that run in the background, performing their jobs without user intervention (mailers, network monitors, printing monitors, etc). CST 352 - Operating Systems
Processes and Threads Background processes that help the OS with some task are called daemons (not demons). Daemon - an attentive benevolent entity. An intermediary between gods and men. CST 352 - Operating Systems
Processes and Threads Process/Thread States: • Suspend – Processes that are waiting for some event to occur (i.e. I/O, Time of Day, etc.) • Active State – Process is ready to run. Waiting for CPU. • Execute State – Switched into the CPU and has control of the execution unit. • Blocked State – Process that are waiting for access to a dynamic resource. CST 352 - Operating Systems
Processes and Threads Process/Thread/Fiber States: Keep in mind the processing hierarchy: Processes contain threads Threads contain fibers • Process state change implies thread state change • Thread state change implies fiber state change The inverse is not true. CST 352 - Operating Systems
Processes and Threads Process State Transition Analysis: Process Creation: Scheduler creates the process control block (TCB) and places it in the suspend list. • Create process data segment. • Create process code segment. • Load op codes from disk into memory • Build run-time stack. CST 352 - Operating Systems
Processes and Threads Process Suspend - Activation: Process System Call is done and process is ready for execute. Scheduler removes the TCB from the suspend list and places it on the active list. CST 352 - Operating Systems
Processes and Threads Process Dispatch: TCB is switched into the CPU based on the scheduling algorithm. • Round Robin • Priority Based • Etc. CST 352 - Operating Systems
Processes and Threads Process Preempt: TCB is switched out of the CPU. Scheduler uses the scheduling algorithm to determine next TCB to be switched in. CST 352 - Operating Systems
Processes and Threads Process Execute - Block: Process requests some unavailable resource. TCB is switched out of the CPU and placed in the “blocked” list. CST 352 - Operating Systems
Processes and Threads Process Execute - Suspend: Process makes a system call that requires a lengthy operation. TCB is switched out of the CPU and placed in the “Suspend” list. CST 352 - Operating Systems
Processes and Threads Process Suspend - Blocked: Process system call is done but the resource required to complete the call is unavailable. TCB is moved from the “Suspend” list to the “Blocked” list. CST 352 - Operating Systems
Processes and Threads Process Block-Activated: Requested resource becomes available. TCB is moved from the Blocked list back to the Active List. CST 352 - Operating Systems
Processes and Threads Process Active - Block: Currently dispatched process “Blocks” an active process. TCB is moved from the Active list to the Blocked List. CST 352 - Operating Systems
Processes and Threads Process Active - Suspend: Currently dispatched process “Suspends” and active process. TCB is moved from the Active list to the Suspend List. CST 352 - Operating Systems
Processes and Threads Process Terminate: Process is terminated by another process. Process exits. • Process resources are returned to the OS. • Process memory segment is cleaned up. • Process run-time stack is cleaned up. CST 352 - Operating Systems
Processes and Threads Q: • What states are required for implementation of a multitasking kernel? CST 352 - Operating Systems
Processes and Threads • Processes must be managed in kernel space. Why is this the case? • It is possible to manage threads in user space. Why is this the case? • What are advantages of managing threads in user space vs. kernel space? CST 352 - Operating Systems
OS Requirements for Process Implementation To exist, a process needs: • Process Control Block – Contains the context of the process main thread. • Process Code Segment – Contains the code relocated to a physical address space. • Process Data Segment – Contains the memory “heap” assigned to the process. • Process Run-time Stack – Contains the call stack assigned to the process main thread. CST 352 - Operating Systems
Processes and Threads When a process gets suspended it can be moved out of physical memory. This activity is called “swapping”. Don’t confuse swapping with virtual memory. CST 352 - Operating Systems
Processes and Threads Swapping of a process involves taking all the process components (TCBs, Code Segment, Data Segment, Heap, and run-time stack(s)) and moving them from memory to disk. CST 352 - Operating Systems
Processes and Threads Paging of a process involves taking a piece of a process (e.g. a page of memory) and moving it from memory to disk. CST 352 - Operating Systems
Processes and Threads The disk space where the process components get written to disk is called the swap file (page file virtual memory systems). • Windows 7 uses virtual memory. The page file is normally on the “C:” drive. • UNIX sets aside a partition on the hard drive. CST 352 - Operating Systems
Processes and Threads An OS that has a poorly tuned process management scheme will do what is known as “thrashing”. Thrashing – The OS spends more time moving process elements from disk to memory and back again than it does running processes. CST 352 - Operating Systems
Processes and Threads Q: • Define Swapping • Define Paging • Does swapping require virtual memory? Why or why not? CST 352 - Operating Systems
OS Requirements for Multitasking Implementation • Data Structures of TCBs for process control: • Suspend List (a linked list) • Active Queue (priority queues, circular linked list) • Blocked List (a table of queues keyed on a resource ID) CST 352 - Operating Systems
OS Requirements for Multitasking Implementation • Interrupt Service routine to handle: • I/O devices • CPU interrupts • Interrupt asserted for task switching • This ISR calls the dispatcher CST 352 - Operating Systems
OS Requirements for Thread Implementation Kernel Space Threads: • Process needs to maintain some reference to threads it owns in a thread table associated with the TCB. • The Kernel schedules threads based on run-time activity of controlling processes. CST 352 - Operating Systems
OS Requirements for Thread Implementation User Space Threads: • Process needs to maintain some reference to threads it owns in a thread table. • User space system calls are made to perform thread management. These calls cause the process to change “thread” context. • The kernel only handles the scheduling of processes. • User space thread management is analogous to “Cooperative Multitasking”. If a thread in a process “runs amok”, the process threads will starve. CST 352 - Operating Systems
OS Requirements for Thread Implementation Hybrid Threads (one-scheme): • Processes live in kernel space. • Threads are managed in kernel space. • User space threads are provided, known as “light-weight” threads (fibers). CST 352 - Operating Systems
OS Requirements for Thread Implementation Hybrid Threads (another scheme): • Processes live in kernel space. • Threads live in user space. • When a thread blocks, it gives the kernel what is basically a pointer to a call-back routine. The kernel then calls the “call-back” when the thread resource become available. CST 352 - Operating Systems
Process Synchronization Race Conditions: Two or more threads are reading or writing to a shared resource. The final result depends on who writes at what precise time. CST 352 - Operating Systems
Process Synchronization Race Conditions: - Example • Process Thread A writes to a memory area then gets switched out. • Process Thread B writes to the same memory area then gets switched out. • Process Thread A gets switched back in and uses the memory area to make some calculation. • Process Thread A has no knowledge that Process Thread B has changed the value used for the calculation. CST 352 - Operating Systems
Process Synchronization Race Conditions: - Example Consider the function: void swap( int& intOne, int& intTwo ) { static int temp; temp = intTwo; intTwo = intOne; intOne = temp; } CST 352 - Operating Systems