ch4 New
ch4 New
Chapter 4: Threads
Overview
Multithreading Models
Thread Libraries
Threading Issues
Motivation
Resource sharing. By default, threads share the memory and the resources
of the process to which they belong. The benefit of sharing code and data is
that it allows an application to have several different threads of activity within
the same address space.
Benefits of Multithread Programming (cont..)
Types of parallelism
Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
Task parallelism – distributing threads across cores, each
thread performing unique operation
As of threads grows, so does architectural support for threading
CPUs have cores as well as hardware threads
Consider Oracle SPARC T4 with 8 cores, and 8 hardware
threads per core
Concurrency vs. Parallelism
Concurrent execution on single-core system:
But does the law take into account contemporary multicore systems?
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Many-to-One
User threads
Thread management done by user-level threads library
Three primary thread libraries: ->
-> POSIX Pthreads
-> Win32 threads
-> Java threads
User-level threads implement in user-level libraries, rather than
via systems calls, so thread switching does not need to call
operating system and to cause interrupt to the kernel.
In fact, the kernel knows nothing about user-level threads and
manages them as if they were single- threaded processes.
Cont..
Disadvantages:
There is a lack of coordination between threads and operating
system kernel. Therefore, process as whole gets one time slice
irrespective of whether process has one thread or 1000 threads
within. It is up to each thread to relinquish control to other threads.
User-level threads requires non-blocking systems call i.e., a
multithreaded kernel. Otherwise, entire process will blocked in the
kernel, even if there are runable threads left in the processes. For
example, if one thread causes a page fault, the process blocks.
Kernel-Level Threads
Supported by the Kernel
Examples: ->Windows XP/2000, ->Solaris , ->Linux, ->Tru64 UNIX,
->Mac OS X
Cont..
In this method, the kernel knows about and manages the threads. No
runtime system is needed in this case.
Instead of thread table in each process, the kernel has a thread table
that keeps track of all threads in the system.
In addition, the kernel also maintains the traditional process table to
keep track of processes. Operating Systems kernel provides system call
to create and manage threads.
Advantages:
Because kernel has full knowledge of all threads, Scheduler may decide
to give more time to a process having large number of threads than
process having small number of threads. x Kernel- level threads are
especially good for applications that frequently block.
Disadvantages:
The kernel-level threads are slow and inefficient. For instance, threads
operations are hundreds of times slower than that of user-level threads.
Pthreads Example
Pthreads Example (Cont.)
Pthreads Code for Joining 10 Threads
Windows Multithreaded C Program
Windows Multithreaded C Program (Cont.)
Java Threads