Processes and Threads Part II

29
Comparative economic systems Market systems Libertarians Monetarists Keynesians Industrial policy school Advocates of income policy (price and incomes control) Non-market systems Indicative planning Directive planning Property, capitalism and socialism Economic systems based on private property Economic systems based on collective property Economic systems based on state property

description

Processes and Threads Part II. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. 2.3 Interprocess Communication 2.3.1 Race Conditions. - PowerPoint PPT Presentation

Transcript of Processes and Threads Part II

Page 1: Processes and Threads Part II

1

Processes and ThreadsPart II

Chapter 2

2.1 Processes2.2 Threads2.3 Interprocess communication2.4 Classical IPC problems2.5 Scheduling

Page 2: Processes and Threads Part II

2

2.3 Interprocess Communication2.3.1 Race Conditions

Two processes want to access shared memory (variable in) at same time and they may put their files in the same slot

Race Condition: When more than one process shares the same memory and the final result depends on the order of execution

Page 3: Processes and Threads Part II

3

2.3.2 Critical Regions (1)

Four conditions to provide mutual exclusion1. No two processes simultaneously in critical

region2. No assumptions made about speeds or

numbers of CPUs3. No process running outside its critical region

may block another process4. No process must wait forever to enter its

critical region

Page 4: Processes and Threads Part II

4

2.3.2 Critical Regions (2)

Mutual exclusion using critical regions

Page 5: Processes and Threads Part II

5

2.3.3 Mutual Exclusion with Busy Waiting (1)

• Disabling interrupts– Each process disables all interrupts just after

entering its critical region(CR) and enables all interrupts before leaving it

– Unsafe for user processes able to turn off all interrupts

• Lock Variables– Having a shared variable (lock), initially 0– Before entering its CR the process checks lock, if

lock is1, it waits; if lock is 0, it sets lock to 1 and enters its CR.

– Before leaving its CR, the process resets lock to 0 – It does not work, why?

Page 6: Processes and Threads Part II

6

2.3.3 Mutual Exclusion with Busy Waiting (2)

Proposed solution to critical region problem(a) Process 0. (b) Process 1.Variable turn is shared by both processes

Strict Alternation

Page 7: Processes and Threads Part II

7

2.3.3 Mutual Exclusion with Busy Waiting(3)Peterson's solution

Page 8: Processes and Threads Part II

8

Mutual Exclusion with Busy Waiting (4)

TSL(Test and Set Lock) Machine instructionFormat: TSL RX, LOCK // LOCK is a shared variable, RX: a register

Function: RX LOCK

LOCK 1

Since it is a machine instruction, so it is always executed atomically.

Page 9: Processes and Threads Part II

9

2.3.5 Semaphores (1)• Definition:

Class Semaphore {Private:

value: integer;list: list of processes

Public:void down() {

value = value –1;if value < 0 {

list.insert(this process)sleep();

}}

void up() {value = value + 1;if value <= 0 {

p = list.remove();wakeup(p);

}}

Page 10: Processes and Threads Part II

10

Semaphore (2)

• Semaphores are system facilities

• The up() and down() operations are system calls.

• The operating system guarantees that the two operations are atomic operations. I.e., when an up() or down() is being executed, no other up() and down() would be executed on the same semaphore.

Page 11: Processes and Threads Part II

11

Semaphore: application • Control execution sequence

– E.g., three processes: p1, p2 and p3, and p3’s stmnt3 must be executed after p1’stmnt1 and p2’s stmnt2 have completed.Semaphore seq = 0;P1 p2 p3… … …Stmnt1; stmnt2 Seq.down();Seq.up(); Seq.up(); Seq.down();… … stmnt3

• Mutual exclusionSemaphore mutex = 1;P1 p2Mutex.down(); mutex.down();Critical region; critical regionMutex.up(); mutex.up()

Page 12: Processes and Threads Part II

12

2.3.5 Semaphores: application

The producer-consumer problem using semaphores

Page 13: Processes and Threads Part II

13

2.3.5 Implementations of Semaphore (3)

• Disable all interrupts

• Use Peterson’s algorithm and treat the up() and down() operations as critical regions.

Page 14: Processes and Threads Part II

14

2.3.6 Mutexes

Implementation of mutex_lock and mutex_unlock

Page 15: Processes and Threads Part II

15

2.3.8 Message Passing (1)

• Processes can communicate with each other via message passing.

• The operating system provides two primitive functions:– Send(dest, &message);– Receive(source, &message);

• Design issues:– Sender waits for acknowledgement, no buffer needed– When sender does not wait, the OS must maintain a

buffer– Message scramble and loss

Page 16: Processes and Threads Part II

16

2.3.8 Message Passing (2)

The producer-consumer problem with N messages

Page 17: Processes and Threads Part II

17

2.4 Classical IPC Problems 2.4.0 The producer and consumer problem

Page 18: Processes and Threads Part II

18

2.4.1 Dining Philosophers (1)

• Philosophers eat/think• Eating needs 2 forks• Pick one fork at a time

Page 19: Processes and Threads Part II

19

2.4.1 Dining Philosophers (2)

A nonsolution to the dining philosophers problemDeadlock? How? How to prevent deadlock

Page 20: Processes and Threads Part II

20

2.4.2 The Readers and Writers Problem (1)

• Multiple readers and writers need to access the database

• Readers only read the content• Writers modify the database• Multiple readers may read at the same time• Only one writer is allowed to write at a time• When a writer is writing, no reader is allowed

to read

Page 21: Processes and Threads Part II

21

2.4.2 The Readers and Writers Problem (2)