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:
The server program starts by declaring variables for the filename (fname) and file content (fcontent).
Two FIFOs, fifo1 and fifo2, are created using the mkfifo function with the specified permission 0600 (read and write for the owner).
File descriptors fd and fd1 are opened for reading and writing, respectively, corresponding to fifo1 and fifo2.
The server reads the filename sent by the client from fifo1 using the read function.
The server opens the file specified by the filename (fname) for reading with file descriptor fd2.
The server then reads the file content in chunks of 100 bytes and writes it to fifo2 using the write function.
If the file descriptor fd2 is negative (file not found), the server sends the "file not found" message to the client through fifo2.
Finally, the file descriptors are closed, and the server program terminates.
🎯 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:
The client program starts by declaring variables for the filename (s) and file content (s1).
File descriptors fd and fd1 are opened for writing and reading, respectively, corresponding to fifo1 and fifo2.
The client prompts the user to enter a filename and reads it from the standard input using scanf.
The client sends the filename to the server through fifo1 using the write function.
The client then reads the file content from fifo2 in chunks of 1000 bytes using the read function.
The received file content is displayed to the user.
Finally, the file descriptors are closed, and the client program terminates.
🎯 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.