0% found this document useful (0 votes)
36 views19 pages

OS - Chapter # 7

Uploaded by

SAMEER AHMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views19 pages

OS - Chapter # 7

Uploaded by

SAMEER AHMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

THREADING & MULTITASKING

CHAPTER # 7 Operating Systems


Chapter 7 - Outline
S H E H E R YAR MALI K

 Multitasking
 Threads
 Types of threads
 Kernel threads
 User level threads
 Multithreading model
 One to One Model
 Many to One Model
 Many to Many Model
 Solaris 2 threads
 Windows 2000 threads
 Linux threads
 Java threads

Chapter # 7 Operating Systems 2


Multitasking
S H E H E R YAR MALI K

 A utility reads records from a tape with a blocking factor of 5


 Process them and writes them into a disk one by one
Begin
OSLecture;
while Not EOF do
Read a record;
Process record;
Write a record;
End while
End
 Assume, round robin scheduling with time slice of 25ms
 Process will be blocked immediately due to read system call
 Utilizes a small fraction of time slice
 When record is read, the process become ready and then it is
dispatched
 Block again due to write system call and so on

Chapter # 7 Operating Systems 3


Multitasking
S H E H E R YAR MALI K

 An improvement to above is to define two tasks within same process


Begin
OSLecture;
while not EOF do
Task-0-Begin
Read a record;
Process a record;
Task-0-End
Task-1-Begin
Write a record;
Task-1-End
End while
End
 Advantage
 Subtask can run concurrently within the same process
 The complier recognizes as different tasks and maintain their different identities

Chapter # 7 Operating Systems 4


Multitasking
S H E H E R YAR MALI K

 When process starts, the OS creates process control block (PCB)


 Now, in addition to PCB, it also creates task control block (TCB) for each task
within a process
 A TCB needs to have save area for each task
 A task also could have priorities and states
 If process time slice is not over but the current task is either over or blocked
 The OS chooses the next highest priority ready task within same process and
schedules it
 If no task is ready in the same process the process will be blocked and OS will
schedules a task from some other process
 This reduces the overheads of context switching at process level
 The context switching at task level is far less time consuming e.g.
 Also it shares the same address space and resources of process
 Some OS called it multitasking and some call it multithreading
 OS/2 use the concept name as multithreading

Chapter # 7 Operating Systems 5


Threads
S H E H E R YAR MALI K

 A thread is a basic unit of CPU utilization


 It is also called a light weight process (LWP)
 A thread is subdivision of work within a process
 Threads are like little mini processes
 It may consist of;
 Program counter
 Register set
 Stack space
 A threads shares with its peer threads its
 Code section
 Data section
 Operating system resources

Chapter # 7 Operating Systems 6


Threads
S H E H E R YAR MALI K

 Thread idea is used for higher throughput and better


performance
 Threads are cheaper in terms of system overheads
 Avoid context switching of processes if thread switch is used
 Each thread run sequentially
 Thread can create child threads
 If thread is blocked, another thread within same process can
run
 A traditional or heavyweight process is equal to a task with
one thread
 Threads provide a mechanism to achieve parallelism
 Threads are very important in the design of distributed and
client server systems

Chapter # 7 Operating Systems 7


Threads
S H E H E R YAR MALI K

 In some systems, threads are supported by the operating


system
 Where the OS is not multithreaded, threads cannot be
managed with application system code
 Some database management systems provide their own
thread management
 Some languages, like Java, provide direct support for writing
the thread based programs
 In the absence of language support, threads can still be
utilized by use of library subroutines

Chapter # 7 Operating Systems 8


Benefits of Multithreading
S H E H E R YAR MALI K

 Responsiveness
 It allows to run a program even if some part of it is in block state
 Resource sharing
 Different threads of a program behaving like multiple processes
are sharing same address space
 Economy
 Overheads of context switching of TCB are much lesser than of
PCB
 Scalability
 Utilization of multiprocessor architecture
 Threads are very useful in multiprocessor architecture
 Due to threading a single program can run on different
processors at same time

Chapter # 7 Operating Systems 9


Single and Multithreaded Processes
S H E H E R YAR MALI K

Chapter # 7 Operating Systems 10


Types of Threads
S H E H E R YAR MALI K

 There are two types of threads i.e. kernel threads and user
level threads
 Kernel threads
 Theses are supported directly by operating system
 The kernel perform thread creation, scheduling and management
 Example kernel threads supporting OS are;
 Windows 95/98/2000/XP
 Solaris, Tru64, Unix
 Linux, Mach and OS/2

Chapter # 7 Operating Systems 11


Types of Threads
S H E H E R YAR MALI K

 User threads
 These are supported above the kernel, via a set of library calls at
the user level
 Library provides support for thread creation, scheduling and
management with no support from kernel
 Example user-threads
 POSIX P thread
 Mach C thread
 Solaris UI thread
 Hybrid approach implements both user-level and kernel
supported threads

Chapter # 7 Operating Systems 12


Multithreading Models
S H E H E R YAR MALI K

 Many to One Model


 Many user-level threads
mapped to single kernel thread
 One thread blocking causes all
to block
 Used on systems that do not
support kernel threads
 Multiple threads may not run in
parallel on multicore system
because only one may be in
kernel at a time
 Few systems currently use this
model
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Chapter # 7 Operating Systems 13


Multithreading Models
S H E H E R YAR MALI K

 One to One Model


 Each user-level thread maps to kernel thread
 Creating a user-level thread creates a kernel thread
 Number of threads per process sometimes restricted due to overhead
 Examples
 Windows
 Linux
 Solaris 9 and later

Chapter # 7 Operating Systems 14


Multithreading Models
S H E H E R YAR MALI K

 Many to Many Model


 Allow many user level threads to
map to many kernel threads
 Allow the operating system to
create a sufficient number of
kernel threads
 Example OS;
 Windows NT/2000 with thread
fiber package
 Windows XP, 7
 Solaris prior to version 9

Chapter # 7 Operating Systems 15


Solaris 2 Threads
S H E H E R YAR MALI K

 Solaris 2 is based on Unix with support for threads at the kernel and user
levels
 It introduce an intermediate level between kernel and user level threads
which is called LWP(light weight process)
 Resource needs of threads;
 Kernel thread
 Small data structure and stack
 Thread switching does not require changing memory access information
 It is relatively fast
 LWP
 PCB with register data
 Accounting and memory information
 Switching between LWPs is relatively slow
 User level thread
 Only need stack and program counter
 No kernel involvement means fast switching
 Kernel only sees the LWPs that support user level threads

Chapter # 7 Operating Systems 16


Windows 2000 Threads
S H E H E R YAR MALI K

 Implements one-to-one mapping


 Each thread contains
 Thread ID
 Register set
 Separate user and kernel stack
 Private data storage area

Chapter # 7 Operating Systems 17


Linux Threads
S H E H E R YAR MALI K

 Linux refers threads as tasks


 Thread creation is done through clone() system call
 Clone() allows a child task to share address space of
the parent task

Chapter # 7 Operating Systems 18


Java Threads (5c,5d,5a,5b)
S H E H E R YAR MALI K

 Java threads may be created by


 Extending thread class
 Implementing the runnable interface
 Java threads are managed by the JVM

Chapter # 7 Operating Systems 19

You might also like