Interprocess communication (IPC)
is a set of programming interfaces that allow a programmer to coordinate
activities among different program processes that can run concurrently in
an operating system. This allows a program to handle many user requests at the
same time. Since even a single user request may result in multiple processes
running in the operating system on the user's behalf, the processes need to
communicate with each other. The IPC interfaces make this possible. Each IPC
method has its own advantages and limitations so it is not unusual for a single
program to use all of the IPC methods.
pipe
In computer programming,
especially in UNIX operating systems, a pipe is a technique for
passing information from one program process to another. Unlike other
forms of interprocess communication (IPC), a pipe is one-way communication
only. Basically, a pipe passes a parameter such as the output of one process to
another process which accepts it as input. The system temporarily holds the
piped information until it is read by the receiving process.
For two-way communication between
processes, two pipes can be set up, one for each direction. A limitation of
pipes for interprocess communication is that the processes using pipes must
have a common parent process (that is, share a common open or initiation
process and exist as the result of a fork system call from a parent
process).
A pipe is fixed in size and is
usually at least 4,096 byte
It is important to note the
following about pipes as an IPC mechanism:
1. Unix pipes are buffers managed
from within the kernel.
2. Note that as a channel of communication, a
pipe operates in one direction only.
3. Some plumbing (closing of
ends) is required to use a pipe.
4. Pipes are useful when both the
processes are schedulable and are resident on the same machine. So, pipes are
not useful for processes across networks.
5. The read end of a pipe reads any way. It
does not matter which process is connected to the write end of the pipe.
Therefore, this is a very insecure mode of communication.
6. Pipes cannot support
broadcast. There is one other method of IPC using special files called “named
pipes".
As an example, consider a case
where one process gets a character string input and communicates it to the
other process which reverses strings. Then we have two processes which need to
communicate. Next we define a pipe and connect it between the processes to
facilitate communication. One process gets input strings and writes into the
pipe. The other process, which reverses strings, gets its input (i.e. reads)
from the pipe.
Suppose two processes, Process A
and Process B, need to communicate, then it is imperative that the process
which writes closes its read end of the pipe and the process which read closes
its write end of the pipe. Essentially, for a communication from Process A to
process B the following should happen. Process A should keep its write end open
and close read end of the pipe. Similarly, Process B should keep its read end
open and close its write end.
1. First we have a parent process
which declares a pipe in it.
2. Next we spawn two child
processes. Both of these would get the pipe definition which we have defined in
the parent. The child processes, as well as the parent, have both the write and
read ends of the pipe open at this time.
3. Next, one child process, say
Process A, closes its read end and the other child process, Process B, closes
its write end.
4. The parent process closes both
write and read ends.
5. Next, Process A is populated with code to
get a string and Process B is populated to reverse a string.
We must first open
a pipe
UNIX allows two ways
of opening a pipe.
FILE *popen(char *command, char *type) -- opens a pipe
for I/O where the command is the process that will be connected to the calling
process thus creating the pipe. The type is either "r" - for
reading, or "w" for writing.
popen() returns is a stream pointer or NULL for any
errors.
A pipe opened by popen() should always be closed
by pclose(FILE *stream).
We use fprintf() and fscanf() to
communicate with the pipe's stream.
int pipe(int fd[2]) -- creates a pipe and returns two
file descriptors, fd[0], fd[1]. fd[0] is opened for
reading, fd[1] for writing.
pipe() returns 0 on success, -1 on failure and
sets errno accordingly.
The standard programming model is that after the pipe has
been set up, two (or more) cooperative processes will be created by a fork and
data will be passed using read() and write().
Pipes opened with pipe() should be closed
with close(int fd).
Implementation
pipe.c
#include <stdio.h>
#include<ctype.h>
main()
{ int p_des[2];
pipe( p_des ); /* The pipe
descriptor */
printf("Input a string
\n");
if ( fork () == 0 )
{ dup2(p_des[1], 1); /*The dup command replaces the standard I/O
channels by pipe descriptors.*/
close(p_des[0]); /*
process-A closing read end of the pipe */
execlp("./get_str", "get_str", 0); /*** exit(1);
***/
}
else if ( fork () == 0 )
{ dup2(p_des[0], 0);
close(p_des[1]); /* process-B
closing write end of the pipe */
execlp("./rev_str",
"rev_str", 0); /*** exit(1); ****/
}
else
{ close(p_des[1]); /* parent
closing both the ends of pipe */
close(p_des[0]);
wait(0);
wait(0); }
fflush(stdout);
}
/*****************************************/
get_str.c
#include <stdio.h>
#include<ctype.h>
void get_str(char str[])
{ char c;
int ic;
c = getchar();
ic = 0;
while ( ic < 10 && ( c != EOF && c != '\n'
&& c != '\t' ))
{ str[ic] = c; c = getchar(); ic++; }
str[ic] = '\0'; return;
}
/*****************************************/
rev_str.c
void rev_str(char str1[], char
str2[])
{ char c; int ic; int rc;
ic = 0; c = str1[0];
while( ic < 10 && (c != EOF && c != '\0' &&
c != '\n') )
{ ic++; c = str1[ic]; }
str2[ic] = '\0'; rc = ic - 1; ic = 0;
while (rc-ic > -1)
{ str2[rc-ic] = str1[ic]; ic++; }
return;
}
Thanks for sharing a worthy information. This is really helpful for learning. Keep doing more.
ReplyDeleteJapanese Training Institutes in Chennai
Japanese Coaching Classes in Chennai
Japanese Language Classes in Chennai
Spanish Coaching in Chennai
Spanish Classes near me
French Class in Chennai
French Training