Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does it mean to have a famous pipeline in Linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the famous pipeline in Linux means. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

I. the concept of pipeline

Pipes, also known as "anonymous management" or "anonymous pipes", are a very basic and frequently used IPC method.

1. Pipeline essence

The essence of a pipe is also a file, but a pseudo file, which is actually a kernel buffer with a size of 4K.

After the pipe is created, it produces two file descriptors, one on the reader side and the other on the write side.

The data in the pipeline can only be written from the writer and read from the reader.

1. Pipeline principle

A pipe is a buffer of the kernel or, more specifically, a circular queue. The data is written from one end of the queue and read out from the other end, as shown below:

3. Advantages of piping

simple

4. The shortcomings of the pipeline

You can only communicate in one way, and if you need two-way communication, you need to set up two pipes.

Can only be applied to consanguineous processes, such as parent-child processes

Buffer size is limited, usually 1 page, that is, 4k

Second, the creation of pipes

Three steps for pipe creation:

The parent process calls the pipe function to create the pipeline

The parent process calls the fork function to create a child process

The parent process closes fd [0], and the child process shuts down fd [1]

It is shown in the following figure:

Third, the reading and writing behavior of the pipeline

The buffer size of the pipe is fixed at 4k, so if the data in the pipe is full, the data can no longer be written, and the write call to the process will block until there is enough space to write the data again.

The read action of the pipe is faster than the write action, and once the data is read away, the pipe will free up the corresponding space for subsequent data writing. When all the data is read, the process's read () call will block until the data is written again.

IV. Routine

Communication between father and son:

# include # include int main () {int fd [2]; pid_t pid; char buf [1024]; char * data = "hello world!"; / * create pipe * / if (pipe (fd) = =-1) {printf ("ERROR: pipe create failed!\ n"); return-1;} pid = fork () If (pid = = 0) {/ * child process * / close (fd [1]); / / child process reads data and closes read (fd [0], buf, sizeof (buf)); / / reads data printf ("child process read:% s\ n", buf) from pipeline; close (fd [0]) } else if (pid > 0) {/ * parent process * / close (fd [0]); / / parent process writes data and closes the reader write (fd [1], data, strlen (data)); / / writes data printf ("parent process write:% s\ n", data) to the pipeline; close (fd [1]);} return 0;}

Brotherly correspondence:

# include # include int main () {int fd [2]; int I = 0; pid_t pid; char buf [1024]; char * data = "hello world!"; / * create pipe * / if (pipe (fd) = =-1) {printf ("ERROR: pipe create failed!\ n"); return-1;} for (I = 0) I < 2; iTunes +) {pid = fork (); if (pid = =-1) {printf ("ERROR: fork error!\ n"); return-1;} else if (pid = = 0) {break }} / * judge the created child process and the parent process by I (I = = 0) {/ * the first child process, the brother process * / close (fd [0]); / / the brother process writes data to the brother process and closes the reader write (fd [1], data, strlen (data)) Printf ("elder brother send:% s\ n", data); close (fd [1]);} else if (I = = 1) {/ * second child process, younger process * / close (fd [1]); read (fd [0], buf, sizeof (buf)); printf ("younger brother receive:% s\ n", buf); close (fd [0]) } else {/ * parent process * / close (fd [0]); close (fd [1]); for (I = 0; I < 2; iSuppli +) {wait (NULL);}} return 0;} above is all the content of the article "what does a named pipeline in Linux mean?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report