UNIX/Linux IPC : Reading from a pipe. How to know length of data at runtime?
Since it seems that you intend to make a single read of all the data from the pipe, I think the following will serve you better than delimiter+encoding or miniheader techniques suggested in other answers:
From the pipe (7) manpage:
If all file descriptors referring to
the write end of a pipe have been
closed, then an attempt to read(2)
from the pipe will see end-of-file
(read(2) will return 0).
The following example was taken from the pipe (2) manpage and reversed so that the child does the writing, the parent the reading (just to be sure). I also added a variable size buffer. The child will sleep for 5 seconds. The delay will ensure that the exit() of the child can have nothing to do with pipeio (the parent will print a complete line before the child exits).
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char *
slurpfd(int fd)
{
const int bytes_at_a_time = 2;
char *read_buffer = NULL;
int buffer_size = 0;
int buffer_offset = 0;
int chars_io;
while (1) {
if (buffer_offset + bytes_at_a_time > buffer_size) {
buffer_size = bytes_at_a_time + buffer_size * 2;
read_buffer = realloc(read_buffer, buffer_size);
if (!read_buffer) {
perror("memory");
exit(EXIT_FAILURE);
}
}
chars_io = read(fd,
read_buffer + buffer_offset,
bytes_at_a_time);
if (chars_io <= 0) break;
buffer_offset += chars_io;
}
if (chars_io < 0) {
perror("read");
exit(EXIT_FAILURE);
}
return read_buffer; /* caller gets to free it */
}
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
assert(argc == 2);
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child writes argv[1] to pipe */
close(pipefd[0]); /* Close unused read end */
write(pipefd[1], argv[1], strlen(argv[1]) + 1);
close(pipefd[1]); /* Reader will see EOF */
/* sleep before exit to make sure that there
will be a delay after the parent prints it's
output */
sleep(5);
exit(EXIT_SUCCESS);
} else { /* Parent reads from pipe */
close(pipefd[1]); /* Close unused write end */
puts(slurpfd(pipefd[0]));
close(pipefd[0]);
wait(NULL); /* Wait for child */
_exit(EXIT_SUCCESS);
}
}
From your comment I see now that you may want to read the data as it becomes available, to update the UI or whatever, to reflect your system's status. To do that open the pipe in non-blocking (O_NONBLOCK) mode. Read repeatedly whatever is available until -1 is returnd and errno == EAGAIN and do your parsing. Repeat unil read returns 0, which indicates that the child has closed the pipe.
To use an in-memory buffer for File* functions you can use fmemopen() in the GNU C library.
How do I get stdin length from a pipe? echo hello | ./get_stdin_size
You can use the fstat()
syscall to tell if standard input is a pipe (Either anonymous or named), or a file (And if a file, find its size):
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(void) {
struct stat s;
if (fstat(STDIN_FILENO, &s) < 0) {
perror("fstat");
return EXIT_FAILURE;
}
switch (s.st_mode & S_IFMT) {
case S_IFIFO:
puts("standard input is a pipe.");
break;
case S_IFREG:
printf("standard input is a file that is %ld bytes in size.\n",
(long)s.st_size);
break;
case S_IFCHR:
if (isatty(STDIN_FILENO)) {
puts("standard input is a terminal.");
} else {
puts("standard input is a character device.");
}
break;
default:
puts("standard input is something else.");
}
return 0;
}
Example:
$ gcc testpipe.c
$ cat testpipe.c | ./a.out
standard input is a pipe.
$ ./a.out < testpipe.c
standard input is a file that is 525 bytes in size.
$ ./a.out
standard input is a terminal.
Reading data of variable size in buffer using read system call in C
Two most common and simplest methods are to either write the length first in a fixed-size, or to have a special record-terminator that tells that the record has ended.
understanding pipes in UNIX
File descriptors are allocated as the smallest available. 0, 1, and 2 are already taken when the application starts (they inherit stdin, stdout, and stderr), so the next two descriptors you make will be 3 and 4.
Retrieve the amount of bytes stored in the write end of a Linux anonymous PIPE
Amount of bytes available for read from the pipe can be requested by
ioctl(fd, FIONREAD, &nbytes);
Here fd
is a file descriptor, and variable nbytes
, where result will be stored, is int
variable.
Taken from: man 7 pipe.
Amount of bytes available for write is a different story.
Related Topics
Can "Text File Busy" Happen When Two Processes Trying to Execute a Perl File in The Same Time
Which Is The Correct Way to Register a New Net_Device
How to Copy from Tmux (Copy Mode) Running on a Remote Ssh Connection to Your Local Clipboard
Dlopen with Two Shared Libraries, Exporting Symbols
Gunicorn Does Not Start After Boot
Permission Denied: '/Var/Lib/Pgadmin/Sessions' in Docker
See What Process Last Touched a File
How to Get Clock_Gettime(2) Clock in Shell
Detect When Reader Closes Named Pipe (Fifo)
Tk Initialization Failed: No Display Name and No $Display Environment Variable
Tmux .Tmux.Conf Doesn't Load Properly
Ld_Library_Path: How to Find a Shared Object
Socket() Returns 0 in C Client Server Application
Emacs/Xterm Color Annoyance on Linux