Inter-Process Communication Using FIFO (Named Pipes)

Subject: OS (Operating Systems)

Contributed By: Nunugoppula Ajay

Created At: March 10, 2025

Question:


Develop a two-way communication system between two processes using FIFO (Named Pipes) in a Linux environment. The two processes will act as User1 and User2, enabling them to send and receive messages in an alternating manner, similar to a chat system.

Explanation Video:

Custom Image

Explanation:

Problem Description:

  1. Two separate programs (fifo1.c and fifo2.c) will communicate using a named pipe (/tmp/myfifo).
  2. User1 starts by writing a message, and User2 reads it.
  3. User2 then writes a response, and User1 reads it.
  4. This cycle continues, allowing a conversation between both users.
  5. Both programs should properly open, write, read, and close the FIFO in each iteration.

Constraints:

  • Use mkfifo() to create a named pipe (/tmp/myfifo).
  • Use open(), write(), and read() system calls.
  • Ensure proper synchronization (one process writes while the other reads).
  • The program should run indefinitely until manually stopped (or until an exit condition is implemented).

Example Execution:

Terminal 1 (Running fifo1.c)

User1: Hello User2!
User2: Hi User1! How are you?
User1: I'm good, thanks!

Terminal 2 (Running fifo2.c)

User1: Hello User2!
User2: Hi User1! How are you?
User1: I'm good, thanks!

Expected Outcome:

  • A working chat-like system between two users.
  • Correct message synchronization (User1 writes → User2 reads → User2 writes → User1 reads).
  • FIFO file is used as an intermediary for communication.
Source Code:
//fifo1.c

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    int fd;
    char *myfifo = "/tmp/myfifo";  // FIFO file path
    mkfifo(myfifo, 0666);  // Create FIFO if it doesn’t exist

    char str1[80], str2[80];

    while (1) {
        // Write first
        fd = open(myfifo, O_WRONLY);  // Open FIFO for writing
        printf("User1: ");
        fgets(str2, 80, stdin);  // Get user input
        write(fd, str2, strlen(str2) + 1);
        close(fd);

        // Read response
        fd = open(myfifo, O_RDONLY);  // Open FIFO for reading
        read(fd, str1, sizeof(str1));
        printf("User2: %s\n", str1);
        close(fd);
    }

    return 0;
}

//fifo2.c

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    int fd;
    char *myfifo = "/tmp/myfifo";  // FIFO file path
    mkfifo(myfifo, 0666);  // Create FIFO if it doesn’t exist

    char str1[80], str2[80];

    while (1) {
        // Read first
        fd = open(myfifo, O_RDONLY);  // Open FIFO for reading
        read(fd, str1, sizeof(str1));
        printf("User1: %s\n", str1);
        close(fd);

        // Write response
        fd = open(myfifo, O_WRONLY);  // Open FIFO for writing
        printf("User2: ");
        fgets(str2, 80, stdin);  // Get user input
        write(fd, str2, strlen(str2) + 1);
        close(fd);
    }

    return 0;
}
Share this Article & Support Us:
Status
printf('Loading...');