CMP 221 Note
CMP 221 Note
CMP 221 Note
OPERATING SYSTEM
An operating system (OS) is the software component of a computer system that is responsible for the
management and coordination of activities and the sharing of the resources of the computer. The OS acts as a
host for application programs that are run on the machine. As a host, one of the purposes of an OS is to handle
the details of the operation of the hardware.
Every general-purpose computer consists of the hardware, operating system, system programs, and
application programs. The hardware consists of memory, CPU, ALU, and I/O devices, peripheral device, and
storage device. System program consists of compilers, loaders, editors, OS, etc. The application program
consists of business programs, database programs.
1. User's View
2. System View
User View
The user view of the computer refers to the interface being used. Such systems are designed for one user to
monopolize its resources, to maximize the work that the user is performing. In these cases, the operating
system is designed mostly for ease of use, with some attention paid to performance, and none paid to
resource utilization.
System View
Operating system can be viewed as a resource allocator also. A computer system consists of many resources
like - hardware and software that must be managed efficiently. The operating system acts as the manager of
the resources, decides between conflicting requests, controls execution of programs etc.
1. Processor management which involves putting the tasks into order and pairing them into manageable size
1
CMP 221 ROUGH NOTE
2. Memory management which coordinates data to and from RAM (random-access memory) and
5. Application which allows standard communication between software and your computer.
2. It performs basic computer tasks e.g. managing the various peripheral devices e.g. mouse, keyboard
3. It provides a user interface, e.g. command line, graphical user interface (GUI)
4. It handles system resources such as computer's memory and sharing of the central processing unit (CPU)
5. It provides file management which refers to the way that the operating system manipulates, stores,
6. Error Handling is done by the operating system. It takes preventive measures whenever required to avoid
errors.
3. Multiprocessor System
4. Desktop System
6. Clustered System
8. Handheld System
2
CMP 221 ROUGH NOTE
In this type of system, there is no direct interaction between user and the computer.
The user has to submit a job (written on cards or tape) to a computer operator.
Then a special program, the monitor, manages the execution of each program in the batch.
The monitor is always in the main memory and available for execution.
In this the operating system picks up and begins to execute one of the jobs from memory.
Once this job needs an I/O operation operating system switches to another job (CPU and OS always busy).
Jobs in the memory are always less than the number of jobs on disk(Job Pool).
If several jobs are ready to run at the same time, then the system chooses which one to run through the
In Non-multiprogrammed system, there are moments when CPU sits idle and does not do any work.
Time Sharing Systems are very similar to Multiprogramming batch systems. In fact time sharing systems are
an extension of multiprogramming systems.
In Time sharing systems the prime focus is on minimizing the response time, while in multiprogramming the
prime focus is to maximize the CPU usage.
3
CMP 221 ROUGH NOTE
Multiprocessor Systems
A Multiprocessor system consists of several processors that share a common physical memory. Multiprocessor
system provides higher computing power and speed. In multiprocessor system all processors operate under
single operating system. Multiplicity of the processors and how they do act together are transparent to the
others.
1. Enhanced performance
2. Execution of several tasks by different processors concurrently, increases the system's throughput without
3. If possible, system divides task into many subtasks and then these subtasks can be executed in parallel in
Desktop Systems
Earlier, CPUs and PCs lacked the features needed to protect an operating system from user programs. PC
operating systems therefore were neither multiuser nor multitasking. However, the goals of these operating
systems have changed with time; instead of maximizing CPU and peripheral utilization, the systems opt for
maximizing user convenience and responsiveness. These systems are called Desktop Systems and include PCs
running Microsoft Windows and the Apple Macintosh. Operating systems for these computers have benefited
in several ways from the development of operating systems for mainframes.
Microcomputers were immediately able to adopt some of the technology developed for larger operating
systems. On the other hand, the hardware costs for microcomputers are sufficiently low that individuals have
4
CMP 221 ROUGH NOTE
sole use of the computer, and CPU utilization is no longer a prime concern. Thus, some of the design decisions
made in operating systems for mainframes may not be appropriate for smaller systems.
1. As there are multiple systems involved, user at one site can utilize the resources of systems at other sites
2. Fast processing.
1. Client-Server Systems
2. Peer-to-Peer Systems
Client-Server Systems
Centralized systems today act as server systems to satisfy requests generated by client systems. The general
structure of a client-server system is depicted in the figure below:
Server Systems can be broadly categorized as: Compute Servers and File Servers.
5
CMP 221 ROUGH NOTE
Compute Server systems, provide an interface to which clients can send requests to perform an action, in
response to which they execute the action and send back results to the client.
File Server systems, provide a file-system interface where clients can create, update, read, and delete
files.
Peer-to-Peer Systems
The growth of computer networks - especially the Internet and World Wide Web (WWW) – has had a
profound influence on the recent development of operating systems. When PCs were introduced in the 1970s,
they were designed for personal use and were generally considered standalone computers. With the
beginning of widespread public use of the Internet in the 1990s for electronic mail and FTP, many PCs became
connected to computer networks.
In contrast to the Tightly Coupled systems, the computer networks used in these applications consist of a
collection of processors that do not share memory or a clock. Instead, each processor has its own local
memory. The processors communicate with one another through various communication lines, such as high-
speed buses or telephone lines. These systems are usually referred to as loosely coupled systems ( or
distributed systems). The general structure of a client-server system is depicted in the figure below:
Clustered Systems
Like parallel systems, clustered systems gather together multiple CPUs to accomplish computational work.
Clustered systems differ from parallel systems, however, in that they are composed of two or more
The definition of the term clustered is not concrete; the general accepted definition is that clustered
computers share storage and are closely linked via LAN networking.
A layer of cluster software runs on the cluster nodes. Each node can monitor one or more of the others. If
the monitored machine fails, the monitoring machine can take ownership of its storage, and restart the
application(s) that were running on the failed machine. The failed machine can remain down, but the
users and clients of the application would only see a brief interruption of service.
6
CMP 221 ROUGH NOTE
Asymmetric Clustering - In this, one machine is in hot standby mode while the other is running the
applications. The hot standby host (machine) does nothing but monitor the active server. If that server
Symmetric Clustering - In this, two or more hosts are running applications, and they are monitoring each
other. This mode is obviously more efficient, as it uses all of the available hardware.
Parallel Clustering - Parallel clusters allow multiple hosts to access the same data on the shared storage.
Because most operating systems lack support for this simultaneous data access by multiple hosts, parallel
clusters are usually accomplished by special versions of software and special releases of applications.
Clustered technology is rapidly changing. Clustered system's usage and it's features should expand greatly
as Storage Area Networks(SANs). SANs allow easy attachment of multiple hosts to multiple storage units.
Current clusters are usually limited to two or four hosts due to the complexity of connecting the hosts to
shared storage.
Handheld Systems
Handheld systems include Personal Digital Assistants(PDAs), such as Palm-Pilots or Cellular Telephones with
connectivity to a network such as the Internet. They are usually of limited size due to which most handheld
devices have a small amount of memory, include slow processors, and feature small display screens.
Many handheld devices have between 512 KB and 8 MB of memory. As a result, the operating system and
applications must manage memory efficiently. This includes returning all allocated memory back to the
Currently, many handheld devices do not use virtual memory techniques, thus forcing program
Processors for most handheld devices often run at a fraction of the speed of a processor in a PC. Faster
processors require more power. To include a faster processor in a handheld device would require a larger
7
CMP 221 ROUGH NOTE
The last issue confronting program designers for handheld devices is the small display screens typically
available. One approach for displaying the content in web pages is web clipping, where only a small
Some handheld devices may use wireless technology such as BlueTooth, allowing remote access to e-mail and
web browsing. Cellular telephones with connectivity to the Internet fall into this category. Their use continues
to expand as network connections become more available and other options such as cameras and MP3
players, expand their utility.
OS Kernel
The kernel is the central component of a computer operating systems. The only job performed by the kernel is
to the manage the communication between the software and the hardware. A Kernel is at the nucleus of a
computer. It makes the communication between the hardware and software possible. While the Kernel is the
innermost part of an operating system, a shell is the outermost one.
Features of Kennel
Low-level scheduling of processes
Inter-process communication
Process synchronization
Context switching
Types of Kernels
There are many types of kernels that exists, but the two most popular kernels are:
1.Monolithic
A monolithic kernel is a single code or block of the program. It provides all the required services offered by the
operating system. It is a simplistic design which creates a distinct communication layer between the hardware
and software.
2. Microkernels
Microkernel manages all system resources. In this type of kernel, services are implemented in different
address space. The user services are stored in user address space, and kernel services are stored under kernel
address space. So, it helps to reduce the size of both the kernel and operating system.
Process
A process is a program in execution. Process is not as same as program code but a lot more than it. A process
is an 'active' entity as opposed to program which is considered to be a 'passive' entity. Attributes held by
process include hardware state, memory, CPU etc.
8
CMP 221 ROUGH NOTE
Process memory is divided into four sections for efficient working:
The Text section is made up of the compiled program code, read in from non-volatile storage when the
program is launched.
The Data section is made up the global and static variables, allocated and initialized prior to executing the
main.
The Heap is used for the dynamic memory allocation, and is managed via calls to new, delete, malloc, free,
etc.
The Stack is used for local variables. Space on the stack is reserved for local variables when they are
declared.
WAITING- The process is waiting for some event to occur (such as an I/O completion or reception of a
signal).
9
CMP 221 ROUGH NOTE
CPU registers and Program Counter. Program Counter holds the address of the next instruction to be
CPU Scheduling information: Such as priority information and pointers to scheduling queues.
Accounting information: The User and kernel CPU time consumed, account numbers, limits, etc.
Process Scheduling
The act of determining which process in the ready state should be moved to the running state is known
as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver minimum
response time for all programs. For achieving this, the scheduler must apply appropriate rules for swapping
processes IN and OUT of CPU.
Scheduling fell into one of the two general categories:
Scheduling Queues
All processes, upon entering into the system, are stored in the Job Queue.
Processes waiting for a device to become available are placed in Device Queues. There are unique device
A new process is initially put in the Ready queue. It waits in the ready queue until it is selected for execution
(or dispatched). Once the process is assigned to the CPU and is executing, one of the following several events
can occur:
The process could issue an I/O request, and then be placed in the I/O queue.
The process could create a new subprocess and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.
10
CMP 221 ROUGH NOTE
In the first two cases, the process eventually switches from the waiting state to the ready state, and is then
put back in the ready queue. A process continues this cycle until it terminates, at which time it is removed
from all queues and has its PCB and resources deallocated.
Operations on Process
Below we have discussed the two major operation Process Creation and Process Termination.
Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other processes. The process
which creates other process, is termed the parent of the other process, while the created sub-process is
termed its child.
Each process is given an integer identifier, termed as process identifier, or PID. The parent PID (PPID) is also
stored for each process.
On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The first thing done by
it at system start-up time is to launch init, which gives that process PID 1. Further Init launches all the system
daemons and user logins, and becomes the ultimate parent of all other processes.
A child process may receive some amount of shared resources with its parent depending on system
implementation. To prevent runaway children from consuming all of a certain system resource, child
processes may or may not be limited to a subset of the resources originally allocated to the parent.
There are two options for the parent process after creating the child :
Wait for the child process to terminate before proceeding. Parent process makes a wait() system call, for
either a specific child process or for any particular child process, which causes the parent process to block
until the wait() returns. UNIX shells normally wait for their children to complete before issuing a new
prompt.
11
CMP 221 ROUGH NOTE
Run concurrently with the child, continuing to process without waiting. When a UNIX shell runs a process
as a background task, this is the operation seen. It is also possible for the parent to run for a while, and
then wait for the child later, which might occur in a sort of a parallel processing operation.
If fork or spawn is called for n times, the number of child processes or new processes created will be: 2n – 1.
Process Termination
By making the exit (system call), typically returning an int, processes may request their own termination. This
int is passed along to the parent if it is doing a wait(), and is typically zero on successful completion and some
non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including:
A parent may kill its children if the task assigned to them is no longer needed i.e. if the need of having a
child terminates.
If the parent exits, the system may or may not allow the child to continue without a parent (In UNIX
systems, orphaned processes are generally inherited by init, which then proceeds to kill them.)
When a process ends, all of its system resources are freed up, open files flushed and closed, etc. The process
termination status and execution times are returned to the parent if the parent is waiting for the child to
terminate, or eventually returned to init if the process already became an orphan.
The processes which are trying to terminate but cannot do so because their parent is not waiting for them are
termed zombies. These are eventually inherited by init as orphans and killed off.
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready
queue to be executed. The selection process is carried out by the short-term scheduler (or CPU scheduler).
The scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU
to one of them.
CPU Scheduling
Another component involved in the CPU scheduling function is the Dispatcher. The dispatcher is the module
that gives control of the CPU to the process selected by the scheduler.
To make the best use of CPU and not to waste any CPU cycle, CPU would be working most of the time(Ideally
100% of the time). Considering a real system, CPU usage should range from 40% (lightly loaded) to 90%
(heavily loaded.)
12
CMP 221 ROUGH NOTE
Throughput
It is the total number of processes completed per unit time or rather say total amount of work done in a unit
of time.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from time of submission of the
process to the time of completion of the process.
Waiting Time
The sum of the periods spent waiting in the ready queue or the amount of time a process has been waiting in
the ready queue to acquire or get control of the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get into the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is produced. Remember, it
is the time till the first response and not the completion of process execution(final response).
Scheduling Algorithms
To decide which process to execute first and which process to execute last to achieve maximum CPU
utilisation, computer scientists have defined some algorithms, they are:
2. Shortest-Job-First(SJF) Scheduling
3. Priority Scheduling
First Come First Serve, is just like FIFO(First in First out) Queue data structure, where the data element
which is added to the queue first, is the one who leaves the queue first.
It's easy to understand and implement programmatically, using a Queue data structure, where a new
process enters through the tail of the queue, and the scheduler selects process from the head of the
queue.
13
CMP 221 ROUGH NOTE
A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order,
with Arrival Time 0, and given Burst Time, let's find the average waiting time using the FCFS scheduling
algorithm.
Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2, which will
be (21 + 3) ms = 24 ms.
For process P4 it will be the sum of execution times of P1, P2 and P3.
The GANTT chart above perfectly represents the waiting time for each process.
14
CMP 221 ROUGH NOTE
1. It is Non Pre-emptive algorithm, which means the process priority doesn't matter.
If a process with very least priority is being executed, more like daily routine backup process, which takes
more time, and all of a sudden some other high priority process arrives, like interrupt to avoid system
crash, the high priority process will have to wait, and hence in this case, the system will crash, just
3. Resources utilization in parallel is not possible, which leads to Convoy Effect, and hence poor resource
It is of two types:
1. Non Pre-emptive
2. Pre-emptive
To successfully implement it, the burst time/duration time of the processes should be known to the
15
CMP 221 ROUGH NOTE
This scheduling algorithm is optimal if all the jobs/processes are available at the same time. (either Arrival
As you can see in the GANTT chart above, the process P4 will be picked up first as it has the shortest burst
time, then P2, followed by P3 and at last P1.
We scheduled the same set of processes using the First come first serve algorithm in the previous tutorial, and
got average waiting time to be 18.75 ms, whereas with SJF, the average waiting time comes out 4.5 ms.
16
CMP 221 ROUGH NOTE
In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as they arrive, but as a process
with short burst time arrives, the existing process is preempted or removed from execution, and the shorter
job is executed first.
As you can see in the GANTT chart above, as P1 arrives first, hence it's execution starts immediately, but just
after 1 ms, process P2 arrives with a burst time of 3 ms which is less than the burst time of P1, hence the
process P1(1 ms done, 20 ms left) is preemptied and process P2 is executed.
As P2 is getting executed, after 1 ms, P3 arrives, but it has a burst time greater than that of P2, hence
execution of P2 continues. But after another millisecond, P4 arrives with a burst time of 2 ms, as a result P2(2
ms done, 1 ms left) is preemptied and P4 is executed.
After the completion of P4, process P2 is picked up and finishes, then P2 will get executed and at last P1.
The Pre-emptive SJF is also known as Shortest Remaining Time First, because at any given point of time, the
job with the shortest remaining time is executed first.
Priority Scheduling
Priority can be decided based on memory requirements, time requirements or any other resource
requirement.
17
CMP 221 ROUGH NOTE
Once a process is executed for given time period that process is preemptied and other process executes
18
CMP 221 ROUGH NOTE
Threads
Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads
are also known as Lightweight processes. Threads are popular way to improve application through parallelism.
The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in
parallel.
As each thread has its own independent resource for process execution, multiple processes can be executed in
parallel by increasing number of threads.
Types of Thread
There are two types of threads:
1. User Threads
2. Kernel Threads
19
CMP 221 ROUGH NOTE
User threads, are above the kernel and without kernel support. These are the threads that application
programmers use in their programs.
Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel level threads,
allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:
In the many to one model, many user-level threads are all mapped onto a single kernel thread.
Thread management is handled by the thread library in user space, which is efficient in nature.
The one to one model creates a separate kernel thread to handle each and every user thread.
Most implementations of this model place a limit on how many threads can be created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
20
CMP 221 ROUGH NOTE
The many to many model multiplexes any number of user threads onto an equal or smaller number of
kernel threads, combining the best features of the one-to-one and many-to-one models.
Blocking the kernel system calls does not block the entire process.
Benefits of Multithreading
1. Responsiveness
21
CMP 221 ROUGH NOTE
4. Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a
5. Context Switching is smooth. Context switching refers to the procedure followed by CPU to change from
Multithreading Issues
Below we have mentioned a few issues related to multithreading. Well, it's an old saying, All good things,
come at a price.
Thread Cancellation
Thread cancellation means terminating a thread before it has finished working. There can be two approaches
for this, one is Asynchronous cancellation, which terminates the target thread immediately. The other
is Deferred cancellation allows the target thread to periodically check if it should be cancelled.
Signal Handling
Signals are used in UNIX systems to notify a process that a particular event has occurred. Now in when a
Multithreaded process receives a signal, to which thread it must be delivered? It can be delivered to all, or a
single thread.
Security Issues
Yes, there can be security issues because of extensive sharing of resources between multiple threads.
There are many other issues that you might face in a multithreaded process, but there are appropriate
solutions available for them. Pointing out some issues here was just to study both sides of the coin.
Deadlock
Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource held by
another process.
22
CMP 221 ROUGH NOTE
1. Mutual Exclusion
Resources shared such as read-only files do not lead to deadlocks but resources, such as printers and tape
In this condition processes must be prevented from holding one or more resources while simultaneously
3. No Preemption
Preemption of process resource allocations can avoid the condition of deadlocks, where ever possible.
4. Circular Wait
Circular wait can be avoided if we number all resources, and require that processes request resources only
23
CMP 221 ROUGH NOTE
Handling Deadlock
The above points focus on preventing deadlocks. But what to do once a deadlock has occured. Following three
strategies can be used to remove deadlock after its occurrence.
1. Preemption
We can take a resource from one process and give it to other. This will resolve the deadlock situation, but
2. Rollback
In situations where deadlock is a real possibility, the system can periodically make a record of the state of
each process and when deadlock occurs, roll everything back to the last checkpoint, and restart, but
Context Switch
Switching the CPU to another process requires saving the state of the old process and loading the saved state
The context of a process is represented in the Process Control Block (PCB) of a process; it includes the value
of the CPU registers, the process state and memory-management information. When a context switch occurs,
the Kernel saves the context of the old process in its PCB and loads the saved context of the new process
scheduled to run.
Context switch time is pure overhead, because the system does no useful work while switching. Its speed
varies from machine to machine, depending on the memory speed, the number of registers that must be
copied, and the existence of special instructions (such as a single instruction to load or store all registers).
Context Switching has become such a performance bottleneck that programmers are using new structures
24
CMP 221 ROUGH NOTE
Introduction to Memory Management
Memory management is the process of controlling and coordinating computer memory, assigning portions
called blocks to various running programs to optimize overall system performance.
Main Memory refers to a physical memory that is the internal memory to the computer. The word main is
used to distinguish it from external mass storage devices such as disk drives. Main memory is also known as
RAM. The computer is able to change only data that is in main memory. Therefore, every program we execute
and every file we access must be copied from a storage device into main memory.
All the programs are loaded in the main memeory for execution. Sometimes complete program is loaded into
the memory, but some times a certain part or routine of the program is loaded into the main memory only
when it is called by the program, this mechanism is called Dynamic Loading, this enhance the performance.
Swapping
A process needs to be in memory for execution. But sometimes there is not enough main memory to hold all
the currently active processes in a timesharing system. So, excess process are kept on disk and brought in to
run dynamically. Swapping is the process of bringing in each process in main memory, running it for a while
and then putting it back to the disk.
Memory Protection
Memory protection is a phenomenon by which we control memory access rights on a computer. The main aim
of it is to prevent a process from accessing memory that has not been allocated to it. Hence prevents a bug
within a process from affecting other processes, or the operating system itself, and instead results in a
segmentation fault or storage violation exception being sent to the disturbing process, generally killing of
process.
Memory Allocation
Memory allocation is a process by which computer programs are assigned memory or space. It is of three
types :
1. First Fit:
2. Best Fit:
3. Worst Fit:
25
CMP 221 ROUGH NOTE
Fragmentation
Fragmentation occurs in a dynamic memory allocation system when most of the free blocks are too small to
satisfy any request. It is generally termed as inability to use the available memory.
In such situation processes are loaded and removed from the memory. As a result of this, free holes exists to
satisfy a request but is non contiguous i.e. the memory is fragmented into large no. Of small holes. This
phenomenon is known as External Fragmentation.
Also, at times the physical memory is broken into fixed size blocks and memory is allocated in unit of block
sizes. The memory allocated to a space may be slightly larger than the requested memory. The difference
between allocated and required memory is known as Internal fragmentation i.e. the memory that is internal
to a partition but is of no use.
Paging
A solution to fragmentation problem is Paging. Paging is a memory management mechanism that allows the
physical address space of a process to be non-contagious. Here physical memory is divided into blocks of equal
size called Pages. The pages belonging to a certain process are loaded into available memory frames.
Page Table
A Page Table is the data structure used by a virtual memory system in a computer operating system to store
the mapping between virtual address and physical addresses.
Virtual address is also known as Logical address and is generated by the CPU. While Physical address is the
address that actually exists on memory.
Segmentation
Segmentation is another memory management scheme that supports the user-view of memory. Segmentation
allows breaking of the virtual address space of a single process into segments that may be placed in non-
contiguous areas of physical memory.
Segment numbers(S)
Virtual Memory
Virtual Memory is a space where large programs can store themselves in form of pages while their execution
and only the required pages or portions of processes are loaded into the main memory. This technique is
useful as large virtual memory is provided for user programs when a very small physical memory is there.
26
CMP 221 ROUGH NOTE
In real scenarios, most processes never need all their pages at once, for following reasons :
Error handling code is not needed unless that specific error occurs, some of which are quite rare.
Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays are
1. Large programs can be written, as virtual space available is huge compared to physical memory.
3. More physical memory available, as programs are stored on virtual memory, so they occupy very less
27