Threads G.Anuradha (Reference : William Stallings)

40
Threads G.Anuradha (Reference : William Stallings)

Transcript of Threads G.Anuradha (Reference : William Stallings)

Page 1: Threads G.Anuradha (Reference : William Stallings)

Threads

G.Anuradha(Reference : William Stallings)

Page 2: 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)

Page 3: Threads G.Anuradha (Reference : William Stallings)

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

Page 4: Threads G.Anuradha (Reference : William Stallings)

Threads and ProcessesMS-DOS

UNIX

Java

Windows 2000Solaris Linux

Page 5: Threads G.Anuradha (Reference : William Stallings)

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

Page 6: Threads G.Anuradha (Reference : William Stallings)

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

Page 7: Threads G.Anuradha (Reference : William Stallings)

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.

Page 8: Threads G.Anuradha (Reference : William Stallings)

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

Page 9: Threads G.Anuradha (Reference : William Stallings)

Thread Functionality

• Thread States• Thread Synchronization• User level threads• Kernel level threads

Page 10: Threads G.Anuradha (Reference : William Stallings)

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

Page 11: Threads G.Anuradha (Reference : William Stallings)

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.

Page 12: Threads G.Anuradha (Reference : William Stallings)

Performance benefits of threads that do not block and entire process

Page 13: Threads G.Anuradha (Reference : William Stallings)

Multithreading example on a uniprocessor

Page 14: Threads G.Anuradha (Reference : William Stallings)

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

Page 15: Threads G.Anuradha (Reference : William Stallings)

Types of threads

• User-level threads• Kernel-level threads

Page 16: Threads G.Anuradha (Reference : William Stallings)

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

Page 17: Threads G.Anuradha (Reference : William Stallings)

Relationship between ULT states and Process states

Page 18: Threads G.Anuradha (Reference : William Stallings)

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

Page 19: Threads G.Anuradha (Reference : William Stallings)

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

Page 20: Threads G.Anuradha (Reference : William Stallings)

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.

Page 21: Threads G.Anuradha (Reference : William Stallings)

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.

Page 22: Threads G.Anuradha (Reference : William Stallings)

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

Page 23: Threads G.Anuradha (Reference : William Stallings)

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

Page 24: Threads G.Anuradha (Reference : William Stallings)

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

Page 25: Threads G.Anuradha (Reference : William Stallings)

Relationship Between Threads and Processes

Page 26: Threads G.Anuradha (Reference : William Stallings)

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

Page 27: Threads G.Anuradha (Reference : William Stallings)

One-To-Many Relationship

• Used in distributed operating systems. Eg Cloud operating systems

• Activity• CASE STUDIES ON trix • CASE STUDIES ON CLOUD OS

Page 28: Threads G.Anuradha (Reference : William Stallings)

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

Page 29: Threads G.Anuradha (Reference : William Stallings)

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

Page 30: Threads G.Anuradha (Reference : William Stallings)

UNIX process state transition Diagram

Page 31: Threads G.Anuradha (Reference : William Stallings)

Process states in Unix

Page 32: Threads G.Anuradha (Reference : William Stallings)

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

Page 33: Threads G.Anuradha (Reference : William Stallings)

Process DescriptionProcess Image

User –level context

Register Context System level context

Page 34: Threads G.Anuradha (Reference : William Stallings)

User-level context

Page 35: Threads G.Anuradha (Reference : William Stallings)

Register context

Page 36: Threads G.Anuradha (Reference : William Stallings)

System level context

Page 37: Threads G.Anuradha (Reference : William Stallings)

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

Page 38: Threads G.Anuradha (Reference : William Stallings)

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.

Page 39: Threads G.Anuradha (Reference : William Stallings)

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

Page 40: Threads G.Anuradha (Reference : William Stallings)

Linux process/Thread model