Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20
CHAPTER4: THREADS
Instructor: Dr.Fouzia Idrees
Thread 2
A thread is the smallest unit of processing that can
be performed in an OS. In most modern operating systems, a thread exists within a process - that is, a single process may contain multiple threads. A thread is a single sequence stream within in a process 3 Cont… 4
In multitasking environment the processes are allowed to run
concurrently, while multithreading allows sub-processes to run concurrently.
When multiple threads are running concurrently, this is known
as multithreading, which is similar to multitasking.
An operating system with multitasking capabilities allows
programs (or processes) to run seemingly at the same time.
A single program with multithreading capabilities allows
individual sub-processes (or threads) to run seemingly at the City University Peshawar same time. Example 5
One example of multithreading is downloading a
video while playing it at the same time. Multithreading is also used extensively in computer-generated animation. Among the widely-used programming languages that allow developers to work on threads in their program source code are Java, Python and .NET.
City University Peshawar
6
Consider an application that uses several connections
to one or more servers. In this situation, each connection can be run within a dedicated thread. One thread is waiting for command results, the other threads can be processing received results or sending new commands. Such an approach may increase throughput because the application spends less idle time while waiting for results. City University Peshawar Benefits 7
Responsiveness – may allow continued execution if
part of process is blocked, especially important for user interfaces Resource Sharing – threads share resources of process, easier than shared memory or message passing Economy – cheaper than process creation, thread switching lower overhead than context switching Scalability – process can take advantage of multiprocessor architectures
City University Peshawar
User Threads and Kernel Threads 8
User threads – threads are creation and management
is done by the application. They are associated with user application Kernel is not aware Management done by user-level threads library The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. The application begins with a single thread and begins running in that thread.
City University Peshawar
.
City University Peshawar
10 City University Peshawar 11
Kernel threads - Thread management done by the
Kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Windows is an example of this approach.
City University Peshawar
12 City University Peshawar 13 City University Peshawar Multithreading Models 14
Some operating system provide a combined user
level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors.
City University Peshawar
Cont.. 15
Multithreading models are three types
Many-to-One
One-to-One
Many-to-Many
City University Peshawar
Many-to-One 16
Many to one model maps many user level threads
to one Kernel level thread. Thread management is done in user space. When thread makes a blocking system call, the entire process will be blocks. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.
City University Peshawar
17 City University Peshawar One-to-One 18
There is one to one relationship of user level thread
to the kernel level thread. This model provides more concurrency than the many to one model. Support multiple thread to execute in parallel on microprocessors.
City University Peshawar
Many-to-many 19
Many user level threads multiplexes to the Kernel
thread of smaller or equal numbers. The number of Kernel threads may be specific to either a particular application or a particular machine.