Our Feeds

Thursday, 24 March 2016

Ajith KP

Named Pipes (FIFO): In Linux for Interprocess Communication

Hello GuyZ,
    Named pipes or FIFO are communication media between processes of different ancestry. Named pipes exist as a device special file in the file system. When all I/O is done by sharing processes, the named pipe remains in the file system for later use. Here, the processes are,
  • Client - which send data to server
  • Server - Retrieve data from client and process it, and send back result
Here the server process will retrieve the string send by client and convert it to capital letter and send back to client.

Code of Client

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
/*
 [*] Ajith Kp [@ajithkp560]
 [*] http://www.terminalcoders.blogspot.com
*/
int main(int argc, char const *argv[]) {
    int rd, wr, n;
    char buff[1024];
    printf("Enter data to send: ");
    gets(buff);
    wr = open("input", O_WRONLY);
    rd = open("output", O_RDONLY);
    write(wr, buff, strlen(buff));
    read(rd, buff, 1024);
    printf("Uppercase Data: %s\n", buff);
    return 0;
}

Code of Server

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
/*
 [*] Ajith Kp [@ajithkp560]
 [*] http://www.terminalcoders.blogspot.com
*/
int main(int argc, char const *argv[]) {
    int rd, wr;
    char buff[1024];
    mkfifo("input", 0666);
    mkfifo("output", 0666);
    rd = open("input", O_RDONLY);
    wr = open("output", O_WRONLY);
    read(rd, buff, 1024);
    for(int i=0;i<strlen(buff);i++){
        buff[i] = toupper(buff[i]);
    }
    write(wr, buff, strlen(buff)+1);
    return 0;
}

Pic 1: Named pipe server waits for data and Named pipe client asks user input
Pic 2: Named pipe server got the data input by user from client and return the result to client