Fifo pipes

PHOTO EMBED

Thu Jun 06 2024 18:51:53 GMT+0000 (Coordinated Universal Time)

Saved by @dbms

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#define FIFO_NAME "myfifo"

int main() {

    int fd;

    char *message = "Hello from writer process!";

    

    // Create the FIFO if it does not exist

    if (mkfifo(FIFO_NAME, 0666) == -1) {

        perror("mkfifo");

        exit(EXIT_FAILURE);

    }

    // Open the FIFO for writing

    fd = open(FIFO_NAME, O_WRONLY);

    if (fd == -1) {

        perror("open");

        exit(EXIT_FAILURE);

    }

    // Write the message to the FIFO

    if (write(fd, message, strlen(message) + 1) == -1) {

        perror("write");

        close(fd);

        exit(EXIT_FAILURE);

    }

    printf("Writer: Wrote message to FIFO.\n");

    

    // Close the FIFO

    close(fd);

    return 0;

}
content_copyCOPY