Professional Documents
Culture Documents
LP-Unit4
LP-Unit4
LP-Unit4
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
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).
Returns:0 if ok,
-1 on error
Process
Kernel
Pipe from parent to child
Parent Child
For a pipe from the parent to child ,the parent closes the read end of the
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>
int main() close(fd[1]);
{ 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);
Parameters
• fd:file descripter
• buf: buffer to write data to
• cnt: length of buffer
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
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
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);
pclose() function:
int pclose(FILE *fp);