Home php教程 PHP开发 Linux C Programming - Pipes

Linux C Programming - Pipes

Dec 13, 2016 am 11:37 AM

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
#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, '

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24