Sunteți pe pagina 1din 18

THREADS

• Introduction
• Threads Usage
• Design Issues for Thread Packages
• Implementing a Thread Package

1
Process vs. Thread (1)

• Process: Program in execution


– Resources, privileges, etc
• Thread: unit of execution
– Program counter, Stack Pointer, registers
• Each process has one or more threads
• Each thread belong to one process

2
Process vs. Thread (2)
• Processes
– Inter-process communication is expensive:
need to context switch
– Secure: one process cannot corrupt another
process

3
Process vs. Thread (3)
• Threads
– Inter-thread communication cheap: can use
process memory and may not need to
context switch
– Not secure: a thread can write the memory
used by another thread

4
1.Intoduction
• Threads are mini processes or
light weight processes.

• Threads are used to structure the


large applications into many
parts that could be logically
executed at the same time.

5
Thread Characteristics
• All the threads have same address
space, which means that they also
share the same global variables.
Per Thread
Items

Program counter

Stack

Register

Child threads
6
State
2.Thread Usage
Dispatcher
File Thread
Server Worker
Process thread

Shared
block
cache

Request for mailbox (b) Team model


Work arrives
7
(a) Dispatcher/Worker
Pipeline Model

Request for
Kernel
Work arrives 8
Design Issues for thread
Packages
• Thread management
• Thread creation and destruction
• Accessing Data
• Handling Deadlock

9
Accessing Data
• Access to shared data is done using
critical regions.
• Critical regions are implemented in
threads package using mutex and a
synchronization feature called
condition variable.
• Mutex is a binary semaphore.
• condition variable is similar
condition variable used for
synchronization in monitors 10
Handling Deadlock
Critical region

Thread_1 locks
mutex m1 Thread_2 R2
m2
R1
R3
R4
Thread_1 m1

11
Handling Deadlocks
Lock mutex; lock mutex;
check data structures; mark resource
as busy;
while(resource busy) unlock mutex;

wait(condition variable)
wakeup(condition variable);
mark resource as busy;
Unlock mutex;

12
Threads implimentation:
• Threads are generally provided in the
form of thread package.

• There are basically two approaches


to impliment a thread package:
1. Implementing threads in user-
space.

2. Implementing threads in the


kernel. 13
Implementing Threads in User-
Space
3 4
0 1 2 ad d
1.Threads run on top of a Run a d a d a d re
r e a
h re hre hre Th h
time system T T T T

2. Kernel manages single-


threaded processes
User-space

Run time system


Kernel
space Kernel

Fig : A user-level threads package 14


Implementing Threads in the
Kernel
3
1.No Run time system 0
d
1
d
2 ad
d re ad
ea e a a
e Th r e
r r r h
Th Th Th T
2.Kernel has one table per process
3. All the blocking calls are implemented
as system call
User-
space

Kernel
space
Kernel

Fig : A Thread Package managed


15
by the Kernel
User-level, Kernel-level and
Combined

16
Scheduler Activations
• Scheduler Activations is an approach
to combine user-space and kernel
threads.

• Here kernel assigns virtual


processors and runtime system
allocates the threads to processes

• Increases efficiency by avoiding


17
transactions.
18

S-ar putea să vă placă și