operating systems
DESCRIPTION
Operating Systems. Threads. Overview Multithreading Models Threading Issues. Overview. A thread is a basic unit of CPU utilization A traditional (or heavyweight) process has a single thread of control Single-threaded applications Simple implementations e.g. assignments - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/1.jpg)
Course: Operating SystemsInstructor: Umar Kalim
NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk
Operating Systems
![Page 2: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/2.jpg)
Threads• Overview• Multithreading Models• Threading Issues
![Page 3: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/3.jpg)
Overview• A thread is a basic unit of CPU utilization
• A traditional (or heavyweight) process has a single thread of control
• Single-threaded applications− Simple implementations e.g. assignments
• Multi-threaded applications− Web browser− Web server
![Page 4: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/4.jpg)
Single and Multithreaded Processes
ThreadID
Program counter
Registers
Stack
![Page 5: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/5.jpg)
Relationship between threads and processes• The operating system creates a process for the purpose of
running a program. − Every process has at least one thread.
• On some operating systems, a process can have more than one thread.
− Some programs like word processors are designed to have only one instance of themselves running at the same time.
• Sometimes, such programs just open up more windows to accommodate multiple simultaneous use.
• After all, you can go back and forth between five documents, but you can only edit one of them at a given instance.
− Command line interpreters and multiple users ~ processes
• Access rights• Protection of other users from failures
− Graphical User Interface ~ threads• Multiple aspects at one instance, user input and painting
![Page 6: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/6.jpg)
Processes and Threads• The concept of a process and thread are interrelated by a
sense of ownership and of containment• Process
− A process is the "heaviest" unit of kernel scheduling− Processes own resources allocated by the operating
system• Resources include memory,• file handles,• sockets, • device handles, and user interfaces.
− Processes do not share address spaces or file resources except through explicit methods
− Processes are typically pre-emptively multitasked. • However, Windows 3.1 and older versions of Mac OS
used co-operative or non-preemptive multitasking.
![Page 7: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/7.jpg)
Processes and Threads• Thread
− A thread is the "lightest" unit of kernel scheduling.
− At least one thread exists within each process. − If multiple threads can exist within a process,
then they share the same memory and file resources.
− Threads are pre-emptively multitasked if the operating system's process scheduler is pre-emptive.
− Threads do not own resources except for a stack and a copy of the registers including the program counter.
![Page 8: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/8.jpg)
Benefits• Responsiveness
− blocking and non-blocking requests• Resource Sharing
− same address space• Economy
− Allocating resources to processes as compared to sharing resources via threads
• Utilization of MP Architectures− Multi-threaded applications can utilize
multiprocessor platforms by enhancing concurrency
![Page 9: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/9.jpg)
Multi-threading and reality• In an ideal world, five processors would do five
times the work of one processor
• But we live in a world of contention for shared resources, of disk and memory bottlenecks, single-threaded applications, multithreaded applications that require synchronous processing, and poorly coordinated processors
![Page 10: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/10.jpg)
Course: Operating SystemsInstructor: Umar Kalim
NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk
Types of threads
Kernel threadsUser level threads
![Page 11: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/11.jpg)
Kernel threads• A kernel thread is a kernel entity, like processes and
interrupt handlers• It is the entity handled by the system scheduler. • Kernel threads consist of a set of registers, a stack, and a
few corresponding kernel data structures. − The user structure contains process-related information − The uthread structure contains thread-related
information. • Unlike processes, all threads within a process share the
same address space. • Similar to processes, when a kernel thread makes a blocking
call, only that thread blocks. • All modern machines support kernel threads, most often via
the POSIX threads interface ``pthreads''. − Some dedicated parallel machines support kernel threads
poorly or not at all. For example, the Blue Gene/L microkernel does not support pthreads.
![Page 12: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/12.jpg)
Kernel threads• The advantage of kernel threads over processes
is faster creation and context switching compared with processes.
• Parallel programming• Kernel threads cannot be accessed from the
user mode environment, except through the threads library.
![Page 13: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/13.jpg)
Kernel Threads• Supported by the Kernel
• Examples− Windows XP/2000− Solaris− Linux− Tru64 UNIX− Mac OS X
![Page 14: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/14.jpg)
User level threads• Like a kernel thread, a user-level thread includes
a set of registers and a stack, and shares the entire address space with the other threads in the enclosing process
• Unlike a kernel thread, however, a user-level thread is handled entirely in user code
• OS is unaware of a user-level thread's existence• The primary advantages of user-level threads are
efficiency and flexibility − Because the operating system is not involved, user-
level threads can be made to use very little memory− User-level threads are also more flexible because
the thread scheduler is in user code, • for example, the application's priority structure can
be directly used by the thread scheduler
![Page 15: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/15.jpg)
User level threads• The primary disadvantage of user-level threads
compared to kernel threads is the lack of operating system support.− For example, when a user-level thread makes a
blocking call, the kernel does not start running another user-level thread. Instead, the kernel suspends the entire calling kernel thread or process, even though another user-level thread might be ready to run.
![Page 16: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/16.jpg)
User Threads• User threads are mapped to kernel threads by
the threads library, in an implementation dependent manner. − The threads library uses a proprietary interface to
handle kernel threads.
• Three primary thread libraries:− POSIX Pthreads− Win32 threads− Java threads
![Page 17: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/17.jpg)
Course: Operating SystemsInstructor: Umar Kalim
NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk
Questions?
![Page 18: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/18.jpg)
Multithreading Models• Many-to-One
• One-to-One
• Many-to-Many
![Page 19: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/19.jpg)
Many-to-One• Many user-level
threads mapped to single kernel thread
• Entire process will block if a thread makes a blocking call
• Examples:− Solaris Green
Threads− GNU Portable
Threads
![Page 20: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/20.jpg)
One-to-One• Each user-level thread maps to kernel thread• Examples
− Windows NT/XP/2000− Linux− Solaris 9 and later
![Page 21: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/21.jpg)
Many-to-Many Model• Allows many user level threads to be mapped to
many kernel threads• Allows the operating system to create a
sufficient number of kernel threads• Solaris prior to version 9• Windows NT/2000 with the ThreadFiber
package
![Page 22: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/22.jpg)
Many-to-Many Model
![Page 23: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/23.jpg)
Two-level Model• Similar to M:M, except that it allows a user
thread to be bound to kernel thread• Examples
− IRIX− HP-UX− Tru64 UNIX− Solaris 8 and earlier
![Page 24: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/24.jpg)
Two-level Model
![Page 25: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/25.jpg)
Threading Issues• Semantics of fork() and exec() system calls• Thread cancellation• Signal handling• Thread pools• Thread specific data• Scheduler activations
![Page 26: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/26.jpg)
Semantics of fork() and exec()• Does fork() duplicate only the calling thread or
all threads?
![Page 27: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/27.jpg)
Thread Cancellation• Terminating a thread before it has finished
− E.g. Multiple threads searching in a DB, if one thread finds the result, cancel the others
• Two general approaches:− Asynchronous cancellation terminates the
target thread immediately• Cancellation during data update ~ consistency
issues− Deferred cancellation allows the target thread
to periodically check if it should be cancelled• Cancellation points
![Page 28: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/28.jpg)
Signal Handling• Signals are used in UNIX systems to notify a process that a
particular event has occurred− Synchronous ~ same process, divide by zero− Asynchronous ~ external event, terminate process
• A signal handler is used to process signals1. Signal is generated by particular event2. Signal is delivered to a process3. Signal is handled
• Handlers− Default− User defined
• Options− Deliver the signal to the thread to which the signal
applies− Deliver the signal to every thread in the process− Deliver the signal to certain threads in the process− Assign a specific thread to receive all signals for the
process
![Page 29: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/29.jpg)
Thread Pools• Create a number of threads in a pool where they
await work− Web server
• Advantages:− Usually slightly faster to service a request with an
existing thread than create a new thread− Allows the number of threads in the application(s)
to be bound to the size of the pool
![Page 30: Operating Systems](https://reader035.vdocuments.us/reader035/viewer/2022062409/5681470b550346895db442ea/html5/thumbnails/30.jpg)
Course: Operating SystemsInstructor: Umar Kalim
NUST Institute of Information Technology, Pakistanhttp://www.niit.edu.pk
Questions?
•Recommended Reading:− Book ~ 143 - 146− OSRC
• http://www.nondot.org/sabre/os/articles− Reading list @
http://www.niit.edu.pk/~umarkalim/courses/fall2006/os.html