itft_semaphores and bounded buffer

14
Semaphores and Bounded Buffer

Upload: sneh-prabha

Post on 21-May-2015

266 views

Category:

Education


5 download

DESCRIPTION

semaphore and bounded buffer method

TRANSCRIPT

Page 1: ITFT_Semaphores and bounded buffer

Semaphores and Bounded Buffer

Page 2: ITFT_Semaphores and bounded buffer

Semaphores

• Semaphore is a type of generalized lock• Defined by Dijkstra in the last 60s

• Main synchronization primitives used in UNIX

• Consist of a positive integer value

• Two operations• P(): an atomic operation that waits for semaphore to

become positive, then decrement it by 1

• V(): an atomic operation that increments semaphore by 1 and wakes up a waiting thread at P(), if any.

Page 3: ITFT_Semaphores and bounded buffer

Semaphores vs. Integers

• No negative values

• Only operations are P() and V()• Cannot read or write semaphore values

• Except at the initialization times

• Operations are atomic• Two P() calls cannot decrement the value below

zero

• A sleeping thread at P() cannot miss a wakeup from V()

Page 4: ITFT_Semaphores and bounded buffer

Binary Semaphores

• A binary semaphore is initialized to 1

• P() waits until the value is 1• Then set it to 0

• V() sets the value to 1• Wakes up a thread waiting at P(), if any

Page 5: ITFT_Semaphores and bounded buffer

Two Uses of Semaphores

1. Mutual exclusion• Lock was designed to do this

lock->acquire();

// critical section

lock->release();

Page 6: ITFT_Semaphores and bounded buffer

Two Uses of Semaphores

1. Mutual exclusion1. The lock function can be realized with a binary

semaphore: semaphore subsumes lock.• Semaphore has an initial value of 1

• P() is called before a critical section

• V() is called after the critical section

semaphore litter_box = 1;

P(litter_box);

// critical section

V(litter_box);

Page 7: ITFT_Semaphores and bounded buffer

Two Uses of Semaphores

1. Mutual exclusion• Semaphore has an initial value of 1

• P() is called before a critical section

• V() is called after the critical section

semaphore litter_box = 1;

P(litter_box);

// critical section

V(litter_box);litter_box = 1

Page 8: ITFT_Semaphores and bounded buffer

Two Uses of Semaphores

1. Mutual exclusion• Semaphore has an initial value of 1

• P() is called before a critical section

• V() is called after the critical section

semaphore litter_box = 1;

P(litter_box); // purrr…

// critical section

V(litter_box);litter_box = 1 0

Page 9: ITFT_Semaphores and bounded buffer

Producer-Consumer with a Bounded Buffer

• A classic problem

• A producer put things into a shared buffer

• A consumer takes them out

Page 10: ITFT_Semaphores and bounded buffer

Problem Constraints

• The solution involves both synchronization and mutual exclusion

• Constraints• The consumer must wait if buffers are empty

(synchronization constraint)

• The producer must wait if buffers are full (synchronization constraint)

• Only one thread can manipulate the buffer at a time (mutual exclusion)

Page 11: ITFT_Semaphores and bounded buffer

Implementing Semaphore

• How to implement semaphore?• Almost exactly like lock.

• Using spinlock or queue

• What hardware support is needed?• Interrupt disable

• Test-and-set

Page 12: ITFT_Semaphores and bounded buffer

Implementing Semaphoreclass semaphore {

int value;

}

semaphore::semaphore(int i) {

value = i;

}

semaphore::p() {

// disable interrupts

while (value == 0) {

// enable interrupts

// disable interrupts

}

value --;

// enable interrupts

}

semaphore::v() {

// disable interrupts

value ++;

// enable interrupts

}

Page 13: ITFT_Semaphores and bounded buffer

Implementing Semaphore with test and setclass semaphore {

int value;

}

semaphore::semaphore(int

i) {

value = i;

}

semaphore::p() {

while

(test_and_set(guard));

while (value == 0) {

// queue the

thread

// guard = 0 and

sleep

}

value --;

guard = 0;

}

semaphore::v() {

while

(test_and_set(guard));

if (anyone waiting) {

// wake up one thread

// put in on ready

queue

} else {

value ++;

}

guard = 0;

}

Page 14: ITFT_Semaphores and bounded buffer

Semaphore in UNIX

• Managing concurrent access to shared memory.

• Semaphore system calls

• Creation: semget( … )

• Incr/Decr/set : semop(…)

• Deletion: semctl(semid, 0, IPC_RMID, 0);

• See examples: seminit.c, sema.csemb.c