Purpose of communication
1. Data transfer
One process needs to send data to another process.
2. Resource Sharing
Share the same resources among multiple processes.
3. Notification events
A process needs to send a message to another/group process to notify them that an event has occurred.
4. Process Control
Some processes want full control over the execution of another process, at which point the control process wants to be able to intercept all the operations of another process and be able to know its state changes in a timely manner.
Communication mode
1. Nameless pipe (PIPE): Data transmission
2. Well-known pipeline (FIFO): Data transmission
3. Signal (signal): Notification event
4. Message Queuing
5. Memory Sharing
6. Semaphores: Resource Sharing
7. Socket (SOCKET)
Pipeline communication
One process writes data at the end of the pipeline, and another process reads the data from the head of the pipe. Pipelines include nameless pipes and two of known pipelines, which can only be used for communication between parent and child processes, which can be used to run communication with any two processes in the same system.
Characteristics of pipeline communication
1. Pipeline communication is unidirectional, with fixed read and write ends.
2. After the data has been read out of the pipeline by the process, the data does not exist in the pipeline.
3. When the process goes to read the empty pipeline, the process blocks.
4. The process is blocked when the process is writing data to a full pipeline.
5. Pipe capacity is 64KB.
Nameless Pipe
In a Linux system, once a nameless pipe is created, manipulating the nameless pipe is the same as manipulating the file. The read end of a nameless pipe is treated as a file, and the writing end of a nameless pipe is considered a file. You can therefore use functions such as read,write,close to access nameless pipes.
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>voidMain () {//Creating Pipelines intpipefd[2]; Pipe (PIPEFD); //Create a process intpid; PID=Fork (); //Pipeline Communication if(PID >0){ //Write Data Charwbuf[ -] ="Hello world!"; Write (pipefd[1], WBUF, -); //wait for ReadWait (NULL); //To close a write pipelineClose (pipefd[1]); }Else if(PID = =0){ //reading Data Charrbuf[ -]; Read (pipefd[0], Rbuf, -); //Show Dataprintf"%s\n", RBUF); //To close a read pipelineClose (pipefd[0]); } //End ProcessExit0);}
[Country EMBED strategy] [080] [Unknown pipe communication]