There are many ways to communicate between Linux processes and processes. The main ways to communicate between Linux processes are:
1, Nameless pipe
2. Famous pipe
3, Signal
4, Message Queuing
5. Shared Memory
6, Signal volume
7, Socket
Pipeline operations are simpler means of communication, one process writes data to the pipeline, and another process reads data from the pipeline. Pipelines include nameless pipes and well-known pipes. The former can be used only for communication between parent processes and child processes, which can be used between any two processes.
A well-known pipeline is also called a FIFO file, a well-known pipeline once created, Linux creates a file, reads and writes to the file, and communicates data between processes.
There are some differences between FIFO files and plain files:
1, read the FIFO file process can only be opened in a rdonly way
2, the process of writing FIFO files can only be opened in a wronly way
3, FIFO files inside the content is read, it disappeared, but the contents of ordinary files will not be read after the disappearance of
For a well-known pipe learning, or using a function, but the focus is two functions, one is to create a pipeline, one is to delete the pipeline. Because the pipe is the corresponding file, the operation of the pipe is the operation of the file, and the method used is consistent with the method of the file operation.
5.2 Creating a well-known pipeline
5.2.1 Name of the letter
Mkfifo
5.2.2 Function prototype
int Mkfifo (const char *pathname, mode_t mode)
5.2.3 Function Function
Create a well-known pipeline (FIFO file)
5.2.4 owned header file
<sys/types.h> <sys/stat.h>
5.2.5 return value
success: The file descriptor for the FIFO file
failure:-1
5.2.6 Parameter Description
Pathname: FIFO file name to create (with path)
mode: Permissions for the file created
0777:rwxrwxrwx
0666:rw-rw-rw-
5.3 Delete a named pipe
5.3.1 Name of the letter
unlink
5.3.2 Function prototype
int unlink (const char *pathname)
5.3.3 function Function
Delete files (including FIFO files)
5.3.4 owned header file
<unistd.h>
5.3.5 return value
Success: 0
failed: -1
5.3.6 Parameter Description
Pathname: FIFO file name to delete (with path)
Below, you need to write code to learn famous pipeline programming.
Write two programs, one program is responsible for creating a well-known pipeline, and to write data to a well-known pipeline, another program is responsible for reading well-known pipe data, and delete a well-known pipeline.
Corresponds to the Write process:
Running programs
As you can see, the program is blocked because the program is waiting for other processes to read the FIFO file contents. In the directory more than one fifo.txt file, this is the creation of the FIFO file. Look at this file and there's data written in it.
For read processes:
To run the program:
The write process ends because a process reads data from a FIFO file and the write process is no longer blocked. Read the process read FIFO file data successfully.
The following changes to read the process code, after reading the data, do not delete the FIFO file.
To run the program:
found that the FIFO file was not deleted. But in the file, there is no content, because it was read away.