synchronization n.
Download
Skip this Video
Download Presentation
Synchronization

Loading in 2 Seconds...

play fullscreen
1 / 31

Synchronization - PowerPoint PPT Presentation


  • 99 Views
  • Uploaded on

Synchronization. CS470 -- Spring 2002. Overview. Concurrency Problems and Mutual Exclusion Requirements Software methods with busy-wait Hardware Support for Mutual Exclusion Software methods without busy-wait Semaphores Message Passing. Concurrency Problems.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Synchronization' - elden


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
synchronization

Synchronization

CS470 -- Spring 2002

overview
Overview
  • Concurrency Problems and Mutual Exclusion Requirements
  • Software methods with busy-wait
  • Hardware Support for Mutual Exclusion
  • Software methods without busy-wait
    • Semaphores
    • Message Passing
concurrency problems
Concurrency Problems
  • Interleaved & Overlapped computation - both exhibit similar problems
  • Ordering access to global resources - leads to reservation of resources
  • Reservation of resources leads to inefficient use.
  • Lack of reproducibility makes debugging and testing difficult.
example
Example
  • int GetTicket(void) {

static customerNbr;

return customerNbr++;

}

Fails to give sequential unduplicated tickets

vocabulary
Vocabulary
  • Mutual Exclusion protects a critical resource by allowing no more than one thread at a time to execute in a critical section of code which handles that resource.
  • Mutual exclusion can lead to deadlock or starvation
win32 critical sections
Win32 Critical Sections
  • Works for threads in same process
  • CRITICAL_SECTION data structure
    • InitializeCriticalSection (&cs) or InitializeCriticalSectionAndSpinCount( &cs, cnt)
    • EnterCriticalSection (&cs) or TryEnterCriticalSection(&cs)
    • LeaveCriticalSection(&cs)
  • Implemented with a semaphore
mutual exclusion requirements
Mutual Exclusion Requirements
  • Mutual exclusion - at most one thread at a time can be in the critical section
  • Threads not in the critical section and not trying to enter it cannot interfere with those trying to enter it
  • No deadlock or starvation possible if no thread dallies in the critical section
software approaches
Software Approaches
  • Standard programming methods are both difficult to code and error prone
    • Some failed approaches
    • Dekker’s Algorithm
    • Peterson’s Algorithm
  • All make inefficient use of processor because they spin in busy-wait loops.
attempt 1 taking turns
Attempt 1 - Taking Turns

BOOL turn = FALSE;

DoThread(BOOL me) {

DoNonCritical ( );

while (turn != me) ;

DoCriticalSection( );

turn = !me;

DoMoreNonCritical( );

}

Does Mutual

Exclusion

without deadlock

or starvation;

but failure outside of critical section can prevent other thread from entering.

attempt 2 checking other
Attempt 2 - checking other

BOOL inside[2] =

{FALSE, FALSE};

DoThread(BOOL me) {

DoNonCritical ( );

while (inside[!me]) ;

inside[me] = TRUE;

DoCriticalSection ( );

inside[me] = FALSE;

DoMoreNonCritical ( ); }

Does not

guarantee

mutual

exclusion

attempt 3 early locking
Attempt 3 - early locking

BOOL inside[2] =

{FALSE, FALSE};

DoThread(BOOL me) {

DoNonCritical ( );

inside[me] = TRUE;

while (inside[!me]) ;

DoCriticalSection ( );

inside[me] = FALSE;

DoMoreNonCritical ( ); }

Just swap

two lines….

Does mutual

exclusion; but

can easily

deadlock.

attempt 4 intermittant locks
Attempt 4 - intermittant locks

DoNonCritical ( );

do {

inside[me] = FALSE;

Sleep(DELAY);

inside[me] = TRUE;

} while (inside[!me]);

DoCriticalSection ( );

inside[me] = FALSE;

DoMoreNonCritical ( );

Mutual exclusion is achieved; but starvation could result.

peterson s algorithm
Peterson’s Algorithm

BOOL turn, inside[2] = {FALSE, FALSE};

DoThread(BOOL me) {

DoNonCritical ( );

inside[me] = TRUE;

turn = !me;

while (inside[!me] && turn != me) ;

DoCriticalSection ( );

inside[me] = FALSE;

DoMoreNonCritical ( ); }

Simpler

Algorithm

hardware disabling interrupts
Hardware: Disabling Interrupts
  • Method:

DisableInterrupts( );

DoCriticalSection ( );

EnableInterrupts ( );

  • Based on thread context switch being triggered by (clock) interrupt
  • Works only with uniprocessor
  • Reduces total system concurrency
  • Intel: cli and sti instructions

Works for

n threads

hardware test and set
Hardware: Test and Set
  • Special atomic instruction:

BOOL TestSet (BOOL *bitPtr) {

BOOL ret = *bitPtr;

*bitPtr = TRUE;

return ret;

}

  • Intel 386: lock bts mem, reg/imm

where 2nd operand is bit offset in first operand

using test set
Using Test & Set

BOOL bit;

DoThread (void *c ) {

DoNonCritical ( );

while (TestSet(&bit)) ;

DoCriticalSection ( );

bit = FALSE;

DoMoreNonCritical ( );

}

Starvation

could occur;

but works for

n threads.

hardware exchange
Hardware: Exchange
  • Special Atomic Instruction:

Exchange(BOOL *a, BOOL *b) {

BOOL temp = *a;

*a = *b;

*b = temp;

}

  • Intel 386: lock xchg mem, reg
using exchange
Using Exchange

Starvation is

possible; but

works for n

threads.

BOOL bit = FALSE;

DoThread(void *c) {

BOOL key;

DoNonCritical( );

key = TRUE;

do {Exchange(bit, key); } while (key);

DoCriticalSection ( );

bit = FALSE;

DoMoreNonCritical ( ); }

win32 interlocked operations
Win32 Interlocked Operations
  • Allows for atomic operations on DWORD by threads in same process or otherwise sharing memory
  • DWORD target must be at even modulo four address on Intel multiprocessor machines
interlocked operation list
Interlocked Operation List
  • InterlockedIncrement(PLONG)
  • InterlockedDecrement(PLONG)
  • InterlockedExchange(PLONG target, LONG value)
  • InterlockedExchangeAdd(PLONG Addend, LONG increment)
  • InterlockedExchange(PVOID dest, PVOID exchange, PVOID comperand)
hardware support summary
Hardware Support Summary
  • Advantages
    • Applies to n threads
    • Simple and easy to verify
    • Different variables give different locks
  • Disadvantages
    • Busy-wait wastes cpu resources
    • Starvation is possible
    • Deadlock is possible -- e.g. waiting on lock held by lower priority process.
counting semaphores 1 of 2
Counting Semaphores (1 of 2)

struct semaphore {

DWORD count;

ThreadList tList;

}

void Wait(struct semaphore *s) {

if (- - s  count < 0) {

EnqueueAndBlock(self, s  tList);

}

}

Wait is

atomic.

counting semaphores 2 of 2
Counting Semaphores (2 of 2)

void Signal(struct semaphore *s) {

if (++s  count <= 0) {

MoveThreadToRunQueueFrom(

s  tList);

}

}

Signal

is

atomic.

binary semaphores 1 of 2
Binary Semaphores (1 of 2)

struct BSemaphore {

BOOL value;

ThreadList tList;

}

void BWait(struct BSemaphore *s) {

if (s  value == TRUE) {

s  value = FALSE;

} else {

EnqueueAndBlock(self, s  tList); }}

BWait is

atomic.

binary semaphores 2 of 2
Binary Semaphores (2 of 2)

void BSignal(struct BSemaphore *s) {

if (s  tList == NULL) {

s  value = TRUE;

} else {

MoveThreadToRunQueueFrom(

s  tList);

}

}

BSignal

is

atomic.

using semaphores
Using Semaphores

Could use

Binary

Semaphores.

Could have

multiple critical

sections, etc.

Use TestSet, etc.

to implement in

operating system.

struct semaphore s;

DoThread(void *c) {

DoNonCritical( );

Wait(&s);

DoCriticalSection( );

Signal(&s);

DoMoreNonCritical( );

}

message passing
Message Passing
  • Message Operations
    • Send (destination, message)
    • Receive (source, message)
  • Synchronization
    • Send - blocking or non-blocking
    • Receive - blocking or non-blocking, may be able to check for arrived messages
    • typical: non-blocking sends, blocking receives with arrival check
message addressing
Message Addressing
  • Direct
    • Send has explicit address of addressee
    • Receive
      • explicit address of sender
      • implicit, known only after receipt
  • Indirect via mailboxes
    • static via permanent ports
    • dynamic with connect / disconnect
  • Queuing - FIFO, message priorities
mutual exclusion via messages
Mutual Exclusion via Messages
  • Initialize mbox with 1 message
  • DoThread(void *c) {

DoNonCritical ( );

Receive( mbox, message);

DoCriticalSection ( );

Send ( mbox, message);

DoMoreNonCritical ( );

}

producer consumer 1 of 2
Producer / Consumer (1 of 2)
  • Initialize mayproduce with n messages, mayconsume as empty
  • Producer(void *c) {

MESSAGE pmsg;

while (TRUE) {

receive(mayproduce, &pmsg);

pmsg = Produce( );

send(mayconsume, &pmsg);

} }

producer consumer 2 of 2
Producer / Consumer (2 of 2)
  • Consumer(void *c) {

MESSAGE cmsg;

while (TRUE) {

Receive(mayconsume, &cmsg);

Consume(&cmsg);

Send(mayproduce, &cmsg);

}

}

  • Allows multiple servers and clients