FIFO Client/Server Program in Unix Using C Programming

🎯 Overview

In this blog post, we will explore the implementation of a FIFO (First-In-First-Out) client/server program in the Unix environment using the C programming language. FIFOs are special files that allow interprocess communication, where data is read in the same order it was written. We will enhance the existing code, provide explanations, and update the source code to a newer version. Let's dive in!

🎯 FIFO Server Program and Explanation

C Programming Source code

c programming

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<string.h>


int main() {

   char fname[25] = "";

   char fcontent[100] = "";

   int fd, fd1, fd2;


   // Create FIFOs

   mkfifo("fifo1", 0600);

   mkfifo("fifo2", 0600);


   // Open FIFOs for reading and writing

   fd = open("fifo1", O_RDONLY);

   fd1 = open("fifo2", O_WRONLY);


   // Read the filename from FIFO1

   read(fd, fname, 25);


   // Open the file for reading

   fd2 = open(fname, O_RDONLY);


   // Read file content and write it to FIFO2

   while (read(fd2, fcontent, 100) != 0) {

      printf("%s\n", fcontent);

      if (fd2 < 0)

         write(fd1, "file not found", 15);

      else

         write(fd1, fcontent, strlen(fcontent));

   }


   // Close file descriptors

   close(fd);

   close(fd1);

   close(fd2);

   return 0;

}

Explanation:

🎯 FIFO Client Program and Explanation

C Programming Source Code

c programming

#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<string.h>


int main() {

   char s[100] = "";

   char s1[1000] = "";

   int fd, fd1;


   // Open FIFOs for writing and reading

   fd = open("fifo1", O_WRONLY);

   fd1 = open("fifo2", O_RDONLY);


   printf("\nEnter the file name: ");

   scanf("%s", s);


   // Send the filename to FIFO1

   write(fd, s, strlen(s));


   // Read the file content from FIFO2 and display it

   while (read(fd1, s1, 1000) != 0) {

      printf("File Content: %s", s1);

   }


   // Close file descriptors

   close(fd);

   close(fd1);

   return 0;

}

Explanation:

🎯 Sample Output


Enter the file name: strcmp.c

File Content: #include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<stdlib.h>

include<string.h>

int main(int argc,char *argv[])

{

   if(strcmp(argv[1],argv[2])==0)

      printf("The strings are equal");

   else

      printf("The strings are not equal");

}

In this blog post, we have learned how to implement a FIFO client/server program in Unix using the C programming language. The server program creates FIFOs, reads the filename from the client, opens the specified file, and sends its content back to the client. The client program prompts the user for a filename, sends it to the server, and displays the received file content. Understanding interprocess communication mechanisms like FIFOs is crucial for developing efficient and scalable systems in Unix environments.