Documente Academic
Documente Profesional
Documente Cultură
process.
Since threads have some of the properties of a process, it is
sometimes called as lightweight process
Thread comprises:
Thread ID
A program counter
A register set
A Stack
Process
Thread 2
Time
Thread 1
Preemptive multithreading
Allows the operating system to determine time for context switching.
Disadvantage Context switching happens in an inappropriate time
and may leaf to negative effects. This may be avoided by cooperative
multithreading.
Cooperative multithreading
Depends on the threads themselves to release control once they are at
a stopping point.
Disadvantage Problems may be created if any thread waits for a
resource availability
ICT Academy of Tamil Nadu
Heavyweight process
Information global to
all threads in a
process
Information local to
each thread
Address space
Other global process data
.
.
.
Registers
Registers
Registers
Stack
Stack
Stack
Mask
Mask
Mask
TSD
TSD
TSD
Threads
Data
Code
Open Files
Registers
Signal
Stack
Thread
Data
Code
Open Files
Signal
Registers
Registers
Registers
Stack
Stack
Stack
Thread
Thread
Thread
10
11
User
Level
Threads
Kernel
Level
Threads
12
13
CPU
14
15
Kernel Level
Threads
Threads
Process Scheduling
Thread Scheduling
CPU
16
User level threads are faster to create and Kernel level threads are slower to create and
manage.
manage.
Implemented by a thread library at the user Operating system support directly to Kernel
level.
threads.
User level thread can run on any operating Kernel level threads are specific to the operating
system.
system.
Support provided at the user level called user Support may be provided by Kernel is called
level thread.
Kernel level threads.
Multithread
application
cannot
advantage of multiprocessing.
take Kernel
routines
multithreaded.
themselves
can
be
17
Software Design
Performance
Cooperation
18
19
Create
Exit
Suspend
Resume
Sleep
Wake
20
Cancel
Join
21
22
One-to-One
Model
Many-toOne Model
Many-toMany
Model
23
User Thread
Kernel Thread
24
User Thread
Kernel Thread
25
User Thread
Kernel Thread
26
27
28
do
{
entry section
critical section
exit section
remainder section
} while(1);
29
Wait
Enter
Critical Section
Exit
Signal
30
31
32
For any two process, the data variables that are shared are:
int a;
boolean flag [2];
33
34
35
36
37
38
39
40
41
42
43
44
45
are
different
critical
sections
with
different
semaphores.
They acquire many resources simultaneously.
46
Binary
semaphores
Counting
semaphores
47
48
49
50
51
52
53
Full
Used when the counter for number of slots indicates full.
Empty
Used when the counter doe number of slots indicates empty.
Mutex
Producer and consumer will not access modifiable shared
section of the buffer.
54
55
No busy waits
56
57
do
{
.
.
//produces an item
.
.
wait (empty);
wait (mutex);
.
.
//Add an item
.
.
signal (mutex);
signal (full);
}while(1);
ICT Academy of Tamil Nadu
58
do
{
wait (full);
wait (mutex);
.
.
//Remove an item
.
.
signal (mutex);
signal (empty);
.
.
//consume the item
.
.
}while(1);
ICT Academy of Tamil Nadu
59
allowed to read it
Also if a reader is reading from the data area, no other reader
60
61
do
{
wait (w);
.
.
// writing is performed
.
.
signal (w) ;
}while(1);
An Initiative of Government of India,
Government of Tamil Nadu and CII
62
when i is modified
i is the count variable used to keep
track of the number of processes
do
{
wait (mutex);
i++;
if (i==1)
wait (w);
signal (mutex);
.
.
//reading is performed
.
.
wait (mutex);
i--;
if (i==0)
signal (w);
signal (mutex);
}while(1);
An Initiative of Government of India,
Government of Tamil Nadu and CII
63
64
65
66
67
68
Process
A
Process
B
Resource A
Resource B
69
Mutual exclusion
Only one process accessing one resource.
Resource holding
One process holding one resource which do not release that process and
waits for other process for accessing purpose.
No preemption
A resource that cannot be released from the process
Circular wait
All processes holds one or more resources that demands the next process in
the chain
70
71
72
73
74
Two processes can access the resource and the records in the
database is locked
3 different levels of locking :
Entire database is locked
A subsection of the database is locked
75
If the records are not locked then races condition takes place
P1 access R1 and locks it.
P2 access R2 and locks it.
P1 request R2 but R2 is locked by P2.
P2 request R1 but R1 is locked by P1.
76
77
78
79
80
81
82
Deadlock
Prevention
Deadlock
Avoidance
Deadlock
detection
Detect the
deadlock when it
occurs and recover
from it gracefully.
83
Mutual
Exclusion
Resource
Holding
No
Preemption
Circular
Wait
84
85
86
87
88
Safe State
All process gets all resources requested by them.
Process may be allocated in the following order
P1, P2, P3,,Pn
Unsafe State
When Pi enters to terminate state, then the next process namely Pi+1, in
its order, receives that resource
89
deadlock
unsafe
state
safe
state
90
The resources that is allocated will reverse the edge of the direction
The resources that is released by the process is returned to the edge
of the direction
No cycle of graph is created
ICT Academy of Tamil Nadu
91
92
93
94
95
Work and Finish are two vector with length m and n namely
Work=vector[1..m] and Finish=vector[1..n]
Work = Available
Finish [i] = False where i= 0, 1,,n-1
Find i where,
Finish[i]=False
Need[i]<=Work
Work = Work+Allocation
Finish[i]=True, then go to Step 1
96
If Request[i]<=Need[i], go to step 2
If Request[i]<=Available, go to step 3
Available= Available- Request[i]
Allocation[i]=Allocation[i]+Request[i]
Need[i]=Need[i]- Request[i]
97
98
99
P5
R1
R3
R4
P1
P2
P3
R2
P4
R5
100
P1
P2
P3
P4
101
102
Work and Finish are two vector with length m and n namely
Work=vector[1..m] and Finish=vector[1..n]
Work = Available
If Allocation 0, then Finish[i] = False else Finish[i] = True where i= 0,
1,,n-1
Find i where
Finish[i]=False
Request [i]<=Work
103
Work = Work+Allocation
Finish[i]=True, then go to Step 2
104
Process
termination
Resource
preemption
105
106
Starvation
The same process can always be selected as the victim by considering
the cost factor
107
108
context.
The thread states are ready, running, blocked, waiting, and
sleeping
The thread operations are create, exit, suspend, resume,
sleep, wake.
Multithreading refers to the ability of an operating system to
support multiple, concurrent paths of execution within a
single process.
ICT Academy of Tamil Nadu
109
110
111
112
113
114
115