1 / 214

Chapter 2: Process/Thread

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

pearlk
Download Presentation

Chapter 2: Process/Thread

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Process/Thread Instructor: Hengming Zou, Ph.D. In Pursuit of Absolute Simplicity求于至简,归于永恒

  2. Content • Processes • Threads • Inter-process communication • Classical IPC problems • Scheduling

  3. 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

  4. The Need for Process • What is the principle motivation for inventing process? • To support multiprogramming

  5. 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

  6. Multiprogramming of 4 programs Programs in memory Conceptual view Time line view

  7. Process Creation Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process

  8. Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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.

  15. 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

  16. 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

  17. Thread • Sequence of executing instructions from a program • i.e. the running computation • Play analogy: one actor on stage in a play

  18. 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

  19. 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

  20. The Thread Model • Some items shared by all threads in a process • Some items private to each thread

  21. Shared and Private Items

  22. Shared and Private Items Each thread has its own stack

  23. A Word Processor w/3 Threads Display thread Backup thread Input thread

  24. Implementation of Thread • How many options to implement thread? • Implement in kernel space • Implement in user space • Hybrid implementation

  25. Kernel-Level Implementation • Completely implemented in kernel space • OS acts as a scheduler • OS maintains information about threads • In additions to processes

  26. Kernel-Level Implementation

  27. 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!

  28. 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

  29. User-Level Implementation

  30. 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!

  31. 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

  32. 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

  33. 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

  34. Scheduler Activations • Problem: • Reliance on kernel (lower layer) calling procedures in user space (higher layer) • Violates layered structure of OS design

  35. 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

  36. 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

  37. Hybrid Implementation

  38. 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

  39. 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

  40. Multiple Threads

  41. Multiple Threads • Many programs are written in single-threaded processes • Make them multithreaded is very tricky • Can cause unexpected problems

  42. Multiple Threads Conflicts between threads over the use of a global variable

  43. Multiple Threads • Many solutions: • Prohibit global variables • Assign each thread its private global variables

  44. Multiple Threads Threads can have private global variables

  45. 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

  46. 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 - - - - - - - - - - - >

  47. Cooperating Threads • Example • thread A: x=1 • thread B: x=2 • Possible results? • Is 3 a possible output? • yes

  48. 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

  49. 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

  50. 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

More Related