Shell Scripting
There are certain expressions that convey special meanings. In other
words, they are not what they look like. A shell carries out substitution
whenever it encounters such expressions. Hence, substitution is defined
as a mechanism carried out by a shell in which it substitutes the value of
an expression with its actual value.
Escape sequences:
An escape sequence is a group of character(s) that does not represent its
actual value when it is used as a string literal. Some of the escape
sequences are listed below:
Sr. No. Escape Sequences Significance (actual value)
1 \n new line
2 \f form feed
3 \r carriage return
4 \b backspace
5 \t horizontal tab
6 \v vertical tab
7 \\ backslash
Shell substitutes an escape sequence with its actual value.
Example:
In this shell script, firstly we have used an echo command to print a
string. Note that we have used an escape sequence ( \n ) at the end of
the string. It will add a new line character after printing the string.
#!/bin/sh
// Print the string
echo -e "Hello World! \n"
// Print the string
echo -e "Hello GeeksforGeeks"
Output:
Variable Substitution:
The shell allows us to manipulate the value of a variable based upon its
initialization status.
Sr
No. Expression Significance
1 ${myVariable} substitute the value of myVariable.
If myVariable is not-set (or null) then the value is
2 ${myVariable:-value}
substituted for myVariable.
If myVariable is not-set (or null), then it is set
3 ${myVariable:=value}
to value.
${myVariable:? If myVariable is not-set (or null) then
4
message} the message is printed as standard error.
If myVariable is set then the value is substituted
5 ${myVariable:+value}
for myVariable.
Example:
These expressions are demonstrated in the below shell script.
#!/bin/sh
# If myVariable is unset or null
# then assign 12 to it
echo ${myVariable:- 11}
echo "1. The value of myVariable is ${myVariable}"
# If myVariable is unset or null
# then assign "GeeksforGeeks" to it
echo ${myVariable:="GeeksforGeeks"}
echo "2. Value of myVariable is ${myVariable}"
# unset myVariable
unset myVariable
# If myVariable is set then substitute
# the value
echo ${myVariable:+"GeeksforGeeks"}
echo "3. Value of myVariable is $myVariable"
myVariable="GeeksforGeeks"
# If myVariable is set then substitute
# the value
echo ${myVariable:+"Bhuwanesh"}
echo "4. Value of myVariable is $myVariable"
# If myVaraible is not-set or null then
# print the message
echo ${myVariable:?"message"}
echo "5. Value of myVariable is ${myVariable}"
unset myVariable
# If myVaraible is not-set or null then
# print the message
echo ${myVariable:?"message"}
echo "6. Value of myVariable is ${myVariable}"
Output:
Command Substitution:
Command substitution is a mechanism that is followed by programmers
in a bash script. In this mechanism, the output of a command replaces the
command itself. Bash operates the expansion by executing a command
and then replacing the command substitution with the standard output of
the command. In simple words, the output of a UNIX command is
bundled and then used as a command.
To understand it in a better way, let us consider an example. The seq
command in Linux is used to print numbers from START to END in steps
of INCREMENT.
Syntax:
seq START INCREMENT END
Return type:
Prints numbers from START to END each in the new line by the
difference of INCREMENT.
Example:
In the below script we are printing numbers from 2 to 20 with a
difference of 2. In other words, we are printing even numbers up to 30.
#!/bin/bash
# your code goes here
seq 2 2 30
Output:
We can use the output of the above command as a new command.
Consider the below script,
Example:
#!/bin/bash
# your code goes here
echo $(seq 2 2 20)
Output:
References
1. Abraham Silber schatz, Peter Baer Galvin, Greg Gagne: "Operating
System Principles", Wiley India, 7th edition, 2006.
2. William Stallings: "Operating Systems: Internals and Design
Principles", Pearson, 6th edition, 2009
3. Sumitabha Das, "Unix concepts and applications" McGraw Hill
Education; 4th edition (1 July 2017)
4. [Link]
5. [Link]
6. [Link]
Shell Scripting
There are certain expressions that convey special meanings. In other
words, they are not what they look like. A shell carries out substitution
whenever it encounters such expressions. Hence, substitution is defined
as a mechanism carried out by a shell in which it substitutes the value of
an expression with its actual value.
Escape sequences:
An escape sequence is a group of character(s) that does not represent its
actual value when it is used as a string literal. Some of the escape
sequences are listed below:
Sr. No. Escape Sequences Significance (actual value)
1 \n new line
2 \f form feed
3 \r carriage return
4 \b backspace
5 \t horizontal tab
6 \v vertical tab
7 \\ backslash
Shell substitutes an escape sequence with its actual value.
Example:
In this shell script, firstly we have used an echo command to print a string.
Note that we have used an escape sequence ( \n ) at the end of the string.
It will add a new line character after printing the string.
#!/bin/sh
// Print the string
echo -e "Hello World! \n"
// Print the string
echo -e "Hello GeeksforGeeks"
Output:
Variable Substitution:
The shell allows us to manipulate the value of a variable based upon its
initialization status.
Sr
No. Expression Significance
1 ${myVariable} substitute the value of myVariable.
If myVariable is not-set (or null) then the value is
2 ${myVariable:-value}
substituted for myVariable.
If myVariable is not-set (or null), then it is set
3 ${myVariable:=value}
to value.
${myVariable:? If myVariable is not-set (or null) then
4
message} the message is printed as standard error.
If myVariable is set then the value is substituted
5 ${myVariable:+value}
for myVariable.
Example:
These expressions are demonstrated in the below shell script.
#!/bin/sh
# If myVariable is unset or null
# then assign 12 to it
echo ${myVariable:- 11}
echo "1. The value of myVariable is ${myVariable}"
# If myVariable is unset or null
# then assign "GeeksforGeeks" to it
echo ${myVariable:="GeeksforGeeks"}
echo "2. Value of myVariable is ${myVariable}"
# unset myVariable
unset myVariable
# If myVariable is set then substitute
# the value
echo ${myVariable:+"GeeksforGeeks"}
echo "3. Value of myVariable is $myVariable"
myVariable="GeeksforGeeks"
# If myVariable is set then substitute
# the value
echo ${myVariable:+"Bhuwanesh"}
echo "4. Value of myVariable is $myVariable"
# If myVaraible is not-set or null then
# print the message
echo ${myVariable:?"message"}
echo "5. Value of myVariable is ${myVariable}"
unset myVariable
# If myVaraible is not-set or null then
# print the message
echo ${myVariable:?"message"}
echo "6. Value of myVariable is ${myVariable}"
Output:
Command Substitution:
Command substitution is a mechanism that is followed by programmers in
a bash script. In this mechanism, the output of a command replaces the
command itself. Bash operates the expansion by executing a command
and then replacing the command substitution with the standard output of
the command. In simple words, the output of a UNIX command is bundled
and then used as a command.
To understand it in a better way, let us consider an example. The seq
command in Linux is used to print numbers from START to END in steps
of INCREMENT.
Syntax:
seq START INCREMENT END
Return type:
Prints numbers from START to END each in the new line by the difference
of INCREMENT.
Example:
In the below script we are printing numbers from 2 to 20 with a difference
of 2. In other words, we are printing even numbers up to 30.
#!/bin/bash
# your code goes here
seq 2 2 30
Output:
We can use the output of the above command as a new command.
Consider the below script,
Example:
#!/bin/bash
# your code goes here
echo $(seq 2 2 20)
Output:
References
1. Abraham Silber schatz, Peter Baer Galvin, Greg Gagne: "Operating
System Principles", Wiley India, 7th edition, 2006.
2. William Stallings: "Operating Systems: Internals and Design
Principles", Pearson, 6th edition, 2009
3. Sumitabha Das, "Unix concepts and applications" McGraw Hill
Education; 4th edition (1 July 2017)
4. [Link]
5. [Link]
6. [Link]
Shell Scripting – Functions and it’s types
Shell scripting is a powerful tool used to automate tasks in Unix-like operating
systems. A shell serves as a command-line interpreter, and shell scripts
often perform file manipulation, program execution, and text output. Here,
we’ll look into functions in shell scripting, exploring their structure, usage,
and types to help you effectively incorporate them into your scripts.
What is a Function in Shell Scripting?
A function is a collection of statements that execute a specified task. Its main
goal is to break down a complicated procedure into simpler subroutines that
can subsequently be used to accomplish the more complex routine. For the
following reasons, functions are popular:
Code Reusability: Functions allow you to write code once and
use it multiple times throughout your script.
Enhanced Readability: Functions help organize code into logical
blocks, making scripts easier to read and understand.
Modularity: Functions enable modular programming, where
different tasks are handled by separate, self-contained functions.
Ease of Maintenance: Functions simplify debugging and
updating code since changes can be made in one place rather
than throughout the script.
Basic Structure of a Function in Shell Scripting
The basic syntax for defining a function in shell scripting is:
function_name(){
// body of the function
}
The function_name can be any valid string and the body can be
any sequence of valid statements in the scripting language.
The body of the function can include any sequence of valid shell
commands or statements.
Example
Let us try to understand the concept of functions by looking at an example.
The following is a code to print all the prime numbers between a range
[le,ri].
It consists of a function ‘is_prime()‘ which is used to check if the given
number is a prime or not. In this function, we use the variable ‘$1’ to access
the first argument, which is the number itself. In scripting languages, we can
access arguments by ‘$i’, where ‘i’ is a number that signifies the position of
the argument.
echo -n "Enter Left-End: "
read le
echo -n "Enter Right-End: "
read ri
is_prime(){
if [ $1 -lt 2 ]; then
return
fi
ctr=0
for((i=2;i<$1;i++)){
if [ $(( $1 % i )) -eq 0 ]; then
ctr=$(( ctr +1 ))
fi
}
if [ $ctr -eq 0 ]; then
printf "%d " "$1"
fi
}
printf "Prime Numbers between %d and %d are: " "$le" "$ri"
for((i=le;i<=ri;i++)){
is_prime $i
}
printf "\n"
Types of Functions
The functions in shell scripting can be boxed into a number of categories.
The following are some of them:
1. The functions that return a value to the caller.
The return keyword is used by the functions for this purpose. The following
is one such function used to calculate the average of the given numbers.
find_avg(){
len=$#
sum=0
for x in "$@"
do
sum=$((sum + x))
done
avg=$((sum/len))
return $avg
}
find_avg 30 40 50 60
printf "%f" "$?"
printf "\n"
Output:
Explanation:
The function ‘find_avg()’ calculates the average of the given
numbers and returns it using the ‘return’ statement.
Since the ‘return’ value is restricted to integers between 0 and
255, it prints the result using ‘$?’, which holds the exit status of the
last executed command.
2. The functions that terminate the shell using the ‘exit’
keyword.
These functions use the ‘exit’ command to stop the shell entirely. This can
be useful for error handling or when a certain condition requires the script to
stop immediately.
is_odd(){
x=$1
if [ $((x%2)) == 0 ]; then
echo "Invalid Input"
exit 1
else
echo "Number is Odd"
fi
}
is_odd 64
Output:
Explanation: The function ‘is_odd()’ checks if a number is odd. If it’s even,
it outputs an error message and exits the script using ‘exit 1’.
3. The functions that alter the value of a variable or variables.
These functions can modify the values of variables directly within their scope
or globally if the variables are defined outside the function.
a=1
increment(){
a=$((a+1))
return
}
increment
echo "$a"
Output:
Explanation: The function ‘increment()’ increases the value of the variable
‘a’ by 1, demonstrating how functions can directly alter variable values.
4. The functions that echo output to the standard output.
These functions output information directly to the standard output using the
‘echo’ command.
hello_world(){
echo "Hello World"
return
}
hello_world
Output:
Explanation: The function ‘hello_world()’ simply prints “Hello World” to the
standard output, illustrating a basic use of echoing in functions.
References
1. Abraham Silber schatz, Peter Baer Galvin, Greg Gagne: "Operating
System Principles", Wiley India, 7th edition, 2006.
2. William Stallings: "Operating Systems: Internals and Design
Principles", Pearson, 6th edition, 2009
3. Sumitabha Das, "Unix concepts and applications" McGraw Hill
Education; 4th edition (1 July 2017)
4. [Link]
5. [Link]
6. [Link]
What is Linux Operating System
The Linux Operating System is a type of operating system that is similar to
Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain
of the operating system because it manages how the computer interacts with
its hardware and resources. It makes sure everything works smoothly and
efficiently. But the Linux Kernel alone is not enough to make a complete
operating system. To create a full and functional system, the Linux Kernel is
combined with a collection of software packages and utilities, which are
together called Linux distributions. These distributions make the Linux
Operating System ready for users to run their applications and perform tasks
on their computers securely and effectively. Linux distributions come in
different flavors, each tailored to suit the specific needs and preferences of
users.
Table of Content
Linux History
What is Linux?
What is Linux Operating System?
What is a “distribution?”
Why use Linux?
Architecture of Linux
Which distribution is right for you?
Installing Linux
Installing software on Linux
Advantages of Linux
Disadvantages of Linux
Linux Commands
Linux History
Linus Torvalds designed the free and open-source Linux operating system
kernel in 1991. Torvalds set out to develop a free and flexible system
for personal computers, drawing ideas from the UNIX operating system and
the MINIX operating system. Teamwork in development was encouraged
with the initial release of the Linux kernel, which attracted developers and
enthusiasts globally quickly. Various open-source software packages
integrated with the Linux kernel created fully operational operating systems,
occasionally referred to as Linux distributions. Over the years, Linux has
become known as a key component of modern computing, powering
everything from servers and personal computers to supercomputers and
smartphones. Due to its flexibility, durability, and strong community
support, developers, businesses, and educational institutions frequently opt
for it.
What is Linux?
Linux is a free and open-source family of operating systems that is resilient
and flexible. In 1991, an individual by the name as Linus Torvalds
constructed it. The system’s source code is accessible to everyone for
anyone to look at and change, making it cool that anyone can see how the
system works. People from all across the world are urged to work together
and keep developing Linux due to its openness. Since the beginning, Linux
has grown into a dependable and safe OS that is used in an array of
gadgets, including PCs, cell phones, and huge supercomputers. It is well-
known for being cost-effective, which implies that employing it doesn’t cost
a lot, and efficient, which indicates it can complete a lot of jobs quickly. A
lot of people love Linux, and
What is Linux Operating System?
Developed by Linus Torvalds in 1991, the Linux operating system is a
powerful and flexible open-source software platform. It acts as the basis for
a variety of devices, such embedded systems, cell phones, servers, and
personal computers. Linux, that’s well-known for its reliability, safety, and
flexibility, allows users to customize and improve their environment to suit
specific needs. With an extensive and active community supporting it, Linux
is an appealing choice for people as well as companies due to its wealth of
resources and constant developments.
What is a “distribution?”
Linux distribution is an operating system that is made up of a collection of
software based on Linux kernel or you can say distribution contains the
Linux kernel and supporting libraries and software. And you can get Linux-
based operating system by downloading one of the Linux distributions and
these distributions are available for different types of devices like
embedded devices, personal computers, etc. Around 600 + Linux
Distributions are available and some of the popular Linux distributions
are:
MX Linux
Manjaro
Linux Mint
elementary
Ubuntu
Debian
Solus
Fedora
openSUSE
Deepin
Why use Linux?
Because it is free, open-source, and extremely flexible, Linux is widely
utilized. For servers and developers, it is the ideal option because it offers
strong security, stability, and performance. Generally interoperable
hardware, a broad software library, and a vibrant community that offers
support and regular updates are the many benefits of Linux. Due to its
adaptability, users can customize the operating system according to their
own needs, whether they become for personal or large enterprise use.
Architecture of Linux
Linux architecture has the following components:
Linux Architecture
1. Kernel: Kernel is the core of the Linux based operating system. It
virtualizes the common hardware resources of the computer to
provide each process with its virtual resources. This makes the
process seem as if it is the sole process running on the machine.
The kernel is also responsible for preventing and mitigating
conflicts between different processes. Different types of the kernel
are:
Monolithic Kernel
Hybrid kernels
Exo kernels
Micro kernels
2. System Library: Linux uses system libraries, also known as
shared libraries, to implement various functionalities of the
operating system. These libraries contain pre-written code that
applications can use to perform specific tasks. By using these
libraries, developers can save time and effort, as they don’t need
to write the same code repeatedly. System libraries act as an
interface between applications and the kernel, providing a
standardized and efficient way for applications to interact with the
underlying system.
3. Shell: The shell is the user interface of the Linux Operating
System. It allows users to interact with the system by entering
commands, which the shell interprets and executes. The shell
serves as a bridge between the user and the kernel, forwarding
the user’s requests to the kernel for processing. It provides a
convenient way for users to perform various tasks, such as
running programs, managing files, and configuring the system.
4. Hardware Layer: The hardware layer encompasses all the
physical components of the computer, such as RAM (Random
Access Memory), HDD (Hard Disk Drive), CPU (Central
Processing Unit), and input/output devices. This layer is
responsible for interacting with the Linux Operating System and
providing the necessary resources for the system and applications
to function properly. The Linux kernel and system libraries enable
communication and control over these hardware components,
ensuring that they work harmoniously together.
5. System Utility: System utilities are essential tools and programs
provided by the Linux Operating System to manage and configure
various aspects of the system. These utilities perform tasks such
as installing software, configuring network settings, monitoring
system performance, managing users and permissions, and much
more. System utilities simplify system administration tasks,
making it easier for users to maintain their Linux systems
efficiently.
Which distribution is right for you?
Choosing the right Linux distribution depends on your needs and
experience level:
For Beginners: Because of its simple user interface and strong
community support, Ubuntu is a wonderful choice for initially Linux
users. On the opposite hand, Linux Mint make it straightforward
for novices to transition to Linux by offering an experience
comparable to Windows out of the box.
For Advanced Users: Advanced users who appreciate
customization and direct control might opt for Arch Linux, it is
known for its simplistic style and ability to create highly unique
systems from the ground up. Another choice is Gentoo, that
provides total control of the system but requires manual setup and
a lengthy learning process.
For Developers: Fedora was a popular choice among developers
due to its focus upon modern technology and software, making it a
perfect platform for software testing and development. On the
other hand, Debian is well known for its reliability and extensive
package repository, which implies it may be used in both
production and development environments.
For Servers: For server environments, CentOS is a powerful,
community-maintained distribution that matches Red Hat
Enterprise Linux (RHEL) quite somewhat. As an alternative,
Ubuntu Server offers an extensive list of server applications in
addition to strong community support and ease of use.
For Lightweight Systems: Lubuntu is frequently picked by users
either like lightweight operating systems or have outdated
equipment due to its ability to utilize system resources efficiently
while maintaining functionality. Another slim option is Puppy Linux,
that is made to run well on outdated hardware while maintaining
the essential functions and applications.
Installing Linux
Selecting a Ubuntu, Fedora, or Linux Mint distribution which suits your
needs is the initial step in the straightforward procedure for installing Linux.
Download the ISO file first from the distribution’s official website. Next,
utilize an application like Etcher for macOS and Linux or Rufus for
Windows to create a bootable USB drive. Following you insert the USB
drive into your computer and restart it, you may set the USB drive as the
primary boot device by traversing to the BIOS or UEFI settings. Upon
booting from the USB the hard drive, the Linux setup will show up. To
partition the drive, choose your time zone, create user accounts, and
change system settings, simply adhere to the instructions displayed on the
screen. When the installation concludes, disconnect the USB drive and turn
on your computer normally. For mare detailed way to install the Linux Mint
Refer this link.
Installing software on Linux
On Linux, installing software is simple. For Debian-based systems (like
Ubuntu), use package managers like apt and sudo apt install
package_name; for Fedora, use dnf and sudo dnf install package_name.
Software centers are another source for a graphical application installation
and searching interface. For Python installation specifics, detailed guidance
can be found in the provided link.
Advantages of Linux
The main advantage of Linux is it is an open-source operating
system. This means the source code is easily available for
everyone and you are allowed to contribute, modify and distribute
the code to anyone without any permissions.
In terms of security, Linux is more secure than any other operating
system. It does not mean that Linux is 100 percent secure, it has
some malware for it but is less vulnerable than any other
operating system. So, it does not require any anti-virus software.
The software updates in Linux are easy and frequent.
Various Linux distributions are available so that you can use them
according to your requirements or according to your taste.
Linux is freely available to use on the internet.
It has large community support.
It provides high stability. It rarely slows down or freezes and there
is no need to reboot it after a short time.
It maintains the privacy of the user.
The performance of the Linux system is much higher than other
operating systems. It allows a large number of people to work at
the same time and it handles them efficiently.
It is network friendly.
The flexibility of Linux is high. There is no need to install a
complete Linux suite; you are allowed to install only the required
components.
Linux is compatible with a large number of file formats.
It is fast and easy to install from the web. It can also install it on
any hardware even on your old computer system.
It performs all tasks properly even if it has limited space on the
hard disk.
Disadvantages of Linux
It is not very user-friendly. So, it may be confusing for beginners.
It has small peripheral hardware drivers as compared to windows.
Linux Commands
Basic tools for utilizing the command line interface (CLI) to communicate
with the operating system are Linux commands. Commonly used
commands include ls to list contents of directories, cd to modify directories,
and pwd to show the path of the current directory. With commands like cp
(copy), mv (move), and rm (delete), someone can manage file activities.
Commands for system information and management include free for
memory use, df to evaluate disk space usage, and top for monitoring
system processes. Utilizing networking commands such
netstat, ifconfig, and ping, users can monitor and troubleshoot network
connections. Package management differs depending on the distribution;
Fedora-based machines use dnf, while Debian-based systems use apt.
Commands like cat, grep, awk, and sed are required for editing text files.
Knowing and performing these directions well enables. For clear
understanding about the Linux commands refer this link.
References
1. Abraham Silber schatz, Peter Baer Galvin, Greg Gagne: "Operating
System Principles", Wiley India, 7th edition, 2006.
2. William Stallings: "Operating Systems: Internals and Design
Principles", Pearson, 6th edition, 2009
3. Sumitabha Das, "Unix concepts and applications" McGraw Hill
Education; 4th edition (1 July 2017)
4. [Link]
5. [Link]
6. [Link]
Inter Process Communication (IPC)
Processes can coordinate and interact with one another using a method
called inter-process communication (IPC) . Through facilitating process
collaboration, it significantly contributes to improving the effectiveness,
modularity, and ease of software systems.
Types of Process
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 cooperative 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
cooperation between them. Processes can communicate with each other
through both:
Methods of IPC
Shared Memory
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:
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 :
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link? Is the size of a message that the
link can accommodate fixed or variable?
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.
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:
Examples of IPC systems
Posix : uses shared memory method.
Mach : uses message passing
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.
References:
Operating System Concepts by Galvin et al.
Lecture notes/ppt of Ariel J. Frank, Bar-Ilan University
Inter-process communication (IPC) is the mechanism through which
processes or threads can communicate and exchange data with each other
on a computer or across a network. IPC is an important aspect of modern
operating systems, as it enables different processes to work together and
share resources, leading to increased efficiency and flexibility.
Role of Synchronization in IPC
In IPC, synchronization is essential for controlling access to shared
resources and guaranteeing that processes do not conflict with one
another. Data consistency is ensured and problems like race situations are
avoided with proper synchronization.
Advantages of IPC
Enables processes to communicate with each other and share
resources, leading to increased efficiency and flexibility.
Facilitates coordination between multiple processes, leading to
better overall system performance.
Allows for the creation of distributed systems that can span
multiple computers or networks.
Can be used to implement various synchronization and
communication protocols, such as semaphores, pipes, and
sockets.
Disadvantages of IPC
Increases system complexity, making it harder to design,
implement, and debug.
Can introduce security vulnerabilities, as processes may be able
to access or modify data belonging to other processes.
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.
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.