OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
Lecture 13 threads presentation 16 slides
1. CSE 211 Introduction to
Operating Systems
Dr. Anwar Majid Mirza
mirza@giki.edu.pk
Lecture No. 13
February 05, 2002
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology
Topi-23460, District Swabi, N.W.F.P., Pakistan
2. Thread Functionality: States and
Synchronization
Like processes, threads have execution states and
may synchronize with one another.
Thread States
The key states for a thread are Running, Ready
and Blocked.
Generally, it does not make sense to associate
suspend states with threads, because such states
are process-level concepts.
In particular, if a process is swapped out, all of its
threads are necessarily swapped out because they
all share the address space of the process.
3. Thread States
There are four basic thread operations associated
with a change in thread state:
Spawn: Typically, when a new process is spawned,
a thread for that process is also spawned.
Subsequently, a thread within a process may
spawn another thread within the same process.
Block: When a thread needs to wait for an event, it
will block. The processor may now turn to the
execution of another ready thread.
Unblock: When the event for which a thread is
blocked occurs, the thread is moved to the ready
state.
Finish: When a thread completes, its register
context and stacks are de-allocated.
4. Benefits of Threads
Consider a program that does two remote
procedure calls (RPCs) to two different hosts, to
obtain a combined result.
In a single threaded program, the results are
obtained in sequence, so that the program has to
wait for a response from each server in turn.
Rewriting the program to use a separate thread for
each RPC results in a substantial speedup.
Note that if this program operates on a uni-
processor, the requests must be generated serially
and the results processed serially; however, the
program waits concurrently for the two replies.
This is shown on next slide.
5. Benefits of threads - Speed
Process 1
RPC
Request
RPC
Request
Server Server
Thread A
Process 1
RPC
Request
Server
Thread B
Process 1
RPC
Request
Server
Running
Blocked
6. Benefits of Threads
– Multi threads on a uni-processor
On a uni-processor, multiprogramming
enables the interleaving of multiple threads
within multiple processes.
For example, in the figure on next slide, three
threads in two processes are interleaved on
the processor.
Execution passes from one thread to another
either when the currently running thread is
blocked or its time slice is exhausted.
8. Thread Synchronization
All of the threads of a process share the same
address space and other resources, such as
open files.
Any alteration of a resource by one thread affects
the environment of the other threads in the same
process.
It is therefore, necessary to synchronize the
activities of various threads so that they do not
interfere with each other or corrupt data
structures.
The issues raised and the techniques used in the
synchronization of threads are, in general, the
same as for the synchronization of processes.
10. User-Level Threads (ULTs)
All thread management is done by the
application (using the threads library)
Kernel not aware of the existence of
threads
By default, an application starts as 1
thread
11. ULTs – Advantages and
disadvantages
Advantages:
– Thread switching does not require kernel
mode privileges; faster
– Scheduling is application specific; flexible
– ULTs can run on any O.S.
Disadvantages:
– System calls done by one thread cause the
whole process to be blocked
– Cannot take advantage of multi-processors.
Kernel assigns each process to one
processor
12. Kernel-Level Threads (KLTs)
Windows NT and OS/2 are examples of
this approach
The O.S. provides an application
programming interface (API) to the Kernel
thread facility
Kernel maintains context information for
the process and the threads
Switching between threads requires the
kernel
13. KLTs – Advantages and
disadvantages
Advantages:
– Threads do not block each others. Kernel
schedules threads independently
– Kernel can schedule different threads on
different processors
– Kernel routines can be multi-threaded
Disadvantage:
– Transfer of control within one process
requires a kernel mode switch
14. Combined Approach for
Threads
Example is Solaris
Thread creation done in the user space
Bulk of scheduling and synchronization of
threads done in the user space
The multiple ULTs from a single application are
mapped onto some (smaller or equal) number of
KLTs.
The programmer may adjust the number of KLTs
for a particular application and machine to
achieve the best overall performance.
16. Announcement
OS Mid-Term Test 1
Date: February 11 (Monday), 2002
Time: 7:45 am (sharp)
Duration: One Hour
Venue: Examination Hall (FES)
Course: All the lectures taken so far,
(including this week) + All the assignments
and Quizzes taken so far.
Type: The test will be of Multiple Choice
Questionnaire (MCQ) type.