Operating System Fundamentals: by Dr. Umar Khokhar and Dr. Binh Tran Georgia Gwinnett College, Lawrenceville (GA)
Operating System Fundamentals: by Dr. Umar Khokhar and Dr. Binh Tran Georgia Gwinnett College, Lawrenceville (GA)
Operating System Fundamentals: by Dr. Umar Khokhar and Dr. Binh Tran Georgia Gwinnett College, Lawrenceville (GA)
Learning Outcomes:
Before we define the term Operating system (OS), we need to define and classify the term software:
1.1.1 Software
The piece of code/program or set of instructions to execute a specific task is called software. The
software can be of two types: System Software and Application Software
System Software: are designed to manage the systems and their activities e.g. Operating System
and Utility Programs etc.
Application Software: allow the users to interact with the computational systems or system
software e.g. Microsoft office suite, Adobe products and video players etc. The application
software can be further categorized as web application software (cloud based) and desktop
application software.
Let us understand, how the operating system acts as intermediary between the hardware and the users.
Now the first question is, can we access hardware directly without OS or not? The answer is yes but it
would be extremely difficult and inefficient, one has to communicate with computer in machine
language to control the complex computational logic. Another example to better understand the role
of OS as intermediary is described as follows:
If you want to turn on car Air Conditioner (AC) then can you turn on AC without pressing the AC
button? The answer is yes but it would be inconvenient, one has to open the hood of the car and
manually turn that machine on. The AC button (on the dashboard of car) acts as interface through
which one can access that machine. Similarly, the OS acts as interface between the user and the
hardware (CPU, RAM, Hard drive etc.).
OS as Resource Manager: Hardware has many resources e.g. Switches, transistors, CPU, Buses,
Memory, Fans etc. If the user wants to directly use these resources (bypassing the OS) then it will be
extremely difficult to manage and keep a balance between the devices as well (You need to
continuously change the resources requirements e.g. fan speed, memory, CPU utilization etc.
depending upon the process).
Example: If you want to watch a movie on your Personal Computer (PC) without having an OS and
movie player software using machine language. Then you have to continuously manage the resources
e.g. fan rotation, screen resolution, changes of colors, memory requirements and CPU utilization etc.
which humanly impossible to manage.
OS as Platform: Since, each application program has its own resources requirement e.g. if we run
ten (10) application programs simultaneously then it might create clash of resources for hardware
where applications will fight for resources (if the application programs have to direct access to
hardware). That is why we install applications on OS which manages the resources and creates a
balance between the applications.
Tier 0: Lowest level, where we have the computer hardware e.g. CPU, RAM, ROM, Hard drive, I/O
devices and buses etc.
Tier 1: Operating system
Tier 2: System and Application Programs are located on this tier e.g. Compliers, Assemblers, Device
Drivers, Antiviruses and other application programs etc.
Tier 3: Users are located on this tier and if the users wants to get anything done on hardware then
they will access it through the application software and OS. For example, if a user wants to watch a
movie then the user will go to VLC player (or any other movie player software) and watch the video.
The VLC player forwards the instructions (which tell about the resources requirement) to OS and OS
will further execute them and allows the access to hardware.
Tier
System and Application Programs
2
Tier
1
Operating System
Tier
CPU R AM Hardware 0
Before we explore the computer system organization and operation, let us discuss some OS related
key terminologies.
1.2.1 Kernel
The Kernel is the core component of the OS which helps the users to interact with the hardware. The
kernel controls the communication between user and the hardware. If an application software wants to
access any hardware or I/O device then it will first request to kernel and kernel will check the
resources and allocate the resources or turn down the request.
If an application software wants to access any hardware e.g. CPU, RAM or ROM etc. then it will
request to kernel that request is called system call.
1.2.3 Bit
1.2.4 Bytes
1.2.5 Word
1.2.6 Registers
CPU has two internal parts Control Unit (CU) and Arithmetic Logic Unit (ALU). The CU fetches the
instructions from RAM and the ALU firstly executes it and then forward the output to Main Memory
(MM) or I/O device.
• The CPU and the DC are connected through common bus which provides the access to shared
memory. Each DC is in charge of a specific type of device e.g. disk drivers, audio and video I/O
devices etc. The CPU and the DC both can execute in parallel therefore there could be a clash of
memory access which can be resolved by memory management controllers. Since, the CPU is
much faster than I/O devices therefore to avoid the CPU idle state the DC uses a local buffer
which manages the operations in the following fashion:
a) The DC takes the data/process information from the I/O devices and then stores it in the
local buffer.
b) When the data gets completed then the DC sends an interrupt to CPU so, CPU can dis-
engage itself with the on-going tasks.
c) CPU then moves the data/Instruction from the local buffer to MM and then executes it.
CPU
Main
I/O
Memory
Device
Device Buffer
Controller (Local)
The primary goal of general-purpose OS is convenience (user friendly) while the efficiency is
considered as a secondary goal. However, it is other way around for Network OS or any other server-
based computing environment.
There are six (6) main functions of a general-purpose OS:
• Process Management
• Memory Management
• I/O Device Management
• File System Management
• Network Management
• Security and Protection
A program/software does nothing unless its instructions are executed by a CPU so, a program in
execution is called process. A process needs certain resources to accomplish its tasks e.g. CPU, RAM,
I/O devices and these resources are allocated to the process while it is being executed. The OS
reclaimed all resources when the process gets terminated or completed. So, the program itself is not a
process but a passive entity whereas the process is an active entity (you cannot carry a process on
storage media but can store a program). A process can perform multiple tasks simultaneously using
either the parent-child model or threading. The detailed description of both of these models are
discussed in Chapter 3. The single threaded processes have one Program Counter (PC) which tells the
complier about the next instruction to be executed, however the multithreaded processes have one
PC/thread. The OS does the following to manage a process:
• Execute
• Create and Terminate
• Abort
To execute a program all of the instructions and the data must be loaded onto RAM which further
provides instructions to CPU to execute a process. The Memory management activities are:
• Allocating and de-allocating Memory
• Assigning priority to processes
• Keeping a log/track of memory utilization
Logical storage unit is called “File” which is mapped onto the physical media. To store a data onto
the physical media, the data must be in the form of a file. For managing storage:
• File System Management
• Mass Storage Management
• Caching
The most visible component of an OS is the file which is the collection of related information defined
by its creators and organized in directories.
If the multiple users have access to the files then it is important to define access controls of the file.
The OS manages the file systems using filing methods e.g. FAT, NTFS and XFS etc. These filing
methods defines the file creation, deletion, location and back up methods of various file formats.
As we know, the RAM (Random Access Memory) is too small to accommodate all of the data and the
programs and moreover volatile, therefore, we use secondary storage to back up the main memory.
Besides the user’s data, compilers, assemblers, word processors and formatters are stored on disk
until located onto the RAM. The mass storage mainly manages the free space and storage allocation.
1.3.3.3 Caching
The cache is the small memory which mainly assists the operations of the RAM and unlike RAM it is
directly located onto the CPU. The cache holds the temporary data/instructions on it and saves the
time to fetch the instructions over and over from the RAM. The detailed description and the working
of the cache is discussed in chapter 7.
1.3.3.4 I/O subsystem
The I/O subsystem manages the memory and the device drivers optimally. The I/O subsystem also
manages the resources to facilitate the I/O subsystems.
1.3.4 Protection and Security
The protection is the mechanism for controlling access of processes/users to the resources (e.g. which
process can access which resources and how much it can access etc.). The security module acts as
defense of the system against internal and external attacks.
The Protection and the security module can distinguish among the users based on the user ID,
passwords and can define the access rights for each user of the systems.
Different devices have different computational requirements and the power constraints, which is why
we need different operating systems for various types of computing environments. The summary of
different computing environments is presented in Table 1.1.
Learning Outcomes:
OS provides many services for the execution of the programs. Although, these services vary from OS
to OS but there are few mandatory/common services which an OS should offer. The detailed
description of these services is presented as follows:
2.1.5 Communications
Communication protocols and modules are also required which let the processes to communicate with
other processes or computers with other computers on the network.
Many errors can occur in CPU & memory or even in I/O devices (Power Failure, Memory error, lack
of papers in printers etc.). Therefore, an error detection and correction module are an integral service
of an OS.
When we run any application, we are in the user mode but as you know all the resources can be
controlled or accessed through Kernel. So, when a user program needs to access hardware (CPU,
RAM, Hard Disk or I/O devices etc.), it makes a system call and go to the Kernel mode.
Example: When we open an Excel sheet, we are in the user mode, once you update some data on the
sheet and want to save this change on Hard disk, then the application (MS-Excel) will make a system
call to kernel, then we will enter in Kernel mode and the result will be stored on the hard disk. After
getting the task, you will return to user mode. Similarly, if you want to print a number or data on
monitor (e.g. In Java scripting, system.out () )then this request will go to Kernel and through kernel,
the user will access the monitor and the result will be displayed on monitor.
Since, a user needs to access many resources, the types of system calls depend on the use of these
resources. There are six main categories of system calls:
a. Process Control
b. File Manipulation
c. Device Manipulation
d. Information Maintenance
e. Communication
f. Protection
In a general-purpose OS, there usually more than different hundred (500) system calls e.g. Windows-
10 has more than two thousand (200) different system calls.
a) Process Control: The Process control system calls mainly manages the processes e.g.
process creation, termination, loading, aborted and deleted etc. some of the process
related system calls are:
fork( ): Create a child process
exit( ): Terminate a process
Kill ( ): Terminate a process abnormally
Nice ( ): Increase a priority of a process
b) File Management: The file management system calls are used to create, delete, open,
close, read and write the file. Some of the file management system calls:
create( ): To create a file
open( ): To open the file
Write ( ): Write filr
mkdir ( ): Create a new directory
c) I/O devices Management: The I/O devices management system calls are used to control
the devices and allows the users to read and write the data from devices.
d) Information: If we need to get information about the devices or processes then we use
information related system calls e.g.
Getpid( ): To find the id of the process (metadata of the file size, permission and types
etc.)
Set timer( ): Set time and data on the computer
e) Communication: The communication related system calls are used to create and delete
the communication and forward/receive messages.
f) Protection: Provides the mechanism for controlling access to the resources. Some of the
protection related system calls are as follows:
Set_permission ( )
Get_permission ( )
Allow_ user ( )
Deny_user ( )
So far, we have discussed that we need system calls for the implementation and the working of the
OS (services of the OS). The system programs are basically utility programs which help/provide the
interface to user to create a system call. The system programs are called system utilities.
System
Users System
calls
Program
System programs are divided into six categories. The detailed description of these system programs
categories is presented as follows:
a) File Management: Create, delete, copy, rename and prints.
b) Status Information: Sometimes, we need to find information about the file size, time (when the
file was created, Hard drive space etc.) then we use status information program.
c) File Modification: Use text editors to:
1. Create and modify the files
2. File searching (Bar to search the file)
d) High-level language support: OS provides small system programs such as compilers,
assemblers, debuggers and interpreters which converts high-level programming languages into
low-level programming so, hardware can understand the instructions.
e) Program Execution: OS also offers system programs like linkers and loaders which loads the
program instructions onto RAM and then forwards to CPU.
f) Communication: The communication system programs enable the TCP/IP protocol and allows
the sockets to communication with other computers over the network and internet.
2.5 OS Design and Implementation
To design and implement an OS, there are mainly three (3) steps:
• Design Goals
• Mechanism and Policies
• Implementation
Design Goals: First problem in designing an OS is to finalize the design goals and specifications.
There are two types of design goals:
a) User Goals: Goals of the users e.g. user convenience, secure & fast and easy to learn
b) System Goals: Goals of the system developers e.g. reliable, flexible, easy to design and
upgradable etc.
Polices: What to do (What OS will be doing)?
Mechanism: How to get it done (Development of Algorithms and Mathematical Modeling)?
Implementation: In the earlier OS, the assembly and system programming language were most
commonly used programming language to design an OS. Then high-level programming language
“Basic” have been used for long for OS designing and implementation. Now a days, we use a mixture
of the programming languages for implementation of OS e.g.
o Lowest level (CPU and other hardware components/ interface) Assembly
o Main Body High level Programming languages e.g. C/C++/Basic/Java
o System calls High level Programming languages
The high-level programming languages are easier to configure but do slower execution.
2.6 OS Structures
The general-purpose OS is a very large program e.g. Windows 11 have roughly 50 Million lines of
code. A proper structure of the OS would be extremely helpful in designing and organizing the
various functions of the OS. We have discussed the five (5) OS structures:
1) Simple
2) Layered
3) Microkernel
4) Modules
5) Hybrids
2.6.1 Simple
In earlier computers, there was just one CPU, small RAM and few I/O devices. Therefore, a simple
structure also known as Monolithic can be used to design an OS which can optimally manage the OS
functionalities. In Monolithic structure, all the components of OS e.g. Process management, Memory
Management, device management and File management etc. are housed under one single unit.
There is no concept of the modules so, if the CPU wants to fetch any data (for any process of
program) then it will be referred to the main program of the OS to pick up the data. The figure 2.2
presents the monolithic OS structure.
One of the biggest advantages of the monolithic structure over other structures is fast execution of the
requests. However, since the user’s programs and the OS files are co-located therefore it is more
prone to errors and viruses, since the user’s programs can bring errors inside the Kernel space.
Moreover, the upgradation of the OS is also a cumbersome process since, the whole body/code will
need to be replaced/updated. One of the examples of the simple OS structure is MS-DOS.
User
Mode
Kernel OS functionalities
Mode DOS Kernel
(Process management, Memory management etc.)
Hardware
(CPU, RAM, I/O, ROM etc.)
Kernel Mode
API/ System Call Interface
Layer N
Hardware
(CPU, RAM, I/O, ROM etc.)
2.6.4 Modules
Just like layers, the OS can be designed using loadable kernel modules. These modules are
independently coded but offers more flexibility than layers.
Unlike the layers, the modules can communicate to other modules as well. The Linux and Solaris use
the modules structure of the OS.
2.6.5 Hybrids
Most of the Modern OSs use the hybrid structures (use more than two models) e.g.
• Linux & Solaris [Monolithic + Modules]
• Windows [Monolithic + Microkernel]
• MAC [Microkernel + Layered]
User
Mode
IPC
Kernel
Mode
Hardware
(CPU, RAM, I/O, ROM etc.)
In this section, we will be discussing structures of three (3) modern OSs; MAC, IOS and Android.
The detailed description is presented as follows.
2.7.1 MAC OS
Just like any other OS, the MAC OS is divided into two parts; User address space and Kernel. In the
User address space, the top most layer offers the framework of GUI (AQUA) and then MAC uses
Cocoa API (an Object-oriented framework) for interaction wit kernel. The Kernel environment
involves the BSD (Berkley Software Distribution) and the MACH. The MACH provides the memory
management and IPC etc. while the CLI (Command Line Interface) & Networking routines uses BSD
framework. The figure 2.5 presents the MAC OS structure.
Top Layer
AQUA (GUI)
Cocoa (API)
Kernel Environment
BSD MACH
Cocoa Touch
(API)
Media
Core Services
Core OS
2.7.3 Android
The android structure is quite similar to IOS (layered stack) where at the bottom of the stack, it uses
the Linux Kernel (customized by Google), which manages the process, memory and devices etc.
Google has made a special API for application development where the Java files are first compiled to
byte code and then executable.
Chapter 3
Processes
Learning Outcomes:
Process
Terminated
New
Dispatch to CPU
Ready Running
Waiting
ID: The identity of the process, so the process can be distinguished (e.g. P1, P2 etc.)
State: In which state the process is currently located (e.g. New, Running etc.)
Pointer: The connections of the process (for which we are designing this PCB) e.g. it is connected
with which child or parent process.
Priority: Let say, we have five processes (P1, P2 …P5) then what will be the priority of the process.
Program Counter: Points to the next instruction.
CPU Register: Holds the information about the registers which will be used by the process.
I/O Information: Devices required/used by the process.
Process ID
State
Pointer
Priority
Program Counter
CPU Register
I/O Information
3.1.4 Thread
Thread is a lightweight process which is the basic unit of the CPU execution. The process can have
many threads which execute together and offer multiprocessing environment. Each thread has its own
ID, stack and the set of the registers. The threads are mainly of two types; User Level Thread (ULT)
and Kernel Level Thread (KLT). We will discuss the threads in detail in chapter 4. The ULTs cannot
make system calls and if they need to access any resource then it requests through the process.
Each thread has its own code, data and set of registers which they don’t share with other registers.
MTS
LLS LLS
Pi
Blocked Queue
Figure 3.5: Process Scheduling
The process scheduling steps are presented as follows:
1) The High-Level Scheduler (HLS) is responsible for admitting new processes into the ready
queue. The HLS is also termed as Admission Scheduler or Long-Term Scheduler.
2) The Low-Level Scheduler (LLS) moves the processes from ready queue (if CPU becomes
available) to running state (CPU). It allows the other processes to advance (moves forward) in the
ready queue, so, mew processes will be admitted to the system. The processes can be of two
types; CPU bound and I/O bound. The CPU bound processes (once enter in the running state)
execute and then get terminated. The I/O bound processes (once enter in the running state) moves
to blocked queue (waiting state) and joins the blocked queue at the end. The process waits for I/O
event to be occurred and once the I/O event occurs then LLS moves the process back to ready
queue and then LLS moves it to running state.
3) Each process gets same time slice and stays in the running state for 10-100ms. If the process
didn’t get complete during that time slice then its state will be stored in the memory and LLS will
move it out and put it back to the ready queue. When the incomplete process turn comes then the
CPU restores its original state. The restoration of the previous state of the process is called
context switching (context switching takes 10 microseconds).
1) Process Creation: When we run a program (execute) then it becomes a process (process is
created). This main process is called parent process, which can create child processes. Each child
has its own process ID and usually parent and children can share resources and can execute
simultaneously. When the parent creates child then that child creates more children and
eventually form a tree of processes. To create a child, parent use fork ( ) system call and the child
will be the clone of the parent but have its own process ID and PCB. The detailed description of
the process creation is presented as follows:
P1
fork ()
C1 P1
fork ()
C1 C2 P1 C3
fork ()
C1 C5 C6 P1 C7 P1
C4 C2
Figure 3.6: Process Creation Tree
Process
Process
Completed
Child 1 Child 2
Kernel
exit ( )
Kernel
Unbounded
Direct Blocking Non-Blocking Zero Capacityy
Buffer
Bounded
Indirect Send Send
Capaacity
Unbounded
Receive Receive
Capacity
b) Bounded Buffer: as name suggests, the producer contains limited number of buffer and if the
buffer gets full then producer will wait (stop producing the information) unless the consumer
consumed it. (e.g. if we have three (3) buffers and all of them get full then producer will not
produce).
ii. Indirect Communication: Firstly, the OS creates a Mailbox (M) then the process
send/receive messages and finally delete the Mailbox (M).
In the indirect communication model, there might be a situation where two or more
processes might try to access the same Mailbox and can create a deadlock situation. This
deadlock can be avoided by using multiple Mailboxes or using OS acknowledgment
systems.
3.4.3 Synchronization
Message passing can also be performed using synchronization which can be implemented either
blocking or non-blocking model. The detailed description is presented as follows:
a) Blocking: The blocking method is synchronous (TCP) and can be implemented using blocking
send or blocking receive.
Blocking Send: the sender will be blocked until the receiver receives the message (means the sender
will not send the next message unless the receiver receives the previous messages).
Blocking Receive: The receiver will be blocked until a message is available (will not receive the
other message).
b) Non-Blocking: The non-blocking method is asynchronous (UDP) and can also be implemented
using non- blocking send or non-blocking receive.
Non-blocking Send: The Sender will send messages and continue to its next sending process
(regardless of acknowledgement from the receiver).
Non-blocking Receive: The receiver will receive either a message or null from the sender and will
continue whether it has received or not.
3.4.4 Buffering
In buffering, the communication between the two (2) processes takes place through a temporary queue.
The message stores in a queue (when the sender sends the message) and then receiver picks the message
from the queue. To implement the queue, we can use the any one of the three (3) methods:
a) Zero Capacity: The queue has no storage capacity, when the sender sends the message unless the
receiver receives/picks the message, the sender cannot send another message.
b) Bounded Capacity: The buffer has limited queue of the messages and if the limit gets full and if
the receiver is not picking up the messages then the sender will wait unless the buffer becomes
available.
c) Unbounded Capacity: Unlimited number of buffers.
• Sockets
• Remote Procedural Calls (RPC)
• Pipelining
3.5.1 Sockets
The socket is the endpoint of communication between the two (2) devices e.g. two (2) people (having
mobile phones) want to communicate with each other. Then one of them initiates the communication
(using mobile phone), the mobile phone acts as a socket (endpoint of communication).
The concatenation of the IP address and the port number is called socket. It defines the service and
the IP address of the host system. For example, 192.168.2.1:80 (socket) tells the host name
(192.168.2.1) and the service port (80, http). So, when two (2) computers communicate then in fact
they communicate to the sockets (ports).
In client-server model, the client requests for the services from the server. If the server is located
outside the LAN then the server is called remote server. For example, if you want to get your clothes
dry-cleaned then you need to call dry cleaner, the dry cleaner gets the service done and return you
back. The server (dry-cleaner) gets the request done and returns the results to the client. In RPC, there
are five (5) components:
a) Client: The user who requests for the services
b) Client Stub: The client manager which encapsulates and de-encapsulates the messages.
c) RPC Runtime: Transport layer (transportation time)
d) Server Stub: Encapsulates and de-encapsulates the messages.
e) Server: Execute the request
1 10 5 6
2 9 4 7
Network 3
Network
Routines Internet Routines
(TCP/IP) 8
(TCP/IP)
3.5.3 Pipelining
Usually, the OS performs the sequential execution on the processes. Let us consider the following
example to better understand the prominence of pipeline.
We have auto manufacturing unit, where we have twenty-four (24) people work together to build a car.
Assume that, there are only three (3) step process to build a car; Engine, Fitting and Paint.
In the sequential execution, all the twenty-four (24) people (altogether) do the engine job first, then fit
the engine and finally paint the car. After finishing the one car, then they will work on the second car.
By using the sequential execution, only one (1) car can be built in one day. In order to improve the
efficiency of work, we divide the people into three (3) group (G1, G2 and G3) of eight (8) employees.
Now, first group (G1) builds the engine of car 1, handover it to fitting group (G2) and then start the
engine job on car 2. Once, the G2 people fit the engine of car 1, they will move on to the car 2 and
handover the car 1 to G3 (paint people). This mechanism is called the pipeline where we can improve
the productivity and optimizes the hardware.
In computer (CPU) performs four actions on the processes:
• Fetch the instruction
• Decode the instruction
• Operand and variables
• Execute
The OS uses pipeline and divides the resources to perform these operations.
The pipelines can be of two types:
Ordinary Pipes: Pipes created by the parent process (cannot access outside the process).
Name Pipes: Can be accessed without parent-child relationship (between the different processes).
Chapter 4
Threads
Learning Outcomes:
To implement the multiprocessing applications, the developers have mainly two options; Parent-child
model and threading. Since, the threading is extremely lightweight (for processor) and fast therefore it is
more common among developers than the than schemes. Each thread is responsible for only one single task,
additionally, the thread creation is lightweight than the process creation.
There are mainly two types of threaded processes; Single threaded and multi-threaded. The detailed
description of these processes is presented as follows:
4.2.1 Single Threaded processes
The processes which include only one thread which performs one specific task. The figure 4.1 presents
the single threaded process.
Registers
Thread ID
Stack Thread
In multi-threaded processes, each thread has its own ID, registers and stack, which they don’t share with
other threads. However, the codes, data and files offered by the process can be shared with other threads.
The threads can create child threads but through processes. In multi-threaded processes, the threads are
always co-operating (not independent) and cannot make system calls. The figure 4.2 presents the multi-
threaded process concept.
ID ID ID
The applications create threads using the thread library which exists within the user address space (e.g. in
C programming pthread.h etc.). The thread library is responsible for:
• Creation and termination of the threads
• Communication between the threads
• Scheduling (writing, reading etc.)
For ULTs, the kernel is not aware of threading (doesn’t know that the application has been designed
using threads). The pros and cons of ULTs are summarized as follows:
Pros of ULT
1) Thread creation is simple and easy.
2) ULTs don’t need any kernel level privilege and run on any OS.
3) ULTs are extremely fast and efficient.
Cons of ULT
1) Lack of concentration between threads and OS (Kernel) so, the process as a whole gets one
time slice, irrespective of whether process has one (1) thread or one hundred (100) threads.
2) If one of the ULTs block then the entire process gets block.
P1 P2
User
Space
Kernel Kernel
Hardware
ULT ULT
ULT
ULT
Kernel
Thread
One ULT is mapped to single KLT which means for each ULT, there is single KLT. If one ULT blocks
then other threads continue to work. The only disadvantage of one to one model is each time whenever
we create ULT then a corresponding KLT should be created (system call) for mapping which increases
the overhead on OS.
ULT ULT
ULT
T1 T2 T3
m1 m1 m1
m2 m2 m2
m3 m3 m3
… … …
Stack (Local
Variable
Chapter 5
Deadlocks
Learning Outcomes:
• Deadlock Description
• Characteristics of Deadlocks
• Deadlock Handling
5.1 What is deadlock?
If a process is unable to change its waiting state indefinitely because the requested resources are held
by another waiting process. For example, if you went to a party and you want to eat noodles. To eat
noodles, you need two items; Plate and fork. Assume, that you got plate but couldn’t find fork and there
is another person who got the fork but no plate. Now, you are asking him to give you his fork and he is
asking for your plate (no one is willing to step down). This scenario will result in deadlock situation.
In multiprogramming environment, the number of processes execute simultaneously & compete for the
limited number of resources. If a resource is not available then the process enters in a waiting state and
if this waiting state depends on another waiting process then this situation creates a deadlock.
Let us understand the deadlock with another example. The figure 5.1 presents the deadlock concept.
Process
(P1) Printer
Scanner Process
(P2)
For example, Process (P1) requires two resources (Printer and scanner) to execute itself. The P1 has
already occupied scanner (runs the desired task) then request to access printer. In the meanwhile,
another process (P2) (also requires printer and scanner) has already occupied printer (running its tasks)
and requests for scanner. This situation leads towards deadlock where, P1 is requesting for printer (held
by P2) and P2 is requesting for scanner (held by P1).
For the graphical view of the resources allocation process and visualizing the deadlock, the Resources
Allocation Graph (RAG) is used. The RAG involves two attributes; Vertex (nodes) and Edge
(connections/arrows). The processes are represented as circle and the resources are represented as
square.
Vertex
Process Resources
Single Multi-
Instance Instance
Assign Request
Process Process
Resource Resource
If the edge (arrow) direction is from resources to process, it means resource is assigned to that process and
if the direction of edge (arrow) is from process to resource then the process is requesting for resource.
The following RAG represents the deadlock between two resources and the processes.
Process Resource
(P1) (R1)
Resource Process
(R2) (P2)
In this example, both processes (P1 and P2) require two resources (R1 and R2) to get executed. The resource
(R1) is assigned to P2 and resource (R2) is assigned to P1. The P1 is requesting for R1 (which is held by
P2) and P2 is requesting for R2 (held by P1). Whenever the processes didn’t get the requested resources
then they enter in a waiting state. The waiting state can be finite (amount of time is known to the process,
it may cause starvation) or infinite (amount of time is not known) where deadlock will surely occur. If the
RAG doesn’t contain cycle then there will be no deadlock. However, if the RAG contains a cycle with a
single instance resource then deadlock will surely occur, while presence of multi-instance resource can
avoid the deadlock.
Each process follows a system and involves three step model:
a) Every process requests for the resources.
b) If entertained then the process will use resource.
c) Process must release the resource after use.
5.2 Deadlock Characteristics
There are four (4) characteristics which result in deadlock.
• Mutual Exclusion
• Hold and Wait
• Non-Preemption
• Circular Wait
The detailed description of these four (4) characteristics are discussed as follows:
5.2.3 Non-Preemption
In the noodle example, you got a plate and there is another person who is holding the fork. You
have snatched the fork him and ate the noodles. In this example, the deadlock will not occur. A
resource cannot be pre-empted from a process by another process. The resources can be released
voluntarily by the process holding it.
The deadlock will not occur unless all the four conditions meet (if above mentioned three
characteristics occur and the circular wait is absent the deadlock will not occur).
5.2.4 Circular wait
Each process must be waiting for a resource which is being held by another process. The figure
5.5 presents the circular wait of the process.
P1 P2 P3
Prevention
The deadlock prevention methods are proactive approach where the system developers design an
environment/system which ensures that deadlock should not occur. The prevention method
involves the violation of deadlock four (4) conditions. The prevention methods are expensive and
only used in real time systems.
Avoidance
System maintains a set of data (complete information of available resources, occupied resources
etc.) and use that information to make the decision (whether it can entertain the process or not) to
be in safe state.
Ignorance
The system developers ignore the problem as if it doesn’t exist. The ignorance model is used if it
is not worth it to solve the problem or not e.g. personal computers etc.
In this section, we will mainly discuss deadlock prevention method in detail. As discussed, the
deadlock prevention is a high cost method which guarantees that deadlock will not occur.
Since, there is no backward request is required therefore it will be in increasing flow and no
possibility of the cycling.
One of the most common method for deadlock detection and voidance is called Banker’s Algorithm.
In the banker’s algorithm, we have to give detailed information to OS bout all incoming processes
e.g. which processes are in running state, waiting state, holding time and need of resources etc. So,
the OS decides which process can be executed or which cannot.
In banker’s algorithm, the OS always makes a safe sequence (means no deadlock). To better
understand the Banker’s algorithm, let us consider the following example (Table 5.1). Some of the
key terminologies used in the table are defined as follows:
Allocation: Currently, how many resources have been allocated.
Maximum Need: The maximum requirement of the process to get it executed.
Currently Available: Currently available resources which can be calculated by using the following
equation:
Note: Practically, the process cannot define the static need in advanced/dynamic need (based on the
current computation).
Example: Assume that we have five (5) Processes (P1, P2, P3, P4 and P5) which require three (3)
resources (R1, R2 and R3) to get it executed. If we have Eleven (11) instances of R1, seven (7)
instances of R2 and ten (10) instances of R3. The table 5.1 presents the calculation of the safe
sequence.
Learning Outcomes:
• Memory Hierarchy
• Memory Allocation Policies
• Address Translation and Paging
The memory hierarchy uses the “locality of reference” to offer three traits of the memory
optimization. To understand the locality of reference concept, let us take an example of a
process/program execution. As we know, the program executes (code) sequentially e.g. A program
has one thousand (1000) instructions which are mainly stored onto Secondary memory (also called
secondary storage). Currently, the CPU is executing instruction (I200), then it will pre-fetch the next
few instructions (e.g. I201 to I220) to Cache Memory and some more instructions (e.g. I221 to I250) which
reduces the access time for CPU. So, the system doesn’t need to access Secondary memory to access
instructions of the process currently being executed. By using this concept, we can achieve the small
access time and large storage as well.
We can calculate overhead access time of the CPU using hit ratio of the main memory. For example,
hit ratio is 90% (data would be found in Main Memory) and the access time of the main memory is
10ms while the access time of the secondary memory is 100ms then:
𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡 𝑜𝑜𝑜𝑜 𝑡𝑡ℎ𝑒𝑒 𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃 = 0.9 × (10) + 0.1 (10 + 100)
= 20 𝑚𝑚𝑚𝑚
There are two (2) important questions, which we will answer in this chapter:
1. How processes/instructions move from secondary memory to main memory which is called
memory allocation (contiguous or non-contiguous)?
2. Whenever CPU generates address to fetch instructions (of currently being executed program),
then it generates Logical Address (LA) which is understandable for the secondary memory
but the main memory understands only Physical Address (PA). Since, the CPU mostly
interacts with Main Memory (MM) (which needs PA) so, therefore address translation is
required.
6.3 Memory Allocation Policies
10 KB
P2
4 KB 2 KB 4 KB
First Last
Ni Node Node
Nn
10 KB
P1 P2 3KB P1
4 KB 2 KB 4 KB
10 KB
P1
(4KB)
The internal fragmentation occurs because the partition cannot be reused (one
process/partition).
6.4.2 Variable Sized Partitioning: In variable sized partitioning, there is no pre-defined
partitioning and the process can occupy the space based on its requirement. The variable
size partitioning can never suffer from internal fragmentation; however, the external
fragmentation can still occur. For example, we have a memory of 10 KB (variable sized)
and we want to store two (2) processes (P1=4KB and P2=3KB). The figure 6.6 presents
the example of the variable sized partitioning.
10 KB
P1 P2
4KB 3KB
Figure 6.6: Example of Variable Sized Contiguous Memory Allocation
6.5.1.2 Best Algorithm: The memory will be scanned and the system will search for the
best match with the process (size) to be mapped e.g. we have a memory with
three (3) Partitions (Pr); Pr1=100KB, Pr2=150KB, Pr3=75 KB and if we want to
store a process (P1=70 KB) then by using best algorithm, it will be mapped onto
Pr3.
6.5.1.3 Worst Algorithm: The memory will be scanned and the system will search for
the worst match with the process (size) to be mapped e.g. we have a memory with
three (3) Partitions (Pr); Pr1=100KB, Pr2=150KB, Pr3=75 KB and if we want to
store a process (P1=70 KB) then by using best algorithm, it will be mapped onto
Pr2.
To better understand the variable sized partitioning with these algorithms, let us
consider the following example.
Example:
Assume that we have four (4) processes:
P1=300 KB
P2=25 KB
P3=125 KB
P4=50 KB
The current state of the memory is illustrated as follows:
50 KB 150 KB 300 KB 350 KB 600 KB
25KB 25KB
Figure 6.7: Example of Variable Sized Partitioning using first, best and worse algorithms
We can see from figure 6.7, by using first algorithm, to store the process (P1=300 KB),
the first available memory slot is green (350 KB). Further P2 and P3 will be stored onto
orange (150 KB) slot and finally the P4 will be stored onto the remaining green memory
slot.
By using the “best” algorithm, the P1 and P2 will be stored onto green memory slot. The
process P3 best fits with orange memory slot and after that there is no enough storage in
contiguous fashion for P4 to be stored. So, it suffers from external fragmentation of 50
KB.
In “worse” algorithm, the P1 will be stored onto green memory slot (worse fit), P2 and P3
will be stored onto orange memory slot while P4 will be mapped onto the remaining
green memory slot. There will be no external fragmentation in the worse algorithm.
First
P1 P P1 P3
43 390 32
Best
P1 P4 P3 P2
43 109 32 40
Worse P1 P2
243 290
Figure 6.8: Example of Fixed Sized Partitioning using first, best and worse algorithms
We can clearly see that the best algorithm proves to be best in contiguous fixed sized partitioning.
Limit 0
Relocation
Register 1
Register
2
Logical Physical
Address Yes Address
∑ CPU ≤ ∑
No
Trap n-1
n
Main Memory
Figure 6.8: Logical Address translation for fixed sized Contiguous Memory Allocation
In contiguous memory allocation, the address translation (Logical to Physical address) is extremely
easy. We need to create a data structure which holds the base address (limit register) (maximum
number of registers in main memory for that process) of the process being executed. If the requested
logical address is smaller than or equal (≤) to the limit register then it means it is a legit request and
the relocation register (starting address of the process in main memory) will be added to the calculate
the PA of the process. Let us understand the address translation with an example.
Assume that, a process (P1) is running on CPU and currently, the CPU is executing its instruction
“Logical address=20” and CPU wants to fetch the next instruction “Logical address=21”. If the
maximum number of instructions (limit register) for this process are “100” then this request will pass
the legitimacy test (20 ≤ 100). Assume that relocation register (starting address of P1, I1 (first
instruction)) value is 500 then the physical address will be 521:
𝑃𝑃ℎ𝑦𝑦𝑦𝑦𝑦𝑦𝑦𝑦𝑦𝑦𝑦𝑦 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 = 𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿 𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 + 𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅 𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅
If the CPU generates “Logical address=110” then this request will be turned down by the limit
register, since, the 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙 𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎 ≥ 𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿 𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅𝑅
The N-CMA can be implemented using paging and segmentation. In this chapter, we will mainly
discuss paging.
To avoid, the external fragmentation, we use N-CMA where, it is not mandatory to place all the
instructions belong to specific process. For the N-CMA:
• Partitioning the Secondary and the Main Memory
• Size of each partition/block size is fixed (since, it is fixed therefore there would be some
internal fragmentation but no external fragmentation.
• The partition of the secondary and the main memory size would be of the same size.
The partition of the secondary memory is called “page” while the partition of the main memory is
called “frame”. The frames and the page are of equal sized to avoid misconfiguration when the
instructions will move from secondary memory to main memory. The instructions of a process can be
stored in a contiguous or non-contiguous fashion in secondary memory. Let us take an example to
understand why N-CMA is different and difficult than CMA.
Assume, that we a process (X1) whose instructions are stored over three pages (P1, P2 and P3) (pages
shown in figure 6.9 are shown in a contiguous fashion, which is not mandatory).
P1
X1 P2
P1
P3
x
P3
P2
Main Memory
Secondary Memory
Now, we can see from the figure 6.9, the address translation method of contiguous memory allocation
cannot be used since, the instructions are not collocated (sequential) on MM. In the CMA, we all need to
know the Base Address (BA) of the process (on MM) and by adding the BA & the LA of the instruction,
we can find the Physical Address of the process’s instruction. However, in N-CMA, the pages
(instructions) are stored in a random fashion, therefore we need to find a different algorithmic solution to
find the address of the pages (holding that instruction). In the N-CMA with paging, assume that the CPU
wants to fetch the instruction 24 (I24) on Page 2 (P2):
P1
P1 X1 P2
P D
P3
P3
CPU
P2
Main Memory
Secondary Memory
Figure 6.10: Non-Contiguous Memory Allocation Address generation
The Logical Address is divided into two parts P (Page Number) and the D (Instruction Offset). One of
the methods of address translation (N-CMA) is using linked list, where the pages of the same process
are interlinked with other pages using address pointers. The P1 knows the address of the P2 and P2
knows the address of the P3. All we need to know is the BA of the first page and then linked list will
resolve the LA to PA. However, this method is extremely slow and makes the long overall access
time.
P1 F4
P2 D24 P2 F2 F2 D24
P3 F5
CPU
P1
X1
F1 P2
P3
F2 P2
F3
F4 P1
F5 P3
• Pros:
o Fast Access
o No External Fragmentation
• Cons:
o Overhead of PT for every process (not stored on PCB, it is stored separately).
o Internal Fragmentation
Chapter 7
File System Interface
Learning Outcomes:
A file is a collection of bits, bytes and characters. The computers understand/interpret the data in the
form of files e.g. xyz.mp3 etc. The file can be defined as “Named collection of related information on
secondary storage.” The data cannot write to secondary storage unless it is in a file format where
each file format/system has a different structure e.g. .txt, .mp3, .bat and. docs etc.
There are mainly three (3) types of files:
Text Files: Sequence of characters organized into lines.
Source Files: Sequence of sub-routines or functions (executable to perform the tasks)
Object Files: The collection of words/sequences of bytes organized into blocks, when we run a
program which uses the digital resources, it mainly utilizes the objects files for transferring of the
instructions.
The files can be divided/differentiated based on the type of the data e.g. Numeric, characters and the
binary, all the file related parameters are contiguous.
Rewind Read/Write
Skip
200 - 300
Root
Directory
The single level directory is simple to implement, however it offers naming and grouping
problems (The files belong to same application can get overlapped).
Root
Directory
Root
Directory
Sub-Directory-1
File 1 File File
File 2 3 4
Sub-Directory-2
File 5 File 6
The tree structure allows the sharing of files between the different directories and offers
all those six (6) operations of the file system.
This command allows the eclipse files to be used in D drive. When we uninstall the application then
we need to detach the files from the file systems (Unmount):
The file sharing allows the multiple users to access/share the files. When we enable the file sharing
then the access control and protection method must be configured to protect the files from
unauthorized access. In *nix systems, the users are categorized in three types; Administrators, Groups
and other (public) and users can have three (3) access rights; Read (R), Write (W) and Execute (X).
The administrators can create and manage the users, assign them a specific group with certain rights
or they can have general access of the system. The users having full privilege (RWX) are assigned ‘7’
(means user can read (4), write (2) and execute (1)). If the administrators want the users to only
execute the file then ‘1’ will be assigned and similarly ‘2’ allows the users to have write privilege. For
example, we create a file (abc.sh) in a Linux OS then to find its default access privileges, the
following command is used:
𝑙𝑙𝑙𝑙 − 𝑙𝑙 𝑎𝑎𝑎𝑎𝑎𝑎. 𝑠𝑠ℎ
It will show the permissions of the file abc.sh for all the three types of user. If the administrator wants
all the three types of users to have full privilege then:
𝑐𝑐ℎ𝑚𝑚𝑚𝑚𝑚𝑚 111 𝑎𝑎𝑎𝑎𝑎𝑎. 𝑠𝑠ℎ
The figure 7.6 shows the permissions of the file:
Each user is assigned a specific user ID (to attach the permission with the user) and Group ID
(defines the set of permissions attached to that group).
Chapter 8
Cloud Computing Concepts
Learning Outcomes:
The “cloud” refers to servers that are accessed over the internet, and the software and
applications that run on these servers collectively is called cloud computing. Cloud servers are
located in data centers all over the world and are usually undisclosed for security reasons. By
using cloud computing, users and companies do not have to manage the physical servers
themselves or run the software application on their own machines.
The cloud enables users to access the same files and applications from nearly any device that
has a network connection and the processing occurs on the remote servers in the data centers
instead of on the local user device. This how webmail application providers such as Gmail
and Microsoft 365 as well as cloud storage providers like Google Drive and Dropbox have
always operated. However more recently, cloud computing expands this concept to apply it to
emerging technologies such as Computing, Database processing, Machine Learning and other
new innovations.
Switching to cloud computing lowers IT costs and overhead allowing for smaller business that
may not have the capital for their own data centers allowing them to be more competitive. The
cloud also makes it easier for organizations to operate internationally due to the remote
locations of these servers that are all in-synch with each other allowing access from anywhere
worldwide. The following section will discuss the four types of cloud deployments:
A private cloud are many servers located in one or more data centers that are
distributed over a network dedicated to one organization and its subsidiaries.
Private clouds permit only authorized users, providing the organization greater
control over data and its security. Business organizations that have dynamic,
critical, secured, management demand could look to implement a private cloud.
Advantages
Disadvantages
Private clouds have been used for businesses for quite some time and is not a
new concept which contrasts with the next deployment.
Unlike a private cloud, public clouds are then shared with other organizations
even though the individual organizations may not be aware or even be
concerned with this fact.
The servers use virtual machines created on the physical serves and are made
available to whoever wants to pay for the usage of that virtual machine. This is
an example of multitenancy allowing for multiple tenants to essentially rent the
same server processing capabilities.
Server infrastructure belongs to the cloud service providers (CSPs) that manage
them and control the resources allowing for companies to not worry about
capital expenditures. Public clouds have become more and more popular due to
increased network connectivity and as well as the desire to lower IT operating
costs.
Advantages
Disadvantages
Hybrid cloud implementation combine both public and private clouds and may
also include on-premise servers. An organization may use the private cloud for
some more secure applications and then the public cloud for other less secure or
stringent applications. Having both public and private has its benefits such as
backup and redundancy but can also lead to higher operating costs.
Hybrid cloud deployment not only safeguards against vendor lock-in but allows
for more selective use of both the private and public cloud deployment models
leading to maximizing advantages and minimizing disadvantages.
Advantages
Disadvantages
• Complex networking required
• Security and compliance challenges
• Interoperability of various cloud implementations
For joint business organizations, ventures, research organizations and the like a
community cloud can be the appropriate solution. Community cloud members need to
collaborate and evaluate which type of cloud hosting is best for the community and
decide on who will manage it.
Advantages
• Cost reduction
• Increased security, privacy and reliability
• Ease of sharing and collaborate
• Potential compliance benefits
Disadvantages
Cloud
Deployment
Models
Community
Public Cloud Private Cloud
Cloud
Hybrid Cloud
Figure 8.1: Types of Cloud Models
Once a cloud deployment model has been determined another decision is to decide on the three
main cloud service models to satisfy the unique set of business requirements. These three
models are known as Infrastructure as a Service (IaaS), Platform as a Service (PaaS), and
Software as a Service (SaaS).
IaaS SaaS
PaaS
Infrastructure as a Service (IaaS) is a self-service model for managing remote data center
resources. IaaS provides for virtualized computing resources over the Internet hosted by
the third-party cloud service provider such as Amazon Web Services, Microsoft Azure,
or Google Cloud.
Instead of an organization having to spend vast capital on purchasing hardware, they pay
to use IaaS using the consumption model effectively converting capital expenditures to
operating expenditures. This allows for much more flexibility and cost savings thus
leading to is popularity.
Most organizations use IaaS since they are already a familiar with IaaS when they
managed and maintained their on-premise infrastructure but IaaS allows for much more
flexibility and is highly scalable.
Effectively companies are renting the servers and storage they need from the cloud
service provider but they need to provide their own knowledge and technical expertise to
maintain and manage them. Examples of IaaS include Amazon Elastic Compute Cloud
(EC2), Digital Ocean, RackSpace and many others.
Developers can focus on writing code and creating applications without doing time-
consuming IT infrastructure activities such as provisioning of servers, storage, backup
and configuration.
PaaS brings more value to cloud. It reduces management overhead thus lowering costs
allowing organization to focus on revenue generating activities include of infrastructure
upkeep and maintenance.
PaaS can be compared to renting all the tools and equipment necessary for building a
house instead of renting the house itself. Examples of PaaS include: Amazon Elastic
Beanstalk, Heroku and Microsoft Azure App.
Software as a service (SaaS) replaces the traditional on-device software with software
that is licensed on a subscription basis. The software is centrally hosted in the cloud by
the cloud service provider. Most SaaS applications can be easily accessed directly from
a web browser or app without any downloads or installations though some may require
plugins.
Users do not necessarily need to install applications on their device they just need to
have Internet connectivity to access them. SaaS is like renting a house where the
landlord maintains the house, but the tenant gets most of the use of it as if they owned it.
Examples include: Salesforce, MailChimp, Slack, Webmail and numerous other popular
applications.
In summary with Infrastructure as a Service the users host their applications. Platform as a
Service allows for the users to build their applications whereas Software as a Service allows
them to use or consume the applications.
Cloud computing has enabled many new technologies such as Machine Learning, Artificial
Intelligence systems, Data Analytics, Big Data, Data-warehousing and many more. However, the
underlying technology that allows for Cloud computing to be so popular is virtualization. The
next section briefly introduces virtualization and its many benefits.
8.3.1 Virtualization
Type II, or “hosted” hypervisors behave more like a traditional application that can be
started and stopped by a normal program. Examples of Type II hypervisors include
VMWare Workstation and Oracle’s VirtualBox.
The virtual machines that are created emulate the computer system sharing the physical
system hardware and thus have access to the host machine’s CPU, memory, one or more
virtual or physical disk devices; one or more virtual or physical network interfaces as
well as other devices such as video cards, USB devices and more.
There are numerous benefits from using virtual machines and some of them include:
As one can see these benefits are also benefits seen from using cloud computing; thus,
virtualization is the seed that revolutionized the computing world leading to cloud
computing.
8.3.2 Containers
Since the benefits of virtualization and virtual machines are clearly established and have
been implemented by many organizations the next evolution of virtual machines is
something called a container which will be briefly introduced in this section.
Containers are packages of software that contain all of the necessary elements to run in
any environment. In this way, containers effectively “virtualize” the operating system
and an be ran anywhere from a private data center, pubic data center, public cloud or
even an individual personal laptop or mobile device.
Containers allow for development teams to move fast, deploy software applications
efficiently and operate at unprecedented scale. Benefits of containers include the
separation of responsibility, workload portability and application isolation among others.
Containers are often compared to virtual machines (VMs) but are far more lightweight
and carry these advantages:
Consider the physical containers on transport ship where the ship itself is the virtual
machine carrying many containers which are individually isolated and independent.