1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University.

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
Advertisements

1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
1 OMSE 510: Computing Foundations 6: Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Processes CSCI 444/544 Operating Systems Fall 2008.
Jonathan Walpole Computer Science Portland State University
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CS533 - Concepts of Operating Systems 1 Anyone NOT on this list see me after class! Bock, Tony Carroll, Diana Kulkarni, Ashwini LeVitre, Jon Mukherjee,
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
CS533 Concepts of Operating Systems Class 1
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Process Concept An operating system executes a variety of programs
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Processes and Threads.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
CS533 Concepts of Operating Systems Jonathan Walpole.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
CS333 Intro to Operating Systems Jonathan Walpole.
CS533 - Concepts of Operating Systems 1 Anyone NOT on this list see me after class! Arryadi, Rizal Carlson, Kristen Ellet, Burke Florey, David Greenwald,
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
CS533 Concepts of Operating Systems Jonathan Walpole.
What is a Process ? A program in execution.
CS533 Concepts of Operating Systems Class 2 Overview of Threads and Concurrency.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Processes and threads.
Jonathan Walpole Computer Science Portland State University
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Processes in Unix, Linux, and Windows
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Operating Systems Lecture 13.
More examples How many processes does this piece of code create?
Jonathan Walpole Computer Science Portland State University
Processes in Unix, Linux, and Windows
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Jonathan Walpole Computer Science Portland State University
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Jonathan Walpole Computer Science Portland State University
Presentation transcript:

1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University

2 The Process Concept

3  Process – a program in execution  Program –description of how to perform an activity –instructions and static data values  Process –a snapshot of a program in execution –memory (program instructions, static and dynamic data values) –CPU state (registers, PC, SP, etc) –operating system state (open files, accounting statistics etc)

4 Process address space  Each process runs in its own virtual memory address space that consists of:  Stack space – used for function and system calls  Data space – variables (both static and dynamic allocation)  Text – the program code (usually read only)  Invoking the same program multiple times results in the creation of multiple distinct address spaces stack text data Address space

5 Switching among multiple processes  Program instructions operate on operands in memory and (temporarily) in registers Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog2 State Prog1 has CPU Prog2 is suspended Prog2 Code Prog2 Data Load A1, R1 Load A2, R2 Add R1, R2, R3 Store R3, A3 …

6 Switching among multiple processes  Saving all the information about a process allows a process to be temporarily suspended and later resumed from the same point Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State OS suspends Prog1 Prog2 Code Prog2 Data Prog2 State

7 Switching among multiple processes  Saving all the information about a process allows a process to be temporarily suspended and later resumed Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State OS resumes Prog2 Prog2 Code Prog2 Data Prog2 State

8 Switching among multiple processes  Program instructions operate on operands in memory and in registers Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State Prog2 has CPU Prog1 is suspended Prog2 Code Prog2 Data Load A1, R1 Load A2, R2 Sub R1, R2, R3 Store R3, A3 …

9 Why use the process abstraction?  Multiprogramming of four programs in the same address space  Conceptual model of 4 independent, sequential processes  Only one program active at any instant

10 The role of the scheduler  Lowest layer of process-structured OS  handles interrupts & scheduling of processes  Sequential processes only exist above that layer

11 Process states  Possible process states  running  blocked  ready

12 How do processes get created? Principal events that cause process creation  System initialization  Initiation of a batch job  User request to create a new process  Execution of a process creation system call from another process

13 Process hierarchies  Parent creates a child process,  special system calls for communicating with and waiting for child processes  each process is assigned a unique identifying number or process ID (PID)  Child processes can create their own child processes  Forms a hierarchy  UNIX calls this a "process group"  Windows has no concept of process hierarchy all processes are created equal

14 Process creation in UNIX  All processes have a unique process id  getpid(), getppid() system calls allow processes to get their information  Process creation  fork() system call creates a copy of a process and returns in both processes (parent and child), but with a different return value  exec() replaces an address space with a new program  Process termination, signaling  signal(), kill() system calls allow a process to be terminated or have specific signals sent to it

15 Example: process creation in UNIX … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 22)

16 Process creation in UNIX example … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 22) … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 24)

17 Process creation in UNIX example … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 22) … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 24)

18 Process creation in UNIX example … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 22) … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 24)

19 Process creation in UNIX example … pid = fork() if (pid == 0) { // child… … exec(); } else { // parent wait(); } … csh (pid = 22) //ls program main(){ //look up dir … } ls (pid = 24)

20 Process creation (fork)  Fork creates a new process by copying the calling process  The new process has its own  memory address space Instructions (copied from parent) Data (copied from parent) Stack ?? (empty)  register set (copied from parent)  Process table entry in the OS

21 What other process state does the OS manage? Example fields of a process table entry

22 Threads

23 Threads  Processes have the following components:  an address space  a collection of operating system state  a CPU context … or thread of control  On multiprocessor systems, with several CPUs, it would make sense for a process to have several CPU contexts (threads of control)  Thread fork creates new thread not memory space  Multiple threads of control could run in the same memory space on a single CPU system too!

24 Threads  Threads share a process address space with zero or more other threads  Threads have their own  PC, SP, register state, stack  A traditional process can be viewed as a memory address space with a single thread

25 Single thread state within a process

26 Multiple threads in an address space

27 What is a thread?  A thread executes a stream of instructions  it is an abstraction for control-flow  Practically, it is a processor context and stack  Allocated a CPU by a scheduler  Executes in the context of a memory address space

28 Summary of private per-thread state Things that define the state of a particular flow of control in an executing program:  Stack (local variables)  Stack pointer  Registers  Scheduling properties (i.e., priority)

29 Shared state among threads Things that relate to an instance of an executing program (that may have multiple threads)  User ID, group ID, process ID  Address space Text Data (off-stack global variables) Heap (dynamic data)  Open files, sockets, locks Important: Changes made to shared state by one thread will be visible to the others  Reading and writing memory locations requires synchronization! … a major topic for later …

30 How do you program using threads? Split program into routines to execute in parallel  True or pseudo (interleaved) parallelism Alternative strategies for executing multiple rountines

31 Why program using threads?  Utilize multiple CPU’s concurrently  Low cost communication via shared memory  Overlap computation and blocking on a single CPU  Blocking due to I/O  Computation and communication  Handle asynchronous events

32 Thread usage A word processor with three threads

33 Processes versus threads - example  A WWW process GET / HTTP/1.0 HTTPD disk

34 Processes versus threads - example  A WWW process GET / HTTP/1.0 HTTPD disk Why is this not a good web server design?

35 HTTPD Processes versus threads - example  A WWW process GET / HTTP/1.0 HTTPD disk

36 Processes versus threads - example  A WWW process GET / HTTP/1.0 HTTPD disk GET / HTTP/1.0

37 Processes versus threads - example  A WWW process GET / HTTP/1.0 HTTPD disk GET / HTTP/1.0

38 Threads in a web server A multithreaded web server

39 Thread usage  Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread

40 System structuring options Three ways to construct a server

41 Common thread programming models  Manager/worker  Manager thread handles I/O and assigns work to worker threads  Worker threads may be created dynamically, or allocated from a thread-pool  Pipeline  Each thread handles a different stage of an assembly line  Threads hand work off to each other in a producer- consumer relationship

42 What does a typical thread API look like?  POSIX standard threads (Pthreads)  First thread exists in main(), typically creates the others  pthread_create (thread,attr,start_routine,arg)  Returns new thread ID in “thread”  Executes routine specified by “start_routine” with argument specified by “arg”  Exits on return from routine or when told explicitly

43 Thread API (continued)  pthread_exit (status)  Terminates the thread and returns “status” to any joining thread  pthread_join (threadid,status)  Blocks the calling thread until thread specified by “threadid” terminates  Return status from pthread_exit is passed in “status”  One way of synchronizing between threads  pthread_yield ()  Thread gives up the CPU and enters the run queue

44 Using create, join and exit primitives

45 An example Pthreads program #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0; t<NUM_THREADS; t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } pthread_exit(NULL); } Program Output Creating thread 0 Creating thread 1 0: Hello World! 1: Hello World! Creating thread 2 Creating thread 3 2: Hello World! 3: Hello World! Creating thread 4 4: Hello World! For more examples see:

46 Pros & cons of threads  Pros  Overlap I/O with computation!  Cheaper context switches  Better mapping to shared memory multiprocessors  Cons  Potential thread interactions  Complexity of debugging  Complexity of multi-threaded programming  Backwards compatibility with existing code

47 User-level threads  The idea of managing multiple abstract program counters above a single real one can be implemented using privileged or non-privileged code.  Threads can be implemented in the OS or at user level  User level thread implementations  thread scheduler runs as user code (thread library)  manages thread contexts in user space  The underlying OS sees only a traditional process above

48 Kernel-level threads The thread-switching code is in the kernel

49 User-level threads package The thread-switching code is in user space

50 User-level threads  Advantages  cheap context switch costs among threads in the same process! A procedure call not a system call!  User-programmable scheduling policy  Disadvantages  How to deal with blocking system calls!  How to overlap I/O and computation!

51 Concurrent Programming

52 Concurrent programming Assumptions:  Two or more threads  Each executes in (pseudo) parallel  We can’t predict exact running speeds  The threads can interact via access to shared variables Example:  One thread writes a variable  The other thread reads from the same variable  Problem – non-determinism: The relative order of one thread’s reads and the other thread’s writes determines the end result!

53 Race conditions  What is a race condition?  two or more threads have an inconsistent view of a shared memory region (I.e., a variable)  Why do race conditions occur?  values of memory locations replicated in registers during execution  context switches at arbitrary times during execution  threads can see “stale” memory values in registers

54 Counter increment race condition  Incrementing a counter (load, increment, store)  Context switch can occur after load and before increment!

55 Race Conditions  Race condition: whenever the output depends on the precise execution order of the processes!  What solutions can we apply?  prevent context switches by preventing interrupts  make threads coordinate with each other to ensure mutual exclusion in accessing critical sections of code

56 Mutual exclusion conditions  No two processes simultaneously in critical section  No assumptions made about speeds or numbers of CPUs  No process running outside its critical section may block another process  No process must wait forever to enter its critical section

57 Spare Slides - intended for class 4

58 Critical sections with mutual exclusion

59 How can we enforce mutual exclusion?  What about using locks ?  Locks solve the problem of exclusive access to shared data.  Acquiring a lock prevents concurrent access  Expresses intention to enter critical section  Assumption:  Each each shared data item has an associated lock  Every thread sets the right lock before accessing shared data!  Every thread releases the lock after it is done!

60 Acquiring and releasing locks Free Lock Thread A Thread D Thread C Thread B

61 Acquiring and releasing locks Free Lock Thread A Thread D Thread C Thread B Lock

62 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

63 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

64 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B

65 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

66 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

67 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

68 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

69 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock Unlock

70 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock Unlock

71 Acquiring and releasing locks Free Lock Thread A Thread D Thread C Thread B Lock

72 Acquiring and releasing locks Free Lock Thread A Thread D Thread C Thread B Lock

73 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

74 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

75 Acquiring and releasing locks Set Lock Thread A Thread D Thread C Thread B Lock

76 Mutex locks  An abstract data type  Used for synchronization and mutual exclusion  The mutex is either:  Locked (“the lock is held”)  Unlocked(“the lock is free”)

77 Mutex lock operations  Lock (mutex)  Acquire the lock if it is free  Otherwise wait until it can be acquired  Unlock (mutex)  Release the lock  If there are waiting threads wake up one of them

78 How to use a mutex? 1 repeat 2 Lock(myLock); 3 critical section 4 Unlock(myLock); 5 remainder section 6 until FALSE 1 repeat 2 Lock(myLock); 3 critical section 4 Unlock(myLock); 5 remainder section 6 until FALSE Shared data: Mutex myLock;

79 How to implement a mutex?  Both Lock and Unlock must be atomic !  Does a binary “lock” variable in memory work?  Many computers have some limited hardware support for setting locks  Atomic Test and Set Lock instruction  Atomic compare and swap operation  Can be used to implement mutex locks

80 Test-and-set-lock instruction (TSL, tset)  A lock is a single word variable with two values  0 = FALSE = not locked  1 = TRUE = locked  Test-and-set does the following atomically:  Get the (old) value  Set the lock to TRUE  Return the old value If the returned value was FALSE... Then you got the lock!!! If the returned value was TRUE... Then someone else has the lock (so try again later)

81 Test and set lock P1 FALSE Lock

82 Test and set lock P1 Lock FALSE FALSE = Lock Available!!

83 Test and set lock TRUE Lock FALSE P1

84 Test and set lock TRUE Lock P1P2 P3 P4 TRUE

85 Test and set lock TRUE Lock P1P2 P3 P4 TRUE

86 Test and set lock TRUE Lock P1P2 P3 P4 TRUE

87 Test and set lock FALSE Lock P1P2 P3 P4 FALSE TRUE

88 Test and set lock TRUE Lock P1P2 P3 P4 TRUE FALSE TRUE

89 Test and set lock TRUE Lock P1P2 P3 P4 TRUE FALSE

90 Test and set lock TRUE Lock P1P2 P3 P4 TRUE

91 Critical section entry code with TSL 1 repeat 2 while(TSL(lock)) 3 no-op; 4 critical section 5 Lock = FALSE; 6 remainder section 7 until FALSE 1 repeat 2while(TSL(lock)) 3 no-op; 4 critical section 5 Lock = FALSE; 6 remainder section 7 until FALSE JI  Guarantees that only one thread at a time will enter its critical section  Note that processes are busy while waiting  Spin locks

92 Busy waiting  Also called polling or spinning  The thread consumes CPU cycles to evaluate when the lock becomes free !  Shortcoming on a single CPU system...  A busy-waiting thread can prevent the lock holder from running & completing its critical section & releasing the lock!  Why not block instead of busy wait ?

93 Quiz  What is the difference between a program and a process?  Is the Operating System a program?  Is the Operating System a process?  Does it have a process control block?  How is its state managed when it is not running?  What is the difference between processes and threads?  What tasks are involved in switching the CPU from one process to another?  Why is it called a context switch?  What tasks are involved in switching the CPU from one thread to another?  Why are threads “lightweight”?