Assignment Os: Saqib Javed 11912
Assignment Os: Saqib Javed 11912
Assignment Os: Saqib Javed 11912
----------------------------------------------------------------------------------------------------
--------
TO SIR UMER SARWAR
31-12-2020
ASSIGNMENT NO. 3
ANSWERS OF THE QUESTIONS
Question # 1: Provide two programming examples in which
multithreading provides better performance than a single-
threaded solution.
Ans: The process of executing multiple threads simultaneously is known as
multithreading. Some examples where multi-threading improves performance include:
Matrix multiplication:
Individual rows and columns of the matrices can be multiplied in separate
threads, reducing the wait time of the processor for addition .
UI updates:
We can render some UI elements such as a view or images on a background
thread so that the main thread does not block the view, causing performance issues and
noticeable lags.
Multi-threading does not perform well for any sequential program. For
example; program to calculate an individual tax return. Another example where
multithreading does not work well would-be shell program like “Korn” shell.
Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
User level threads are fast to create and manage.
Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on multiple
processes.
If one thread in a process is blocked, the Kernel can schedule another thread of the
same process.
Kernel routines themselves can be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.
Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.
But threads are more vulnerable to problems caused by other threads in the same
process.
Question # 5: Provide two programming examples in which
multithreading does not provide better performance than a
single-threaded solution.
ANS:
If we are running a trivial program (constant time complexity) in a separate
thread, the overhead of creating the threads exceeds the tasks performed by them, thus
decreasing performance when compared to a single threaded alternative. Some
examples include:
There are a variety of reasons why multi-threading would be slower than single-
threading. In many ways, the question is: why do you think multi-threading would be
faster? Using multiple threads introduces extra work. Swapping contexts, to switch back
and forth between threads, is extra work. Doing synchronization, to ensure that multiple
threads communicate safely and wait for each other to complete, is extra work. Multiple
threads typically increase your working set of memory addresses, thereby requiring
more cache and as a result, causes more cache evictions (which is, you guessed it,
extra work).
Suppose you have an application (or part of an application) which is all one big
critical section protected by a lock. Only one thread at a time can hold the lock. The
throughput of the critical section can never be higher with multiple threads than it is with
one thread. In the early days of multiprocessor support in the Linux kernel, there was
such a thing, called the “big kernel lock”. It limited the performance of the kernel so that
the OS really couldn’t run any faster than it would on a single processor. (User mode
programs could run in parallel, so it wasn’t a complete bust!).
Another case is when the performance of the program is limited by, say,
memory bandwidth or disk bandwidth. More threads don’t help, and probably lose a little
because of switching costs.
----------------------------------------------------------------------------------------------------
--------