OS Unit2
OS Unit2
A processor in a computer running Windows has two different modes: user mode and kernel
mode.
The processor switches between the two modes depending on what type of code is running on
the processor. Applications run in user mode, and core operating system components run in
kernel mode. While many drivers run in kernel mode, some drivers may run in user mode.
User mode
When you start a user-mode application, Windows creates a process for the application. The
process provides the application with a private virtual address space and a private handle
table. Because an application's virtual address space is private, one application cannot alter
data that belongs to another application. Each application runs in isolation, and if an
application crashes, the crash is limited to that one application. Other applications and the
operating system are not affected by the crash.
In addition to being private, the virtual address space of a user-mode application is limited. A
processor running in user mode cannot access virtual addresses that are reserved for the
operating system. Limiting the virtual address space of a user-mode application prevents the
application from altering, and possibly damaging, critical operating system data.
Kernel mode
All code that runs in kernel mode shares a single virtual address space. This means that a
kernel-mode driver is not isolated from other drivers and the operating system itself. If a
kernel-mode driver accidentally writes to the wrong virtual address, data that belongs to the
operating system or another driver could be compromised. If a kernel-mode driver crashes,
the entire operating system crashes.
o As discussed above, Kernel is the core part of an OS(Operating system); hence it has
full control over everything in the system. Each operation of hardware and software is
managed and administrated by the kernel.
o It acts as a bridge between applications and data processing done at the hardware
level. It is the central component of an OS.
o It is the part of the OS that always resides in computer memory and enables the
communication between software and hardware components.
o It is the computer program that first loaded on start-up the system (After the
bootloader). Once it is loaded, it manages the remaining start-ups. It also manages
memory, peripheral, and I/O requests from software. Moreover, it translates all I/O
requests into data processing instructions for the CPU. It manages other tasks also
such as memory management, task management, and disk management.
o A kernel is kept and usually loaded into separate memory space, known as protected
Kernel space. It is protected from being accessed by application programs or less
important parts of OS.
o Other application programs such as browser, word processor, audio & video player
use separate memory space known as user-space.
o Due to these two separate spaces, user data and kernel data don't interfere with each
other and do not cause any instability and slowness.
Functions of a Kernel
A kernel of an OS is responsible for performing various functions and has control over the
system. Some main responsibilities of Kernel are given below:
o Device Management
To perform various actions, processes require access to peripheral devices such as a
mouse, keyboard, etc., that are connected to the computer. A kernel is responsible for
controlling these devices using device drivers. Here, a device driver is a computer
program that helps or enables the OS to communicate with any hardware device.
A kernel maintains a list of all the available devices, and this list may be already
known, configured by the user, or detected by OS at runtime.
o Memory Management
The kernel has full control for accessing the computer's memory. Each process
requires some memory to work, and the kernel enables the processes to safely access
the memory. To allocate the memory, the first step is known as virtual
addressing, which is done by paging or segmentation. Virtual addressing is a
process of providing virtual address spaces to the processes. This prevents the
application from crashing into each other.
o Resource Management
One of the important functionalities of Kernel is to share the resources between
various processes. It must share the resources in a way that each process uniformly
accesses the resource.
The kernel also provides a way for synchronization and inter-process
communication (IPC). It is responsible for context switching between processes.
o Accessing Computer Resources
A kernel is responsible for accessing computer resources such as RAM and I/O
devices. RAM or Random-Access Memory is used to contain both data and
instructions. Each program needs to access the memory to execute and mostly wants
more memory than the available. For such a case, Kernel plays its role and decides
which memory each process will use and what to do if the required memory is not
available.
The kernel also allocates the request from applications to use I/O devices such as
keyboards, microphones, printers, etc.
Types of Kernel
There are mainly five types of Kernel, which are given below:
40.1M
710
Prime Ministers of India | List of Prime Minister of India (1947-2020)
Next
Stay
1. Monolithic Kernels
In a monolithic kernel, the same memory space is used to implement user services and
kernel services.
It means, in this type of kernel, there is no different memory used for user services and kernel
services.
As it uses the same memory space, the size of the kernel increases, increasing the overall size
of the OS.
The execution of processes is also faster than other kernel types as it does not use separate
user and kernel space.
Examples of Monolithic Kernels are Unix, Linux, Open VMS, XTS-400, etc.
Advantages:
o The execution of processes is also faster as there is no separate user space and kernel
space and less software involved.
o As it is a single piece of software hence, it's both sources and compiled forms are
smaller.
Disadvantages:
o If any service generates any error, it may crash down the whole system.
o These kernels are not portable, which means for each new architecture, they must be
rewritten.
o Large in size and hence become difficult to manage.
o To add a new service, the complete operating system needs to be modified.
2. Microkernel
Microkernels are easier to manage and maintain as compared to monolithic kernels. Still, if
there will be a greater number of system calls and context switching, then it might reduce the
performance of the system by making it slow.
These kernels use a message passing system for handling the request from one server to
another server.
Only some essential services are provided by microkernels, such as defining memory address
spaces, IPC(Interprocess Communication), and process management. Other services such as
networking are not provided by Kernel and handled by a user-space program known
as servers.
One of the main disadvantages of monolithic kernels that an error in the kernel can crash the
whole system, can be removed in the microkernel. As in a microkernel, if a kernel process
crashes, the crashing of the whole system can still be prevented by restarting the error-caused
services.
Advantages
Disadvantages
o There is more requirement of software for interfacing, which reduces the system
performance.
o Process management is very complicated.
o The messaging bugs are difficult to fix.
3. Hybrid Kernel
Hybrid kernels are also known as modular kernels, and it is the combination of both
Monolithic and Microkernels. It takes advantage of the speed of monolithic kernels and the
modularity of microkernels.
A hybrid kernel can be understood as the extended version of a microkernel with additional
properties of a monolithic kernel. These kernels are widely used in commercial OS, such as
different versions of MS Windows.
It is much similar to a microkernel, but it also includes some additional code in kernel space
to enhance the performance of the system.
Hybrid kernels allow to run some services such as network stack in kernel space to reduce
the performance compared to a traditional microkernel, but it still allows to run kernel code
(such as device drivers) as servers in user-space.
Advantages:
Disadvantages:
4. Nanokernel
As the name suggests, in Nanokernel, the complete code of the kernel is very small, which
means the code executing in the privileged mode of the hardware is very small. Here the
term nano defines a kernel that supports a nanosecond clock resolution.
Advantages
5. Exokernel
Exokernel is still developing and is the experimental approach for designing OS.
This type of kernel is different from other kernels as in this; resource protection is kept
separated from management, which allows us to perform application-specific customization.
Advantages:
o The exokernel-based system can incorporate multiple library operating systems. Each
library exports a different API, such as one can be used for high-level UI
development, and the other can be used for real-time control.
Disadvantages:
As can be seen from this diagram, the processes execute normally in the user mode until a
system call interrupts this. Then the system call is executed on a priority basis in the kernel
mode. After the execution of the system call, the control returns to the user mode and
execution of user processes can be resumed.
In general, system calls are required in the following situations −
If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and receiving
packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
CreateProcess() fork()
Process ExitProcess() exit()
Control WaitForSingleObjec wait()
t()
CreateFile() open()
File ReadFile() read()
Management WriteFile() write()
CloseHandle() close()
SetConsoleMode() ioctl()
Device
ReadConsole() read()
Management
WriteConsole() write()
GetCurrentProcessI getpid(
Information D() )
Maintenance SetTimer() alarm()
Sleep() sleep()
pipe()
CreatePipe()
shmget
Communicati CreateFileMapping(
()
on )
mmap(
MapViewOfFile()
)
There are many different system calls as shown above. Details of some of those system calls
are as follows −
open()
The open() system call is used to provide access to a file in a file system. This system call
allocates resources to the file and provides a handle that the process uses to refer to the file.
A file can be opened by multiple processes at the same time or be restricted to one process. It
all depends on the file organisation and file system.
read()
The read() system call is used to access data from a file that is stored in the file system. The
file to read can be identified by its file descriptor and it should be opened using open() before
it can be read. In general, the read() system calls takes three arguments i.e. the file descriptor,
buffer which stores read data and number of bytes to be read from the file.
write()
The write() system calls writes the data from a user buffer into a device such as a file. This
system call is one of the ways to output data from a program. In general, the write system
calls takes three arguments i.e. file descriptor, pointer to the buffer where data is stored and
number of bytes to write from the buffer.
close()
The close() system call is used to terminate access to a file system. Using this system call
means that the file is no longer required by the program and so the buffers are flushed, the
file metadata is updated and the file resources are de-allocated.
What is the purpose of System Programs?
System programs provide an environment where programs can be developed and executed.
In the simplest sense, system programs also provide a bridge between the user interface and
system calls. In reality, they are much more complex. For example, a compiler is a complex
system program.
System Programs Purpose
The system program serves as a part of the operating system. It traditionally lies between the
user interface and the system calls. The user view of the system is actually defined by system
programs and not system calls because that is what they interact with and system programs
are closer to the user interface.
An image that describes system programs in the operating system hierarchy is as follows −
In the above image, system programs as well as application programs form a bridge between
the user interface and the system calls. So, from the user view the operating system observed
is actually the system programs and not the system calls.
Types of System Programs
System programs can be divided into seven parts. These are given as follows:
Status Information
The status information system programs provide required data on the current or past status of
the system. This may include the system date, system time, available memory in system, disk
space, logged in users etc.
Communications
These system programs are needed for system communications such as web browsers. Web
browsers allow systems to communicate and access information from the network as
required.
File Manipulation
These system programs are used to manipulate system files. This can be done using various
commands like create, delete, copy, rename, print etc. These commands can create files,
delete files, copy the contents of one file into another, rename files, print them etc.
Program Loading and Execution
The system programs that deal with program loading and execution make sure that programs
can be loaded into memory and executed correctly. Loaders and Linkers are a prime example
of this type of system programs.
File Modification
System programs that are used for file modification basically change the data in the file or
modify it in some other way. Text editors are a big example of file modification system
programs.
Application Programs
Application programs can perform a wide range of services as per the needs of the users.
These include programs for database systems, word processors, plotting tools, spreadsheets,
games, scientific applications etc.
Programming Language Support
These system programs provide additional support features for different programming
languages. Some examples of these are compilers, debuggers etc. These compile a program
and make sure it is error free respectively.
An operating system is a framework that enables user application programs to interact with
system hardware. The operating system does not perform any functions on its own, but it
provides an atmosphere in which various apps and programs can do useful work. The
operating system may be observed from the point of view of the user or the system, and it is
known as the user view and the system view. In this article, you will learn the views of the
operating system.
The operating system may be observed from the viewpoint of the user or the system. It is
known as the user view and the system view. There are mainly two types of views of the
operating system. These are as follows:
1. User View
2. System View
User View
The user view depends on the system interface that is used by the users. Some systems are
designed for a single user to monopolize the resources to maximize the user's task. In these
cases, the OS is designed primarily for ease of use, with little emphasis on quality and none
on resource utilization.
The user viewpoint focuses on how the user interacts with the operating system through the
usage of various application programs. In contrast, the system viewpoint focuses on how the
hardware interacts with the operating system to complete various tasks40Next
Most computer users use a monitor, keyboard, mouse, printer, and other accessories to
operate their computer system. In some cases, the system is designed to maximize the output
of a single user. As a result, more attention is laid on accessibility, and resource allocation is
less important. These systems are much more designed for a single user experience and meet
the needs of a single user, where the performance is not given focus as the multiple user
systems.
Another example of user views in which the importance of user experience and performance
is given is when there is one mainframe computer and many users on their computers trying
to interact with their kernels over the mainframe to each other. In such circumstances,
memory allocation by the CPU must be done effectively to give a good user experience. The
client-server architecture is another good example where many clients may interact through a
remote server, and the same constraints of effective use of server resources may arise.
Moreover, the touchscreen era has given you the best handheld technology ever. Smartphones
interact via wireless devices to perform numerous operations, but they're not as efficient as a
computer interface, limiting their usefulness. However, their operating system is a great
example of creating a device focused on the user's point of view.
Some systems, like embedded systems that lack a user point of view. The remote control used
to turn on or off the tv is all part of an embedded system in which the electronic device
communicates with another program where the user viewpoint is limited and allows the user
to engage with the application.
System View
The OS may also be viewed as just a resource allocator. A computer system comprises
various sources, such as hardware and software, which must be managed effectively. The
operating system manages the resources, decides between competing demands, controls the
program execution, etc. According to this point of view, the operating system's purpose is to
maximize performance. The operating system is responsible for managing hardware
resources and allocating them to programs and users to ensure maximum performance.
From the user point of view, we've discussed the numerous applications that require varying
degrees of user participation. However, we are more concerned with how the hardware
interacts with the operating system than with the user from a system viewpoint. The hardware
and the operating system interact for a variety of reasons, including:
1. Resource Allocation
The hardware contains several resources like registers, caches, RAM, ROM, CPUs, I/O
interaction, etc. These are all resources that the operating system needs when an application
program demands them. Only the operating system can allocate resources, and it has used
several tactics and strategies to maximize its processing and memory space. The operating
system uses a variety of strategies to get the most out of the hardware resources, including
paging, virtual memory, caching, and so on. These are very important in the case of various
user viewpoints because inefficient resource allocation may affect the user viewpoint, causing
the user system to lag or hang, reducing the user experience.
2. Control Program
The control program controls how input and output devices (hardware) interact with the
operating system. The user may request an action that can only be done with I/O devices; in
this case, the operating system must also have proper communication, control, detect, and
handle such devices.
According to the computer system, the operating system is the bridge between applications
and hardware. It is most intimate with the hardware and is used to control it as required.
The different types of system view for operating system can be explained as follows:
The system views the operating system as a resource allocator. There are many resources
such as CPU time, memory space, file storage space, I/O devices etc. that are required by
processes for execution. It is the duty of the operating system to allocate these resources
judiciously to the processes so that the computer system can run as smoothly as possible.
The operating system can also work as a control program. It manages all the processes and
I/O devices so that the computer system works smoothly and there are no errors. It makes
sure that the I/O devices work in a proper manner without creating problems.
Operating systems can also be viewed as a way to make using hardware easier.
Computers were required to easily solve user problems. However it is not easy to work
directly with the computer hardware. So, operating systems were developed to easily
communicate with the hardware.
An operating system can also be considered as a program running at all times in the
background of a computer system (known as the kernel) and handling all the application
programs. This is the definition of the operating system that is generally followed.
The Process
Processes are the most fundamental operating system abstraction.
Processes organize information about other abstractions and represent a single thing
that the computer is "doing."
You know processes as app(lication)s.
Organizing Information
Unlike threads, address spaces and files, processes are not tied to a hardware component.
Instead, they contain other abstractions.
Processes contain:
one or more threads,
an address space, and
zero or more open file handles representing files.
Figure 2. The Process
Process as Protection Boundary
The operating system is responsible for isolating processes from each other.
What you do in your own process is your own business but it shouldn’t be able to
crash the machine or affect other processes—or at least processes started by other
users.
Thus: safe intra-process communication is your problem; safe inter-process
communication is an operating system problem.
Intra-Process Communication: Easy
What is a process?
In an operating system, a process can be defined as an entity that represents the basic unit of
work to be implemented in the system. When a user boots up a PC, many processes are
started unknown to the user.
Some of the most common processes are as follows.
A process may be created by another process using the fork() method. The process which
calls the fork() method or simply, the process creating another process is known as the parent
process and the created process is called a child process. A child process can have only one
parent, but a parent process can have multiple child processes. Also, both the parent and child
processes have the same memory, open files, and environment strings, but they have distinct
address spaces.
Process Termination
Process termination occurs when a process is simply terminated. The exit() system call is
used to terminate a process. Now, a process can be terminated because of various reasons.
They can be categorized into two different categories, voluntary and involuntary. Some of the
causes of process termination are:
A process may be terminated naturally after its execution releasing all of its resources.
A child process may be terminated if its parent process requests for the same.
A process can be terminated if it tries to use a resource that it is not allowed to.
Another cause of a process termination can be an I/O failure for a process.
If a parent process is terminated, then it will be another cause for its child process’s
termination.
A process can also be terminated if it requires more memory than what is currently
available in the system.
1. Most processes terminate because they have done their work. Processes after
completing their work, perform a system call to tell the Operating System that they
have completed the work. In UNIX, this system call is ‘exit’. In Windows, this goes
by ‘EndProcess’. For the foreground processes, the user has a clickable icon to exit
the process. It’s usually denoted by the x symbol for windows or the red button in
macOS.
2. There may be some instances where the processes exit involuntarily. This may be due
to some fatal error. For example, if you command the compiler to compile a file
called red.c but no such file exists, the compiler will terminate.
3. Sometimes the process causes an error due to which it has to terminate. These errors
include, but are not limited to:
– Referencing nonexistent memory
– Divide by zero error
– Executing an illegal instruction
In some cases, the process signals the operating system that it wishes to handle the
error itself using an interrupt.
4. There are some processes that instruct the OS to kill other processes. In UNIX, this
command is straightforward – ‘kill’. Anyway, the killer must have the necessary
authorization to kill the other process.
Process Hierarchy
In a computer system, we require to run many processes at a time and some processes need to
create other processes whilst their execution. When a process creates another process, then
the parent and the child processes tend to associate with each other in certain ways and
further. The child process can also create other processes if required. This parent-child like
structure of processes form a hierarchy, called Process Hierarchy.
Unlike sexual reproduction though, the child processes have only one parent. A process can
be a parent by itself and that is usually the case.
In UNIX, a process and all of its children and grandchildren form a process group. When a
user sends a signal from the keyboard, the signal is delivered to all members of the process
group currently associated with the keyboard. Individually, each process can catch the signal
and do as it wishes.
There is no hierarchy system in Windows. Each process is treated equally. The only remote
association with the hierarchy that it does is that a parent process gets a handle. But then
again, this parent process can pass the handle to some other process thereby invalidating the
hierarchy system.
Two-State Process Model
A Two-State Process Model categorizes a process in two categories:
When a process is created, it is in the not in running state and when the CPU runs the process,
it is in the running state. If there is a process in a running state and another process is created
with a higher priority, then the newly created process will be run by the CPU. The former
process will go in the not running state.
Five-State Process Model
The Five-State Process Model categorizes a process in five states:
Implementation of Processes
To implement the process table, the operating system maintains a table called the process
table, with one entry per process.
The process table is a data table which is used by an operating system to keep track of all the
processes present in the system. The entries in the table tell about the information about the
states of the processes, their program counters, stack pointers, allocated memories, the status
of their files and their accounting and scheduling information. They contain every
information about the present processes and also, of those processes which are switched with
the current processes.
Some of the fields of a typical process table is given below.
Process Management Memory Management File Management
Priority
Scheduling parameters
Process identity
Parent process
Process group
Signals
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple threads.
Why Multithreading?
A thread is also known as lightweight process. The idea is to achieve parallelism by dividing
a process into multiple threads. For example, in a browser, multiple tabs can be different
threads. MS Word uses multiple threads: one thread to format the text, another thread to
process inputs, etc. More advantages of multithreading are discussed below
Process vs Thread?
The primary difference is that threads within the same process run in a shared memory space,
while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share
with other threads their code section, data section, and OS resources (like open files and
signals). But, like process, a thread has its own program counter (PC), register set, and stack
space.
Advantages of Thread over Process
1. Responsiveness: If the process is divided into multiple threads, if one thread completes its
execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to process
context switch. Process context switching requires more overhead from the CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a single
process, then we can schedule multiple threads on multiple processor. This will make process
execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all threads
within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own stack
and registers.
5. Communication: Communication between multiple threads is easier, as the threads shares
common address space. while in process we have to follow some specific communication
technique for communication between two process.
6. Enhanced throughput of the system: If a process is divided into multiple threads, and each
thread function is considered as one job, then the number of jobs completed per unit of time
is increased, thus increasing the throughput of the system.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
Thread is a single sequence stream within a process. Threads have same properties as of the
process so they are called as light weight processes. Threads are executed one after another
but gives the illusion as if they are executing in parallel. Each thread has different states.
Each thread has
1. A program counter
2. A register set
3. A stack space
Threads are not independent of each other as they share the code, data, OS resources etc.
Similarity between Threads and Processes –
Only one thread or process is active at a time
Within process both execute sequential
Both can create children
Differences between Threads and Processes –
Threads are not independent, processes are.
Threads are designed to assist each other, processes may or may not do it
Types of Threads:
1. User Level thread (ULT) – Is implemented in the user level library, they are not created
using the system calls. Thread switching does not need to call OS and to cause interrupt to
Kernel. Kernel doesn’t know about the user level thread and manages them as if they were
single-threaded processes.
Advantages of ULT –
Can be implemented on an OS that doesn’t support multithreading.
Simple representation since thread has only program counter, register set, stack
space.
Simple to create since no intervention of kernel.
Thread switching is fast since no OS calls need to be made.
Limitations of ULT –
No or less co-ordination among the threads and Kernel.
If one thread causes a page fault, the entire process blocks.
2. Kernel Level Thread (KLT) – Kernel knows and manages the threads. Instead of thread
table in each process, the kernel itself has thread table (a master one) that keeps track of all
the threads in the system. In addition kernel also maintains the traditional process table to
keep track of the processes. OS kernel provides system call to create and manage threads.
Advantages of KLT –
Since kernel has full knowledge about the threads in the system, scheduler may
decide to give more time to processes having large number of threads.
Good for applications that frequently block.
Limitations of KLT –
Slow and inefficient.
It requires thread control block so it is an overhead.
Summary:
1. Each ULT has a process that keeps track of the thread using the Thread table.
2. Each KLT has Thread Table (TCB) as well as the Process Table (PCB).
3. It takes more time for creation. It takes less time for creation.
12. Changes to the parent process do Since all threads of the same process share address
not affect child processes. space and other resources so any changes to the
main thread may affect the behavior of the other
S.NO Process Thread
13. A system call is involved in it. No system call is involved, it is created using APIs.
Threading Issues in OS
There are several threading issues when we are in a multithreading environment. In this
section, we will discuss the threading issues with system calls, cancellation of thread, signal
handling, thread pool and thread-specific data.
Threading Issues in OS
1. System Calls
2. Thread Cancellation
3. Signal Handling
4. Thread Pool
5. Thread Specific Data
2. Thread cancellation
Termination of the thread in the middle of its execution it is termed as ‘thread cancellation’.
Let us understand this with the help of an example. Consider that there is a multithreaded
program which has let its multiple threads to search through a database for some information.
However, if one of the thread returns with the desired result the remaining threads will be
cancelled.
Now a thread which we want to cancel is termed as target thread. Thread cancellation can be
performed in two ways:
Asynchronous Cancellation: In asynchronous cancellation, a thread is employed to
terminate the target thread instantly.
Deferred Cancellation: In deferred cancellation, the target thread is scheduled to check itself
at regular interval whether it can terminate itself or not.
The issue related to the target threads are listed below:
What if the resources had been allotted to the cancel target thread?
What if the target thread is terminated when it was updating the data, it was sharing with
some other thread.
Here the asynchronous cancellation of the thread where a thread immediately cancels the
target thread without checking whether it is holding any resources or not creates troublesome.
However, in deferred cancellation, the thread that indicates the target thread about the
cancellation, the target thread crosschecks its flag in order to confirm that it should it be
cancelled immediately or not. The thread cancellation takes place where they can be
cancelled safely such points are termed as cancellation points by Pthreads.
3. Signal Handling
Signal handling is more convenient in the single-threaded program as the signal would be
directly forwarded to the process. But when it comes to multithreaded program, the issue
arrives to which thread of the program the signal should be delivered.
Let’s say the signal would be delivered to:
Well, how the signal would be delivered to the thread would be decided, depending upon the
type of generated signal. The generated signal can be classified into two type’s synchronous
signal and asynchronous signal.
Synchronous signals are forwarded to the same process that leads to the generation of the
signal. Asynchronous signals are generated by the event external to the running process thus
the running process receives the signals asynchronously.
So if the signal is synchronous it would be delivered to the specific thread causing the
generation of the signal. If the signal is asynchronous it cannot be specified to which thread
of the multithreaded program it would be delivered. If the asynchronous signal is notifying to
terminate the process the signal would be delivered to all the thread of the process.
The issue of an asynchronous signal is resolved up to some extent in most of the
multithreaded UNIX system. Here the thread is allowed to specify which signal it can accept
and which it cannot. However, the Window operating system does not support the concept of
the signal instead it uses asynchronous procedure call (ACP) which is similar to the
asynchronous signal of the UNIX system.
UNIX allow the thread to specify which signal it can accept and which it will not whereas the
ACP is forwarded to the specific thread.
4. Thread Pool
When a user requests for a webpage to the server, the server creates a separate thread to
service the request. Although the server also has some potential issues. Consider if we do not
have a bound on the number of actives thread in a system and would create a new thread for
every new request then it would finally result in exhaustion of system resources.
We are also concerned about the time it will take to create a new thread. It must not be that
case that the time require to create a new thread is more than the time required by the thread
to service the request and then getting discarded as it would result in wastage of CPU time.
The solution to this issue is the thread pool. The idea is to create a finite amount of threads
when the process starts. This collection of threads is referred to as the thread pool. The
threads stay in the thread pool and wait till they are assigned any request to be serviced.
Whenever the request arrives at the server, it invokes a thread from the pool and assigns it the
request to be serviced. The thread completes its service and return back to the pool and wait
for the next request.
If the server receives a request and it does not find any thread in the thread pool it waits for
some or the other thread to become free and return to the pool. This much better than creating
a new thread each time a request arrives and convenient for the system that cannot handle a
large number of concurrent threads.
Thread Libraries in OS
Thread Libraries has a collection of functions that useful in creating and controlling threads.
Programmers can access these thread libraries using an application programming interface
(API). Thread libraries can be the user level library or kernel level library.
If the thread library is implemented at the userspace then code and data of the thread library
would reside in user space. In this case, invoking any function from thread library would be a
simple function call and it won’t be a system call.
If the thread library is implemented at the kernel space then code and data of the library
would reside in the kernel space and would be supported by the operating system. In this
case, invoking a function from thread library would be a system call to the kernel. In the
section further, we would be discussing three kinds of thread libraries.
Thread Libraries in Operating System
1. PthreadsLibrary
2. Win32 Library
3. Java Library
Thread Library
A thread library provides the programmer with an Application program interface for creating
and managing thread.
Ways of implementing thread library
There are two primary ways of implementing thread library, which are as follows −
The first approach is to provide a library entirely in user space with kernel support. All
code and data structures for the library exist in a local function call in user space and not
in a system call.
The second approach is to implement a kernel level library supported directly by the
operating system. In this case the code and data structures for the library exist in kernel
space.
Invoking a function in the application program interface for the library typically results in a
system call to the kernel.
The main thread libraries which are used are given below −
POSIX threads − Pthreads, the threads extension of the POSIX standard, may be
provided as either a user level or a kernel level library.
WIN 32 thread − The windows thread library is a kernel level library available on
windows systems.
JAVA thread − The JAVA thread API allows threads to be created and managed directly
as JAVA programs.
The act of determining which process is in the ready state, and should be moved to
the running state is known as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to
deliver minimum response time for all programs. For achieving this, the scheduler must apply
appropriate rules for swapping processes IN and OUT of CPU.
Non Pre-emptive Scheduling: When the currently executing process gives up the
CPU voluntarily.
Pre-emptive Scheduling: When the operating system decides to favour another
process, pre-empting the currently executing process.
All processes, upon entering into the system, are stored in the Job Queue.
Processes in the Ready state are placed in the Ready Queue.
Processes waiting for a device to become available are placed in Device Queues.
There are unique device queues available for each I/O device.
A new process is initially put in the Ready queue. It waits in the ready queue until it is
selected for execution(or dispatched). Once the process is assigned to the CPU and is
executing, one of the following several events can occur:
The process could issue an I/O request, and then be placed in the I/O queue.
The process could create a new subprocess and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an interrupt, and
be put back in the ready queue.
In the first two cases, the process eventually switches from the waiting state to the ready
state, and is then put back in the ready queue. A process continues this cycle until it
terminates, at which time it is removed from all queues and has its PCB and resources
deallocated.
Types of Schedulers
There are three types of schedulers available:
Swapping may be necessary to improve the process mix, or because a change in memory
requirements has overcommitted available memory, requiring memory to be freed up. This
complete process is descripted in the below diagram:
1. Switching the CPU to another process requires saving the state of the old process
and loading the saved state for the new process. This task is known as a Context
Switch.
2. The context of a process is represented in the Process Control Block(PCB) of a
process; it includes the value of the CPU registers, the process state and memory-
management information. When a context switch occurs, the Kernel saves the context
of the old process in its PCB and loads the saved context of the new process
scheduled to run.
3. Context switch time is pure overhead, because the system does no useful work
while switching. Its speed varies from machine to machine, depending on the
memory speed, the number of registers that must be copied, and the existence of
special instructions(such as a single instruction to load or store all registers). Typical
speeds range from 1 to 1000 microseconds.
4. Context Switching has become such a performance bottleneck that programmers are
using new structures(threads) to avoid it whenever and wherever possible.
Operations on Process
Below we have discussed the two major operation Process Creation and Process
Termination.
Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other
processes. The process which creates other process, is termed the parent of the other process,
while the created sub-process is termed its child.
Each process is given an integer identifier, termed as process identifier, or PID. The parent
PID (PPID) is also stored for each process.
On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The
first thing done by it at system start-up time is to launch init, which gives that process PID 1.
Further Init launches all the system daemons and user logins, and becomes the ultimate
parent of all other processes.
A child process may receive some amount of shared resources with its parent depending on
system implementation. To prevent runaway children from consuming all of a certain system
resource, child processes may or may not be limited to a subset of the resources originally
allocated to the parent.
There are two options for the parent process after creating the child :
Wait for the child process to terminate before proceeding. Parent process makes
a wait() system call, for either a specific child process or for any particular child
process, which causes the parent process to block until the wait() returns. UNIX shells
normally wait for their children to complete before issuing a new prompt.
Run concurrently with the child, continuing to process without waiting. When a
UNIX shell runs a process as a background task, this is the operation seen. It is also
possible for the parent to run for a while, and then wait for the child later, which
might occur in a sort of a parallel processing operation.
There are also two possibilities in terms of the address space of the new process:
Typically, the execlp system call is used after the fork system call by one of the two
processes to replace the process memory space with a new program. The execlp system call
loads a binary file into memory - destroying the memory image of the program containing the
execlp system call – and starts its execution. In this manner the two processes are able to
communicate, and then to go their separate ways.
Process Termination
By making the exit(system call), typically returning an int, processes may request their own
termination. This int is passed along to the parent if it is doing a wait(), and is typically zero
on successful completion and some non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including :
CPU scheduling is a process that allows one process to use the CPU while the execution of
another process is on hold(in waiting state) due to unavailability of any resource like I/O etc,
thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient,
fast, and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in
the ready queue to be executed. The selection process is carried out by the short-term
scheduler (or CPU scheduler). The scheduler selects from among the processes in memory
that are ready to execute and allocates the CPU to one of them.
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program from where
it left last time.
The dispatcher should be as fast as possible, given that it is invoked during every process
switch. The time taken by the dispatcher to stop one process and start another process is
known as the Dispatch Latency. Dispatch Latency can be explained using the below figure:
2. When a process switches from the running state to the ready state (for example,
when an interrupt occurs).
3. When a process switches from the waiting state to the ready state(for example,
completion of I/O).
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling
scheme is non-preemptive; otherwise, the scheduling scheme is preemptive.
Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process, the process
keeps the CPU until it releases the CPU either by terminating or by switching to the waiting
state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh
operating systems.
It is the only method that can be used on certain hardware platforms because It does not
require the special hardware(for example a timer) needed for preemptive scheduling.
In non-preemptive scheduling, it does not interrupt a process running CPU in the middle of
the execution. Instead, it waits till the process completes its CPU burst time, and then after
that it can allocate the CPU to any other process.
Some Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF basically
non-preemptive) Scheduling and Priority (non- preemptive version) Scheduling, etc.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is
necessary to run a certain task that has a higher priority before another task although it is
running. Therefore, the running task is interrupted for some time and resumed later when the
priority task has finished its execution.
Thus this type of scheduling is used mainly when a process switches either from running state
to ready state or from waiting state to ready state. The resources (that is CPU cycles) are
mainly allocated to the process for a limited amount of time and then are taken away, and
after that, the process is again placed back in the ready queue in the case if that process still
has a CPU burst time remaining. That process stays in the ready queue until it gets the next
chance to execute.
Some Algorithms that are based on preemptive scheduling are Round Robin Scheduling
(RR), Shortest Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc.
CPU Scheduling: Scheduling Criteria
There are many different criteria to check when considering the "best" scheduling algorithm,
they are:
CPU Utilization
To make out the best use of the CPU and not to waste any CPU cycle, the CPU would be
working most of the time(Ideally 100% of the time). Considering a real system, CPU usage
should range from 40% (lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit of time or rather says the total amount
of work done in a unit of time. This may range from 10/second to 1/hour depending on the
specific processes.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from the time
of submission of the process to the time of completion of the process(Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been
waiting in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get
into the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is
produced. Remember, it is the time till the first response and not the completion of process
execution(final response).
In general CPU utilization and Throughput are maximized and other factors are reduced for
proper optimization.
Scheduling Algorithms
To decide which process to execute first and which process to execute last to achieve
maximum CPU utilization, computer scientists have defined some algorithms, they are:
2. Shortest-Job-First(SJF) Scheduling
3. Priority Scheduling