Event Counter
Event Counter
Threads
• Most modern applications are multithreaded
• Threads run within application
• Multiple tasks with the application can be implemented
by separate threads
• Update display
• Fetch data
• Spell checking
• Answer a network request
• Process creation is heavy-weight while thread creation is
light-weight
• Can simplify code, increase efficiency
• Kernels are generally multithreaded
Benefits
• Responsiveness – may allow continued execution if
part of process is blocked, especially important for user
interfaces
• Resource Sharing – threads share resources of
process, easier than shared memory or message
passing
• Economy – cheaper than process creation, thread
switching lower overhead than context switching
• Scalability – process can take advantage of
multiprocessor architectures
Less time to Threads enhance
terminate a efficiency in
thread than a Switching communication
Takes less between two between
process
time to threads takes programs
create a less time than
new thread switching
than a between
process processes
Single and Multithreaded Processes
Single thread Vs multi thread
Multicore programming
User Level
Thread (ULT)
Kernel level
Thread (KLT)
User-Level Threads (ULTs)
• Thread
management is
done by the
application
• The kernel is not
aware of the
existence of
threads
Scheduling of user level threads
Relationships Between ULT
States and Process States
Possible
transitions
from 4.6a:
4.6a→4.6b
4.6a→4.6c
4.6a→4.6d
Advantages of ULTs
ULTs
can
Scheduling can
run on
be application
any OS
specific
Thread switching does
not require kernel
mode privileges (no
mode switches)
Disadvantages of ULTs
Examples:
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Many-to-Many Model
Many-to-Many Model
Allows many user level threads to be
mapped to many kernel threads
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Two-level Model
Thread Libraries
Thread library provides programmer
with API for creating and managing
threads
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Bounded-Buffer – Producer
item next_produced;
while (true){
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing
*/
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
• Shared data
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
• Solution is correct, but can only use
BUFFER_SIZE-1 elements
Bounded-Buffer – Insert() Method
while (true) {
/* Produce an item */
while((((in + 1) % BUFFER SIZE count) == out)
}
Bounded Buffer – Remove() Method
while (true) {
while (in == out)
; // do nothing -- nothing to
consume
register1 = count
register1 = register1 + 1
count = register1
• count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
• Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1
{register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1
{register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}
Critical Sections
A section of code, common to n cooperating processes, in which the processes may
be accessing common variables.
Critical Section Code in which only one process can execute at any one time.
Exit Section The end of the critical section, releasing or allowing others in.
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
}
Bakery Algorithm
• N process solution
Synchronization Hardware
• Many systems provide hardware support
for critical section code
• Uniprocessors – could disable interrupts
• Currently running code would execute
without preemption
• Generally too inefficient on multiprocessor
systems
• Operating systems using this not broadly scalable
• Modern machines provide special atomic
hardware instructions
• Atomic = non-interruptable
• Either test memory word and set value
• Or swap contents of two memory words
TestAndSet Instruction
• Definition:
while (true) {
while ( TestAndSet (&lock )) ; /* do nothing
// critical section
lock = FALSE;
// remainder section
}
boolean waiting[n];
boolean lock;
lock = FALSE; waiting[i] = FALSE;
do
{
waiting[i]=TRUE;
Key=TRUE;
While(waiting[i] && key)
key=TestAndSet(&lock);
Waiting[i]=FALSE;
//CS
j=(i+1)%n;
While((j!=i)&&!waiting[j])
j=(j+1)%n;
If(j==i)
lock=FALSE;
Else
Waiting[j]=FALSE;
//Remainder section
} while(TRUE);
Swap Instruction
• Definition:
// critical section
lock = FALSE;
// remainder section
}
Semaphore
• Semaphore S – integer variable
• Two standard operations modify S: wait() and signal()
• Originally called P() and V()
• Can only be accessed via two indivisible (atomic) operations
• wait (S) {
while S <= 0; // no-op
S--;
}
• signal (S) {
S++;
}
Semaphore as General Synchronization Tool
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
• 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
mutex=1, full=0,empty=N
while (true) {
// produce an item
wait (empty);
wait (mutex);
signal (mutex);
signal (full);
}
The structure of the consumer process
while (true) {
wait (full);
wait (mutex);
signal (mutex);
signal (empty);
}
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.
• Shared Data
• Data set
• Semaphore mutex initialized to 1.
• Semaphore wrt initialized to 1.
• Integer readcount initialized to 0.
Readers-Writers Problem (Cont.)
• The structure of a writer process
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
Readers-Writers Problem (Cont.)
• The structure of a reader process
while (true) {
wait (mutex) ;
readcount ++ ;
if (readercount == 1) wait (wrt) ;
signal (mutex);
// reading is performed
wait (mutex) ;
readcount - - ;
if (redacount == 0) signal (wrt) ;
signal (mutex) ;
}
Dining-Philosophers Problem
• Shared data
• Bowl of rice (data set)
• Semaphore chopstick [5] initialized to 1
Philo 1
Wait(chopstick[0]); //0
Wait(chopstick[1]); //0
Eat
Signal(chopstick[0]);
Signal(chopstick[1]);
Dining-Philosophers Problem (Cont.)
• The structure of Philosopher i:
While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
}
Sleeping Barber Problem
• If there are no customers, the barber falls asleep in the
chair
• A customer must wake the barber if he is asleep
• If a customer arrives while the barber is working, the
customer leaves if all chairs are occupied or sits in an
empty chair if it's available
• When the barber finishes a haircut, he inspects the
waiting room to see if there are any waiting customers
and falls asleep if there are none
Problems with Semaphores
• Correct use of semaphore
operations:
• 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.
Semaphore Implementation with no Busy waiting
(Cont.)
• Implementation of wait:
wait (S){
S--;
if (S < 0) {
add this process to waiting queue
block(); }
}
• Implementation of signal:
Signal (S){
S++;
if (S <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
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 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
dp.pickup (i)
EAT
dp.putdown (i)