Processes and Threads
Total Page:16
File Type:pdf, Size:1020Kb
9/17/15! DISTRIBUTED COMPUTER SYSTEMS PROCESSES AND THREADS Dr. Jack Lange Computer Science Department University of Pittsburgh Fall 2015 Outline n" Heavy Weight Processes n" Threads and Thread Implementation n" User Level Threads n" Kernel Level Threads n" Light Weight Processes n" Scheduler Activation n" Client/Server Implementation n" Multithreaded Server n" Xwindow n" Server General Design Issues 1! 9/17/15! Process and Process Management PROCESS CONCEPT Process Concept n" A primary task of an operating system is to execute and manage processes n" What is a program and what is a process? n" A program is a specification, which contains data type definitions, how data can be accessed, and set of instructions that when executed accomplishes useful “work” n" A sequential process is the activity resulting from the execution of a program with its data by a sequential processor n" Program is passive, while a process is active A process is an instance of a program in execution! 2! 9/17/15! Process Traditional Structure n" Address Space n" Code – Binary code n" Static and dynamic data n" Execution stack – Contains data such as subroutine parameter, return address, and temporary variables n" Process CPU State n" Process Status Word (PSW) – Execution mode, Last operation outcome, Interrupt level, … n" Instruction Register (IR) – Current instruction being executed n" Program Counter (PC) – Next instruction to be executed n" Stack pointer (SP) n" General purpose registers Process and Process Management PROCESS ADDRESS SPACE 3! 9/17/15! Memory Layout 0xffffffff! 0xffffffff! ! ! ! Kernel Space ! ! ! ! (1GB) ! 0xc0000000! ! Kernel Space ! ! ! ! (2GB) ! ! ! ! ! ! ! 0x80000000! ! ! ! ! ! User Mode Space ! ! ! ! (3GB) ! ! ! User Mode Space ! ! ! ! (2GB) ! ! ! ! ! ! 0! 0! 32bit Linux Kernel/User 32bit Windows Default Memory Split Memory Split Process Address Space Max Name Interpretation Stack Code Segment (Text) Program executable statements Data Segment 1."Initialized Data 1."Static or global data initialized with non-zero values 2." Zero-Initialized or Block 2."Uninitialized static or global Started by Symbol (BSS) data data Heap Categories occupy a single contiguous area. Data Heap Dynamic memory allocation Stack Segment •" Local variables of the scope. Text •" Function parameters 0 4! 9/17/15! Process and Process Management PROCESS STATE Process States n" Process in one of 5 states Created! n" Created n" Ready 1! n" Running n" Blocked Ready! 2! n" Exit n" Transitions between states 5! 1 - Process enters ready queue 3! 2 - Scheduler picks this process Blocked# Running! 3 - Scheduler picks a different (waiting)! process 4! 4 - Process waits for event (such as I/O) 5 - Event occurs 7! 6 - Process exits 6! 7! 7 - Process ended by another process Exit! 5! 9/17/15! Processes Representation n" To users and to other processes, a process is identified by its unique Process ID (PID) n" PID <= 30,000 in Unix n" In the OS, processes are represented by entries in a Process Table (PT) n" PID is index to (or pointer to) a PT entry n" PT entry = Process Control Block (PCB) n" PCB is a large data structure that contains or points to all information about the process n" Linux, defined in task_struct – It contains about 70 fields n" NT – defined in EPROCESS – It contains about 60 fields What’s In A Process Table Entry? Process management! File management! May be# Registers! Root directory! stored# Program counter! Working (current) directory! on stack! CPU status word! File descriptors! Stack pointer! User ID! Process state! Group ID! Priority / scheduling parameters! Process ID! Parent process ID! Memory management! Signals! Pointers to text, data, stack! Process start time! !or! Total CPU usage! Pointer to page table! 6! 9/17/15! Process Information Process ID! UID GID EUID EGID CWD! Memory Map! Signal Dispatch Table! Priority! Signal Mask! Stack! File Descriptors! CPU State! Process CPU State l" The CPU state is defined by the registers’ contents n" Process Status Word (PSW) n" exec. mode, last op. outcome, interrupt level n" Instruction Register (IR) n" Current instruction being executed n" Program counter (PC) n" Stack pointer (SP) n" General purpose registers 7! 9/17/15! Process control block (PCB) PCB kernel user CPU state PSW memory text IR files data accounting PC priority heap SP user general CPU registers purpose storage stack registers Operating System Control Structure Memory Table! Process 1 PCB Process Table! Process 1 Memory Process 2 Process 1 PCB Processes Process n Device Table! Devices Files Process 1 PCB File Table! 8! 9/17/15! Process and Process Management CONTEXT SWITCHING Processes in the OS n" Two “layers” for processes n" Lowest layer of process-structured OS handles interrupts, scheduling n" Above that layer are sequential processes n" Processes tracked in the process table n" Each process has a process table entry Processes! 0 1 …! N-2 N-1 Scheduler 9! 9/17/15! Process Management n" In multi-programming and time sharing environments, process management is required n" A program may require multiple processes n" Many processes may be instances of the same program n" Many processes from different programs can run simultaneously n" How does the OS manage multiple processes running concurrently? n" What kind of information must be kept? n" What functions must the OS perform to run processes correctly. Switching Between Processes n" An important task of the OS is to manage CPU allocation among concurrent processes n" When the OS takes away the CPU from a running process, the OS must perform a switch between the processes n" This referred to as context switch n" It is also referred to as a “state save” and “state restore” n" What is a context? n" What operations must take place to achieve this switch? n" How can the OS guarantee that the interrupted process can resume correctly? 10! 9/17/15! Process Context n" Process Context is the necessary information of the current process operational state that must be stored in order to later resume process operation from an identical position as when the process was interrupted. n" Context information is system dependent and typically includes: n" User Level Context n" Register Level Context n" System Level Context n" Address space, stack space, Program Counter (PC), Stack Pointer (SP), Instruction Register (IR), Program Status Word (PSW) and other general processor registers, profiling or accounting information, associated kernel data structures and current state of the process CPU Switch From Process to Process 11! 9/17/15! Process Context Switch n" Save processor context, including program counter and other registers n" Update the process control block with the new state and any accounting information n" Move process control block to appropriate queue - ready, blocked n" Select another process for execution n" Update the process control block of the process selected n" Update memory-management data structures n" Restore context of selected process and set the PC to the that process code – typically, implicit in the interrupt return instruction Context Switch Design Issues n" Context can be costly and must be minimized n" Context switch is purely system overhead, as no “useful” work accomplished during context switching n" The actual cost depends OS and on the support provided by the hardware n" The more complex the OS and the PCB -> longer the context switch n" Some hardware provides multiple sets of registers per CPU, allowing multiple contexts to be loaded at once n" A “full” process switch may require a significant number of instruction execution. 12! 9/17/15! What Happens On A Trap/Interrupt? 1." Hardware saves program counter (on stack or in a special register) 2." Hardware loads new PC, identifies interrupt 3." Assembly language routine saves registers 4." Assembly language routine sets up stack 5." Assembly language calls C to run service routine 6." Service routine calls scheduler 7." Scheduler selects a process to run next (might be the one interrupted…) 8." Assembly language routine loads PC & registers for the selected process Process Concept Revisited Heavy and Light Weight Process Model 13! 9/17/15! Process Characteristics n" In modern operating systems, the two main characteristics of a process are treated separately n" The unit of resource ownership is usually referred to as a process or task n" The unit of execution is usually referred to a thread or a “lightweight process” Heavyweight Processes n" A virtual address space Code Data Files which holds the process image Registers Stack n" Protected access to processors, files, and other I/O resources n" Use message passing or shared memory to communicate with other Thread! processes Single-Threaded Process! 14! 9/17/15! “Heavyweight” Process Model n" Simple, uni-threaded model n" Security provided by address space boundaries n" High cost for context switch n" Coarse granularity limits degree of concurrency . User Kernel Threads User and Kernel Level Threads 15! 9/17/15! Threads: “Processes” Sharing Memory n" Process == Address Space n" Thread == Program Counter / Stream of Instructions n" Two examples n" Three processes, each with one thread n" One process with three threads Process 1! Process 2! Process 3! Process 1! User# space! Threads! Threads! System# Kernel! Kernel! space! Process and Thread Information Per process items! Address space! Open files! Child processes! Signals & handlers! Accounting info! Global variables! Per Thread Items! Per Thread Items! Per Thread Items! Program counter! Program counter! Program counter! Registers!