2.14k likes | 2.16k Views
Dive into the concepts of processes and threads in OS, from creation to termination, states, and management. Learn the differences, hierarchies, and models for efficient multitasking. Explore the challenges and implementations, including kernel-level and user-level approaches.
E N D
Chapter 2: Process/Thread Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity求于至简,归于永恒
Content • Processes • Threads • Inter-process communication • Classical IPC problems • Scheduling
Definition of A Process • Informal • A program in execution • A running piece of code along with all the things the program can read/write • Formal • One or more threads in their own address space • Note that process != program
The Need for Process • What is the principle motivation for inventing process? • To support multiprogramming
The Process Model • Conceptual viewing of the processes • Concurrency • Multiple processes seem to run concurrently • But in reality only one active at any instant • Progress • Every process makes progress
Multiprogramming of 4 programs Programs in memory Conceptual view Time line view
Process Creation Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process
Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)
Process Hierarchies • Parent creates a child process • Child processes can create its own process • Processes creation forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of such hierarchy, i.e. all processes are created equal
Process States • Possible process states • running, blocked, ready Running Process blocks for input Scheduler picks another process Scheduler picks this process Blocked Ready Input becomes available
Process Space • Also called Address Space • All the data the process uses as it runs • Passive (acted upon by the process) • Play analogy: • all the objects on the stage in a play
Process Space • Is the unit of state partitioning • Each process occupies a different state of the computer • Main topic: • How multiple processes spaces can share a single physical memory efficiently and safely
Manage Process & Space • Who manages process & space? • The operating systems • How does OS achieve it? • By maintain information about processes • i.e. use process tables
Fields of A Process Table • Registers, Program counter, Status word • Stack pointer, Priority, Process ID • Parent group, Process group, Signals • Time when process started • CPU time used, Children’s CPU time, etc.
Problems with Process • While supporting multiprogramming on shared hardware • Itself is single threaded! • i.e. a process can do only one thing at a time • blocking call renders entire process unrunnable
Threads • Invented to support multiprogramming on process level • Manage OS complexity • Multiple users, programs, I/O devices, etc. • Each thread dedicated to do one task
Thread • Sequence of executing instructions from a program • i.e. the running computation • Play analogy: one actor on stage in a play
Threads • Processes decompose mix of activities into several parallel tasks (columns) • Each job can work independently of others job1 job2 job3 Thread 1 Thread 2 Thread 3
The Thread Model 3 processes each with 1 thread Proc 1 Proc 2 Proc 3 Process User space Thread Thread Kernel Kernel Kernel space 1 process with 3 threads
The Thread Model • Some items shared by all threads in a process • Some items private to each thread
Shared and Private Items Each thread has its own stack
A Word Processor w/3 Threads Display thread Backup thread Input thread
Implementation of Thread • How many options to implement thread? • Implement in kernel space • Implement in user space • Hybrid implementation
Kernel-Level Implementation • Completely implemented in kernel space • OS acts as a scheduler • OS maintains information about threads • In additions to processes
Kernel-Level Implementation • Advantage: • Easier to programming • Blocking threads does not block process • Problems: • Costly: need to trap into OS to switch threads • OS space is limited (for maintaining info) • Need to modifying OS!
User-Level Implementation • Completely implemented in user space • A run-time system acts as a scheduler • Threads voluntarily cooperate • i.e. yield control to other threads • OS need not know existence of threads
User-Level Implementation • Advantage: • Flexible, can be implemented on any OS • Faster: no need to trap into OS • Problems: • Programming is tricky • blocking threads block process!
User-Level Implementation • How do we solve the problem of blocking thread blocks the whole process? • Modifying system calls to be unblocking • Write a wrap around blocking calls • i.e. call only when it is safe to do so
Scheduler Activations • A technique that solves the problem of blocking calls in user-level threads • Method: use up-call • Goal – mimic functionality of kernel threads • gain performance of user space threads
Scheduler Activations • Kernel assigns virtual processors to process • Runtime sys. allocate threads to processors • Blocking threads are handled by OS upcall • i.e. OS notify runtime system about blocking calls
Scheduler Activations • Problem: • Reliance on kernel (lower layer) calling procedures in user space (higher layer) • Violates layered structure of OS design
Hybrid Implementation • Can we have the best of both worlds • i.e. kernel-level and user-level implementations • While avoiding the problems of either? • Hybrid implementation
Hybrid Implementation • User-level threads are managed by runtime systems • Kernel-level threads are managed by OS • Multiplexing user-level threads onto kernel- level threads
Multiple Threads • Can have several threads in a single address space • That is what thread is invented for • Play analogy: several actors on a single set • Sometimes interact (e.g. dance together) • Sometimes do independent tasks
Multiple Threads • Private state for a thread vs. global state shared between threads • What private state must a thread have? • Other state is shared between all threads in a process
Multiple Threads • Many programs are written in single-threaded processes • Make them multithreaded is very tricky • Can cause unexpected problems
Multiple Threads Conflicts between threads over the use of a global variable
Multiple Threads • Many solutions: • Prohibit global variables • Assign each thread its private global variables
Multiple Threads Threads can have private global variables
Cooperating Threads • Often we create threads to cooperate • Each thread handles one request • Each thread can issue a blocking disk I/O, • wait for I/O to finish • then continue with next part of its request
Cooperating Threads • Ordering of events from different threads is non-deterministic • e.g. after 10 seconds, different threads may have gotten differing amounts of work done • thread A ---------------------------------> • thread B - - - - > • thread C - - - - - - - - - - - >
Cooperating Threads • Example • thread A: x=1 • thread B: x=2 • Possible results? • Is 3 a possible output? • yes
Cooperating Threads • 3 is possible because assignment is not atomic and threads can interleave • if assignment to x is atomic • then only possible results are 1 and 2
Atomic Operations • Atomic: indivisible • Either happens in its entirety without interruption or has yet to happen at all • No events from other threads can happen in between start & end of an atomic event
Atomic Operations • On most machines, memory load & store are atomic • But many instructions are not atomic • double-precision floating on a 32-bit machine • two separate memory operations