4 threads
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

4 threads

on

  • 2,122 views

 

Statistics

Views

Total Views
2,122
Views on SlideShare
2,122
Embed Views
0

Actions

Likes
0
Downloads
67
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

4 threads Presentation Transcript

  • 1. Threads• Overview• Multithreading Models• Thread Libraries• Threading Issues
  • 2. 1. Overview• A thread is a basic unit of CPU utilization, It includes a thread ID, a program counter, a register set, and a stack• It shares code section, data section, and other OS resources like open files and signals with other threads belonging to the same process• A traditional (or heavyweight) process has a single thread of control• Difference between traditional single-threaded & multithreaded process 2 Loganathan R, CSE, HKBKCE
  • 3. 1. Overview Contd…1.1 Motivation• A single application may be required to perform several similar tasks • Example : A web server may have several of clients concurrently accessing it • Tradition Solution : The server run as a single process that accepts requests and when it receives a request, creates a separate process to service that request • Process creation is time consuming and resource intensive• The server create a separate thread to listen for client requests, when a request made, it create another thread to service the request • RPC servers are multithreaded a server receives a message, it services the message using a separate thread, which allows the server to service several concurrent requests. • Example :Javas RMI systems• OS kernels are now multithreaded, several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling • Example : Linux uses a kernel thread for managing free memory 3 Loganathan R, CSE, HKBKCE
  • 4. 1. Overview Contd…1.2 Benefits• Responsiveness – Multithreading allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. – For example a multithreaded web browser could allow user interaction in one thread while an image was being loaded in another thread• Resource Sharing – By default, threads share the memory and the resources of the process to which they belong• Economy – Allocating memory and resources for process creation is costly, more economical to create and context-switch threads• Utilization of Multiprocessor Architectures – Multithreading on a multi-CPU machine increases concurrency, threads may be running in parallel on different processors, Loganathan R, CSE, HKBKCE 4
  • 5. 2. Multithreading Models• Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads• User Threads - Thread management done by user-level threads without kernel support. User thread libraries: POSIX Pthreads , Win32 threads, Java threads• Kernel Threads - Supported and managed directly by the OS. Examples : Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X• The relationship between user threads and kernel threads are established in 3 ways2.1 Many-to-One Model• Many user-level threads mapped to single kernel thread• Thread management is done by the thread library in user space, so it is efficient• Disadvantages – The entire process will block if a thread makes a blocking system call – Multiple threads are unable to run in parallel on multiprocessors since only one thread can access the kernel at a time• Examples – Solaris Green Threads – GNU Portable Threads Loganathan R, CSE, HKBKCE 5
  • 6. 2. Multithreading Models Contd…2.2 One -to-One Model• Each user-level thread maps to kernel thread• It provides more concurrency i.e. allows another thread to run when a thread makes a blocking system call• Allows multiple threads to run in parallel on multiprocessors• Disadvantages • Creating a user thread requires creating the corresponding kernel thread• Examples : Windows NT/XP/2000, Linux Many-to-One Model One -to-One Model Loganathan R, CSE, HKBKCE 6
  • 7. 2. Multithreading Models Contd…2.3 Many-to-Many Model• Allows many user level threads to be mapped to many kernel threads• Allows the user and OS to create a sufficient number of user and kernel threads• When a thread performs a blocking system call, the kernel can schedule another thread for execution• Windows NT/2000 with the ThreadFiber packageTwo-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 Many-to-Many Two-level Model Model Loganathan R, CSE, HKBKCE 7
  • 8. 3. Thread Libraries• A thread library provides the programmer an API for creating and managing threads• Two ways of implementation – Provide a library entirely in user space with no kernel support - code and data structures for the library exist in user space – A kernel-level library supported directly by the operating system - code and data structures for the library exist in kernel space3.1 Pthreads• Provided as either a user or kernel-level library• A POSIX standard (IEEE 1003.1c) 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)• All Pthreads programs must include the pthread.h header file• pthread _t t id declares the identifier t id for the thread to be created• The pthread_attr_t attr declares the attributes for the thread• The attributes are set in the function call pthread_attr_init(&attr)• A separate thread is created with the pthread_creat e () function call• pthread_join () to wait Loganathan R, CSE, HKBKCE 8
  • 9. 3. Thread Libraries Contd…3.2 Windows XP Threads• Threads are created in the Win32 API using the CreateThread() function with thread parameters• Parameters includes security information, the size of the stack, and a flag that can be set to indicate if the thread is to start in a suspended state• WaitForSingleObj ect () function for waiting3.3 Java Threads• Threads are the fundamental model of program execution in a Java and managed by the JVM• Java threads may be created by: –Extending Thread class (derive Thread class and override run()) –Implementing the Runnable interface[public interface Runnable{public abstract void run();}]• start () method creates the new thread then allocates memory initialize it in JVM, and calls run() to run thread in JVM• Join() method to wait• Java Thread States Loganathan R, CSE, HKBKCE 9
  • 10. 4. Threading Issues …4.1 The fork() and exec() System Calls• Does fork() duplicate only the calling thread or all threads?• Some UNIX have Both versions• Exec() system call works in the same way (will replace the entire process)• If exec() is called immediately after forking, duplicating only the calling thread is appropriate4.2 Cancellation• Terminating a thread before it has completed• Multiple threads are concurrently searching a database and one thread returns the result, the remaining threads might be canceled• A thread that is to be canceled is referred as the target thread• Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled• Difficulty in cancellation• Canceling a thread asynchronously may not free a necessary system-wide resource (OS will only reclaim System resources only)• Deferred cancellation occurs only after the target thread has checked a flag to determine if it should be canceled or not• Checking whether it should be canceled at a point when it can be canceled safely is known as cancellation points in Pthreads. Loganathan R, CSE, HKBKCE 10
  • 11. 4. Threading Issues Contd……4.3 Signal Handling• Signals are used in UNIX systems to notify a process that an particular event has occurred may be received either synchronously or asynchronously.• Signals whether synchronous(illegal memory access & Division by 0) or asynchronous (external event like CTL+C,& Timer expire), follow the same pattern: 1. Signal is generated by particular event 2. Signal is delivered to a process 3.Signal is handled• A signal handler is used to process signals – default signal handler that is run by the kernel when to handle the signal – user-defined signal handler that is called to override default action• In single-threaded programs, signals are always delivered to a process• In multithreaded programs: – 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• Multithreaded versions of UNIX allow a thread to specify which signals it will accept and which it will block• Windows does not explicitly provide support for signals, they can be emulated using asynchronous procedure calls (APCs) Loganathan R, CSE, HKBKCE 11
  • 12. 4. Threading Issues Contd……4.4 Thread Pools• Create a number of threads at start & place in a pool where they wait for work• 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• The number of threads in the pool can be set based on factors like the number of CPUs in the system, the amount of physical memory, and the expected number of concurrent client requests• Win32 API provides several functions related to thread pools4.5 Thread Specific Data• Allows each thread to have its own copy of data4.6 Scheduler Activations• Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application to be adjusted dynamically• An intermediate data structure between the user and kernel threads is placed and it is known as a lightweight process, or LWP Lightweight• To the user-thread library, the LWP appears to be a virtual processor on which the application can LWP Process schedule a user thread to run and each LWP attached to a kernal thread• If a kernel thread blocks, the LWP blocks, the user-level thread attached to the LWP also blocks• Communication between the user-thread library and the kernel is known as scheduler activation• The kernel provides an application with a set of virtual processors (LWPs) for the application to schedule user threads onto LWP and informs an application about certain events is known as an upcall• Upcalls are handled by the thread library with an upcall handler which run on a virtual processor is responsible to switch between threads 12