Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write and read from a named pipe in C?

Tags:

c

named-pipes

I have 2 programs (write.c and read.c). I want to continuously write to the named pipe from standard input, and read from it on the other end (and write to standard output). I've made something work, but it isn't working right. The program on the other end reads in the wrong order or reads special characters (so it reads more then it needs?). I also want to be able to compare the named pipe output to a certain string.

Anyways, here's the code from both files:

write.c:

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

#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }

void main()
{
    int fd, n;

    char buf[BUFFSIZE];


    mkfifo("fifo_x", 0666);
    if ( (fd = open("fifo_x", O_WRONLY)) < 0)
        err("open")

    while( (n = read(STDIN_FILENO, buf, BUFFSIZE) ) > 0) {
        if ( write(fd, buf, strlen(buf)) != strlen(buf)) { 
            err("write");
        }
    }
    close(fd);
}

read.c:

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

#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }

void main()
{
    int fd, n;
    char buf[BUFFSIZE];

    if ( (fd = open("fifo_x", O_RDONLY)) < 0)
        err("open")


    while( (n = read(fd, buf, BUFFSIZE) ) > 0) {

        if ( write(STDOUT_FILENO, buf, n) != n) { 
            exit(1);
        }
    }
    close(fd);
}

Example of input:

hello how are you
123 
test

Example of incorrect output:

hello how are you
b123
o how are you
btest
 how are you
b

Another example of input:

test
hi

And output:

test
hi
t
like image 616
mythic Avatar asked Dec 06 '16 13:12

mythic


People also ask

How do you open a named pipe?

Processes can use the open() function to access named pipes and then use the regular I/O functions for files, such as read() , write() , and close() , when manipulating named pipes. Buffered I/O functions can also be used to access and manipulate named pipes.

What are named pipes explain logical view of reading and writing pipes?

A named pipe is a one-way or duplex pipe that provides communication between the pipe server and some pipe clients. A pipe is a section of memory that is used for interprocess communication. A named pipe can be described as first in, first out (FIFO); the inputs that enter first will be output first.

What is FIFO in C programming?

FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last.


1 Answers

The buffer modify by read is not a valid c string so

write(fd, buf, strlen(buf)) != strlen(buf) // write.c

is undefined behaviour. You should do

write(fd, buf, n) != n

because you read n octet with read().

It's funny because you do it for read.c but not for write.c


The type of n must but ssize_t and not int, man read.


main() must return a int Declare main prototype

like image 96
Stargateur Avatar answered Nov 15 '22 19:11

Stargateur