Linux C Programming - Pipes
In Linux, a pipe is also a kind of file, but it is special. We can use the pipe function to create a pipe, and its prototype is declared as follows:
#inlcude
int pipe(int fields[2] );
In fact, it is equivalent to a communication buffer, fields[0] is used for reading, and fields[1] is used for writing. In the following example, a pipe is created as a communication buffer. The parent process creates a child process. The child process writes a string into the pipe through the fields[1] descriptor of the pipe, while the parent process uses the fields[1] of the pipe. 0] Read this string from the pipe and display it:
#include
#include
#include
#include
#include
#include
#define BUF_SIZ 255 // message buffer size
int main(int argc , char **argv)
{
char buffer[BUF_SIZ + 1];
int fd[2];
// receive a string as parameter
if (argc != 2)
{
fprintf (stderr, "Usage : %s stringna", argv[0]);
exit(1);
}
// create pipe for communication
if (pipe(fd) != 0)
{
fprintf(stderr, "C reate pipe error : %sna", strerror(errno));
exit(1);
}
if (fork() == 0) // in child process write msg to pipe
{
close(fd[0]); "Printf (" Child%LD WRITE to PIPENA ", Getpid ());
Snprintf (buffer, buf_siz,"%s ", argv [1]);
Write (fd [1], buffer, strlen (buffer)) ; T Printf ("Child%ld quit.na", getpid ());
}
else // in Parent Process, Read MSG from Pipe
{
Close (fd [1]); read from pipena", getpid());
memset(buffer, '

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









