threads
DESCRIPTION
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) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/1.jpg)
Threads
G.Anuradha(Reference : William Stallings)
![Page 2: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/4.jpg)
Threads and ProcessesMS-DOS
UNIX
Java
Windows 2000Solaris Linux
![Page 5: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/9.jpg)
Thread Functionality
• Thread States• Thread Synchronization• User level threads• Kernel level threads
![Page 10: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/12.jpg)
Performance benefits of threads that do not block and entire process
![Page 13: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/13.jpg)
Multithreading example on a uniprocessor
![Page 14: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/15.jpg)
Types of threads
• User-level threads• Kernel-level threads
![Page 16: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/17.jpg)
Relationship between ULT states and Process states
![Page 18: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/25.jpg)
Relationship Between Threads and Processes
![Page 26: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/30.jpg)
UNIX process state transition Diagram
![Page 31: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/31.jpg)
Process states in Unix
![Page 32: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/33.jpg)
Process DescriptionProcess Image
User –level context
Register Context System level context
![Page 34: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/34.jpg)
User-level context
![Page 35: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/35.jpg)
Register context
![Page 36: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/36.jpg)
System level context
![Page 37: Threads](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070413/56814c74550346895db995e9/html5/thumbnails/40.jpg)
Linux process/Thread model