11/5/2011
UNIT 10C Concurrency: Multitasking & Deadlock
15110 Principles of Computing, Carnegie 1 Mellon University - CORTINA
Multitasking & Operating Systems
• Multitasking - The coordination of several computational processes on one processor. • An operating system (OS) is the system software responsible for the direct control and management of hardware and basic system operations. • An OS provides a foundation upon which to run application software such as word processing programs, web browsers, etc. from Wikipedia
15110 Principles of Computing, Carnegie 2 Mellon University - CORTINA
1 11/5/2011
Operating System “Flavors”
• UNIX – System V, BSD, Linux – Proprietary: Solaris, Mac OS X • Windows – Windows XP – Windows Vista – Windows 7
15110 Principles of Computing, Carnegie 3 Mellon University - CORTINA
Multitasking
• Each (un-blocked) application runs for a very short time on the computer's processor and then another process runs, then another... • This is done at such a fast rate that it appears to the computer user that all applications are running at the same time. – How do actors appear to move in a motion picture?
15110 Principles of Computing, Carnegie 4 Mellon University - CORTINA
2 11/5/2011
Critical Section
• A critical section is a section of computer code that must only be executed by one process or thread at a time. • Examples: – A printer queue receiving a file to be printed – Code to set a seat as reserved – Web server that generates and returns a web page showing current registration in course
15110 Principles of Computing, Carnegie 5 Mellon University - CORTINA
A Critical Section
Cars may not make turns through this intersection. They may only proceed straight ahead. When a car stops at this intersection, it can proceed straight ahead if there is no car to its left and no car to its right. Otherwise it must wait some random amount of seconds and then check again to see if it's safe to proceed. If not, it waits again, and so on.
15110 Principles of Computing, Carnegie 6 Mellon University - CORTINA
3 11/5/2011
Shared Computing Resources
• memory • tape drives • disk drives • printers • communication ports • input devices (keyboard, mouse)
15110 Principles of Computing, Carnegie 7 Mellon University - CORTINA
Deadlock
• Deadlock is the condition when two or more processes are all waiting for some shared resource that other processes of the group hold, causing all processes to wait forever without proceeding. • How can deadlock occur at the intersection with the 4-way stop?
15110 Principles of Computing, Carnegie 8 Mellon University - CORTINA
4 11/5/2011
Dining Philosopher’s Problem
Socrates
Aristotle Plato
Homer
15110 Principles of Computing, Carnegie 9 Mellon University - CORTINA
The Dining Philosopher’s
• Each philosopher thinks for a while, then picks up his left fork, then picks up his right fork, then eats, then puts down his left fork, then puts down his right fork, thinks for a while... – We assume here that each philosopher thinks and eats for random times, and a philosopher cannot be interrupted while he picks up or puts down a single fork. • Each fork models a "resource" on a computer controlled by an OS. • Original problem proposed by Edsgar Dijkstra.
15110 Principles of Computing, Carnegie 10 Mellon University - CORTINA
5 11/5/2011
Dining Philosopher’s Problem
• There are N philosophers. • Philosopher i does the following: 1. THINK N=4
2. Pick up fork i. Fork 3 Fork 2 3. Pick up fork (i+1) modulo N. 2 4. EAT 3 1 5. Put down fork i. 0 6. Put down fork (i+1) modulo N. Fork 0 Fork 1 7. Go to step 1. NOTE: (i+1) modulo N = i+1 , if 0 < i < N-1 (i+1) modulo N = 0, if i = N-1
15110 Principles of Computing, Carnegie 11 Mellon University - CORTINA
Dining Philosopher’s Problem
• There are N philosophers. • Philosopher i does the following: 1. THINK N=4
2. Pick up fork i. Fork 3 Fork 2 3. Pick up fork (i+1) modulo N. 2 4. EAT 3 1 5. Put down fork i. 0 6. Put down fork (i+1) modulo N. Fork 0 Fork 1 7. Go to step 1. How can deadlock occur here?
15110 Principles of Computing, Carnegie 12 Mellon University - CORTINA
6 11/5/2011
Removing Deadlock • Philosopher i performs the following: 1. THINK 2. If i is not equal to N-1: a. Pick up fork i b . Pick up fork i+1 N=4 3. If i equals N-1: a. Pick up fork 0 Fork 3 Fork 2 b . Pick up fork N-1 2 4. EAT 5. If i is not equal to N-1: 3 1 a. Put down fork i 0 b . Put down fork i+1 Fork 0 Fork 1 6. If i equals N-1: a. Put down fork 0 b . Put down fork N-1 Can a philosopher still starve? 7. Goto step 1 15110 Principles of Computing, Carnegie 13 Mellon University - CORTINA
Semaphores
• A (binary) semaphore S is a shared variable that holds an integer that is initialized to 1. • We can use a semaphore to protect a critical section so that only one process accesses this section at a given time: Initially, semaphore S is 1. The non-critical program code first process to request S sets S to 0, blocking other processes Request S from proceeding once they get to their request operations. Once CRITICAL SECTION the first process finishes running the critical section, it releases S, Release S causing S to increase to 1, non-critical program code allowing another process to proceed into the critical section.
15110 Principles of Computing, Carnegie 14 Mellon University - CORTINA
7 11/5/2011
Summary
• Concurrent computing and multitasking are very challenging problems in computer science. • The simple rules of sequential computation no longer apply when we compute – in a distributed manner across networks – in a single computer system in a multitasking environment
15110 Principles of Computing, Carnegie 15 Mellon University - CORTINA
8