IPC MECHANISM
The acronym of IPC is inter process communication. Before we talk about ipc,it is necessary to know about process. Process is defined as,”when one or more than one thread are executing with an address space”
IPC mechanism is a set of methods to exchanging the data between multiple threads in one or more process.There are various methods of IPC just like pipes, fifo, message queue and shared memory.Every mechanism has its own properties.But here I am going to discuss about pipes and fifo only.
Pipe :- A pipe is a mechanism for inter process communication in which data is written to the pipe by one process and read by another process.pipe has no name thatswhy it is known as unnamed process. In pipe data always send in buffer.In pipe data
send in unidirectional.
In pipes when we use fork ( ) it creates a process like a duplicate of main. That means when we use fork there are two processes are generated one is known as parent process and another one is known as child process. Fork returns -1 when forking is failed.when it returns 0 that mean its a child process and when fork return positive value that it is a parent process.
If child process exit before parent process its known as zombie process . If parent process exit before child process then it is known as orphan process . In this case child is managed by Init process.pipe function is declared in header file #include.
three client server program using pipes :- here I have done three client server program using pipes.in which there are three clients and one server and three processing unit.
From clients I sent data using buffer and data sent from client to server using pipe. Then execl function is used in child process of server. And data read from client to server using a file descriptor. Which is send to processing unit by server using write operation by another file descriptor. Then again forking is done in server program and processing unit is called using execl function. In processing unit data transmitted by server is in the form of string thats why we have to use sscanf in processing side to convert data in respective integer and character type.after processing the data result is again converted in the form of string and sent to server using write operation using file descriptor.
Then data read by server using read operation and result sent by server to client using write operation with the help of file descriptor in between client and server.
For the synchronization we can use sleep or wait function.
Same process is done for rest of two clients.we can do this program by using only two pipes , one is used in between client and server and another one is used in between server and processing unit.
FIFO :- The acronym for fifo is first in first out. fifo is also known is named pipe. fifo is a related process.processes open in the fifo by name in order to communicate through it.
The name allows unrelated processes to communicate through it.Multiple processes can open(), write() and read() from the FIFO. open for writing blocks until someone opens for reading .when data write from one side ,it will go on ‘block on write’ if there will be reader in another side and same thing happened if reader tries to read and there is no writer in another side then reader will go on ‘block on read’.
Creating a FIFO:
int mkfifo(const char *path, mode_t mode);
before creating a fifo using mkfifo we use access( fifo_name,flag) to check that the same name of fifo is already created or not. After creating fifo the second step is open the fifo
in write only (O_WRONLY) or read only (O_RDONLY).which returns file descriptor fd. Then read or write operation is perform using this file descriptor.