Concurrency: mutual exclusion and synchronization

Concurrency: mutual exclusion and synchronization Concurrency: mutual exclusion and synchronization - Start

2016-04-09 48K 48 0 0

Description

Slides. are . mainly. . taken. from «O. perating. Systems: Internals and Design Principles”, . 8/E . William Stallings (Chapter . 5). . Sistemi di Calcolo (II semestre) – Roberto Baldoni. Multiple Processes. ID: 277491 Download Presentation

Embed code:
Download Presentation

Concurrency: mutual exclusion and synchronization




Download Presentation - The PPT/PDF document "Concurrency: mutual exclusion and synchr..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentations text content in Concurrency: mutual exclusion and synchronization

Slide1

Concurrency: mutual exclusion and synchronization

Slides

are

mainly

taken

from «O

perating

Systems: Internals and Design Principles”,

8/E

William Stallings (Chapter

5).

Sistemi di Calcolo (II semestre) – Roberto Baldoni

Slide2

Multiple Processes

Operating System design is concerned with the management of processes and threads:MultiprogrammingMultiprocessingDistributed Processing

Slide3

ConcurrencyArises in Three Different Contexts:

Slide4

Table 5.1

Some Key Terms Related

to Concurrency

Slide5

Multiprogramming Concerns

Output of process must be independent of the speed of execution of other concurrent processes

Slide6

Principles of Concurrency

Interleaving and overlapping can be viewed as examples of concurrent processingboth present the same problemsUniprocessor – the relative speed of execution of processes cannot be predicteddepends on activities of other processesthe way the OS handles interruptsscheduling policies of the OS

Slide7

Difficulties of Concurrency

Sharing of global resourcesDifficult for the OS to manage the allocation of resources optimallyDifficult to locate programming errors as results are not deterministic and reproducible

Slide8

Race Condition

Occurs when multiple processes or threads read and write data itemsThe final result depends on the order of executionthe “loser” of the race is the process that updates last and will determine the final value of the variable

Slide9

Operating System Concerns

Design and management issues raised by the existence of concurrency:The OS must:

Slide10

Table 5.2

Process Interaction

Slide11

Resource Competition

Concurrent processes come into conflict when they are competing for use of the same resource for example: I/O devices, memory, processor time, clock

Slide12

Figure 5.1

Illustration of Mutual Exclusion

Slide13

Requirements for Mutual Exclusion

Must be enforcedA process that halts must do so without interfering with other processesNo deadlock or starvationA process must not be denied access to a critical section when there is no other process using itNo assumptions are made about relative process speeds or number of processesA process remains inside its critical section for a finite time only

Slide14

Mutual Exclusion: Hardware Support

Interrupt Disabling uniprocessor system disabling interrupts guarantees mutual exclusion

Disadvantages: the efficiency of execution could be noticeably degraded this approach will not work in a multiprocessor architecture

Slide15

Mutual Exclusion: Hardware Support

Compare&Swap Instruction also called a “compare and exchange instruction”a compare is made between a memory value and a test valueif the values are the same a swap occurscarried out atomically

Slide16

(a) Compare and swap instruction (

b) Exchange instruction

Figure 5.2 Hardware Support for Mutual Exclusion

Slide17

(a) Compare and swap instruction (

b) Exchange instruction

Figure 5.2 Hardware Support for Mutual Exclusion

Slide18

Special Machine Instruction:Advantages

Applicable to any number of processes on either a single processor or multiple processors sharing main memory Simple and easy to verify It can be used to support multiple critical sections; each critical section can be defined by its own variable

Slide19

Special Machine Instruction:Disadvantages

Busy-waiting is employed, thus while a process is waiting for access to a critical section it continues to consume processor time Starvation is possible when a process leaves a critical section and more than one process is waiting Deadlock is possible

Slide20

Table 5.3

Common

Concurrency Mechanisms

Slide21

Semaphore

May be initialized to a nonnegative integer value

The

semWait

operation decrements the value

The

semSignal

operation increments the value

Slide22

Consequences

Slide23

Figure 5.3

A

Definition

of Semaphore Primitives

Slide24

Figure 5.4

A Definition of Binary Semaphore Primitives

Slide25

Strong/Weak Semaphores

A queue is used to hold processes waiting on the semaphore

Slide26

Slide27

Figure 5.6

Mutual Exclusion Using Semaphores

Slide28

Slide29

Producer/Consumer Problem

Slide30

Slide31

Figure 5.9

An Incorrect

Solution

to the

Infinite-Buffer

Producer/Consumer

Problem Using

Binary Semaphores

Slide32

Table 5.4

Possible Scenario for the Program of Figure 5.9

Note:

White areas represent the critical section controlled by semaphore

s

.

Slide33

Figure 5.10

A Correct Solution to the Infinite-Buffer Producer/Consumer Problem Using Binary Semaphores

Slide34

Figure 5.11

A Solution

to the Infinite-Buffer Producer/Consumer Problem Using Semaphores

Slide35

Slide36

Figure 5.13

A Solution to the Bounded-Buffer Producer/Consumer Problem Using Semaphores

Slide37

Implementation of Semaphores

Imperative that the semWait and semSignal operations be implemented as atomic primitivesCan be implemented in hardware or firmwareSoftware schemes such as Dekker’s or Peterson’s algorithms can be usedUse one of the hardware-supported schemes for mutual exclusion

Slide38

(a) Compare and Swap Instruction (

b) Interrupts

Figure 5.14

Two Possible Implementations of Semaphores

Slide39

Monitors

Programming language construct that provides equivalent functionality to that of semaphores and is easier to controlImplemented in a number of programming languagesincluding Concurrent Pascal, Pascal-Plus, Modula-2, Modula-3, and JavaHas also been implemented as a program librarySoftware module consisting of one or more procedures, an initialization sequence, and local data

Slide40

Monitor Characteristics

Slide41

Synchronization

Achieved by the use of condition variables that are contained within the monitor and accessible only within the monitorCondition variables are operated on by two functions:cwait(c): suspend execution of the calling process on condition ccsignal(c): resume execution of some process blocked after a cwait on the same condition

Slide42

Slide43

Figure 5.16

A Solution to the Bounded-Buffer Producer/Consumer Problem Using a Monitor

Slide44

Message Passing

When processes interact with one another two fundamental requirements must be satisfied: Message Passing is one approach to providing both of these functionsworks with distributed systems and shared memory multiprocessor and uniprocessor systems

Slide45

Message Passing

The actual function is normally provided in the form of a pair of primitives:send (destination, message)receive (source, message)A process sends information in the form of a message to another process designated by a destinationA process receives information by executing the receive primitive, indicating the source and the message

Slide46

Table 5.5

Design Characteristics of Message Systems for

Interprocess

Communication and Synchronization

Slide47

Synchronization

Slide48

Blocking Send, Blocking Receive

Both sender and receiver are blocked until the message is deliveredSometimes referred to as a rendezvousAllows for tight synchronization between processes

Slide49

Nonblocking Send

Slide50

Addressing

Schemes for specifying processes in send and receive primitives fall into two categories:

Slide51

Direct Addressing

Send primitive includes a specific identifier of the destination processReceive primitive can be handled in one of two ways:require that the process explicitly designate a sending processeffective for cooperating concurrent processesimplicit addressingsource parameter of the receive primitive possesses a value returned when the receive operation has been performed

Slide52

Indirect Addressing

Slide53

Slide54

Slide55

Figure 5.20

Mutual Exclusion Using Messages

Slide56

Figure 5.21

A Solution to the Bounded-Buffer Producer/Consumer Problem Using Messages

Init

.

Mayproduce is full

Init

.

Mayconsume

is

empty

Slide57

Readers/Writers Problem

A data area is shared among many processes

some processes only read the data area, (readers) and some only write to the data area (writers)

Conditions that must be satisfied:

any number of readers may simultaneously read the file

only one writer at a time may write to the file

if a writer is writing to the file, no reader may read it

Slide58

Figure 5.22

A Solution to the Readers/Writers Problem Using Semaphores: Readers Have Priority

Slide59

Table 5.6

State of the Process Queues for Program of Figure 5.23

Slide60

Figure 5.23

A Solution to the Readers/Writers Problem Using Semaphores: Writers Have Priority

Slide61

Figure 5.24

A Solution to the Readers/Writers Problem Using Message Passing

Slide62

Summary

MonitorsMonitor with signalAlternate model of monitors with notify and broadcastMessage passingSynchronizationAddressingMessage formatQueueing disciplineMutual exclusionReaders/writers problemReaders have priorityWriters have priority

Principles of concurrency

Race condition

O

S concerns

Process interaction

Requirements for mutual exclusion

Mutual exclusion: hardware support

Interrupt disabling

Special machine instructions

Semaphores

Mutual exclusion

Producer/consumer problem

Implementation of semaphores

Slide63

Slide64

Slide65

Slide66

Slide67


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.