process synchronization

38
V.A. CSED,TU Process Synchronization Organized By: V.A.

Upload: vinay-arora

Post on 20-Jan-2015

1.411 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Process Synchronization

V.A.

CSED,TU

Process Synchronization

Organized By: V.A.

Page 2: Process Synchronization

VA.

CSED,TU

Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content has been taken mainly from the following books:Operating Systems Concepts By Silberschatz & Galvin,

Operating systems By D M Dhamdhere, System Programming By John J Donovan

etc…

Page 3: Process Synchronization

VA.

CSED,TU

Process & Synchronization

Process – Program in Execution.

Synchronization – Coordination.

Independent process cannot affect or be affected by the execution of another process

Cooperating process can affect or be affected by the execution of another process

Advantages of process cooperation

Information sharing Computation speed-up Modularity Convenience

Page 4: Process Synchronization

VA.

CSED,TU

Buffer

Page 5: Process Synchronization

VA.

CSED,TU

Bounded-Buffer – Shared-Memory Solution

Shared Data

#define BUFFER_SIZE 10

typedef struct {

. . .

} item;

item buffer [BUFFER_SIZE];

int in = 0;

int out = 0;

Can only use BUFFER_SIZE-1 elements

Page 6: Process Synchronization

VA.

CSED,TU

Producer – Consumer

Page 7: Process Synchronization

VA.

CSED,TU

Bounded-Buffer – Producer

while (true) { /* Produce an item */

while (((in = (in + 1) % BUFFER SIZE count) == out)

; /* do nothing -- no free buffers */

buffer[in] = item;

in = (in + 1) % BUFFER SIZE;

}

Page 8: Process Synchronization

VA.

CSED,TU

Bounded Buffer – Consumer

while (true) {

while (in == out)

; // do nothing -- nothing to consume

// remove an item from the buffer

item = buffer[out];

out = (out + 1) % BUFFER SIZE;

return item;

}

Page 9: Process Synchronization

VA.

CSED,TU

Setting Final value of Counter

Page 10: Process Synchronization

VA.

CSED,TU

Buffer Types

Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process

Unbounded-buffer places no practical limit on the size of the buffer

Bounded-buffer assumes that there is a fixed buffer size

Page 11: Process Synchronization

VA.

CSED,TU

RC & CS

Race Condition – Where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which access takes place.

Critical Section – Segment of code in which Process may be changing common variables, updating a table, writing a file and so on.

Page 12: Process Synchronization

VA.

CSED,TU

Peterson’s Solution

Page 13: Process Synchronization

VA.

CSED,TU

Peterson’s Solution

Var flag : array [0…1] of Boolean;Turn : 0..1;

BeginFlag[0] = false;Flag[1] = false;

Parbegin Repeat Repeat

Flag[0] = true Flag[1] = trueTurn = 1 Turn = 0While flag[1] && turn==1 While flag[0] && turn==0 Do {nothing}; Do {nothing};{Critical Section} {Critical Section}Flag[0] = false; Flag[1] = false;{Remainder} {Remainder}

Forver; Forver;Parendend

Page 14: Process Synchronization

VA.

CSED,TU

Peterson’s Solution

Page 15: Process Synchronization

VA.

CSED,TU

Synchronization Hardware

Many Systems provide hardware support for critical section code

Uni-Processors – Could disable Interrupts

Currently running code would execute without preemption Generally too inefficient on multiprocessor systems

Modern machines provide special atomic hardware instructions Atomic :- Uninterruptible

Either Test memory word and Set value Or Swap contents of two memory words

Page 16: Process Synchronization

VA.

CSED,TU

TestAndSet Instruction

Definition:

boolean TestAndSet (boolean *target)

{

boolean rv = *target;

*target = TRUE;

return rv:

}

Page 17: Process Synchronization

VA.

CSED,TU

Solution using TestAndSet

Shared Boolean variable Lock, Initialized to FALSE.

Solution: do { while ( TestAndSet (&lock )) ; /* do nothing

// critical section

lock = FALSE;

// remainder section

} while ( TRUE);

Page 18: Process Synchronization

VA.

CSED,TU

Swap Instruction

Definition:

void Swap (boolean *a, boolean *b)

{

boolean temp = *a;

*a = *b;

*b = temp:

}

Page 19: Process Synchronization

VA.

CSED,TU

Solution using Swap

Shared Boolean variable lock initialized to FALSE, Each process has a local Boolean variable key.

Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section

lock = FALSE;

// remainder section

} while ( TRUE);

Page 20: Process Synchronization

VA.

CSED,TU

Semaphore

Synchronization tool that does not require busy waiting Semaphore S – Integer Variable Two standard operations modify S: wait() and signal()

Originally called P() and V() Less complicated

Can only be accessed via two indivisible (atomic) operations wait (S) { while S <= 0

; // no-op S--; }

signal (S) { S++; }

Page 21: Process Synchronization

VA.

CSED,TU

Semaphore as General Synchronization Tool

Counting Semaphore – Integer value can range over an unrestricted domain

Binary Semaphore – Integer value can range only between 0 and 1, Can be simpler to Implement

Also known as Mutex locks

Can implement a counting semaphore S as a binary semaphore

Provides mutual exclusion Semaphore S; // initialized to 1 wait (S); Critical Section signal (S);

Page 22: Process Synchronization

VA.

CSED,TU

Semaphore Implementation

Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time

Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section.

Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied

Note that applications may spend lots of time in critical sections and therefore this is not a good solution.

Page 23: Process Synchronization

VA.

CSED,TU

Semaphore Implementation with no Busy waiting

With each Semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items:

Value (of type Integer) Pointer to next record in the list

Two operations:

Block – Place the Process invoking the operation on the appropriate waiting queue.

Wakeup – Remove one of Processes in the waiting queue and place it in the ready queue.

Page 24: Process Synchronization

VA.

CSED,TU

Semaphore Implementation with no Busy waiting (Cont.)

Implementation of WAIT:

wait (S){ value--; if (value < 0) {

add this process to waiting queue block(); }

}

Implementation of SIGNAL:

Signal (S){ value++; if (value <= 0) {

remove a process P from the waiting queue wakeup(P); }

}

Page 25: Process Synchronization

VA.

CSED,TU

Deadlock and Starvation

Deadlock – Two or more Processes are waiting Indefinitely for an event that can be caused by only one of the waiting processes

Let S and Q be two Semaphores initialized to 1P0 P1

wait (S); wait (Q); wait (Q); wait (S);

. .

. .

. . signal (S); signal (Q); signal (Q); signal (S);

Starvation – Indefinite Blocking. A process may never be removed from the semaphore queue in which it is suspended.

Page 26: Process Synchronization

VA.

CSED,TU

Producer Consumer

Page 27: Process Synchronization

VA.

CSED,TU

Solution to PC must satisfy 3 conditions

Page 28: Process Synchronization

VA.

CSED,TU

Solution to PC with Busy Wait

Page 29: Process Synchronization

VA.

CSED,TU

Solution to PC with Signaling

Page 30: Process Synchronization

VA.

CSED,TU

Indivisible Operations for PC

Page 31: Process Synchronization

VA.

CSED,TU

Readers & Writers in Banking System

Page 32: Process Synchronization

VA.

CSED,TU

Correctness Condition for R-W Problem

Page 33: Process Synchronization

VA.

CSED,TU

Solution outline for R-W without Writer priority

Page 34: Process Synchronization

VA.

CSED,TU

Dining Philosophers

Page 35: Process Synchronization

VA.

CSED,TU

Outline of a Philosopher Process Pi

Page 36: Process Synchronization

VA.

CSED,TU

An Improved Outline of a Philosopher Process

Page 37: Process Synchronization

VA.

CSED,TU

Reference List

Operating Systems Concepts By Silberschatz & Galvin,

Operating systems By D M Dhamdhere,

System Programming By John J Donovan,

www.os-book.com

www.cs.jhu.edu/~yairamir/cs418/os2/sld001.htm

http://gaia.ecs.csus.edu/~zhangd/oscal/pscheduling.html

http://www.edugrid.ac.in/iiitmk/os/os_module03.htm

http://williamstallings.com/OS/Animations.html

etc…

Operating Systems Concepts By Silberschatz & Galvin,

Operating systems By D M Dhamdhere,

System Programming By John J Donovan,

www.os-book.com

www.cs.jhu.edu/~yairamir/cs418/os2/sld001.htm

http://gaia.ecs.csus.edu/~zhangd/oscal/pscheduling.html

http://www.edugrid.ac.in/iiitmk/os/os_module03.htm

http://williamstallings.com/OS/Animations.html

etc…

Page 38: Process Synchronization

VA.

CSED,TU

Thnx…