Threads1 - anuradhasrinivas

Download Report

Transcript Threads1 - anuradhasrinivas

Threads
G.Anuradha
(Reference : William Stallings)
Characteristics of a Process
• Resource Ownership: Process image is in a
virtual address space and time to time it has
been allocated resources . OS provides
protection to the process (Process or Task)
• Scheduling/execution: Process has an
execution state and priority and OS does the
scheduling and execution (Thread or Light
Weight Process)
Multithreading
• Ability of and OS to support multiple threads
of execution within a single process
• Single thread of execution per process is called
as single-threaded approach
• Process in a multithreaded environment is the
unit of resource allocation and protection
– Virtual address space that holds the process
image
– Protected access to processor(s),resources
Threads and Processes
MS-DOS
UNIX
Java
Windows
2000
Solaris
Linux
Thread
• Within a process there may be thread(s)
– Has an execution state
– Saved thread context when not running
– An execution stack
– Some per-thread static storage for local variables
– Access to memory and resources of its process
shared with all other threads in that process
Single threaded and multithreaded
process models
Share the
state and
resources
of the
process
and
reside in
the same
address
space and
access the
same data
Benefits of threads
1. Takes Less time to create a thread than a
process
2. Takes less time to terminate a thread than a
process
3. Takes less time to switch between threads
within the same process
4. Enhance efficiency in communication
between different executing programs.
Examples of users of threads in single
–user multiprocessing system
•
•
•
•
Foreground and background work
Asynchronous processing
Speed execution
Modular program structure
Note:- 1. Suspension of a process suspends all the threads in that
process as well
2. Termination of a process terminates all the threads in that process
as well
Thread Functionality
•
•
•
•
Thread States
Thread Synchronization
User level threads
Kernel level threads
Thread states
•
•
•
•
Running
Ready
Blocked
Note:- There is no suspended state becos its
only in the process level. If a process is
suspended all threads associated with it are
also suspended
Operations associated with threads
• Associated with change in thread state we have
four basic thread operations
• Spawn: When a new process is spawned a thread for
that process is also spawned.
• Block: When a thread need to wait for an event it
will block. The processor may now turn to the
execution of another ready thread.
• Unblock: After the event for a blocked thread occurs
its moved into the ready queue
• Finish: After a thread completes, its register context
and stacks are deallocated.
Performance benefits of threads that
do not block and entire process
Multithreading example on a
uniprocessor
Thread Synchronization
• All threads of a process share the same
address space and other resources of a
process
• Any alternation in the resources by one thread
affects the environment of other threads in
the same process
• Activities of various threads need to be
properly synchronised
Types of threads
• User-level threads
• Kernel-level threads
User-level threads
• Application does the work of thread
management
• Thread libraries are used for this
purpose
• Kernel is unaware of the thread
creations and schedules the
process as a unit assigning a single
execution state
• Thread library
• Data structure for the new
thread
• Passes control to one of the
threads within this process
that is in the ready state
Relationship between ULT states and
Process states
Relationship between ULT states and
Process states
In figures b and c kernel switches control back to process B so execution
resumes in thread 2
Advantages of ULT
1. Thread switching requires only user mode
privileges. So its saves the overhead of two
mode switches
2. Scheduling is application specific without
disturbing the OS scheduler
3. ULTs run on any operating system. Thread
library utilities are shared by all applications
Disadvantage of ULT
• Many system calls are blocking system calls.
This not only blocks that thread but all threads
in a process
• Multithreaded application cannot take
advantage of multiprocessing. One single
thread within a process can execute at a time.
How to overcome the disadvantages?
• Write an application as multiple processes
rather than multiple threads. This eliminates
the advantage of threads. Overhead increases
• Jacketing:- convert a blocking system call into
nonblocking system call. In this case instead of calling
a I/O routine a I/o jacket routine is called which
checks whether the I/O is buzy or not. If I/O is buzy it
enters the Ready state and passes control to another
thread.
Kernel level threads
• No thread management
code in the application
area
• Just an API
• All threads are
supported by a single
process.
• Scheduling by kernel is
done by thread basis
Advantages and disadvantages of KLT
• Advantages
– Kernel can simultaneously schedule multiple
threads from the same process on multiple
processors
– If one thread is blocked the kernel can schedule
another thread
• Disadvantage
– Transfer of control is mode switch
Combined Approaches
• Thread creation, scheduling,
synchronization of threads
is done in the user space.
• Multiple ULTs can be
mapped onto some number
of KLTs
• Multiple threads within the
same application can run in
parallel on multiple
processors
• Blocking system call need
not block the entire process
Relationship Between Threads and
Processes
TRIX
• Follows a many-to-many relationship between
threads and processes
• There is a concept of domain and thread
– Domain is a static entity consisting of an address
space and ports through which messages may be send
and received
– Thread is a single execution path with an execution
stack, processor state and scheduling information
• Multiple threads may execute in a single domain
or a single user activity can be performed in
multiple domains as well
One-To-Many Relationship
• Used in distributed operating systems. Eg
Cloud operating systems
• Activity
• CASE STUDIES ON trix
• CASE STUDIES ON CLOUD OS
UNIX SVR4 Process Management
• In UNIX most of the OS
executes within the
environment of a user
process.
• So it has user and
kernel mode of
execution
Categories of process in unix
• System processes
– Run in kernel mode
– Executes OS code to
perform administrative
and housekeeping
function
• User Processes
– Run in user mode
– Executes user programs
and utilities in kernel
mode to execute
instructions which
belong to the kernel
– Enters the kernel mode
by issuing a system call
when an exception is
generated or when an
interrupt occurs
UNIX process state transition Diagram
Process states in Unix
Processes which are unique in UNIX
• Process 0 is a special process that is created
when the system boots. (Swapper process)
• Process 0 spawns process 1 (init process)
• All processes in UNIX has process 1 as its
ancestor
Process Description
Process Image
User –level
context
Register Context
System level
context
User-level context
Register context
System level context
Process Control
• When a parent process is forked(kernel mode)
– A slot is allocated in the process table for the new
process
– An unique process ID is assigned to child process
– Copy of process image of the parent is made
– Increments counters for any files owned by the
parent
– Assigns the child process to a ready to run state
– Returns the ID number of the child to the parent
process and 0 to child process
Dispatcher routine
• Stay in parent process. Control returns to user
mode at the point of fork call of parent
• Transfer control to child process
• Transfer control to another process. Both
parent and child are left in Ready to Run state.
Linux process and thread mgt
• Task- task struct
• Task-struct
–
–
–
–
–
–
–
–
–
State:Execution state of a process
Scheduling information
Identifiers- process has UID and group has GID
Interprocess communicaiton
Links
Times and timers
File system
Virtual memory
Processor-specific context
Linux process/Thread model