UNIT 1part B Threads THREADS A thread is
UNIT 1_part B Threads
THREADS • A thread is basic unit of CPU utilization. • Thread is flow in execution. . • A thread is contained inside a process and different threads in the same process share some resources (most commonly memory), while different processes do not.
THREAD CONTROL BLOCK • basic unit of CPU utilization; • it comprises a thread ID, a program counter, a register set, and a stack.
What is thread • A thread is a flow of execution through the process code, with its own program counter, system registers and stack. • A thread is also called a light weight process. Threads provide a way to improve application performance through parallelism. • Each thread belongs to exactly one process and no thread can exist outside a process. • Each thread represents a separate flow of control. • Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors
Difference between process &thread Process Thread • Process is heavy weight or resource intensive. • Process switching needs interaction with OS. • In multiple process environment each process executes the same code but has its own memory & file resources. • If one process is blocked then no other process can execute until the first process is unblocked. • Multiple process without using threads use more resources. • In multiple processes each process operates independently of other. • Thread is light weight taking lesser resources than a process. • Thread switching does not interact with OS • All threads can share same set of open files, child processes • While one thread is blocked and waiting , second thread in the same task can run. • Multiple threads uses fewer resources. • One thread can read , write or change another thread’s data.
SINGLE AND MULTI THREAD
• Single Thread – Has single thread of control – It allows the process to perform only 1 task at a time. • Multi thread – Has many threads – Simultaneous execution of different task
BENEFITS • Responsiveness – Allows a program continue running if part of it is blocked or its is performing a lengthy operation • Resource Sharing – Threads share the memory and the resources of the parent process • Economy – In Solaris, creating a process is 30 times slower than creating a thread, context switching is 5 times slower. • Scalability – Multithreading on a multi-CPU machine increase parallelism
User level thread & kernel level thread
User Threads • Thread management done by user-level threads library – Thread creation, scheduling, are done in user level • Fast to create and manage • Drawback: – If kernel is single thread, then user level thread performing a blocking system call will cause entire process to block
KERNEL THREAD • Supported by OS – Thread creation, scheduling, are done in user level by kernel • Thread management is performed by os, thus kernel thread are slow. • If thread perform blocking system call, kernel can schedule another thread in application for execution
Multithreading Models • Many-to-One • One-to-One • Many-to-Many
MANY-TO-ONE • Many user-level threads mapped to single kernel thread. • Examples: – Solaris Green Threads – GNU Portable Threads
ONE-TO-ONE MODEL • • Each user-level thread maps to kernel thread Allow anther thread to run if block Run parallel Drawback: along with user thread , kernel thread should be created. Examples – Windows NT/XP/2000 – Linux
MANY-TO-MANY • Allows many user level threads to be mapped to many kernel threads • Allows the operating system to create a sufficient number of kernel threads • Examples – IRIX – HP-UX – Tru 64 UNIX – Solaris 8 and earlier
MULTICORE PROGRAMMING • Multicore systems putting pressure on programmers, challenges include – Dividing activities • Find area that can be divided into separate, concurrent tasks – Balance • Ensure concurrent tasks perform equal work of equal value – Data splitting • Data accessed must be divided to run on speparate cores. – Data dependency • One task depends on data from another, ensure synchrornization – Testing and debugging • Test many execution path
Concurrent Execution on a Single-core System Parallel Execution on a Multicore System
Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing – Library entirely in user space – Kernel-level library supported by the OS
3 main thread libraries • 1) POXIS pthread: is provided either user-level thread and kernel – level thread. • 2) Windows: the window thread library is a kernel – level library available on windows system. • 3) Java: Java thread API allows thread to be created and managed directly in java programming.
Pthreads • May be provided either as user-level or kernel-level • A POSIX standard (IEEE 1003. 1 c) API for thread creation and synchronization • API specifies behavior of the thread library, implementation is up to development of the library • Common in UNIX operating systems (Solaris, Linux, Mac OS X)
THREAD POOL • General idea behind a thread pool is to create a number of threads at process startup & place them into pool, where they sit and wait for work. • When a server receives a request , it awakens a thread from this pool , if one is available – and passes the request for service. • If pool contains no thread then server waits until one becomes free.
Benefits of pools • Servicing a request with an existing thread is faster than waiting to create a thread. • A thread pool limits the number of thread that exists at one point. • Separating the task to be performed from the mechanics of creating the task allows us to use different strategies for running the task.
THREAD POOLS • Limits the number of threads that exist at anyone point. This is particularly important on systems that cannot support a large number of concurrent threads. • Example: • DWORD WINAPI Pool. Function (AVOID Param) { • /** • * this function runs as a separate thread. • **/
THREAD POOLS • A pointer to pool function() is passed to one of the function in the thread pool API, and thread pool executes this function. • One such member in thread pool API is Queueuserworkitem().
THREAD POOLS • Queue. User. Work. Item ( ) function, which is passed three parameters: 1. LPTHREAD_START_ROUTINE Function – a pointer to the function that is to run as a separate thread. 2. PVOID Param – the parameters passed to Function. 3. ULONG Flags – flags indicating how the thread pool is to create and manage execution of the thread.
Threading Issues • Semantics of fork() and exec() system calls. • Signal handling • Thread Cancellation • Scheduler local storage • Scheduler activations
Threading Issues-Semantics of fork() and exec() • Does fork() duplicate only the calling thread or all threads? • One that duplication all threads – the child thread does not call exec() after forking • Only the thread that invoked the fork() system call is duplicated – exec() is called immediately after forking
Threading Issues-Signal Handling • Signals are used in UNIX systems to notify a process that a particular event has occurred • A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled
Synchronous signal & asynchronous signal • All signals, whether synchronous or asynchronous , follow the same pattern: 1. A signal is generated by the occurrence of a particular event. 1. A generated signal is delivered to a process. 2. One delivered, the signal must be handled.
• Signal Asynchronously – when a signal is generated by an event external to a running process. • Example: • include terminating process with specific • Keystrokes ( such as <control> <C> ) and have a timer expire. • Every signal may be handled by one of two possible handlers: • 1. A default signal handler • 2. A user-defined signal handler
• Default Signal Handler – run by the kernel • when handling that signal. • User-defined signal handler – that is called to handle the signal. • Asynchronous procedure calls (APCs) – the Windows can be emulated using this.
• Methods for delivering signals in multiple threading. • 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
Threading Issues-Thread Cancellation • Terminating a thread before it has finished • Two general approaches: – Asynchronous cancellation terminates the target thread immediately – it is troublesome if a thread to be canceled is in the middle of updating shared data – Deferred cancellation allows the target thread to periodically check if it should be cancelled – allow threads to be canceled safely
Scheduler Activations • Both M: M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application • Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library • This communication allows an application to maintain the correct number kernel threads
Scheduler Activations • upcall handler – upcalls are handled by the thread libarary and it must run on a virtual processor.
Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • clone() allows a child task to share the address space of the parent task (process)
Linux Threads
Linux Tasks A process, or task, in Linux is represented by a task_struct data structure This structure contains information in a number of categories
Linux Process/Thread Model Figure 4. 16 Linux Process/Thread Model
Linux Threads A new process is created by copying the attributes of the current process Linux does not recognize a distinction between threads and processes User-level threads are mapped into kernel-level processes The clone() call creates separate stack spaces for each process The new process can be cloned so that it shares resources
Linux Clone () Flags
- Slides: 41