0% found this document useful (0 votes)
22 views

LP Unit4

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

LP Unit4

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

Inter Process Communication

UNIT-IV
Process Types
• A process can be of two types:
1. Independent process
2. 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.
Process Types
• Advantages of Co-operative Process:
• Increasing computational speed
• Convenience
• Modularity.
What is IPC?
• 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.
What is IPC?
• Sharing of information between processes.
• IPC is a mechanism to transfer data between
processes
Inter-process communication (IPC) serves as a means for
transmitting data among multiple threads situated within one
or more processes or programs. These processes may be
active on a solitary computer or distributed across a network
of machines.
It is a set of programming interfaces that enable a
programmer to coordinate actions across multiple processes
that can run concurrently in an operating system. This enables
a given program to handle several user requests at the same
time.
Because each user request may cause multiple processes to
operate in the operating system, the processes may need to
communicate with one another. Because each IPC protocol
technique has its own set of advantages and disadvantages, it
is not uncommon for a single program to use many protocols.
Synchronization in Inter Process Communication
Within the realm of Inter Process Communication in an operating system,
synchronization assumes a crucial role. It guarantees that processes
engage in communication and harmonize their actions with security and
meticulousness. The subsequent techniques exemplify synchronization
methodologies employed in IPC:

● Semaphores: Semaphores are a synchronization tool used to control


access to shared resources by multiple processes. Semaphores are
essentially counters that are used to regulate access to shared
resources.
● Mutexes: Mutexes (short for "mutual exclusion") are a synchronization
mechanism used to ensure that only one process can access a shared
resource at a time.
● Monitors: Monitors are a synchronization mechanism used to regulate
access to shared resources in a multi-process environment. A monitor
provides a mechanism for processes to request and release ownership
of a shared resource.
● Condition Variables: Condition variables are used to coordinate access
to shared resources by multiple processes. A process waits on a
condition variable until another process signals that it can proceed.
Approaches for Inter Process Communication in OS

We will now go through some different approaches to inter process communication in OS, which are as follows:
Why IPC?
• To share resources
• For client/server applications
• For modularity
• For convenience
• For computational speed up
Different ways of Information Sharing
• In Linux we have three ways to share the
information between processes
1) Two processes p1 and p2 sharing some
information that resides in a file in file system.
• To access this data each process must go through
the kernel (read, write).

2) Two process p3 and p4 are sharing information


that resides with in the kernel.
• Each operation to access the shared information
involves a system call to the kernel. Ex: pipe,
message queue, semaphores
3) Two process p5 and p6 have a region of
shared memory that each process can
reference.
• Once shared memory is set up by the each
process, then the process can access the data
in the shared memory without involving the
kernel.
IPC Techniques
• We can implement the IPC using five
techniques
1. Pipes
2. FIFOs
3. Message Queue
4. Shared Memory
5. Semaphores
Pipes
• Pipes are the oldest form of unix ipc and are
provided by all unix systems.
• Pipes are used to share the information between
parent and child process.

• Parent process pipe child process


• (write) (read)
Limitations of Pipes
• Pipes have two limitations
1. Pipes are half duplex (i.e data flows in only
one direction).
2. Pipes can be used only between processes
that have a common ancestor.

Normally a pipe is created by a process, that


process calls fork, and the pipe is used
between the parent and the child.
How pipe is created?
⮚ pipe () function
pipe() function is used to create a pipe between parent and
child.
⮚ Syntax:
# include<unistd.h>
int pipe (int filedes [2]);

Returns:0 if ok,
-1 on error

Two file descriptors are returned through the filedes argument.


file des[0] is open for reading
file des [1] is open for writing
The output of filedes [1] is the input for filedes [0].
shows the two ends of the pipe connected in a single
process

Process

shows that the data in the pipe flows


through the kernel

Kernel
Pipe from parent to child
Parent Child

For a pipe from the parent to child ,the parent closes the read end of the

pipe(fd[0]) and the child closes the write end (fd[1]).


Pipe from child to parent
Parent Child

For a pipe from the child to parent, the parent closes fd[1] and the child closes
the fd[0].
c program to provide IPC using pipes?
#include<stdio.h>
#include<fcntl.h> else
#include<unistd.h>
{
#include<sys/stat.h>
close(fd[1]);
int main()
{ n=read(fd[0],str,14);
char str[20]; printf(“%s”,str);
pid_t pid; }
int fd[2],n;
}
pipe(fd);
pid=fork();
if(pid>0)
{
close(fd[0]);
write(fd[1],"IPC using pipes",14);
Two way communication using Pipes
• When a two-way flow of data is desired, we
must create two pipes and use one for each
direction
•Create pipe 1, create pipe 2
•Fork
•Parent closes read end of pipe 1
•Parent closes write end of pipe 2
•Child closes write end of pipe 1
•Child closes read end of pipe2
Two way communication using Pipes
Two way communication using Pipes
Drawbacks of pipes
• Pipes are used only between related
processes. when a common ancestor has
created the pipe.
• Pipes are not permanent. Because a process
creates the pipe and the termination of that
process leads to their destruction.
FIFOs
• FIFOs are also called named pipes.
• By using FIFOs, unrelated processes also can
exchange the data.
• FIFOs are permanent.
• FIFO is a file.
• Creating a FIFO is similar to creating a file.
• Unlike Pipes, FIFO has a pathname associated
with it, allowing unrelated processes to access
a single FIFO
Creation of FIFO
⮚mkfifo() function
• mkfifo function is used to create a new FIFO.
⮚Syntax:
• #include<sys/types.h>
• #include<sys/stat.h>
• int mkfifo(const char *pathname, mode_t
mode);
• Returns:0 if OK ,-1 on error
Creation of FIFO
• Here pathname is the name of FIFO
• Once a fifo is created it must be opened for
reading or writing ,using either the open system
call or standard i/o functions fopen or freopen.
• Specification of the mode is same as open()
function.
• Open() function is used to open an existing FIFO.
• Normal I/O functions all work with FIFOs.
C Program for creating FIFO
#define FIFO2 "/tmp/fifo2"
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int ret_val;
ret_val = mkfifo(FIFO2, 0666);
if (ret_val == -1) {
printf("Error creating the named pipe");
}
else
{
printf("fifo is created");
}
}

Output:
Fifo is created
Two way communication using FIFO
read function in FIFO
#include <unistd.h>
size_t read (int fd, void* buf, size_t cnt);

fd: file descriptor


buf: buffer to read data from
cnt: length of buffer

Returns: How many bytes were actually read


• return Number of bytes read on success
• return 0 on reaching end of file
• return -1 on error
write function in FIFO
#include <unistd.h>
size_t write (int fd, void* buf, size_t cnt);

Parameters
• fd:file descripter
• buf: buffer to write data to
• cnt: length of buffer

Returns: How many bytes were actually written


• return Number of bytes written on success
• return 0 on reaching end of file
• return -1 on error
C Program of FIFO on server side

#include <stdio.h>
// Write the input arr2ing on FIFO and
#include <string.h>
close it
#include <fcntl.h> write(fd, arr2,
#include <sys/stat.h> strlen(arr2)+1);
#include <sys/types.h> close(fd);
#include <unistd.h>
int main()
// Open FIFO for Read only
{
int fd; fd = open(myfifo,
O_RDONLY);
// FIFO file path
char * myfifo = "/tmp/myfifo"; // Read from FIFO
// Creating the named file(FIFO) read(fd, arr1, sizeof(arr1));
mkfifo(myfifo, 0666);

char arr1[80], arr2[80]; // Print the read message


while (1) printf("User2: %s\n", arr1);
{ close(fd);
// Open FIFO for write only
}
fd = open(myfifo,
O_WRONLY); return 0;
}
// Take an input arr2ing
from user.
// 80 is maximum
length…reads a text line
fgets(arr2, 80, stdin);
C Program of FIFO on client side
#include <stdio.h>
/ First open in read only and read
#include <string.h>
fd1 = open(myfifo,O_RDONLY);
#include <fcntl.h>
#include <sys/stat.h>
read(fd1, str1, 80);
#include <sys/types.h>
#include <unistd.h> // Print the read string and close
int main() printf("User1: %s\n", str1);
{ close(fd1);
int fd1;
// Now open in write mode and write
// FIFO file path
// string taken from user.
char * myfifo = "/tmp/myfifo";
fd1 = open(myfifo,O_WRONLY);
// Creating the named file(FIFO)
fgets(str2, 80, stdin);
mkfifo(myfifo, 0666); write(fd1, str2, strlen(str2)+1);
close(fd1);
char str1[80], str2[80]; }
while (1) return 0;
{ }
To remove FIFO
⮚unlink(): Used to remove the created fifo
special file

⮚Syntax:
Unlink(const char *fifo filename/pathname)
Uses of FIFOs
• FIFOs are used by shell commands to pass
data from one shell pipeline to another
without creating intermediate temporary files.
• FIFOs are used in client-server applications to
pass data between the clients and the servers.
Differences between unnamed & named
pipes(FIFOs)
Unnamed Pipes:
• They are un-named IPC Object.
• PIPE is local to the system and cannot be used for communication
across the network.
• PIPE does not exist in the file system.
• In PIPE, data transfer takes place between the child process and parent
process.
• PIPE is created by pipe () function.
• In PIPE, reader and writer operation is done at same time.
• PIPE vanishes as soon as it is closed, or one of the processes (parent or
child) completes execution.
• PIPE has no control over ownership and permissions.
• PIPE is unidirectional.
• PIPE provides simplex data flow.
• In PIPE, communication is among the process having a common
ancestor (related process).
Differences between unnamed &
named pipes(FIFO)
Named Pipes:
• They are named IPC Object.
• FIFO is capable of communicating across different computers and network.
• FIFO exists in the files system.
• FIFO have multiple processes communicating through it, like multiple client-
server application.
• FIFO is created by mkfifo () function.
• In FIFO, it does not require that both read and write operation to occur at the
same time.
• FIFO exists even when calling process exit. They remain till system reboots.
• Given that FIFO is a file, you can control ownership and permissions.
• FIFO is bi-directional. The same FIFO can be used for reading and writing.
• FIFO provides half duplex data flow.
• In FIFO, it is not necessary for the process having a common ancestor for
communication (unrelated process).
popen and pclose Functions
• Since a common operation is to create a pipe
to another process, to either read its output
or send it input, the standard I/O library has
historically provided the popen and pclose
functions.
• These two functions handle all the work that
we've been doing ourselves: creating a pipe,
forking a child, closing the unused ends of the
pipe, executing a shell to run the command,
and waiting for the command to terminate
popen() function:
#include <stdio.h>
FILE *popen(const char *cmdstring, const char
*type);

Returns: file pointer if OK, NULL on error

pclose() function:
int pclose(FILE *fp);

Returns: termination status of cmdstring, or 1


on error
• The function popen does a fork and exec to
execute the cmdstring, and returns a standard
I/O file pointer. If type is "r", the file pointer is
connected to the standard output of
cmdstring

• If type is "w", the file pointer is connected to


the standard input of cmdstring
• The pclose function closes the standard I/O
stream, waits for the command to terminate,
and returns the termination status of the
shell.
• If the shell cannot be executed, the
termination status returned by pclose is as if
the shell had executed exit.
• The cmdstring is executed by the Bourne shell,
as in the shell expands any of its special
characters in cmdstring. This allows us to say,
for example,
• fp = popen("ls *.c", "r");

You might also like