42
Department of Computer Science DCS COMSATS Institute of Information Technology Concurrent Processes Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts

CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

DCS

COMSATS Institute of Information Technology

Concurrent Processes

Rab Nawaz JadoonAssistant Professor

COMSATS Lahore

Pakistan

Operating System Concepts

Page 2: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Concurrent Processes

If more than one threads exists in a system at the same time, then the threads are said to be concurrent.

Two concurrent thread can execute completely independently of one another or they can execute in a cooperation.

Threads that operate independently of one another but must occasionally communicate and synchronize to perform cooperative tasks are said to execute asynchronously.

Asynchronism is a complex topic and more important for OS designer to answer the issues related to it.

2

Page 3: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Mutual Exclusion

Consider a mail server that processes emails for an organization.

System that monitors the total number of mails that have been sent since the day began.

Receipt of an email is handled by one of several concurrent threads.

Each time one of these threads receives an email from a user, the thread increments a process wide share variable, mailCount, by 1.

What happen if two thread wants to increment mailCount at once.

3

Page 4: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Mutual Exclusion

Assume that each thread execute the following assembly language code to increment the value in mailCount.

LOAD mailCount

ADD 1

STORE mailCount

Suppose mailCount = 21345

Suppose thread1 execute the first two instructions, thus leaving 21346 in the integer.

Its time expires and loses the processor and the system context switch to thread2.

4

Page 5: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Mutual Exclusion

The thread2 executes all three instructions, thus setting mailCount = 21346

Once thread1 resume, it will also update the same value in mailCount.

Due to uncontrolled access to the shared variable, the system has lost track of one email which should be 21347.

In this case this issue may be seen minor, but a similar error occurring in mission critical application such as air traffic control could cost lives.

5

Page 6: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Solution

To solve this problem an exclusive access should be granted to each thread to this shared resource.

Once an access is granted to shared variable the other who wanted to access this, should be kept in waiting until one completed his work.

Serializing access to the shared variable.

This is called mutual exclusion.

6

Page 7: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Bounded Buffer Problem

“counter++” in assembly language

MOV R1, counter

INC R1 MOV counter, R1

“counter--” in assembly languageMOV R2, counter

DEC R2 MOV counter, R2

7

Page 8: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Bounded Buffer Problem

If both the producer and consumer attempt to update the buffer concurrently, the machine language statements may get interleaved.

Interleaving depends upon how the producer and consumer processes are scheduled.

8

Page 9: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Bounded Buffer Problem

Assume counter is initially 5. One interleaving of statements is:

Producer: MOV R1, counter (R1 = 5)INC R1 (R1 = 6)

--------------------------------------------------------------Consumer: MOV R2, counter (R2 = 5)

DEC R2 (R2 = 4)

--------------------------------------------------------------Producer: MOV counter, R1 (counter = 6)

--------------------------------------------------------------Consumer: MOV counter, R2 (counter = 4)

The value of count may be either 4 or 6, where the correct result should be 5.

9

Page 10: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Process Synchronization

Race Condition:

The situation where several processes access and manipulate shared data concurrently, the final value of the data depends on which process finishes last.

10

Page 11: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Example

Bank transactions

11

BalanceD W

Deposit Withdrawal

MOV A, Balance MOV B, Balance

ADD A, Deposited SUB B, Withdrawn

MOV Balance, A MOV Balance, B

Page 12: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Bank Transactions

Bank transactions

Current balance 50,000

Check deposited 10,000

ATM Withdrawal 5,000

12

Page 13: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Bank Transactions

Check Deposit:

MOV A, Balance // A = 50,000

ADD A, Deposited // A = 60,000

ATM Withdrawal:

MOV B, Balance // B = 50,000

SUB B, Withdrawn // B = 45,000

Check Deposit:

MOV Balance, A // Balance = 60,000

ATM Withdrawal:

MOV Balance, B // Balance = 45,000

13

Page 14: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Java Multithreading: A case Study

Producer/Consumer relationship in Java

One thread creates data to store in shared object

Second thread reads data from that object

Large potential for data corruption if unsynchronized

14

Page 15: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

Buffer interface used in producer/consumer examples.

15

Page 16: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science 16

Producer/Consumer (Java) Producer class represents the producer thread

in a producer/consumer relationship (1 of 3).

Page 17: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Cont….(2 of 3)

17

Page 18: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Cont…(3 of 3)

18

Page 19: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

19

Consumer class represents the consumer thread in a producer/consumer relationship. (1 of 3)

Page 20: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Cont…(2 of 3)

20

Page 21: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Cont…(3 of 3)

21

Page 22: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java) UnsynchronizedBuffer class maintains the

shared integer that is accessed by a producer thread and a consumer thread via methods set and get. (1 of 2)

22

Page 23: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Cont…(2 of 2)

23

Page 24: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

24

SharedBuffer class enables threads to modify a shared object without synchronization. (1 of 4)

Page 25: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

SharedBuffer class enables threads to modify a shared object without synchronization. (2 of 4)

25

Page 26: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

26

SharedBuffer class enables threads to modify a shared object without synchronization. (3 of 4)

Page 27: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Producer/Consumer (Java)

27

SharedBuffer class enables threads to modify a shared object without synchronization. (4 of 4)

Page 28: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Critical sections

Mutual Exclusion needs to be enforced only when threads access shared modifiable data.

When a thread is accessing a share modifiable data, it is said to be in Critical Section or Critical region.

To prevent the kind of errors we see earlier, the system should ensure that only one thread at a time can execute the instructions in its critical section.

28

Page 29: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Critical Section

Once a thread has exited its critical section, a waiting thread may enter and execute its critical section.

A thread in critical section has exclusive access to the shared modifiable data and all other threads currently requiring access to that data are kept waiting.

So a thread should execute a critical section as quickly as possible.

A thread must not block inside its critical section.

Carefully coded to avoid infinite loops

29

Page 30: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Mutual Exclusion Primitives

Indicate when critical data is about to be accessed

Mechanisms are normally provided by programming language or libraries

Delimit beginning and end of critical section

enterMutualExclusion

exitMutualExclusion

30

Page 31: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Implementing ME Primitives

Common properties of mutual exclusion primitives

Each mutual exclusion machine language instruction is executed indivisibly i.e. once started it completes without interruption.

Cannot make assumptions about relative speed of thread execution.

Thread not in its critical section cannot block other threads from entering their critical sections

Thread may not be indefinitely postponed from entering its critical section

31

Page 32: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Dekker’s Algorithm

An elegant software implementation to ME was first presented by the Dutch mathematician Dekker.

Gives correct software implementation to ME that is free of deadlock and indefinite postponement.

32

Page 33: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore

A semaphore is a variable (non –ive integer) that,

Controlling access by multiple processes to a common resource in a parallel programming environment.

A useful way to think of a semaphore is as a record of how many units of a particular resource are available (without race conditions) adjust that record as units are required or become free, and if necessary wait until a unit of the resource becomes available.

33

Page 34: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore

Semaphores are a useful tool in the prevention of race conditions.

Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores.

Same functionality that mutexes have.

34

Page 35: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore (Example)

Suppose a library has 10 identical study rooms, intended to be used by one student at a time.

To prevent disputes, students must request a room from the front counter if they wish to make use of a study room.

When a student has finished using a room, the student must return to the counter and indicate that one room has become free.

If no rooms are free, students wait at the counter until someone relinquishes a room.

35

Page 36: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore (Example)

The clerk at the front desk does not keep track of which room is occupied, only the number of free rooms available.

When a student requests a room, the clerk decreases this number, if available.

When a student releases a room, the clerk increases this number.

Once access to a room is granted, the room can be used for as long as desired, and so it is not possible to book rooms ahead of time

36

Page 37: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore (Example)

In this scenario the front desk represents a semaphore, the rooms are the resources, and the students represent processes.

The value of the semaphore in this scenario is initially 10 (assume).

When a student requests a room he or she is granted access and the value of the semaphore is changed to 9. After the next student comes, it drops to 8, then 7 and so on.

If someone requests a room and the resulting value of the semaphore is negative, they are forced to wait.

37

Page 38: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semaphore

When multiple people are waiting, they will either wait in a queue, or use Round-robin scheduling and race back to the counter when someone releases a room.

Semaphore operations are commonly implemented in the nucleus of the OS where process state switching is controlled.

38

Page 39: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Important observation

Fairness and safety are likely to be compromised.

Requesting a resource and forgetting to release it

Releasing a resource that was never requested

Holding a resource for a long time without needing it

Using a resource without requesting it first (or using a resource after releasing it).

If processes follow these rules, multi-resource deadlock may still occur when there are different resources managed by different semaphores.

39

Page 40: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semantic & implementation

One important property of the semaphore variables is that its value cannot be changed except by using the wait() and signal() functions.

Counting semaphores are equipped with two operations,

V signal() and,

P wait()

Operation V increments the semaphore S, and,

Operation P decrements it.

The operations are atomic.

40

Page 41: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science

Semantic & implementation

The value of the semaphore S is the number of units of the resource that are currently available.

The P(S) operates as follows,

if S > 0

then S S – 1

else (wait on S)

V (S) operates as follows,

if (one or more processes are waiting on S)

then (let of the these processes proceed)

else S S+1

41

Page 42: CS Presentation template - Dr. Rab Nawaz Jadoon · 2013. 4. 1. · Rab Nawaz Jadoon Assistant Professor COMSATS Lahore Pakistan Operating System Concepts. Department of Computer Science

Department of Computer Science 42