After reading these days UNP v2, on the process of communication and synchronization of the way to know, now the main points of knowledge summarized as follows:
According to the history, the first pipeline, FIFO, signal, then the SYSTEMV IPC, then the later Poxis IPC,SYSTEMV IPC is the kernel persistence, and poxis according to the implementation of the different kernel has some of the file system persistence.
Pipelines: Divided into pipelines and FIFO, pipelines are generally used for parent-child processes, cannot be transferred across processes, FIFO can be set up on the file system object, belonging to a file type (identified by P), can communicate across processes. Their content is process-persistent, meaning that when the process is finished, the contents of the pipeline are lost. Pipelines are generally simplex, only one-way communication, the premise of pipeline communication is that both ends of the pipeline are open, not like Message Queuing, such as open end can send messages. Pipeline transmission is also a byte stream, if necessary, the need to develop application layer transport protocol to prevent sticky packets.
Message Queuing: As a list of messages, it is not necessary for a process to wait for a message to arrive on that queue until it writes a message to a queue. With kernel persistence, it is said that one process can abort after writing a message, allowing another process to read the message at a later time. A message record with a boundary is stored, each message has a priority, and the Poxis message queue can be notify to notify the message via a signal or thread after the message arrives. SYSTEMV Message Queuing can specify priority, high priority delivery, and support for receiving messages of the specified priority. When writing a one-to-many model, unlike pipelines that need to create multiple connections, Message Queuing only needs to create a connection from which each process can identify its own messages. The disadvantage is that it cannot be combined with select/poll, and it needs to be transferred by pipe if necessary. The newly written application should first consider using the Poxis message queue.
Mutex and condition variables: can implement producer and consumer models, can be dynamically allocated or statically allocated, when dynamically allocated and specified share can be used for inter-process synchronization, the problem is that when the process or thread that owns the mutex is terminated abnormally, it may cause the critical section data to be inconsistent. When a process is blocked on a condition variable, if the call to Pthread_exit/pthread_cancel cancels the thread, it causes the thread to get the mutex for that condition variable again, and the thread exits without releasing the lock, which causes a deadlock. The workaround is to use Pthread_cleanup_push/pthread_cleanup_pop to implement the cleanup handler installation and removal.
Read-write Lock: Can improve the concurrency, according to the implementation of different, can be divided into read first, write priority. Read-write locks are also locks between threads by default, and you need to specify the Pthread_process_shared property if you need to lock between processes.
Record Lock: A fine-grained lock can be provided, and an existing lock cleanup is completed when the program terminates. Record locks are maintained by the kernel, divided into suggested and mandatory locks, and mandatory locks prevent non-collaborative processes from reading a locked file, but this does not guarantee data consistency.
Semaphore: Divided into a well-known semaphore, based on the amount of memory signal, SYSTEMV Semaphore, Poxis a known semaphore opened by a file name, identify the structure of a file system in a kernel, so it is persistent with the kernel, can be used between processes or threads, while memory-based semaphores have the continuity of the process.
There are three different points relative to the mutex:
1. The mutex must be threads unlocked by the line to which he is locked, the semaphore has no such limitation;
2. The semaphore has a value associated with it, which is added one by the hang-out operation and one by the wait operation, so any thread can hang out a signal, even if there is no thread waiting for the semaphore value to be positive, but if a thread calls Pthread_cond_signal, But no thread is blocked in the pthread_cond_wait call, then the signal for the corresponding condition variable is lost.
3. The only function that can be safely called from a signal handler in various synchronization techniques is the Sem_post function. Mutexes are optimized for locking, and conditional variables are optimized for waiting, and semaphores can be used for locking and waiting, which can lead to more overhead and higher complexity.
The SYSTEMV semaphore is a count semaphore set, he maintains a set of semaphores, he allows to increase or decrease the semaphore value is not just 1, so the relative POSIX semaphore is more complex.
Shared memory Area: The shared memory area is the fastest in the IPC form, and once such a memory area is mapped to the address space of the shared his process, the data transfer between these processes does not need to involve the kernel (that is, it does not need to execute any system calls into the kernel), but it generally requires some form of synchronization. In the pipeline, FIFO, Message Queuing needs to pass through the kernel, so the data needs to be replicated four times.
All sharing methods have pros and cons, such as kernel-based some need memory copy, shared memory is fast but need to lock to ensure data consistency, so we should use on demand. There are also signals and sockets that are not covered here because of too much content.
Linux Synchronization and communication