What is the purpose of interprocess communication
The purpose of interprocess communication:
Data transfer: A process has to send its data information to another process. Resource sharing: Several processes share the same resources
Notification event: A process must send a message to another process or a group of processes to inform them of the occurrence of an event
Process control: Some processes want to have complete control over the execution of another process (e.g. the debug process). At this point in time, the control process hopes to be able to intercept all traps and exceptions of another process and to know its time in good time to change the status
We will first discuss the classic IPC method:Pipeline, FIFO, message queue, semaphore and shared memory.
- Pipeline is an old form of the UNIX system's IPC. All UNIX systems offer this communication mechanism
- We call a volume of data that connects one process with another process a "pipe".
At the same time, the pipeline has the following two limitations:
- They are half duplex (data can only flow in one direction)
- Pipes can only be used between two processes with a common ancestor
What to do after the fork depends on the direction of the desired data flow. For the pipeline from the superordinate process to the subordinate process, the superordinate process closes the reading end of the pipe (fd ) and the subordinate process closes the writing end (fd ). In the case of a pipeline from the child process to the parent process, the parent process fd  and the child process fd  closes.
Pipeline read and write rules:
When no data can be read
- O_NONBLOCK disable: The read call is blocked, ie the process is stopped and waits for data to be written.
- O_NONBLOCK enable: Read call returns -1, errno value returns EAGAIN
When the pipe is full
- O_NONBLOCK disable: The write call is blocked and waits for data to be read out.
- O_NONBLOCK enable: The call returns -1, the value of errno returns EAGAIN
If all file descriptors corresponding to the end of the pipe are closed, read returns 0
- When the file descriptors corresponding to the read ends of all pipes are closed, the write operation generates the SIGPIPE signal, which ends the write operation
If the amount of data to be written does not exceed PIPE_BUF, Linux guarantees the atomicity of the write process, otherwise it is no longer guaranteed.
1. The pipeline supports half duplex. Data can only flow in one direction. If bidirectional transmission is required, two pipelines must be created in order for them to work
2. There must be a blood relationship between the processes communicating via the pipeline
3. The pipeline provides streaming services
4. In general, the process terminates, the pipeline is released, and the pipeline life cycle varies with the process
5. In general, the kernel pipes synchronize and exclude each other
FIFO is sometimes called a named pipe, and an unnamed pipe can only be used between two blood-related processes. However, unrelated processes can also communicate via FIFO. Named pipes are a special type of file.
Create named pipes
The difference between anonymous and named pipes
1. An anonymous pipe is created and opened by the pipe function
2. Named pipes are created by mkfifo and opened by open
3. Anonymous pipes must communicate between processes that are blood related, and named pipes can enable interprocess communication that is not related by providing file paths
Rules for opening named pipes
- If the current open operation is to open the FIFO for reading
- O_NONBLOCK disable: Block until there is a process to open the FIFO for writing
- O_NONBLOCK enable: Return success immediately
- When the current operation opens the FIFO for writing
- O_NONBLOCK disable: Block until there is a process to open the FIFO for reading
- O_NONBLOCK enable: error on immediate return, error code ENXIO
The message queue is a linked list of messages stored in the kernel and identified by the message queue identifier
- The message queue provides a method of sending data from one process to another
- Each data block can be considered a type, and the data block accepted by the receiver can have a different type value
- The message queue also has the same shortcomings as the pipeline. Each message has a maximum limit (MSGMAX). The total number of bytes in each message queue is limited (MSGMNB) and the total number of message queues in the system is also limited (MSGMNI).
The shared memory allows two or more processes to share a certain memory area because the data does not have to be copied between the client process and the server process. So this is the fastest type of IPC.
The shared storage is not synchronized and is mutually exclusive
Shared memory functions
The semaphore is different from the previous IPC. It is a counter used for multiple processes to provide access to shared data objects, mainly for synchronization and mutual exclusion.
- Since each process requires shared resources and some resources must be mutually exclusive, the process competes for the use of the resource. This process relationship is known as mutual exclusion.
- Some resources in the system may only be used by one process at a time. These resources are known as critical resources or mutually exclusive resources.
- In the process associated with protecting resources, it is called the critical section
means that multiple processes must work together to complete the task
In order to get shared resources, the process needs to perform some operations
1. Test the semaphore that controls the resource
2. If the value of this semaphore is positive, the process can use the resource. In this case, the semaphore value is decreased by 1, indicating that it has acquired a resource
Otherwise, if the value of this semaphore is 0, the process will sleep until the semaphore value is greater than 0 and the process will return to step 1 after waking up
When a process is no longer using a shared resource controlled by a semaphore, the value of the semaphore increases by 1, and when a process is idle waiting for the semaphore, enable it
Semaphore related functions
- What are the strongest metals on earth
- What are the learning phases of C ++
- Which disasters cost the most
- When was the first Instagram account created?
- How do you hold a flute
- Why can't the human brain feel pain?
- Does the PSN store accept payment by debit card
- What year did Airborne Music begin
- Should I face my boyfriend's ex
- IDEAS FOR DECORATING THE CHRISTMAS PARTY
- Who are the kings of heavy metal
- How to fix screeching brakes
- What style of architecture is Paris Notre Dame
- Miss being a drug addict
- Why do autistic people camouflage who they are
- How are integration and differentiation related?
- Is rocket power that makes air power superfluous
- How can I start a rap band
- Who is better Joe Quesada or Alan Moore
- What are philosophers trying to achieve
- How do I calculate the PHE volume
- What is chronology
- What is special about a Y chromosome
- Brings madness creativity