Lab 4 Develop a C program which demonstrates interprocess communication between a reader process and a writer process. Use mkfifo, open, read, write and close APIs in your program.


Objective:

The objective is to demonstrate interprocess communication between a writer process and a reader process using named pipes (FIFO) in a C program. The program should utilize mkfifo, open, read, write, and close APIs to establish communication between these two processes through a shared named pipe.

Overview:

  1. Writer Process (writerProcess()):

    • Create a named pipe (FIFO) using mkfifo with a specified name (FIFO_NAME).
    • Open the FIFO in write-only mode (O_WRONLY) using open.
    • Write a message or data into the FIFO using write.
    • Close the FIFO using close after writing the data.
  2. Reader Process (readerProcess()):

    • Open the same named pipe created by the writer in read-only mode (O_RDONLY) using open.
    • Read data from the FIFO using read.
    • Print the received data or message.
    • Close the FIFO using close after reading the data.
  3. Main Function (main()):

    • Fork a child process using fork().
    • In the parent process (writer), call the writerProcess() function to write data to the named pipe.
    • In the child process (reader), call the readerProcess() function to read data from the named pipe.
    • Ensure proper handling of errors such as failed forks, pipe creation, and data reading/writing failures.
  4. Execution:

    • Compile the C program using a C compiler (e.g., gcc).
    • Run the compiled program, which will create two processes – a writer and a reader – that communicate through the named pipe.
  5. Cleanup:

    • Ensure the named pipe is properly closed and unlinked after both processes finish communicating.
    • Handle resource release to avoid memory leaks or unwanted resources.



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

#define FIFO_NAME "myfifo"

void writerProcess() {
    int fd;
    char *message = "Hello, reader!";

    mkfifo(FIFO_NAME, 0666);
    fd = open(FIFO_NAME, O_WRONLY);
    write(fd, message, strlen(message) + 1);
    close(fd);

    printf("Data sent to reader successfully!\n");
}

void readerProcess() {
    int fd;
    char readMessage[100];

    fd = open(FIFO_NAME, O_RDONLY);
    read(fd, readMessage, sizeof(readMessage));
    printf("Received message: %s\n", readMessage);
    close(fd);
}

int main() {
    pid_t pid;

    // Create a child process
    pid = fork();

    if (pid < 0) {
        fprintf(stderr, "Fork failed!\n");
        return 1;
    } else if (pid > 0) {
        // Parent process (Writer)
        writerProcess();
    } else {
        // Child process (Reader)
        readerProcess();
    }

    return 0;
}

*******************************************************************
OUTPUT

opera@opera-virtual-machine:~/Desktop$ gcc -o 1 1.c opera@opera-virtual-machine:~/Desktop$ ./1 Data sent to reader successfully! Received message: Hello, reader! opera@opera-virtual-machine:~/Desktop$












Comments