OS CH-1 and 2
OS CH-1 and 2
Introduction :
An operating system is a collection of software between the user and their computer hardware. It
controls and manages all of a computer's resources, including CPU (central processing unit)
usage, memory, input and output, and network connections. An operating system also provides a
UI (user interface) for users to interact with the computer and its programs. This includes the
desktop, shortcut icons, file explorers, menus, taskbars, and much more. The operating system
therefore takes care of interacting with the hardware.
It manages all the computer hardware. It provides the base for application program and acts as
an intermediary between a user and the computer hardware.
The operating system is very important part of almost every computer system.
Managing Hardware
1
1. Role of operating system to manage hardware components of a computer.
Operating system as we all know performs a vital role to hardware components in a computer
system. Without the operating system, hardware components cannot be driven by their software
because only operating system offers a platform for software to be installed and r un. In line with
this importance, Barnsley [2009] averred that an operating system provides the operating system
provides the environment within which programs are executed. Internally, operating systems
vary greatly in their make-up, since they are organized along many different lines.
Laban [2000] opined that operating system provides services to programs and to the users of
those programs.
3
Advantages of Distributed Operating System:
Failure of one will not affect the other network communication, as all systems are
independent from each other
Electronic mail increases the data exchange speed
Since resources are being shared, computation is highly fast and durable
Load on host computer reduces
These systems are easily scalable as many systems can be easily added to the network
Delay in data processing reduces
Disadvantages of Distributed Operating System:
Failure of the main network will stop the entire communication
To establish distributed systems the language which is used are not well defined yet
These types of systems are not readily available as they are very expensive. Not only that
the underlying software is highly complex and not understood well yet
Examples of Distributed Operating System are- LOCUS, etc.
Advantages of RTOS:
Maximum Consumption: Maximum utilization of devices and system, thus more output
from all the resources
Task Shifting: The time assigned for shifting tasks in these systems are very less. For
example, in older systems, it takes about 10 microseconds in shifting one task to another,
and in the latest systems, it takes 3 microseconds.
Focus on Application: Focus on running applications and less importance to applications
which are in the queue.
Real-time operating system in the embedded system: Since the size of programs are
small, RTOS can also be used in embedded systems like in transport and others.
Error Free: These types of systems are error-free.
Memory Allocation: Memory allocation is best managed in these types of systems.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their concentration is very less
on few applications to avoid errors.
Use heavy system resources: Sometimes the system resources are not so good and they
are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult for the designer to
write on.
Device driver and interrupt signals: It needs specific device drivers and interrupts signals
to respond earliest to interrupts.
Thread Priority: It is not good to set thread priority as these systems are very less prone
to switching tasks.
Examples of Real-Time Operating Systems are: Scientific experiments, medical imaging
systems, industrial control systems, weapon systems, robots, air traffic control systems, etc.
5
organizing the computing jobs in a manner that ensures that the CPU always has a job to execute
at any one time.
These operating systems are sometimes referred to as multitasking operating systems because
they allow two or more processes to run simultaneously. This is to mean that data from two or
more processes can be held in the primary memory at a given time.
There are mainly two types of multiprogramming operating systems. These are as follows:
Multitasking Operating System. Multiuser Operating System.
Example : Windows, Lunux, Unix etc
You can roughly divide them into five distinct generations that are characterized by hardware
component technology, software development, and mode of delivery of computer services.
0th Generation
The term 0th generation is used to refer to the period of development of computing, which
predated the commercial production and sale of computer equipment. You consider that the
period might be way back when Charles Babbage invented the Analytical Engine. Afterwards
the computers by John Atanasoff in 1940; the Mark I, built by Howard Aiken and a group of
IBM engineers at Harvard in 1944; the ENIAC, designed and constructed at the University of
Pencylvania by Wallace Eckert and John Mauchly and the EDVAC, developed in 1944-46 by
John Von Neumann, Arthur Burks, and Herman Goldstine (which was the first to fully
implement the idea of the stored program and serial execution of instructions) were designed.
The development of EDVAC set the stage for the evolution of commercial computing and
operating system software.
The hardware component technology of this period was electronic vacuum tubes.
The actual operation of these early computers took place without the benefi t of an operating
system. Early programs were written in machine language and each contained code for initiating
operation of the computer itself.
The mode of operation was called “open-shop” and this meant that users signed up for computer
time and when a user’s time arrived, the entire (in those days quite large) computer system was
turned over to the user. The individual user (programmer) was responsible for all machine set
6
up and operation, and subsequent clean-up and preparation for the next user. This system was
clearly inefficient and dependent on the varying competencies of the individual programmer as
operators.
“closed shop” and was characterised by the appearance of hired operators who would select the
job to be run, initial program load the system, run the user’s program, and then select another
job, and so forth. Programs began to be written in higher level, procedure-oriented languages,
and thus the operator’s routine expanded. The operator now selected a job, ran the translation
program to assemble or compile the source program, and combined the translated object
program
along with any existing library programs that the program might need for input to the linking
program, loaded and ran the composite linked program, and then handled the next job in a
similar fashion.
Application programs were run one at a time, and were translated with absolute computer
addresses that bound them to be loaded and run from these reassigned storage addresses set by
the translator, obtaining their data from specific physical I/O device. There was no provision for
moving a program to different location in storage for any reason. Similarly, a program bound to
specific devices could not be run at all if any of these devices were busy or broken.
The inefficiencies inherent in the above methods of operation led to the development of the
mono-programmed operating system, which eliminated some of the human intervention in
running job and provided programmers with a number of desirable functions. The OS consisted
of a permanently resident kernel in main storage, and a job scheduler and a number of utility
programs kept in secondary storage. User application programs were preceded by control or
specification cards (in those day, computer program were submitted on data cards) which
informed the OS of what system resources (software resources such as compilers and loaders;
and hardware resources such as tape drives and printer) were needed to run a particular
application.
The first generation saw the evolution from hands-on operation to closed shop operation to the
development of mono-programmed operating systems. At the same time, the development of
programming languages was moving away from the basic machine languages; first to assembly
language, and later to procedure oriented languages, the most significant being the development
of FORTRAN by John W. Backus in 1956. Several problems remained, however, the most
obvious was the inefficient use of system resources, which was most evident when the CPU
waited while the relatively slower, mechanical I/O devices were reading or writing program
7
data. In addition, system protection was a problem because the operating system kernel was not
protected from being overwritten by an erroneous application program.
In order to further mitigate the I/O wait problem, system were set up to spool the input batch
from slower I/O devices such as the card reader to the much higher speed tape drive and
similarly, the output from the higher speed tape to the slower printer. In this scenario, the user
submitted a job at a window, a batch of jobs was accumulated and spooled from cards to tape
“off line,” the tape was moved to the main computer, the jobs were run, and their output was
collected on another tape that later was taken to a satellite computer for off line tape-to-printer
output. User then picked up their output at the submission windows.
Program processing was, for the most part, provided by large centralized computers operated
under mono-programmed batch processing operating systems.
The most significant innovations addressed the problem of excessive central processor delay
due to waiting for input/output operations. Recall that programs were executed by processing
the machine instructions in a strictly sequential order. As a result, the CPU, with its high speed
electronic component, was often forced to wait for completion of I/O operations which involved
mechanical devices (card readers and tape drives) that were order of magnitude slower. This
problem led to the introduction of the data channel, an integral and special-purpose computer
with its own instruction set, registers, and control unit designed to process input/output
operations separately and asynchronously from the operation of the computer’s main CPU near
the end of the first generation, and its widespread adoption in the second generation.
The second generation was a period of intense operating system development. Also it was the
period for sequential batch processing. But the sequential processing of one job at a time
remained a significant limitation. Thus, there continued to be low CPU utilization for I/O bound
jobs and low I/O device utilization for CPU bound jobs. This was a major concern, since
computers were still very large (room-size) and expensive machines. Researchers began to
experiment with multiprogramming and multiprocessing in their computing services called the
time-sharing system.
8
in the form of spooling operating systems, such as the HASP (Houston Automatic Spooling)
system that accompanied the IBM OS/360 system. These systems worked by introducing two
new systems programs, a system reader to move input jobs from cards to disk, and a system
writer to move job output from disk to printer, tape, or cards. Operation of spooling system was,
as before, transparent to the computer user who perceived input as coming directly from the
cards and output going directly to the printer.
The idea of taking fuller advantage of the computer’s data channel I/O capabilities continued to
develop. That is, designers recognized that I/O needed only to be initiated by a CPU instruction
–
the actual I/O data transmission could take place under control of separate and asynchronously
operating channel program. Thus, by switching control of the CPU between the currently
executing user program, the system reader program, and the system writer program, it was
possible to keep the slower mechanical I/O device running and minimizes the amount of time
the CPU spent waiting for I/O completion. The net result was an increase in system throughput
and resource utilization, to the benefit of both user and providers of computer services.
The spooling operating system in fact had multiprogramming since more than one program
was resident in main storage at the same time. Later this basic idea of multiprogramming was
extended to include more than one active user program in memory at time. To accommodate
this extension, both the scheduler and the dispatcher were enhanced. The scheduler became able
to manage the diverse resource needs of the several concurrently active used programs, and
the dispatcher included policies for allocating processor resources among the competing user
programs. In addition, memory management became more sophisticated in order to assure that
the program code for each job or at least that part of the code being executed, was resident in
main storage.
The third generation was an exciting time, indeed, for the development of both computer
hardware and the accompanying operating system. During this period, the topic of operating
systems became, in reality, a major element of the discipline of computing.
9
and transfer information among machines that are connected to the network. The machines that
make up distributed system operate as a virtual single processor system from the user’s point of
view; a central operating system controls and makes transparent the location in the system of the
particular processor or processors and file systems that are handling any given program.
2. Memory management: Memory management module performs the task of allocation and
de-allocation of memory space to programs in need of this resources.
3. File management: It manages all the file-related activities such as organization storage,
retrieval, naming, sharing, and protection of files.
4. Device Management: Device management keeps tracks of all devices. This module also
responsible for this task is known as the I/O controller. It also performs the task of
allocation and de-allocation of the devices.
5. I/O System Management: One of the main objects of any OS is to hide the peculiarities
of that hardware devices from the user.
7. Security: Security module protects the data and information of a computer system against
malware threat and authorized access.
10
8. Command interpretation: This module is interpreting commands given by the and acting
system resources to process that commands.
10. Job accounting: Keeping track of time & resource used by various job and users.
11
Disadvantages of Operating System
If any issue occurs in OS, you may lose all the contents which have been stored in your
system
Operating system’s software is quite expensive for small size organization which adds
burden on them. Example Windows
Define Firmware: Firmware is one kind of programming Define Operating System: OS provides
that is embedded on a chip in the device which controls functionality over and above that which is
that specific device. provided by the firmware.
Architecture and Allow 32 bit of data processing Allow 64 bit of data processing
Software simultaneously simultaneously
12
Chapter 2
Processes and process management
A process is an instance of a program that is being executed. When we run a program, it does not
execute directly. It takes some time to follow all the steps required to execute the program, and
following these execution steps is known as a process.
A process can create other processes to perform multiple tasks at a time; the created processes
are known as clone or child process, and the main process is known as the parent process. Each
process contains its own memory space and does not share it with the other processes. It is
known as the active entity. A typical process remains in the below form in memory.
When we start executing the program, the processor begins to process it. It takes the following
steps:
o Firstly, the program is loaded into the computer's memory in binary code after
translation.
o A program requires memory and other OS resources to run it. The resources such that
registers, program counter, and a stack, and these resources are provided by the OS.
o A register can have an instruction, a storage address, or other data that is required by the
process.
o The program counter maintains the track of the program sequence.
o The stack has information on the active subroutines of a computer program.
o A program may have different instances of it, and each instance of the running program
is knowns as the individual process.
Features of Process
o Each time we create a process, we need to make a separate system call for each process
to the OS. The fork() function creates the process.
13
o Each process exists within its own address or memory space.
o Each process is independent and treated as an isolated process by the OS.
o Processes need IPC (Inter-process Communication) in order to communicate with each
other.
o A proper synchronization between processes is not required.
What is Thread?
A thread is the subset of a process and is also known as the lightweight process. A process can
have more than one thread, and these threads are managed independently by the scheduler. All
the threads within one process are interrelated to each other. Threads have some common
information, such as data segment, code segment, files, etc., that is shared to their peer threads.
But contains its own registers, stack, and counter.
o When a process starts, OS assigns the memory and resources to it. Each thread within a
process shares the memory and resources of that process only.
o Threads are mainly used to improve the processing of an application. In reality, only a
single thread is executed at a time, but due to fast context switching between threads
gives an illusion that threads are running parallelly.
o If a single thread executes in a process, it is known as a single-threaded And if multiple
threads execute simultaneously, then it is known as multithreading.
14
Types of Threads
2. Kernel-Level Thread
The kernel-level threads are handled by the Operating system and managed by its kernel. These
threads are slower than user-level threads because context information is managed by the kernel.
To create and implement a kernel-level thread, we need to make a system call.
Features of Thread
o Threads share data, memory, resources, files, etc., with their peer threads within a
process.
o One system call is capable of creating more than one thread.
o Each thread has its own stack and register.
o Threads can directly communicate with each other as they share the same address space.
o Threads need to be synchronized in order to avoid unexpected scenarios.
15
A process is an instance of a program Thread is a segment of a process or a
that is being executed or processed. lightweight process that is managed by the
scheduler independently.
Processes are independent of each Threads are interdependent and share memory.
other and hence don't share a memory
or other resources.
Each process is treated as a new The operating system takes all the user-level
process by the operating system. threads as a single process.
If one process gets blocked by the If any user-level thread gets blocked, all of its
operating system, then the other peer threads also get blocked because OS takes
process can continue the execution. all of them as a single process.
Context switching between two Context switching between the threads is fast
processes takes much time as they are because they are very lightweight.
heavy compared to thread.
The data segment and code segment Threads share data segment and code segment
of each process are independent of the with their peer threads; hence are the same for
other. other threads also.
The operating system takes more time Threads can be terminated in very little time.
to terminate a process.
New process creation is more time A thread needs less time for creation.
taking as each new process takes all
the resources.
16
The main drawback of single threading systems is that only one task can be performed at a time,
so to overcome the drawback of this single threading, there is multithreading that allows
multiple tasks to be performed.
For example:
In the above example, client1, client2, and client3 are accessing the web server without any
waiting. In multithreading, several tasks can run at the same time.
In an operating system, threads are divided into the user-level thread and the Kernel-level thread.
User-level threads handled independent form above the kernel and thereby managed without any
kernel support. On the opposite hand, the operating system directly manages the kernel-level
threads. Nevertheless, there must be a form of relationship between user-level and kernel-level
threads.
There exists three established multithreading models classifying these relationships are:
Many to one multithreading model
One to one multithreading model
Many to Many multithreading models
Many to one multithreading model:
The many to one model maps many user levels threads to one kernel thread. This type of
relationship facilitates an effective context-switching environment, easily implemented even on
the simple kernel with no thread support.
The disadvantage of this model is that since there is only one kernel-level thread schedule at any
given time, this model cannot take advantage of the hardware acceleration offered by
multithreaded processes or multi-processor systems. In this, all the thread management is done
in the userspace. If blocking comes, this model blocks the whole system.
17
In the above figure, the many to one model associates all user-level threads to single kernel-level
threads.
One to one multithreading model
The one-to-one model maps a single user-level thread to a single kernel-level thread. This type
of relationship facilitates the running of multiple threads in parallel. However, this benefit comes
with its drawback. The generation of every new user thread must include creating a
corresponding kernel thread causing an overhead, which can hinder the performance of the
parent process. Windows series and Linux operating systems try to tackle this problem by
limiting the growth of the thread count.
In the above figure, one model associates that one user-level thread to a single kernel-level
thread.
Many to Many Model multithreading model
In this type of model, there are several user-level threads and several kernel-level threads. The
number of kernel threads created depends upon a particular application. The developer can
create as many threads at both levels but may not be the same. The many to many model is a
compromise between the other two models. In this model, if any thread makes a blocking system
call, the kernel can schedule another thread for execution. Also, with the introduction of multiple
threads, complexity is not present as in the previous models. Though this model allows the
creation of multiple kernel threads, true concurrency cannot be achieved by this model. This is
because the kernel can schedule only one process at a time.
18
Many to many versions of the multithreading model associate several user-level threads to the
same or much less variety of kernel-level threads in the above figure.
Inter-Process Communication.
In general, Inter Process Communication is a type of mechanism usually provided by the
operating system (or OS). The main aim or goal of this mechanism is to provide
communications in between several processes. In short, the intercommunication allows a process
letting another process know that some event has occurred.
Let us now look at the general definition of inter-process communication, which will explain the
same thing that we have discussed above.
Definition
"Inter-process communication is used for exchanging useful information between numerous
threads in one or more processes (or programs)."
To understand inter process communication, you can consider the following given diagram that
illustrates the importance of inter-process communication.
19
Race Condition
A condition in which the critical section (a part of the program where shared memory is
accessed) is concurrently executed by two or more threads. It leads to incorrect behavior of a
program.
In layman terms, a race condition can be defined as, a condition in which two or more threads
compete together to get certain shared resources.
For example, if thread A is reading data from the linked list and another thread B is trying to
delete the same data. This process leads to a race condition that may result in run time error.
1. Read-modify-write
2. Check-then-act
The read-modify-write patterns signify that more than one thread first read the variable, then
alter the given value and write it back to that variable. Let's have a look at the following code
snippet.
t occurs when two or more threads operate on the same object without proper synchronization
and their operation incorporates each other.
Suppose, there are two processes A and B that are executing on different processors. Both
processes are trying to call the function bankAccount() concurrently. The value of the shared
variable that we are going to pass in the function is 1000.
Consider, A call the function bankAccount() and passing a value 200 as a parameter. In the same
way, process B is also calling the function bankAccount() and passing a value of 100 as a
parameter.
Check-then-act.
20
This race condition happens when two processes check a value on which they will take each
take an external action. The processes both check the value, but only one process can take
the value with it. The later-occurring process will read the value as null. This results in a
potentially out-of-date or unavailable observation being used to determine what the program
will do next. For example, if a map application runs two processes simultaneously that
require the same location data, one will take the value first so the other can't use it. The later
process reads the data as null.
o Mutual exclusion
o Synchronize the process
Another solution to avoid race condition is mutual exclusion. In mutual exclusion, if a thread is
using a shared variable or thread, another thread will exclude itself from doing the same thing.
In order to prevent the race conditions, one should ensure that only one process can access the
shared data at a time. It is the main reason why we need to synchronize the processes.Here, a
given part of the program can only execute one thread at a time.
A critical section, CS, is a section of code in which a process accesses shared resources.
A section of code, or a collection of operations, in which only one process may be executing at a given
time and which we want to make “sort of” atomic. Atomic means either an operation happens in its
entirely (everything happens at once) or NOT at all; i.e., it cannot be interrupted in the middle. Atomic
operations are used to ensure that cooperating processes execute correctly. Mutual exclusion
mechanisms are used to solve the critical region problem .
Scheduling queues: As processes enter the system, they are put job queue. This
into a queue
21
consists of all process in the system. The process that are residing in main memory and are
ready &
This queue is generally stored as a linked list. A ready queue header contains pointers to the
first & final PCB in the list. The PCB includes a pointer field that points to the next PCB
in the ready queue. The lists of processes waiting for a particular I/O device are kept on a
list called device queue. Each device has its own device queue. A new process is initially
put in the ready queue. It
waits in the ready queue until it is selected for execution & is given the CPU.
SCHEDULERS:
A process migrates between the various scheduling queues throughout its life-time purposes.
The OS must select for scheduling processes from these queues in some fashion. This
selection process is carried out by the appropriate scheduler. In a batch system, more processes
are submitted and then executed immediately. So these processes are spooled to a mass storage
device like disk, where they are kept for later execution.
Types of schedulers:
1. Long term scheduler: Long term scheduler selects process from the disk & loads them
into memory for execution. It controls the degree of multi-programming i.e. no. of processes in
memory. It executes less frequently than other schedulers. If the degree of
multiprogramming is stable than the average rate of process creation is equal to the average
departure rate of processes leaving the system. So, the long term scheduler is needed to be
invoked only when a process leaves the system. Due to longer intervals between executions it
can afford to take more time to decide which process should be selected for execution. Most
processes in the CPU are either I/O bound or CPU bound. An I/O bound process (an
interactive ‘C’ program is one that spends most of its time in I/O operation than it spends
in doing I/O operation. A CPU bound process is one that spends more of its time in doing
computations than I/O operations (complex sorting program). It is important that the
long term scheduler should select a good mix of I/O bound & CPU bound processes.
2. Short - term scheduler: The short term scheduler selects among the process that are
ready to execute & allocates the CPU to one of them. The primary distinction between these
two schedulers is the frequency of their execution. The short-term scheduler must select a new
process for the CPU quite frequently. It must execute at least one in 100ms. Due to the
short duration of time between executions, it must be very fast.
Process CPU
P1 time
3
P2 5
P3 2
P4 4
Using FCFS algorithm find the average waiting time and average turnaround time if the order
is
P1 P2 P3 P4
0 3 8 10 14
The FCFS algorithm is non pre-emptive means once the CPU has been allocated to a
process then the process keeps the CPU until the release the CPU either by terminating or
requesting I/O.
P1 3
P1 1 3
0
P2 1
1
P3 3
2.. Shortest Job First Scheduling (SJF) Algorithm: This algorithm associates with each
process if the CPU is available. This scheduling is also known as shortest next CPU burst,
because the scheduling is done by examining the length of the next CPU burst of the process
rather than its total length. Consider the following example:
Process CPU time
P3 P1 P2 P4
0 2 5 9 14
The waiting time for process P1 = 0, P2 = 2, P3 = 5, P4 = 9 then the turnaround time for
process
The SJF algorithm may be either pre-emptive or non pre-emptive algorithm. The pre-emptive
SJF
is also known as shortest remaining time first. Consider the following example.
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 1 4
0
P2 1
1
P3 3
P1 = 10 - 1 = 9
P2 = 1 – 1 = 0
P3 = 17 – 2 = 15
P4 = 5 – 3 = 2
P1 1 5
0
P2 1
1
P3 3
P4 1 4
P5 5 2
According to the priority scheduling the Gantt chart will be
P2 P5 P1 P3 P4
0 1 6 16 18 19
P1 = 6
P2 = 0
P3 = 16
P4 = 18
P4 = 1
4. Round Robin Scheduling Algorithm: This type of algorithm is designed only for the time sharing system.
It is similar to FCFS scheduling with pre-emption condition to switch between processes. A small unit of time
called quantum time or time slice is used to switch between the processes. The average waiting time under the
round robin policy is quiet long. Consider the
following example:
P1 P2 P3 P4 P1 P2 P3 P4 P1 P2 P4 P2 P4 P2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
P1 = 0 + (4 – 1) + (8 – 5) = 0 + 3 + 3 = 6
P2 = 1 + (5 – 2) + (9 – 6) + (11 – 10) + (12 – 11) + (13 – 12) = 1 + 3 + 3 + 1 + 1 + 1 = 10
P3 = 2 + (6 – 3) = 2 + 3 = 5
Process Synchronization:
A co-operation process is one that can affect or be affected by other processes executing in the
system. Co-operating process may either directly share a logical address space or be allotted to
the shared data only through files. This concurrent access is known as Process synchronization.
Critical Section Problem:
Consider a system consisting of n processes (P0, P1, ………Pn -1) each process has a segment
of code which is known as critical section in which the process may be changing common
variable, updating a table, writing a file and so on. The important feature of the system is that
when the process is executing in its critical section no other process is to be allowed to execute
in its critical section. The execution of critical sections by the processes is a mutually exclusive.
The critical section problem is
to design a protocol that the process can use to cooperate each process must request permission
to enter its critical section. The section of code implementing this request is the entry section.
The critical section is followed on exit section. The remaining code is the remainder section.
1. Mutual Exclusion: If process Pi is executing in its critical section then no any other
process can be executing in their critical section.
2. Progress: If no process is executing in its critical section and some process wish to enter
their critical sections then only those process that are not executing in their remainder section
can enter its critical section next.
3. Bounded waiting: There exists a bound on the number of times that other processes are
allowed to enter their critical sections after a process has made a request.
Semaphores:
Deadlock Avoidance
Avoid deadlock by careful resource scheduling.
Requires that the system has some additional prior information available.
Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need.