+ All Categories
Home > Documents > Concurrency and Communication Lecture 6, May 8, 2003 Mr. Greg Vogl Operating Systems Uganda Martyrs...

Concurrency and Communication Lecture 6, May 8, 2003 Mr. Greg Vogl Operating Systems Uganda Martyrs...

Date post: 02-Jan-2016
Category:
Upload: frederick-foster
View: 223 times
Download: 0 times
Share this document with a friend
Popular Tags:
29
Concurrency and Communication Lecture 6, May 8, 2003 Mr. Greg Vogl Operating Systems Uganda Martyrs University
Transcript

Concurrency and CommunicationConcurrency and Communication

Lecture 6, May 8, 2003Mr. Greg Vogl

Operating SystemsUganda Martyrs University

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

2

IntroductionIntroduction

SourcesRitchie ch. 11, 12, 13.4Burgess 4.4-4.5, 6.4, 7.6Solomon parts 3, 4

Ways processes can interactProcesses compete for or share resourcesProcesses communicate with each other

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

3

ContentsContents Concurrent processes

mutexes/flags, semaphores, monitors, file locks Deadlock

avoidance, prevention, detection, recovery Interprocess communication

signals, messages, pipes, sockets, RPC, DDE, OLE Distributed systems

NFS, AFS, DCE, CORBA, DCE, DCOM

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

4

Possible Process RelationshipsPossible Process Relationships

Fully independent processesUsers working on separate applications

Independent but related processesSeparate programs updating the same database

Concurrent programming applicationsA set of co-operating processese.g. real-time systems handle asynchronous events

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

5

Some Concurrent ProcessesSome Concurrent Processes

Operating systems Real-time process control systems Online databases Network controllers

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

6

Types of ResourcesTypes of Resources

Physical: processor, memory, I/O, disk storage Logical: system/app. data, in memory or on disk Shareable: useable by more than one at a time

Preemptable: can be “borrowed” without harm Consumable: gets used up (e.g. message) Reusable (e.g. physical; CPU and memory)

serial = one user at a time (mutual exclusion)

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

7

Resource Conflict ProblemsResource Conflict Problems e.g. Two processes both updating a database Copies in memory overwrite copy on disk User B’s changes overwrite user A’s changes Both users believe they hold the resource Only one can actually hold the resource, e.g.

Only one person can be booked per airplane seatOrder numbers should be uniqueOnly one variable can hold bank account balance

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

8

Mutual ExclusionMutual Exclusion Race conditions lead to unpredictable results

Uncertain who will get to the resource first Sensitive operations require mutual exclusion

Occur within a critical region of a processAllow only one process at a time; no overlap

Goals of concurrent processesSafe coexistence without data loss/corruptionEfficient and fair sharing of resources

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

9

FlagsFlags Use a flag variable with a name like door_open

Before entering the toilet, check if the door is openYou should only enter if vacant (if the door is open)Close the door after entering, open it after leaving

But it doesn’t work on a computer!Small time gap between checking, entering, closingPossible that two can enter at about the same timeNeed to test-and-set the variable in one instruction

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

10

SemaphoresSemaphores

s = 0, 1, … (non-negative integer variable)If s=0 then the resource is free else it is occupied

Wait(s)if s > 0 then s=s-1 else wait on s

Enter critical region Signal(s)

if any processes waiting then start one else s=s+1

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

11

Advantages and DisadvantagesAdvantages and Disadvantages Wait is like a single test-and-set instruction

indivisible, cannot be interrupted, therefore safe No “busy waiting”

waiting processes are blocked, others are scheduled Can be used to reserve more than one resource

Value of semaphore indicates the quantity available Low-level facility

Hard to program, easy to make mistakes/deadlocks

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

12

Producer-Consumer ProblemProducer-Consumer Problem Producer puts data in a buffer array, N elements

Write only when buffer not full Consumer takes data from buffer

Read only when buffer not empty To implement, the algorithm uses 3 semaphores:

Free: mutual exclusion for buffer access, initially 1Space: space available in buffer, initially NData: data available in buffer, initially 0

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

13

Producer-Consumer AlgorithmProducer-Consumer Algorithm Producer Produce item Wait(space)

Wait(free)– Add item to buffer

Signal(free) Signal(data)

Consumer Wait(data)

Wait(free)– Get item from buffer

Signal(free) Signal(space) Consume item

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

14

MonitorsMonitors Encapsulate data with its access procedures

E.g. provide put_item and get_item procedures Only one process can use a monitor procedure

If being used, put the process request in a queue Reduced chance of programming error

Compiler guarantees mutex, not application program Monitor must also provide synchronisation

E.g. put waiting processes in queue, no busy waiting

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

15

Database lockingDatabase locking File lock

Only necessary when whole file is being processedOther processes cannot access any part of file

Write lockOnly necessary when record is being written toOther processes cannot read or write to the record

Read lockOther processes can read but not write to the record

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

16

DeadlockDeadlock Set of (usually) >1 forever blocked processes

Waiting for an event only caused by a member of setE.g. each needs a resource that another holdsNo process sees/controls the global situationOne or more processes must give up a resource

Sometimes caused by program bug, not usually E.g. traffic jam: cars each waiting for the others E.g. two travelers book half of a two-way journey

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

17

Conditions for deadlockConditions for deadlock

Necessary conditionsMutual exclusion: resource cannot be sharedResource holding: hold some, request moreNo preemption: resources not forcibly removed

Necessary and sufficient conditionCircular wait: closed cycle of two or more processes waiting on each other

– (or a simple program bug: one process waiting on itself)

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

18

Deadlock preventionDeadlock prevention

One shot allocation: wait for all resourcesA process might hold resources long without usingA process might have a long wait to get everything

Preemption by OSE.g. OS saves copy of memory or registers

To prevent circular wait: order resourcesMight be difficult to order; inefficiencies

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

19

Deadlock avoidanceDeadlock avoidance Dynamically predict possibility of deadlock

Time-consuming to look for >2 process deadlock Deny resource request if a deadlock could occur

An unsafe state will not necessarily lead to deadlock Banker’s algorithm: bank only loans resources if

it can meet projected needs of other customersProcess must first declare its max. resource needsSeldom used because overhead would be large

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

20

Deadlock detectionDeadlock detection Periodically look for deadlocks

At fixed intervals depending on deadlock probabilityWhenever doing something that might cause itWhenever a process is blocked from denied requestWhenever a process is blocked a long timeWhenever CPU utilisation is low (nothing else to do)

May take less overhead to detect than prevent Can still consume too much processing time

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

21

Deadlock recoveryDeadlock recovery

Abort processes or preempt resourcesHow many? All? (reboot the PC??) One at a time?Which one(s)? Difficult ones to restart?By whom? operator/user?

Roll back to last checkpointE.g. database logging, enough info to undo Avoid starvation: always roll back youngest process

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

22

SignalsSignals Similar to interrupt; usually process is aborted

SIGFPE is floating point exception e.g. divide by 0SIGSEGV is segment/memory address violationPressing Ctrl-C sends SIGINT (terminal-initiated)SIGTERM is sent by a user process

Some processes may want to trap signalsIgnore, apply handler function, or use default actionSIGKILL is only signal that cannot be trapped

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

23

Shared memoryShared memory

Area of memory shared by multiple processesOS and hardware must support shared segments

OperationsGet a free segment (returns the segment address)Destroy a shared segment (return to free list)Attach to/detach from address space of a processSet/get control parameters

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

24

MessagesMessages Friendly processes communicate and cooperate

exchange messages rather than sharing memorysend(destination, msg); receive(source, msg buffer)

Naming: direct or channel/pipe/mailbox/port? Synchronisation: send/receive blocking or not? Buffering:

queue(s)? in receiver’s or system’s memory? Message size: small or large? maximum?

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

25

PipesPipes Output of a process directed to input of another

E.g. in DOS: dir | more; type myfile | sortPipes are a form of message passing

Temporary intermediate file acts as a bufferManaged as a FIFO queue; blocked if empty/fullDeleted when pipe closes

Named pipes (FIFO) uses permanent fileUNIX mknod creates special file for shared space

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

26

SocketsSockets TCP/IP client-server connection of 2 processes

TCP stream (telephone) or UDP datagram (mailbox) TCP/IP servers provide services through ports

/etc/services lists port no.s of well-known services/etc/protocols lists protocols like TCP and UDP

Servers that receive many requests always runOthers are started by the Internet daemon if neededServers can be single- or multi-threaded

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

27

Remote Procedure CallRemote Procedure Call

Process on one PC runs procedure on another Used for distributed processing Transparent to programmer (no net details) Works much like any other procedures Scaleable and maintainable Facilitates cross-platform interaction Implemented using named pipes or sockets

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

28

Dynamic Data ExchangeDynamic Data Exchange

Conversation between Windows 3.x programsDDE client requests data, DDE server replies

Usually asynchronous: client waits for replyIf synchronous, handle error after timeout period

Client must specify application, topic, and data WordAccess using Visual Basic macros

May 8, 2003 Operating Systems: Lecture 6: Concurrency and Communication

29

Object Linking and EmbeddingObject Linking and Embedding Documents hold objects from different programs

e.g. text, tables, graphicsEmbedded (the object is stored within the doc.)Linked (only a reference is stored; keeps file small)Helps to integrate OLE-compliant programs

Common Object Model Drag and drop ActiveX controls e.g. buttons, text boxes, menus


Recommended