EmbLogic's Blog

Interprocess communication

Interprocess Communication

Interprocess communication is a technique used for exchange of data between two process and threads.The process can be run on same computer or via internet.IPC  is a mechanism through which two process communicates.Signals are used in IPC to send notification to a process or to specific thread within the same process in order to notify it of an event that occurred. When a signal is sent, the operating system interrupts the target process’s normal flow of execution to deliver the signal. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise, the default signal handler is executed.IPC are implemented through Pipe,Fifo,Semaphores,Shared memory,Message queue.

  • The pipe call: function provide a means to transfer data between two programs without invoking the shell to interpret the request command.It also give control over the reading and writing.The pipe function has the following prototype.

#include<unistd.h>

Int pipe(int pipe_descriptor[2]);

 

Pipe is passed apointer to an array of two integer file descriptor.It is important to realize that this is a file descriptor, not a file stream.So we must use low level read and write call to access the data.The real advantage of pipe comes when you wish to pass data beween two process.When a program creates a new process using the fork call,the descriptor that is open previously will remain open.By creating a pipe in the original process and forking to create  anew process.We can pass data from one process to another process down the pipe.By using the fork( ) call ,it creates the new child process.By using the execl( ) command we can replace the whole line with with other program.

You can create a pipe using the following syntax.

if(pipe(file_pipes)==0),creates a pipe.

To write the data into the file descriptor use the sysntax.

Write(file_pipes[1],some_data,strlen(some_data));

To read the data from the file descriptor use the following syntax.

Read(file_pipes[0],buffer,BUFSIZ);

On success the syntax is exit(EXIT_SUCCESS); On failure the sysntax is exit(EXIT_FAILURE);

The program creates a pipe with the pipe call,it then use a fork call to create the new  process.The parent write the data in to the pipe and child process read the data from the pipe.Both the parent and child will exit after the one read and write.The pipe call is us to allow the child to be a different program from its parent rather than just a process running the same program.

 

FIFO: Named pipes (or FIFO) are variants of pipe that allow communication between processes that are not related to each other. The processes communicate using named pipes by opening a special file known as a FIFO file. One process opens the FIFO file from writing while the other process opens the same file for reading. Thus any data written by the former process gets streamed through a pipe to the latter process. The FIFO file on disk acts as the contract between the two processes that wish to communicate.

Creat: A FIFO is created by the mkfifo function:

#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);

Open: mkfifo tries to create a new FIFO. If the FIFO already exists, then an EEXIST error is returned. To open an existing FIFO, use open(), fopen() or freopen()

Close: to close an open FIFO, use close(). To delete a created FIFO, use unlink().

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>