Talk C together (87th back: C language instance-Overview of inter-process communication using pipelines)
Hello, everyone. In the previous session, we talked about the example of inter-process communication. This example is the use of pipelines.Inter-process communication. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
In the previous chapter, we introduced how to use pipelines to communicate process queries. The time is not long. I believe you will remember it. Today, we will introduce another way of inter-process communication: pipelines. As we all know, when we use signals for inter-process communication, it essentially sends a value. If we want to send some data between processes, we can't do anything. At this point, the pipeline will come in handy. We canSend data between processes through pipelines. Next we will introduce MPs queues in detail.
The pipe is similar to the tap water pipe in our life. It can bring tap water into our home. Pipelines also appear in Linux commands. "|" Is usually used to represent pipelines. Here is an example of using pipelines in Linux commands:
Env | grep bash // enter the following command in the terminal and run SHELL =/bin/bash // command to run the result
In the above example, we first use env to output environment variables, and then use the content output by the env command in the pipeline as the input content of the grep command. The grep command retrieves bash from the input content, and display the search result, that is, the command running result we see.
The pipelines we use in the process are similar to those used in the above example:Input data in one process and output data in another process.The two processes send data through pipelines.
We will introduce three pipelines and how to use them.:
The first pipeline is called a pseudo pipeline. The second type of pipeline is called the entry-level pipeline. The third type of media transcoding is a real media transcoding queue.
The pseudo-pipeline uses popen/pclose to open a file stream, and then uses the I/O function to perform streaming operations. Because the popen parameter is a Linux Command and it runs Linux commands by starting shell, the performance is low. It works the same as the pipeline command "|" in the terminal. This is why I call it a pseudo-pipeline.
The entry-level pipeline creates an pipeline through pipe and returns two file descriptors. Then, the read/write System Call is used to perform operations on the returned fd to implement the process communication function. This method must be used with the fork function. Because the two fd instances are in the same array, most of the arrays are local variables, and local variables can only be used in parent and child processes created by fork. Of course, you can define the fd array as a global variable, but the risk of global variables is too high.
In a real sense, an MPS queue needs to use mkfifo to create an MPS queue file, which returns an fd. Next, you can use open/read or other systems to call the fd operation. You can operate the MPs queue file like a normal file. The pipe has another name: Named Pipe (FIFO ).
Today, we only provide a basic overview of pipelines. We will introduce how to use pipelines in detail in the following chapter. At the same time, we will also introduce how to use pipelines to communicate between processes.
For more information, see the following. I want to know what examples will be provided later, and I will try again.