• No results found

critical section - Unit 4.ppt

N/A
N/A
Protected

Academic year: 2020

Share "critical section - Unit 4.ppt"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)
(3)

Race Condition

A situation where several processes access and manipulate the same data

concurrently and the outcome of the execution depends on the particular order in

which the access takes place, is called a race condition.

To guard against the race condition , we need to ensure that only one process at a

time can be manipulating the variable counter. To make such a guarantee, we

(4)

Unit 4: Process Synchronization

Process Synchronization means sharing system resources by processes in a

such a way that, Concurrent access to shared data is handled thereby minimizing

the chance of inconsistent data. Maintaining data consistency demands

mechanisms to ensure synchronized execution of cooperating processes.

Process Synchronization was introduced to handle problems that arose while

(5)

Critical Section Problem

Consider system of

n

processes {

P

0

, P

1

, …, P

n-1

}

Each process has a

critical section

segment of code

Process may be changing common variables, updating table, writing file, etc.

When one process is in its critical section, no other may be executing in its critical

section

Critical-section problem

is to design a protocol that the processes can use to cooperate.

(6)

Critical Section Problem

A Critical Section is a code segment that accesses shared variables and

(7)

Critical Section

(8)

Solution to Critical-Section Problem

A solution to the critical section problem must satisfy the following three requirements:

1.

Mutual Exclusion

- If process

P

i

is executing in its critical section, then no other

processes can be executing in their critical sections

2.

Progress

- If no process is executing in its critical section and there exist some

processes that wish to enter their critical section, then only the processes not in

their remainder section can participate in the selection of the process that will

enter its critical section next

(9)

Peterson’s Solution

Software-based solution to the critical section problem known as Peterson's solution.

Solution restricted to two processes in alternate execution (critical section and

remainder section)

The two processes share two variables:

int turn

boolean flag[2]

The variable

turn

indicates whose turn it is to enter its critical section

The

flag

array is used to indicate if a process is ready to enter its critical section

(10)

do {

flag[i] = true;

turn = j;

while (flag[j] && turn == j);

critical section

flag[i] = false;

remainder section

} while (true);

Provable that

1.

Mutual exclusion is preserved

2.

Progress requirement is satisfied

3.

Bounded-waiting requirement is met

(11)

Provable:

Mutual Exclusion is preserved

Progress is satisfied

(12)

Semaphore

Synchronization is a tool to overcome the difficulty of critical section problem.

Semaphore

S

– integer variable that apart from initialization can be accessed via two

standard atomic operations.

Two standard operations modify

S

:

wait()

and

signal()

Can only be accessed via two indivisible (atomic) operations

(P)

wait (S) {

while (S <= 0)

; // no - op

S--;

}

(V)

signal (S) {

S++;

(13)

Semaphore

We can use semaphores to deal with the n – process critical

section problem.

The n processes share a semaphore , mutex (standing for mutual

exclusion), initialized to 1.

Each process Pi is organized as:

(14)

Semaphore Usage

Counting semaphore

– integer value can range over an unrestricted domain

Binary semaphore

– integer value can range only between 0 and 1

Then equivalent to a

mutex lock

Can solve various synchronization problems

Consider

P

1

and

P

2

that require

S

1

to happen before

S

2

P

1

:

S

1

;

signal(synch); //sync++ has added one resource

P

2

:

(15)

Semaphore Implementation

The main disadvantage of this implementation is that they all require busy

waiting.

This type of semaphore is also called a spinlock because the process “spins”

while waiting for the lock. Spinlocks do have an advantage in that no context

switching is required when a process must wait on a lock and a context switch

may take considerable time. Thus, when locks are expected to be held for

short times, spinlocks are useful, otherwise not.

To overcome the need of busy waiting, we can modify the definition of wait()

(16)

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 in the appropriate waiting queue

wakeup

– remove one of the processes in the waiting queue and place it in the ready queue

typedef struct {

int value;

struct process *list;

} semaphore;

wait(semaphore *S) {

S->value--;

if (S->value < 0) {

add this process to S->list;

block();

}

}

signal(semaphore *S) {

S->value++;

if (S->value <= 0) {

remove a process P from S->list;

wakeup(P);

(17)
(18)
(19)

Deadlock and Starvation

Deadlock

– two or more processes are waiting infinitely for an event that can be caused by

only one of the waiting processes

Let

S

and

Q

be two semaphores initialized to 1

P

0

P

1

wait(S); //exec 1st wait(Q); //exec 2nd

wait(Q); //exec 3rd wait(S); //exec 4th

...

...

signal(S); signal(Q);

signal(Q); signal(S);

Starvation

infinite blocking

(20)

Classical Problems of Synchronization

Classical problems used to test newly-proposed synchronization schemes

Bounded-Buffer Problem

Readers and Writers Problem

(21)

PRODUCER

(22)
(23)
(24)
(25)
(26)
(27)
(28)

Bounded-Buffer Problem

Shared data structures:

n

buffers, each can hold one item

Semaphore

mutex

initialized to the value 1

Semaphore

full

initialized to the value 0

Semaphore

empty

initialized to the value

n

The structure of the producer process

do {

...

/* produce an item in next_produced

*/

...

wait(empty);

wait(mutex);

...

/* add next_produced to the buffer

*/

...

The structure of the consumer process

do {

wait(full);

wait(mutex);

...

/* remove an item from buffer to next_consumed */

...

signal(mutex);

signal(empty);

...

(29)
(30)
(31)

Readers-Writers Problem

A data set is shared among a number of concurrent processes

Readers – only read the data set; they do

not

perform any updates

Writers – can both read and write

Problem

Allow multiple readers to read at the same time

Only a

single

writer can access the shared data at the same time

Several variations of how readers and writers are treated – all involve priorities

Variations to the problem:

First

variation – no reader kept waiting, unless writer has permission to use shared object or no reader should wait

for other readers to finish simply because a writer is waiting.

Second

variation – once writer is ready, it performs a write as soon as possible. OR if a writer is waiting to access

the object, no new reader may start reading.

Both may have starvation, leading to even more variations

first: readers keep coming in while writers are never treated

second: writers keep coming in while readers are never treated

(32)

Readers-Writers Problem

Shared data

Data set

Semaphore

wrt

initialized to 1 (Common to both reader and writer)

Semaphore

mutex

initialized to 1 (is used to ensure mutual exclusion when read_count is updated)

Integer

read_count

initialized to 0

The structure of a writer process

do {

wait(wrt);

...

/* writing is performed */

...

signal(wrt);

} while (true);

The structure of a reader process

do {

wait(mutex);

read_count++;

if (read_count == 1)

wait(wrt);

signal(mutex);

...

/* reading is performed */

...

wait(mutex);

read_count--;

if (read_count == 0)

signal(wrt);

signal(mutex);

} while (true);

Wrt will provide mutual exclusion

(33)
(34)

Dining-Philosophers Problem

Philosophers spend their lives thinking and eating

They do not interact with their neighbors, occasionally

try to pick up 2 chopsticks (one at a time, on either side)

to eat from bowl

Need both chopsticks to eat

Put back in their place both chopsticks when done

eating

It is a simple representation of the need to allocate

several resources among several processes in a

deadlock and starvation free manner.

Shared data for 5 philosophers

Bowl of rice (data set)

Semaphore chopstick[5] initialized to 1

The structure of Philosopher

i

:

do {

wait(chopstick[i]);

wait(chopstick[(i+1)%5]);

// eat

signal(chopstick[i]);

signal(chopstick[(i+1)%5]);

// think

(35)

Dining – Philosopher problem

Allow at most four philosophers to be sitting simultaneously at the table.

Allow a philosopher to pick up her chopsticks only if both chopsticks are available.

Use an asymmetric solution; that is, an odd philosopher picks up her first her left

chopstick and then her right chopstick, whereas an even philosopher picks up her right

chopstick and then her left chopstick.

Finally, any satisfactory solution to the dining philosopher problem must guard against

the possibility that one of the philosopher will starve to death.

(36)

References

Related documents

Section 3 The Research Problem, Objectives and Rationale Section 4 Formulating and Clarifying the Research Topic Section 5 Conducting a Critical Literature Review Section 6

With this set of model parameters: x for the exit rate, b for the baseline proportion female of the firm and of non-network applicants, p for the proportion of network applicants

Each division will ensure that operating units responsible for critical business processes identified in section II develop a Business Continuity Plan that enables the operating

Furthermore, it summarizes the tax incentives for companies (not requiring the residency of foreign owners and managers in Puerto Rico), providing a zoom-in on Act 20 regarding

Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its

That night, in my hotel room, I was reflecting on his advice and also some issues of priorities or balance in my own life including some tension I was feeling between time spent

Generally speaking the anterior influences male sexual activity (sex hormone and sperm production and masculine personality. traits) while the posterior influences

The technical analysis, reviewed by staff from DIT, includes such factors as proposed system architecture and its compatibility with the county’s technical architecture