You are on page 1of 19

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: _____________

Obtained Marks: _____________

Date: _____________

Operating system
(Lab)
PROJECT REPORT : THREADS

Submitted To:
Mr. Muhammad Naveed

Student Name:
Kamran Amin
Mashal ud din
Ilyas Darwesh
Muhammad Ali
Reg. Number:
1880199
1880200
1880159
1880180

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

In this project we are going to present Threads topic of OS. As we thought that this is one of
very important topic in OS. Here we start with

What are Threads?


Thread is an execution unit which consists of its own program counter, a stack, and a
set of registers. Threads are also known as Lightweight processes. Threads are popular way
to improve application through parallelism. The CPU switches rapidly back and forth among
the threads giving illusion that the threads are running in parallel.
As each thread has its own independent resource for process execution, multiple processes
can be executed parallel by increasing number of threads.

Types of Thread
There are two types of threads:

1. User Level Threads


2. Kernel Level Threads

A diagram that demonstrates these is as follows −

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

User - Level Threads


The user-level threads are implemented by users and the kernel is not aware of the existence
of these threads. It handles them as if they were single-threaded processes. User-level threads
are small and much faster than kernel level threads. They are represented by a program
counter(PC), stack, registers and a small process control block. Also, there is no kernel
involvement in synchronization for user-level threads.
Advantages of User-Level Threads
Some of the advantages of user-level threads are as follows −

 User-level threads are easier and faster to create than kernel-level threads. They can
also be more easily managed.
 User-level threads can be run on any operating system.
 There are no kernel mode privileges required for thread switching in user-level threads.
Disadvantages of User-Level Threads
Some of the disadvantages of user-level threads are as follows −

 Multithreaded applications in user-level threads cannot use multiprocessing to their


advantage.
 The entire process is blocked if one user-level thread performs blocking operation.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Kernel-Level Threads
Kernel-level threads are handled by the operating system directly and the thread management
is done by the kernel. The context information for the process as well as the process threads
is all managed by the kernel. Because of this, kernel-level threads are slower than user-level
threads.
Advantages of Kernel-Level Threads
Some of the advantages of kernel-level threads are as follows −

 Multiple threads of the same process can be scheduled on different processors in


kernel-level threads.
 The kernel routines can also be multithreaded.
 If a kernel-level thread is blocked, another thread of the same process can be
scheduled by the kernel.
Disadvantages of Kernel-Level Threads
Some of the disadvantages of kernel-level threads are as follows −

 A mode switch to kernel mode is required to transfer control from one thread to
another in a process.
 Kernel-level threads are slower to create as well as manage as compared to user-level
threads.

Difference between User Level thread and Kernel Level thread

User Level thread Kernel Level thread

User thread are implemented by users. kernel threads are implemented by OS.

OS doesn’t recognized user level threads. Kernel threads are recognized by OS.

Implementation of User threads is easy. Implementation of Kernel thread is


complicated.

Context switch time is less. Context switch time is more.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Context switch requires no hardware Hardware support is needed.


support.

If one user level thread perform blocking If one kernel thread perform blocking
operation then entire process will be operation then another thread can continue
blocked execution.

User level threads are designed as Kernel level threads are designed as
dependent threads. independent threads.

Example : Java thread, POSIX threads. Example : Window Solaris.

Relationship between User level thread and Kernel


level thread

There exist a strong a relationship between user level threads and kernel level threads.
Dependencies between ULT and KLT :

1. Use of Thread Library:


Thread library acts as an interface for the application developer to create number of
threads (according to the number of subtasks) and to manage those threads. This API
for a process can be implemented in kernel space or user space. In real-time
application, the necessary thread library is implemented in user space. This reduces the
system call to kernel whenever the application is in need of thread creation, scheduling
or thread management activities. Thus, the thread creation is faster as it requires only
function calls within the process. The user address space for each thread is allocated at
run-time. Overall it reduces various interface and architectural overheads as all these
functions are independent of kernel support.

2. Synchronization :
The subtasks (functions) within each task (process) can be executed concurrently or
parallely depending on the application. In that case, single-threaded process is not
suitable. There evokes multithreaded process. A unique subtask is allocated to every
thread within the process. These threads may use the same data section or different data

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

section. Typically, threads within the same process will share the code section, data
section, address space, open files etc.

When subtasks are concurrently performed by sharing the code section, it may result in data
inconsistency. Ultimately, requires suitable synchronization techniques to maintain the control
flow to access the shared data (critical section).
In a multithreaded process, synchronization adopted using four different models :
1. Mutex Locks – This allows only one thread at a time to access the shared resource.
2. Read/Write Locks – This allows exclusive writes and concurrent read of a shared resource.
3. Counting Semaphore – This count refers to the number of shared resource that can be
accessed simultaneously at a time. Once the count limit is reached, the remaining threads are
blocked.
4. Condition Variables – This blocks the thread until the condition satisfies(Busy Waiting).
All these synchronization models are carried out within each process using thread library. The
memory space for the lock variables is allocated in the user address space. Thus, requires no
kernel intervention.
5.
1. Scheduling :
The application developer during the thread creation sets the priority and scheduling
policy of each ULT thread using the thread library. On the execution of program, based on the
defined attributes the scheduling takes place by the thread library. In this case, the system
scheduler has no control over thread scheduling as the kernel is unaware of the ULT threads.
2. Context Switching :
Switching from one ULT thread to other ULT thread is faster within the same process,
as each thread has its own unique thread control block, registers, stack. Thus, registers are
saved and restored. Does not require any change of address space. Entire switching takes place
within the user address space under the control of thread library.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT


3. Asynchronous I/O :
After an I/O request ULT threads remains in blocked state, until it receives the
acknowledgment(ack) from the receiver. Although it follows asynchronous I/O, it creates a
synchronous environment to the application user. This is because the thread library itself
schedules an other ULT to execute until the blocked thread sends sigpoll as an ack to the
process thread library. Only then the thread library, reschedules the blocked thread.

For example, consider a program to copy the content(read) from one file and to paste(write) in
the other file. Additionaly, a pop-up that displays the percentage of progress completion.
This process contains three subtasks each allocated to a ULT,
 Thread A – Read the content from source file. Store in a global variable X within the
process address space.
 Thread B – Read the global variable X. Write in the destination file.
 Thread C – Display the percentage of progress done in a graphical representation.
Here, the application developer will schedule the multiple flow of control within a program
using the thread library.
Order of execution: Begins with Thread A, Then thread B and Then thread C.
Thread A and Thread B shares the global variable X. Only when after thread A writes on X,
thread B can read X. In that case, synchronization is to be adopted on the shared variable to
avoid thread B from reading old data. Context switching from thread A to Thread B and then
Thread C takes place within the process address space. Each thread saves and restores the
registers in its own thread control block (TCB). Thread C remains in blocked state, until thread
B starts its first write operation on the destination file. This is the reason behind; the graphical
indication of 100% pops-up a few seconds later although process completion.
Dependency between ULT and KLT :
The one and only major dependency between KLT and ULT arise when an ULT is in
need of the Kernel resources. Every ULT thread is associated to a virtual processor called
Light-weight process. This is created and binned to ULT by the thread library according to the
application need. Whenever a system call invoked, a kernel level thread is created and
scheduled to the LWPs by the system scheduler. These KLT are scheduled to access the kernel
resources by the system scheduler which is unaware of the ULT. Whereas the KLT is aware of
each ULT associated to it via LWPs.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

What if the relationship does not exist ?


If there is no association between KLT and ULT, then according to kernel every process is a
single-threaded process. In that case,
1. The system scheduler may schedule a process with threads that are of less priority or
idle threads. This leads to starvation of high-prioritized thread, which in turn reduces
the efficiency of the system.
2. When a single thread gets blocked, the entire process gets blocked. Then the CPU
utilization even in a multicore system will become much less. Though there may exist
executable threads, kernel considers every process as a single threaded process and
allocates only one core at a time.
3. System scheduler may provide a single time slice irrespective of the number of
threads within a process. A single threaded process and a process with 1000 threads
provided with same time slice will make system more inefficient.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:

 Many to One Model


 One to One Model
 Many to Many Model

Many to One Model

 In the many to one model, many user-level threads are all mapped onto a single
kernel thread.
 Thread management is handled by the thread library in user space, which is efficient
in nature.

One to One Model

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

 The one to one model creates a separate kernel thread to handle each and every user
thread.
 Most implementations of this model place a limit on how many threads can be
created.
 Linux and Windows from 95 to XP implement the one-to-one model for threads.

Many to Many Model

 The many to many model multiplexes any number of user threads onto an equal or


smaller number of kernel threads, combining the best features of the one-to-one and
many-to-one models.
 Users can create any number of the threads.
 Blocking the kernel system calls does not block the entire process.
 Processes can be split across multiple processors.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

What are Thread Libraries?


Thread libraries provide programmers with API for creation and management of threads.
Thread libraries may be implemented either in user space or in kernel space. The user space
involves API functions implemented solely within the user space, with no kernel support.
The kernel space involves system calls, and requires a kernel with thread library support.

Three types of Thread

1. POSIX Pitheads, may be provided as either a user or kernel library, as an extension to

the POSIX standard.


2. Win32 threads, are provided as a kernel-level library on Windows systems.

3. Java threads: Since Java generally runs on a Java Virtual Machine, the

implementation of threads is based upon whatever OS and hardware the JVM is


running on, i.e. either Pitheads or Win32 threads depending on the system.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Benefits of Multithreading
1. Responsiveness
2. Resource sharing, hence allowing better utilization of resources.
3. Economy. Creating and managing threads becomes easier.
4. Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be
distributed over a series of processors to scale.
5. Context Switching is smooth. Context switching refers to the procedure followed by
CPU to change from one task to another.

Multithreading Issues
Below we have mentioned a few issues related to multithreading. Well, it's an old saying, All
good things, come at a price.
Thread Cancellation
Thread cancellation means terminating a thread before it has finished working. There can be
two approaches for this, one is Asynchronous cancellation, which terminates the target
thread immediately. The other is Deferred cancellation allows the target thread to
periodically check if it should be cancelled.
Signal Handling
Signals are used in UNIX systems to notify a process that a particular event has occurred.
Now in when a Multithreaded process receives a signal, to which thread it must be delivered?
It can be delivered to all, or a single thread.
fork() System Call
fork() is a system call executed in the kernel through which a process creates a copy of itself.
Now the problem in Multithreaded process is, if one thread forks, will the entire process be
copied or not?
Security Issues
Yes, there can be security issues because of extensive sharing of resources between multiple
threads.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

There are many other issues that you might face in a multithreaded process, but there are
appropriate solutions available for them. Pointing out some issues here was just to study both
sides of the coin.

Difference between Process and Thread


Process:
Process means any program is in execution. Process control block controls the operation of any
process. Process control block contains information about processes for example Process
priority, process id, process state, CPU, register, etc. A process can create other processes
which are known as Child Processes. Process takes more time to terminate and it is isolated
means it does not share memory with any other process.
The process can have the following states like new, ready, running, waiting, terminated, and
suspended.
Thread:
Thread is the segment of a process means a process can have multiple threads and these
multiple threads are contained within a process. A thread has 3 states: running, ready, and
blocked.
Thread takes less time to terminate as compared to process and like process threads do not
isolate.

Difference between Process and Thread:

PROCESS THREAD

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Process means any program is in execution. Thread means segment of a process.

Process takes more time to terminate. Thread takes less time to terminate.

It takes more time for creation. It takes less time for creation.

It also takes more time for context switching. It takes less time for context switching.

Process is less efficient in term of Thread is more efficient in term of


communication. communication.

Process consumes more resources. Thread consumes less resource.

Process is isolated. Threads share memory.

Process is called heavy weight process. Thread is called light weight process.

Process switching uses interface in operating Thread switching does not require to call a
system. operating system and cause an interrupt to the
kernel.

If one server process is blocked no other server Second thread in the same task could run, while
process can execute until the first process one server thread is blocked.
unblocked.

Process has its own Process Control Block, Stack Thread has Parents’ PCB, its own Thread Control
and Address Space. Block and Stack and common Address space.

Similarity between Threads and Processes –


 Only one thread or process is active at a time
 Within process both execute sequentially
 Both can create children

Applications –
Threading is used widely in almost every field. Most widely it is seen over the internet now
days where we are using transaction processing of every type like recharges, online transfer,
banking etc. Threading is a segment which divide the code into small parts that are of very
light weight and has fewer burdens on CPU memory so that it can be easily worked out and
can achieve goal in desired field. The concept of threading is designed due to the problem of
fast and regular changes in technology and less the work in different areas due to less
application. Then as says “need is the generation of creation or innovation” hence by following

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

this approach human mind develop the concept of thread to enhance the capability of
programming.

The Linux Implementation of Threads


Threads are a popular modern programming abstraction. They provide multiple threads of
execution within the same program in a shared memory address space. They can also share
open files and other resources. Threads allow for concurrent programming and, on multiple
processor systems, true parallelism.

Linux has a unique implementation of threads. To the Linux kernel, there is no concept of a
thread. Linux implements all threads as standard processes. The Linux kernel does not
provide any special scheduling semantics or data structures to represent threads. Instead, a
thread is merely a process that shares certain resources with other processes. Each thread has
a unique task_struct and appears to the kernel as a normal process (which just happens to
share resources, such as an address space, with other processes).
This approach to threads contrasts greatly with operating systems such as Microsoft
Windows or Sun Solaris, which have explicit kernel support for threads (and sometimes call
threads lightweight processes). The name "lightweight process" sums up the difference in
philosophies between Linux and other systems. To these other operating systems, threads are
an abstraction to provide a lighter, quicker execution unit than the heavy process. To Linux,
threads are simply a manner of sharing resources between processes (which are already quite
lightweight)11. For example, assume you have a process that consists of four threads. On
systems with explicit thread support, there might exist one process descriptor that in turn
points to the four different threads. The process descriptor describes the shared resources,
such as an address space or open files. The threads then describe the resources they alone
possess. Conversely, in Linux, there are simply four processes and thus four
normal task_struct structures. The four processes are set up to share certain resources.
Threads are created like normal tasks, with the exception that the clone() system call is
passed flags corresponding to specific resources to be shared:

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

The previous code results in behavior identical to a normal fork(), except that the address
space, filesystem resources, file descriptors, and signal handlers are shared. In other words,
the new task and its parent are what are popularly called threads.
In contrast, a normal fork() can be implemented as

clone(SIGCHLD, 0);

And vfork() is implemented as

clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);

The flags provided to clone() help specify the behavior of the new process and detail what
resources the parent and child will share. Table 3.1 lists the clone flags, which are defined
in <linux/sched.h>, and their effect.

Table 3.1 clone() Flags

Flag Meaning

CLONE_FILES Parent and child share open files.

CLONE_FS Parent and child share file system information.

CLONE_IDLETASK Set PID to zero (used only by the idle tasks).

CLONE_NEWNS Create a new namespace for the child.

CLONE_PARENT Child is to have same parent as its parent.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Flag Meaning

CLONE_PTRACE Continue tracing child.

CLONE_SETTID Write the TID back to user-space.

CLONE_SETTLS Create a new TLS for the child.

CLONE_SIGHAND Parent and child share signal handlers and blocked signals.

CLONE_SYSVSEM Parent and child share System V SEM_UNDO semantics.

CLONE_THREAD Parent and child are in the same thread group.

CLONE_VFORK vfork() was used and the parent will sleep until the child
wakes it.

CLONE_UNTRACED Do not let the tracing process force CLONE_PTRACE on


the child.

CLONE_STOP Start process in the TASK_STOPPED state.

CLONE_SETTLS Create a new TLS (thread-local storage) for the child.

CLONE_CHILD_CLEARTI Clear the TID in the child.


D

CLONE_CHILD_SETTID Set the TID in the child.

CLONE_PARENT_SETTID Set the TID in the parent.

CLONE_VM Parent and child share address space.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Flag Meaning

Kernel Threads
It is often useful for the kernel to perform some operations in the background. The kernel
accomplishes this via kernel threads—standard processes that exist solely in kernel-space.
The significant difference between kernel threads and normal processes is that kernel threads
do not have an address space (in fact, their mm pointer is NULL). They operate only in
kernel-space and do not context switch into user-space. Kernel threads are, however,
schedulable and preemptable as normal processes.
Linux delegates several tasks to kernel threads, most notably the pdflush task and
the ksoftirqd task. These threads are created on system boot by other kernel threads. Indeed, a
kernel thread can be created only by another kernel thread. The interface for spawning a new
kernel thread from an existing one is

int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)

The new task is created via the usual clone() system call with the specified flags argument.
On return, the parent kernel thread exits with a pointer to the child's task_struct. The child
executes the function specified by fn with the given argument arg. A special clone
flag, CLONE_KERNEL, specifies the usual flags for kernel
threads: CLONE_FS, CLONE_FILES, and CLONE_SIGHAND. Most kernel threads pass
this for their flags parameter.
Typically, a kernel thread continues executing its initial function forever (or at least until the
system reboots, but with Linux you never know). The initial function usually implements a
loop in which the kernel thread wakes up as needed, performs its duties, and then returns to
sleep.

OS Lab BSSE-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

OS Lab BSSE-4 SZABIST-ISB

You might also like