Os Unit I
Os Unit I
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.
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 −
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.
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 −
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 −
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 −
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 −
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 −
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.
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.
Problem of reliability.
Question of security and integrity of user programs and data.
Problem of data communication.
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.
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.
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.
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.
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()
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
−
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.
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.
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.
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 −
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.
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
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.
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 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
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.
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.
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.
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.
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
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.
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.
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.
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 ];
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.
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);
}
}
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: