Lecture 5
Example for periority The average waiting time : = 41/5= 8.2
THREAD
Overview Process : single thread or multi thread Thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems Comprises : – Thread ID – Program counter – Register set – Stack Share with other threads with the same process : (Code – Data – Resources )
Thread Example Web browser – Thread to display image – Thread retrieves data from network Word – Thread display graphics – Thread keystrokes
Single and Multithreaded Processes
Benefits Responsiveness – Allow interactive application to continue running if part of, is blocked Resource Sharing – Threads share code and data by default from process but multi-process needs writing code for share memory or message passing Economy – Allocating memory and resources for process creation is costly. – In new process : more overhead, more time consuming and management Scalability – In multi processor parallelism multithreading can be greatly increased in a multiprocessor architecture,
Multicore Programming – New Trend : multi-core on a single chip, each core is single process for operating system – Multi core : means parallelism – Multi-core : needs designers of operating system write scheduling algorithms, use multi processing
Multicore Programming Multi-core systems putting pressure on programmers, challenges include 5 points: – Dividing activities – Balance – Data splitting – Data dependency – Testing and debugging
Multicore Programming Dividing activities – Find areas can be divided into separate and concurrent tasks Balance – Tasks perform equal work of equal value Data splitting – As application … data accessed by tasks must be divided to run on separate core. Data dependency – The data accessed by the tasks must be examined for dependencies between two or more tasks. Testing and debugging – Testing and debugging such concurrent programs is inherently more difficult than testing and debugging single-threaded applications.
Concurrent Execution on a Single-core System
Parallel Execution on a Multicore System
Multithread modeling User threads : manage with out kernel Kernel thread: manage by OS User threads are supported above the kernel and are managed without kernel support, whereas kernel threads are supported and managed directly by the operating system.
User Threads Thread management done by user-level threads library Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads
Kernel Threads Supported by the Kernel Examples – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X
Multithreading Models Many-to-One One-to-One Many-to-Many
Many-to-One Many user-level threads mapped to single kernel thread User thread library in user space Because one thread can access a kernel at a time, multi thread no parallelism in multi processor
Many-to-One Model
One-to-One Each user-level thread maps to kernel thread - allow parallelism if multiprocessor only draw back: create user thread needs to create kernel thread Examples – Windows NT/XP/2000 – Linux – Solaris 9 and later
One-to-one Model
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 No true concurrency because kernel can schedule only one thread at a time The number of kernel threads may be specific to either a particular application or particular machine Windows NT/2000 with the ThreadFiber package
Many-to-Many Model
Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread
Two-level Model
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
Pthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 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)
win32Threads In c program …. Window.h Is a kernel –level library available on windows system