0% found this document useful (0 votes)
9 views25 pages

Os Unit I

An Operating System (OS) is essential software that manages computer hardware and provides services for programs, acting as an interface between users and hardware. It performs critical functions such as process management, memory management, and file management, and comes in various types including batch, time-sharing, distributed, network, and real-time operating systems. The document also discusses the evolution of operating systems, examples of popular OS, and the role of system calls in facilitating interactions between programs and the OS.

Uploaded by

rajalakshmicom85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Os Unit I

An Operating System (OS) is essential software that manages computer hardware and provides services for programs, acting as an interface between users and hardware. It performs critical functions such as process management, memory management, and file management, and comes in various types including batch, time-sharing, distributed, network, and real-time operating systems. The document also discusses the evolution of operating systems, examples of popular OS, and the role of system calls in facilitating interactions between programs and the OS.

Uploaded by

rajalakshmicom85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

An Operating System (OS) is a collection of software that manages computer hardware resources and

provides common services for computer programs. When you start using a Computer System then it's the
Operating System (OS) which acts as an interface between you and the computer hardware. The operating
system is really a low level Software which is categorised as a System Software and supports a computer's
basic functions, such as memory management, tasks scheduling and controlling peripherals etc.
This simple and easy tutorial will take you through step by step approach while learning Operating System
concepts in detail.
What is Operating System?
An Operating System (OS) is an interface between a computer user and computer hardware. An operating
system is a software which performs all the basic tasks like file management, memory management, process
management, handling input and output, and controlling peripheral devices such as disk drives and printers.
Generally, a Computer System consists of the following components:
 Computer Users are the users who use the overall computer system.
 Application Softwares are the softwares which users use directly to perform different activities.
These softwares are simple and easy to use like Browsers, Word, Excel, different Editors, Games etc.
These are usually written in high-level languages, such as Python, Java and C++.
 System Softwares are the softwares which are more complex in nature and they are more near to
computer hardware. These software are usually written in low-level languages like assembly
language and includes Operating Systems (Microsoft Windows, macOS, and Linux), Compiler, and
Assembler etc.
 Computer Hardware includes Monitor, Keyboard, CPU, Disks, Memory, etc.
So now let's put it in simple words:
If we consider a Computer Hardware is body of the Computer System, then we can say an Operating System
is its soul which brings it alive ie. operational. We can never use a Computer System if it does not have an
Operating System installed on it.
Operating System - Examples
There are plenty of Operating Systems available in the market which include paid and unpaid (Open
Source). Following are the examples of the few most popular Operating Systems:
 Windows: This is one of the most popular and commercial operating systems developed and
marketed by Microsoft. It has different versions in the market like Windows 8, Windows 10 etc and
most of them are paid.
 Linux This is a Unix based and the most loved operating system first released on September 17,
1991 by Linus Torvalds. Today, it has 30+ variants available like Fedora, OpenSUSE, CentOS,
UBuntu etc. Most of them are available free of charges though you can have their enterprise versions
by paying a nominal license fee.
 MacOS This is again a kind of Unix operating system developed and marketed by Apple Inc. since
2001.
 iOS This is a mobile operating system created and developed by Apple Inc. exclusively for its
mobile devices like iPhone and iPad etc.
 Android This is a mobile Operating System based on a modified version of the Linux kernel and
other open source software, designed primarily for touchscreen mobile devices such as smartphones
and tablets.
Some other old but popular Operating Systems include Solaris, VMS, OS/400, AIX, z/OS, etc.
Operating System - Functions
To brief, Following are some of important functions of an operating System which we will look in more
detail in upcoming chapters:
 Process Management
 I/O Device Management
 File Management
 Network Management
 Main Memory Management
 Secondary Storage Management
 Security Management
 Command Interpreter System
 Control over system performance
 Job Accounting
 Error Detection and Correction
 Coordination between other software and users
 Many more other important tasks
Operating Systems - History
Operating systems have been evolving through the years. In the 1950s, computers were limited to running
one program at a time like a calculator, but later in the following decades, computers began to include more
and more software programs, sometimes called libraries, that formed the basis for today’s operating systems.
The first Operating System was created by General Motors in 1956 to run a single IBM mainframe
computer, its name was the IBM 704. IBM was the first computer manufacturer to develop operating
systems and distribute them in its computers in the 1960s.
There are few facts about Operating System evaluation:
 Stanford Research Institute developed the oN-Line System (NLS) in the late 1960s, which was the
first operating system that resembled the desktop operating system we use today.
 Microsoft bought QDOS (Quick and Dirty Operating System) in 1981 and branded it as Microsoft
Operating System (MS-DOS). As of 1994, Microsoft had stopped supporting MS-DOS.
 Unix was developed in the mid-1960s by the Massachusetts Institute of Technology, AT&T Bell
Labs, and General Electric as a joint effort. Initially it was named MULTICS, which stands for Multiplexed
Operating and Computing System.
 FreeBSD is also a popular UNIX derivative, originating from the BSD project at Berkeley. All
modern Macintosh computers run a modified version of FreeBSD (OS X).
 Windows 95 is a consumer-oriented graphical user interface-based operating system built on top of
MS-DOS. It was released on August 24, 1995 by Microsoft as part of its Windows 9x family of operating
systems.
 Solaris is a proprietary Unix operating system originally developed by Sun Microsystems in 1991.
After the Sun acquisition by Oracle in 2010 it was renamed Oracle Solaris.
An Operating System provides services to both the users and to the programs.

 It provides programs an environment to execute.


 It provides users the services to execute the programs in a convenient manner.

Following are a few common services provided by an operating system −

 Program execution
 I/O operations
 File System manipulation
 Communication
 Error Detection
 Resource Allocation
 Protection

Program execution

Operating systems handle many kinds of activities from user programs to system programs like printer
spooler, name servers, file server, etc. Each of these activities is encapsulated as a process.

A process includes the complete execution context (code to execute, data to manipulate, registers, OS
resources in use). Following are the major activities of an operating system with respect to program
management −

 Loads a program into memory.


 Executes the program.
 Handles program's execution.
 Provides a mechanism for process synchronization.
 Provides a mechanism for process communication.
 Provides a mechanism for deadlock handling.

I/O Operation

An I/O subsystem comprises of I/O devices and their corresponding driver software. Drivers hide the
peculiarities of specific hardware devices from the users.

An Operating System manages the communication between user and device drivers.

 I/O operation means read or write operation with any file or any specific I/O device.
 Operating system provides the access to the required I/O device when required.

File system manipulation

A file represents a collection of related information. Computers can store files on the disk (secondary
storage), for long-term storage purpose. Examples of storage media include magnetic tape, magnetic disk
and optical disk drives like CD, DVD. Each of these media has its own properties like speed, capacity, data
transfer rate and data access methods.

A file system is normally organized into directories for easy navigation and usage. These directories may
contain files and other directions. Following are the major activities of an operating system with respect to
file management −

 Program needs to read a file or write a file.


 The operating system gives the permission to the program for operation on file.
 Permission varies from read-only, read-write, denied and so on.
 Operating System provides an interface to the user to create/delete files.
 Operating System provides an interface to the user to create/delete directories.
 Operating System provides an interface to create the backup of file system.

Communication

In case of distributed systems which are a collection of processors that do not share memory, peripheral
devices, or a clock, the operating system manages communications between all the processes. Multiple
processes communicate with one another through communication lines in the network.

The OS handles routing and connection strategies, and the problems of contention and security. Following
are the major activities of an operating system with respect to communication −

 Two processes often require data to be transferred between them


 Both the processes can be on one computer or on different computers, but are connected through a
computer network.
 Communication may be implemented by two methods, either by Shared Memory or by Message
Passing.

Error handling

Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices or in the memory
hardware. Following are the major activities of an operating system with respect to error handling −

 The OS constantly checks for possible errors.


 The OS takes an appropriate action to ensure correct and consistent computing.

Resource Management

In case of multi-user or multi-tasking environment, resources such as main memory, CPU cycles and files
storage are to be allocated to each user or job. Following are the major activities of an operating system with
respect to resource management −

 The OS manages all kinds of resources using schedulers.


 CPU scheduling algorithms are used for better utilization of CPU.

Protection

Considering a computer system having multiple users and concurrent execution of multiple processes, the
various processes must be protected from each other's activities.

Protection refers to a mechanism or a way to control the access of programs, processes, or users to the
resources defined by a computer system. Following are the major activities of an operating system with
respect to protection −

 The OS ensures that all access to system resources is controlled.


 The OS ensures that external I/O devices are protected from invalid access attempts.
 The OS provides authentication features for each user by means of passwords.

Operating systems are there from the very first computer generation and they keep evolving with time. In
this chapter, we will discuss some of the important types of operating systems which are most commonly
used.
Batch operating system

The users of a batch operating system do not interact with the computer directly. Each user prepares his job
on an off-line device like punch cards and submits it to the computer operator. To speed up processing, jobs
with similar needs are batched together and run as a group. The programmers leave their programs with the
operator and the operator then sorts the programs with similar requirements into batches.

The problems with Batch Systems are as follows −

 Lack of interaction between the user and the job.


 CPU is often idle, because the speed of the mechanical I/O devices is slower than the CPU.
 Difficult to provide the desired priority.

Time-sharing operating systems

Time-sharing is a technique which enables many people, located at various terminals, to use a particular
computer system at the same time. Time-sharing or multitasking is a logical extension of multiprogramming.
Processor's time which is shared among multiple users simultaneously is termed as time-sharing.

The main difference between Multiprogrammed Batch Systems and Time-Sharing Systems is that in case of
Multiprogrammed batch systems, the objective is to maximize processor use, whereas in Time-Sharing
Systems, the objective is to minimize response time.

Multiple jobs are executed by the CPU by switching between them, but the switches occur so frequently.
Thus, the user can receive an immediate response. For example, in a transaction processing, the processor
executes each user program in a short burst or quantum of computation. That is, if n users are present, then
each user can get a time quantum. When the user submits the command, the response time is in few seconds
at most.

The operating system uses CPU scheduling and multiprogramming to provide each user with a small portion
of a time. Computer systems that were designed primarily as batch systems have been modified to time-
sharing systems.

Advantages of Timesharing operating systems are as follows −

 Provides the advantage of quick response.


 Avoids duplication of software.
 Reduces CPU idle time.

Disadvantages of Time-sharing operating systems are as follows −

 Problem of reliability.
 Question of security and integrity of user programs and data.
 Problem of data communication.

Distributed operating System

Distributed systems use multiple central processors to serve multiple real-time applications and multiple
users. Data processing jobs are distributed among the processors accordingly.

The processors communicate with one another through various communication lines (such as high-speed
buses or telephone lines). These are referred as loosely coupled systems or distributed systems. Processors
in a distributed system may vary in size and function. These processors are referred as sites, nodes,
computers, and so on.

The advantages of distributed systems are as follows −

 With resource sharing facility, a user at one site may be able to use the resources available at another.
 Speedup the exchange of data with one another via electronic mail.
 If one site fails in a distributed system, the remaining sites can potentially continue operating.
 Better service to the customers.
 Reduction of the load on the host computer.
 Reduction of delays in data processing.

Network operating System

A Network Operating System runs on a server and provides the server the capability to manage data, users,
groups, security, applications, and other networking functions. The primary purpose of the network
operating system is to allow shared file and printer access among multiple computers in a network, typically
a local area network (LAN), a private network or to other networks.

Examples of network operating systems include Microsoft Windows Server 2003, Microsoft Windows
Server 2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD.

The advantages of network operating systems are as follows −

 Centralized servers are highly stable.


 Security is server managed.
 Upgrades to new technologies and hardware can be easily integrated into the system.
 Remote access to servers is possible from different locations and types of systems.

The disadvantages of network operating systems are as follows −

 High cost of buying and running a server.


 Dependency on a central location for most operations.
 Regular maintenance and updates are required.

Real Time operating System

A real-time system is defined as a data processing system in which the time interval required to process and
respond to inputs is so small that it controls the environment. The time taken by the system to respond to an
input and display of required updated information is termed as the response time. So in this method, the
response time is very less as compared to online processing.

Real-time systems are used when there are rigid time requirements on the operation of a processor or the
flow of data and real-time systems can be used as a control device in a dedicated application. A real-time
operating system must have well-defined, fixed time constraints, otherwise the system will fail. For
example, Scientific experiments, medical imaging systems, industrial control systems, weapon systems,
robots, air traffic control systems, etc.

There are two types of real-time operating systems.

Hard real-time systems


Hard real-time systems guarantee that critical tasks complete on time. In hard real-time systems, secondary
storage is limited or missing and the data is stored in ROM. In these systems, virtual memory is almost
never found.

Soft real-time systems

Soft real-time systems are less restrictive. A critical real-time task gets priority over other tasks and retains
the priority until it completes. Soft real-time systems have limited utility than hard real-time systems. For
example, multimedia, virtual reality, Advanced Scientific Projects like undersea exploration and planetary
rovers, etc.

a system call is a programmatic way in which a computer program requests a service from the kernel of
the operating system it is executed on. A system call is a way for programs to interact with the operating
system. A computer program makes a system call when it makes a request to the operating system’s
kernel. System call provides the services of the operating system to the user programs via Application
Program Interface(API). It provides an interface between a process and an operating system to allow user-
level processes to request services of the operating system. System calls are the only entry points into
the kernel system. All programs needing resources must use system calls.
A user program can interact with the operating system using a system call. A number of services are
requested by the program, and the OS responds by launching a number of systems calls to fulfill the
request. A system call can be written in high-level languages like C or Pascal or in assembly language. If a
high-level language is used, the operating system may directly invoke system calls, which are predefined
functions.
A system call is a mechanism used by programs to request services from the operating system (OS). In
simpler terms, it is a way for a program to interact with the underlying system, such as accessing hardware
resources or performing privileged operations.
A system call is initiated by the program executing a specific instruction, which triggers a switch
to kernel mode, allowing the program to request a service from the OS. The OS then handles the request,
performs the necessary operations, and returns the result back to the program.
System calls are essential for the proper functioning of an operating system, as they provide a
standardized way for programs to access system resources. Without system calls, each program would
need to implement its own methods for accessing hardware and system services, leading to inconsistent
and error-prone behavior.
Services Provided by System Calls
 Process creation and management
 Main memory management
 File Access, Directory, and File system management
 Device handling(I/O)
 Protection
 Networking, etc.
o Process control: end, abort, create, terminate, allocate, and free memory.
o File management: create, open, close, delete, read files,s, etc.
o Device management
o Information maintenance
o Communication
Features of System Calls
 Interface: System calls provide a well-defined interface between user programs and the operating
system. Programs make requests by calling specific functions, and the operating system responds by
executing the requested service and returning a result.
 Protection: System calls are used to access privileged operations that are not available to normal
user programs. The operating system uses this privilege to protect the system from malicious or
unauthorized access.
 Kernel Mode: When a system call is made, the program is temporarily switched from user mode
to kernel mode. In kernel mode, the program has access to all system resources, including hardware,
memory, and other processes.
 Context Switching: A system call requires a context switch, which involves saving the state of the
current process and switching to the kernel mode to execute the requested service. This can introduce
overhead, which can impact system performance.
 Error Handling: System calls can return error codes to indicate problems with the requested
service. Programs must check for these errors and handle them appropriately.
 Synchronization: System calls can be used to synchronize access to shared resources, such as files
or network connections. The operating system provides synchronization mechanisms, such as locks or
semaphores, to ensure that multiple programs can access these resources safely.
System Calls Advantages
 Access to hardware resources: System calls allow programs to access hardware resources such as
disk drives, printers, and network devices.
 Memory management: System calls provide a way for programs to allocate and deallocate
memory, as well as access memory-mapped hardware devices.
 Process management: System calls allow programs to create and terminate processes, as well as
manage inter-process communication.
 Security: System calls provide a way for programs to access privileged resources, such as the
ability to modify system settings or perform operations that require administrative permissions.
 Standardization: System calls provide a standardized interface for programs to interact with the
operating system, ensuring consistency and compatibility across different hardware platforms and
operating system versions.
How does System Call Work?
Here is the detailed explanation step by step how system call work:
 User need special resources : Sometimes programs need to do some special things which can’t be
done without the permission of OS like reading from a file, writing to a file , getting any information
from the hardware or requesting a space in memory.
 Program makes a system call request : There are special predefined instruction to make a
request to the operating system. These instruction are nothing but just a “system call”. The program
uses these system calls in its code when needed.
 Operating system sees the system call : When the OS sees the system call then it recongnises that
the program need help at this time so it temporarily stop the program execution and give all the control
to special part of itself called ‘Kernel’ . Now ‘Kernel’ solve the need of program.
 Operating system performs the operations :Now the operating system perform the operation
which is requested by program . Example : reading content from a file etc.
 Operating system give control back to the program : After performing the special operation, OS
give control back to the program for further execution of program .
Examples of a System Call in Windows and Unix
System calls for Windows and Unix come in many different forms. These are listed in the table below as
follows:
Process Windows Unix

CreateProcess() Fork()
Process Control ExitProcess() Exit()
WaitForSingleObject() Wait()

Open()
CreateFile()
Read()
File manipulation ReadFile()
Write()
WriteFile()
Close()

Device Management SetConsoleMode() Ioctl()


Process Windows Unix

ReadConsole() Read()
WriteConsole() Write()

GetCurrentProcessID() Getpid()
Information Maintenance SetTimer() Alarm()
Sleep() Sleep()

CreatePipe() Pipe()
Communication CreateFileMapping() Shmget()
MapViewOfFile() Mmap()

SetFileSecurity() Chmod()
Protection InitializeSecurityDescriptor() Umask()
SetSecurityDescriptorgroup() Chown()

open(): Accessing a file on a file system is possible with the open() system call. It gives the file resources
it needs and a handle the process can use. A file can be opened by multiple processes simultaneously or
just one process. Everything is based on the structure and file system.
read(): Data from a file on the file system is retrieved using it. In general, it accepts three arguments:
1. A description of a file.
2. A buffer for read data storage.
3. How many bytes should be read from the file
Before reading, the file to be read could be identified by its file descriptor and opened using the open()
function.
wait(): In some systems, a process might need to hold off until another process has finished running
before continuing. When a parent process creates a child process, the execution of the parent process is
halted until the child process is complete. The parent process is stopped using the wait() system call. The
parent process regains control once the child process has finished running.
write(): Data from a user buffer is written using it to a device like a file. A program can produce data in
one way by using this system call. generally, there are three arguments:
1. A description of a file.
2. A reference to the buffer where data is stored.
3. The amount of data that will be written from the buffer in bytes.
fork(): The fork() system call is used by processes to create copies of themselves. It is one of the methods
used the most frequently in operating systems to create processes. When a parent process creates a child
process, the parent process’s execution is suspended until the child process is finished. The parent process
regains control once the child process has finished running.
exit(): A system call called exit() is used to terminate a program. In environments with multiple threads,
this call indicates that the thread execution is finished. After using the exit() system function, the
operating system recovers the resources used by the process.

Process

A process is basically a program in execution. The execution of a process must progress in a sequential
fashion.

A process is defined as an entity which represents the basic unit of work to be implemented in the system.

To put it in simple terms, we write our computer programs in a text file and when we execute this program,
it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─
stack, heap, text and data. The following image shows a simplified layout of a process inside main memory

S.N. Component & Description

Stack
1 The process Stack contains the temporary data such as method/function
parameters, return address and local variables.

Heap
2
This is dynamically allocated memory to a process during its run time.

Text
3 This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.

Data
4
This section contains the global and static variables.

Program

A program is a piece of code which may be a single line or millions of lines. A computer program is usually
written by a computer programmer in a programming language. For example, here is a simple program
written in C programming language −

#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}

A computer program is a collection of instructions that performs a specific task when executed by a
computer. When we compare a program with a process, we can conclude that a process is a dynamic
instance of a computer program.

A part of a computer program that performs a well-defined task is known as an algorithm. A collection of
computer programs, libraries and related data are referred to as a software.

Process Life Cycle

When a process executes, it passes through different states. These stages may differ in different operating
systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.

S.N. State & Description

Start
1
This is the initial state when a process is first started/created.

Ready
The process is waiting to be assigned to a processor. Ready processes are
2 waiting to have the processor allocated to them by the operating system so that
they can run. Process may come into this state after Start state or while running
it by but interrupted by the scheduler to assign CPU to some other process.

Running
3 Once the process has been assigned to a processor by the OS scheduler, the
process state is set to running and the processor executes its instructions.

Waiting
4 Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.

Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating
5
system, it is moved to the terminated state where it waits to be removed from
main memory.

Process Control Block (PCB)

A Process Control Block is a data structure maintained by the Operating System for every process. The PCB
is identified by an integer process ID (PID). A PCB keeps all the information needed to keep track of a
process as listed below in the table −

S.N. Information & Description

Process State
1 The current state of the process i.e., whether it is ready, running, waiting, or
whatever.

Process privileges
2
This is required to allow/disallow access to system resources.
Process ID
3
Unique identification for each of the process in the operating system.

Pointer
4
A pointer to parent process.

Program Counter
5 Program Counter is a pointer to the address of the next instruction to be
executed for this process.

CPU registers
6 Various CPU registers where process need to be stored for execution for
running state.

CPU Scheduling Information


7 Process priority and other scheduling information which is required to schedule
the process.

Memory management information


8 This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.

Accounting information
9 This includes the amount of CPU used for process execution, time limits,
execution ID etc.

IO status information
10
This includes a list of I/O devices allocated to the process.

The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB −

The PCB is maintained for a process throughout its lifetime, and is deleted once the process terminates.

Definition
The process scheduling is the activity of the process manager that handles the removal of the running
process from the CPU and the selection of another process on the basis of a particular strategy.

Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems
allow more than one process to be loaded into the executable memory at a time and the loaded process
shares the CPU using time multiplexing.

Categories of Scheduling

There are two categories of scheduling:

1. Non-preemptive: Here the resource can’t be taken from a process until the process completes
execution. The switching of resources occurs when the running process terminates and moves to a waiting
state.
2. Preemptive: Here the OS allocates the resources to a process for a fixed amount of time. During
resource allocation, the process switches from running state to ready state or from waiting state to ready
state. This switching occurs as the CPU may give priority to other processes and replace the process with
higher priority with the running process.

Process Scheduling Queues

The OS maintains all Process Control Blocks (PCBs) in Process Scheduling Queues. The OS maintains a
separate queue for each of the process states and PCBs of all processes in the same execution state are
placed in the same queue. When the state of a process is changed, its PCB is unlinked from its current queue
and moved to its new state queue.

The Operating System maintains the following important process scheduling queues −

 Job queue − This queue keeps all the processes in the system.
 Ready queue − This queue keeps a set of all processes residing in main memory, ready and waiting
to execute. A new process is always put in this queue.
 Device queues − The processes which are blocked due to unavailability of an I/O device constitute
this queue.

The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The OS
scheduler determines how to move processes between the ready and run queues which can only have one
entry per processor core on the system; in the above diagram, it has been merged with the CPU.

Two-State Process Model

Two-state process model refers to running and non-running states which are described below –
S.N. State & Description

Running
1
When a new process is created, it enters into the system as in the running state.

Not Running
Processes that are not running are kept in queue, waiting for their turn to
execute. Each entry in the queue is a pointer to a particular process. Queue is
2 implemented by using linked list. Use of dispatcher is as follows. When a
process is interrupted, that process is transferred in the waiting queue. If the
process has completed or aborted, the process is discarded. In either case, the
dispatcher then selects a process from the queue to execute.

Schedulers

Schedulers are special system software which handle process scheduling in various ways. Their main task is
to select the jobs to be submitted into the system and to decide which process to run. Schedulers are of three
types −

 Long-Term Scheduler
 Short-Term Scheduler
 Medium-Term Scheduler

Long Term Scheduler

It is also called a job scheduler. A long-term scheduler determines which programs are admitted to the
system for processing. It selects processes from the queue and loads them into memory for execution.
Process loads into the memory for CPU scheduling.

The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound and
processor bound. It also controls the degree of multiprogramming. If the degree of multiprogramming is
stable, then the average rate of process creation must be equal to the average departure rate of processes
leaving the system.

On some systems, the long-term scheduler may not be available or minimal. Time-sharing operating systems
have no long term scheduler. When a process changes the state from new to ready, then there is use of long-
term scheduler.

Short Term Scheduler

It is also called as CPU scheduler. Its main objective is to increase system performance in accordance with
the chosen set of criteria. It is the change of ready state to running state of the process. CPU scheduler
selects a process among the processes that are ready to execute and allocates CPU to one of them.

Short-term schedulers, also known as dispatchers, make the decision of which process to execute next.
Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler


Medium-term scheduling is a part of swapping. It removes the processes from the memory. It reduces the
degree of multiprogramming. The medium-term scheduler is in-charge of handling the swapped out-
processes.

A running process may become suspended if it makes an I/O request. A suspended processes cannot make
any progress towards completion. In this condition, to remove the process from memory and make space for
other processes, the suspended process is moved to the secondary storage. This process is called swapping,
and the process is said to be swapped out or rolled out. Swapping may be necessary to improve the process
mix.

Comparison among Scheduler

Short-Term Medium-Term
S.N. Long-Term Scheduler
Scheduler Scheduler

It is a process swapping
1 It is a job scheduler It is a CPU scheduler
scheduler.

Speed is in between both


Speed is lesser than Speed is fastest among
2 short and long term
short term scheduler other two
scheduler.

It provides lesser
It controls the degree of It reduces the degree of
3 control over degree of
multiprogramming multiprogramming.
multiprogramming

It is almost absent or
It is also minimal in It is a part of Time
4 minimal in time sharing
time sharing system sharing systems.
system

It can re-introduce the


It selects processes from It selects those
process into memory and
5 pool and loads them into processes which are
execution can be
memory for execution ready to execute
continued.

Context Switching

A context switching is the mechanism to store and restore the state or context of a CPU in Process Control
block so that a process execution can be resumed from the same point at a later time. Using this technique, a
context switcher enables multiple processes to share a single CPU. Context switching is an essential part of a
multitasking operating system features.

When the scheduler switches the CPU from executing one process to execute another, the state from the
current running process is stored into the process control block. After this, the state for the process to run
next is loaded from its own PCB and used to set the PC, registers, etc. At that point, the second process can
start executing.
Context switches are computationally intensive since register and memory state must be saved and restored.
To avoid the amount of context switching time, some hardware systems employ two or more sets of
processor registers. When the process is switched, the following information is stored for later use.

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information

There are many operations that can be performed on processes. Some of these are process creation, process
preemption, process blocking, and process termination. These are given in detail as follows −

Process Creation

Processes need to be created in the system for different operations. This can be done by the following events
User request for process creation

 System initialization
 Execution of a process creation system call by a running process
 Batch job initialization

A process may be created by another process using fork(). The creating process is called the parent process
and the created process is the child process. A child process can have only one parent but a parent process
may have many children. Both the parent and child processes have the same memory image, open files, and
environment strings. However, they have distinct address spaces.
A diagram that demonstrates process creation using fork() is as follows −

Process Preemption

An interrupt mechanism is used in preemption that suspends the process executing currently and the next
process to execute is determined by the short-term scheduler. Preemption makes sure that all processes get
some CPU time for execution.

A diagram that demonstrates process preemption is as follows −

Process Blocking

The process is blocked if it is waiting for some event to occur. This event may be I/O as the I/O events are
executed in the main memory and don't require the processor. After the event is complete, the process again
goes to the ready state.

A diagram that demonstrates process blocking is as follows −


Process Termination

After the process has completed the execution of its last instruction, it is terminated. The resources held by a
process are released after it is terminated.

A child process can be terminated by its parent process if its task is no longer relevant. The child process
sends its status information to the parent process before it terminates. Also, when a parent process is
terminated, its child processes are terminated as well as the child processes cannot run if the parent
processes are terminated.

A process can be of two types:


 Independent process.
 Co-operating process.
An independent process is not affected by the execution of other processes while a co-operating process
can be affected by other executing processes. Though one can think that those processes, which are
running independently, will execute very efficiently, in reality, there are many situations when co-
operative nature can be utilized for increasing computational speed, convenience, and modularity. Inter-
process communication (IPC) is a mechanism that allows processes to communicate with each other and
synchronize their actions. The communication between these processes can be seen as a method of co-
operation between them. Processes can communicate with each other through both:

1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via the shared memory
method and via the message passing method.

An operating system can implement both methods of communication. First, we will discuss the shared
memory methods of communication and then message passing. Communication between processes using
shared memory requires processes to share some variable, and it completely depends on how the
programmer will implement it. One way of communication using shared memory can be imagined like
this: Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory. When process2 needs to use the shared
information, it will check in the record stored in shared memory and take note of the information
generated by process1 and act accordingly. Processes can use shared memory for extracting information as
a record from another process as well as for delivering any specific information to other processes.
Let’s discuss an example of communication between processes using the shared memory method.
i) Shared Memory Method
Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. The producer produces some items and the Consumer
consumes that item. The two processes share a common space or memory location known as a buffer
where the item produced by the Producer is stored and from which the Consumer consumes the item if
needed. There are two versions of this problem: the first one is known as the unbounded buffer problem in
which the Producer can keep on producing items and there is no limit on the size of the buffer, the second
one is known as the bounded buffer problem in which the Producer can produce up to a certain number of
items before it starts waiting for Consumer to consume it. We will discuss the bounded buffer problem.
First, the Producer and the Consumer will share some common memory, then the producer will start
producing items. If the total produced item is equal to the size of the buffer, the producer will wait to get it
consumed by the Consumer. Similarly, the consumer will first check for the availability of the item. If no
item is available, the Consumer will wait for the Producer to produce it. If there are items available,
Consumer will consume them. The pseudo-code to demonstrate is provided below:
Shared Data between the two Processes

#define buff_max 25
#define mod %

struct item{
// different member of the produced data
// or consumed data
---------
}
// An array is needed for holding the items.
// This is the shared place which will be
// access by both process
// item shared_buff [ buff_max ];

// Two variables which will keep track of


// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
int free_index = 0;
int full_index = 0;

Producer Process Code

item nextProduced;
while(1){
// check if there is no space
// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index);
shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}
Consumer Process Code
item nextConsumed;
while(1){
// check if there is an available
// item for consumption.
// if not keep on waiting for
// get them produced.
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}

In the above code, the Producer will start producing again when the (free_index+1) mod buff max will be
free because if it is not free, this implies that there are still items that can be consumed by the Consumer
so there is no need to produce more. Similarly, if free index and full index point to the same index, this
implies that there are no items to consume.
Overall C++ Implementation:

#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#define buff_max 25
#define mod %
struct item {
// different member of the produced data
// or consumed data
// ---------
};
// An array is needed for holding the items.
// This is the shared place which will be
// access by both process
// item shared_buff[buff_max];
// Two variables which will keep track of
// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
std::atomic<int> free_index(0);
std::atomic<int> full_index(0);
std::mutex mtx;
void producer() {
item new_item;
while (true) {
// Produce the item
// ...
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Add the item to the buffer
while (((free_index + 1) mod buff_max) == full_index) {
// Buffer is full, wait for consumer
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
mtx.lock();
// Add the item to the buffer
// shared_buff[free_index] = new_item;
free_index = (free_index + 1) mod buff_max;
mtx.unlock();
}
}
void consumer() {
item consumed_item;
while (true) {
while (free_index == full_index) {
// Buffer is empty, wait for producer
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
mtx.lock();
// Consume the item from the buffer
// consumed_item = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
mtx.unlock();
// Consume the item
// ...
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main() {
// Create producer and consumer threads
std::vector<std::thread> threads;
threads.emplace_back(producer);
threads.emplace_back(consumer);
// Wait for threads to finish
for (auto& thread : threads) {
thread.join();
}
return 0;
}

Note that the atomic class is used to make sure that the shared variables free_index and full_index are
updated atomically. The mutex is used to protect the critical section where the shared buffer is accessed.
The sleep_for function is used to simulate the production and consumption of items.
ii) Messaging Passing Method
Now, We will start our discussion of the communication between processes via message passing. In this
method, processes communicate with each other without using any kind of shared memory. If two
processes p1 and p2 want to communicate with each other, they proceed as follows:

 Establish a communication link (if a link already exists, no need to establish it again.)
 Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)
The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an OS designer
but complicated for a programmer and if it is of variable size then it is easy for a programmer but
complicated for the OS designer. A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id, message length, and control
information. The control information contains information like what to do if runs out of buffer space,
sequence number, priority. Generally, message is sent using FIFO style.

Message Passing through Communication Link.


Direct and Indirect Communication link
Now, We will start our discussion about the methods of implementing communication links. While
implementing the link, there are some questions that need to be kept in mind like :

1. How are links established?


2. Can a link be associated with more than two processes?
3. How many links can there be between every pair of communicating processes?
4. What is the capacity of a link? Is the size of a message that the link can accommodate fixed or
variable?
5. Is a link unidirectional or bi-directional?
A link has some capacity that determines the number of messages that can reside in it temporarily for
which every link has a queue associated with it which can be of zero capacity, bounded capacity, or
unbounded capacity. In zero capacity, the sender waits until the receiver informs the sender that it has
received the message. In non-zero capacity cases, a process does not know whether a message has been
received or not after the send operation. For this, the sender must communicate with the receiver
explicitly. Implementation of the link depends on the situation, it can be either a direct communication
link or an in-directed communication link.
Direct Communication links are implemented when the processes use a specific process identifier for the
communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue of messages.
The sender keeps the message in mailbox and the receiver picks them up.

Message Passing through Exchanging the Messages.


Synchronous and Asynchronous Message Passing:
A process that is blocked is one that is waiting for some event, such as a resource becoming available or
the completion of an I/O operation. IPC is possible between the processes on same computer as well as on
the processes running on different computer i.e. in networked/distributed system. In both cases, the
process may or may not be blocked while sending a message or attempting to receive a message so
message passing may be blocking or non-blocking. Blocking is considered synchronous and blocking
send means the sender will be blocked until the message is received by receiver. Similarly, blocking
receive has the receiver block until a message is available. Non-blocking is considered asynchronous and
Non-blocking send has the sender sends the message and continue. Similarly, Non-blocking receive has
the receiver receive a valid message or null. After a careful analysis, we can come to a conclusion that for
a sender it is more natural to be non-blocking after message passing as there may be a need to send the
message to different processes. However, the sender expects acknowledgment from the receiver in case
the send fails. Similarly, it is more natural for a receiver to be blocking after issuing the receive as the
information from the received message may be used for further execution. At the same time, if the
message send keep on failing, the receiver will have to wait indefinitely. That is why we also consider the
other possibility of message passing. There are basically three preferred combinations:

 Blocking send and blocking receive


 Non-blocking send and Non-blocking receive
 Non-blocking send and Blocking receive (Mostly used)
In Direct message passing, The process which wants to communicate must explicitly name the recipient
or sender of the communication.
e.g. send(p1, message) means send the message to p1.
Similarly, receive(p2, message) means to receive the message from p2.
In this method of communication, the communication link gets established automatically, which can be
either unidirectional or bidirectional, but one link can be used between one pair of the sender and receiver
and one pair of sender and receiver should not possess more than one pair of links. Symmetry and
asymmetry between sending and receiving can also be implemented i.e. either both processes will name
each other for sending and receiving the messages or only the sender will name the receiver for sending
the message and there is no need for the receiver for naming the sender for receiving the message. The
problem with this method of communication is that if the name of one process changes, this method will
not work.
In Indirect message passing, processes use mailboxes (also referred to as ports) for sending and
receiving messages. Each mailbox has a unique id and processes can communicate only if they share a
mailbox. Link established only if processes share a common mailbox and a single link can be associated
with many processes. Each pair of processes can share several communication links and these links may
be unidirectional or bi-directional. Suppose two processes want to communicate through Indirect message
passing, the required operations are: create a mailbox, use this mailbox for sending and receiving
messages, then destroy the mailbox. The standard primitives used are: send(A, message) which means
send the message to mailbox A. The primitive for the receiving the message also works in the same way
e.g. received (A, message). There is a problem with this mailbox implementation. Suppose there are more
than two processes sharing the same mailbox and suppose the process p1 sends a message to the mailbox,
which process will be the receiver? This can be solved by either enforcing that only two processes can
share a single mailbox or enforcing that only one process is allowed to execute the receive at a given time
or select any process randomly and notify the sender about the receiver. A mailbox can be made private to
a single sender/receiver pair and can also be shared between multiple sender/receiver pairs. Port is an
implementation of such mailbox that can have multiple senders and a single receiver. It is used in
client/server applications (in this case the server is the receiver). The port is owned by the receiving
process and created by OS on the request of the receiver process and can be destroyed either on request of
the same receiver processor when the receiver terminates itself. Enforcing that only one process is allowed
to execute the receive can be done using the concept of mutual exclusion. Mutex mailbox is created
which is shared by n process. The sender is non-blocking and sends the message. The first process which
executes the receive will enter in the critical section and all other processes will be blocking and will wait.
Now, let’s discuss the Producer-Consumer problem using the message passing concept. The producer
places items (inside messages) in the mailbox and the consumer can consume an item when at least one
message present in the mailbox. The code is given below:
Producer Code

void Producer(void){
int item;
Message m;
while(1){
receive(Consumer, &m);
item = produce();
build_message(&m , item ) ;
send(Consumer, &m);
}
}

Consumer Code

void Consumer(void){

int item;
Message m;

while(1){

receive(Producer, &m);
item = extracted_item();
send(Producer, &m);
consume_item(item);
}
}

Examples of IPC systems

1. Posix : uses shared memory method.


2. Mach : uses message passing
3. Windows XP : uses message passing using local procedural calls
Communication in client/server Architecture:
There are various mechanism:

 Pipe
 Socket
 Remote Procedural calls (RPCs)
The above three methods will be discussed in later articles as all of them are quite conceptual and deserve
their own separate articles.
Advantages of IPC:
1. Enables processes to communicate with each other and share resources, leading to increased
efficiency and flexibility.
2. Facilitates coordination between multiple processes, leading to better overall system performance.
3. Allows for the creation of distributed systems that can span multiple computers or networks.
4. Can be used to implement various synchronization and communication protocols, such as
semaphores, pipes, and sockets.

Disadvantages of IPC:

1. Increases system complexity, making it harder to design, implement, and debug.


2. Can introduce security vulnerabilities, as processes may be able to access or modify data belonging
to other processes.
3. Requires careful management of system resources, such as memory and CPU time, to ensure that
IPC operations do not degrade overall system performance.
Can lead to data inconsistencies if multiple processes try to access or modify the same data at the same
time.
4. Overall, the advantages of IPC outweigh the disadvantages, as it is a necessary mechanism for
modern operating systems and enables processes to work together and share resources in a flexible and
efficient manner. However, care must be taken to design and implement IPC systems carefully, in
order to avoid potential security vulnerabilities and performance issues.

You might also like