Você está na página 1de 6

Hardware based solution using process synchronization hardware

Many system provide hardware support for critical code

Uniprocessor
Could disable interrupts currently running code would execute without preemption. Generally to efficient on multiprocessor systems.
Operating systems using this is not broadly scalable. Modern machines provide specials atomic hardware instructions. Atomic = non-interruptible. Either test memory word and set value. Or swap contents of two memory words.

Test and set instruction


In computer science the test and set instruction is an instruction used to write to a memory location and returns it value as a single atomic operation.

Boolean set (Boolean*target) { Boolean rv = *target; *target = true; Return rv; }

Solution using Test and Set


Shared Boolean variable lock.. Initialized to false solution Do { While (Test And Set (&lock)) ; /* do nothing // critical Section

Lock = False; //reminder section } while (True);

Swap Instruction
In computer programming the act of swapping two variables refers to mutually exchanging the values of the Variable.Usually, this is done with the data in memory. Void Swap (Boolean*a, Boolean*b) { Boolean temp = *a; *a =*b;| *b=temp; }

Readers Writers problem


A data set is shared among a number of concurrent process

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 single writer can access the shared data at the same time.

Shared data Data set Semaphore mutex initializer

Semaphore wrt initializer Integer readcount initializer

The Structure of a writer process


Do { Wait (wrt); //Writing is performed Signal (wrt); } While (true)

The producer-consumer problem


The producer consumer problem illustrates the need for synchronization in systems where many process share a resource. In order to synchronize these process, we will block the producer when the buffer is full and we will block the consumer when the buffer is empty.

Producer
1. Make a new widget w 2. If buffer is full go to sleep

3. Put widget in buffer 4. If buffer is empty, wake consumer.

Consumer
1.If buffer is empty, go to sleep.

2. Take a widget from buffer. 3. If Buffer is full, wake producer. 4. Consume the widget <w.

Structure Int item count: Procedure Produce () { While (true) { Item =produce item () If (item count== buffer_size){ Sleep () } Put item into buffer (item) Item count =itemcount+1; If (item count==1) Wakeup (consumer) } } }

Procedure consumer () { While (true)

{ (Item count==0) { Sleep () } Semaphore full =0 Semaphore empty = buffer_size Procedure produce () { While (true) { Item = produce Item () Clown (empty) Put item into buffer (item) Up (full) } }

Dining Philosophers Problem


In the computer science the dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques to resolve them.

Suppose Share data Bowl in rice (dataset) Semaphore chopstick [5] initialized to 1

The structure of Philosophers problem


Do { Wait (chopstick) Wait (chopstick [(i+1) %5]); //eat Signal (chopstick [i]); Signal (chopstick [(i+1) % 5]); //think } While(true);

Você também pode gostar