Linux/Windows Process/inter-thread Communication Mechanism

Source: Internet
Author: User

Linux inter-process communication

Introduction to several main methods for inter-process communication in Linux:

A)
Pipeline (PIPE) and famous Pipeline (Named Pipe): pipeline can be used for communications between kinship processes. Famous pipelines overcome the pipe's no-name restrictions. Therefore, in addition to the functions of pipelines, it also allows communication between unrelated processes;

B)
Signal: a signal is a complex communication method used to notify the receiving process of an event. In addition to inter-process communication, the process can also send a signal to the process itself; in addition to the early UNIX signal semantic function Sigal, Linux also supports the signal function sigaction whose semantics complies with the posix.1 standard (in fact, this function is based on BSD, BSD in order to achieve reliable signal mechanism, it can also unify external interfaces and implement the signal function again using the sigaction function );

C)
Message (Message Queue): A chain table of messages, including POSIX Message Queue System V message queue. A process with sufficient permissions can add messages to the queue. A process with the read permission can read messages from the queue. The message queue overcomes the disadvantages of low signal carrying information, and the pipeline can only carry unformatted byte streams and limited buffer size.

D)
Shared Memory: Allows multiple processes to access the same memory space. It is the fastest available IPC format. It is designed to reduce the running efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.

E)
Semaphore (semaphore) is mainly used for synchronization between processes and between different threads of the same process.

F)
Socket: a more general inter-process communication mechanism that can be used for inter-process communication between different machines. It was initially developed by the BSD branch of the UNIX system, but now it can be transplanted to other UNIX-like systems: Linux and System
All the variants of V Support sockets.

Linux Inter-thread communication: mutex, semaphore, and condition variable

Inter-thread communication in Windows: critical section, mutex, semaphore, and event)

Windows inter-process communication: pipelines, Memory Sharing, message queue, semaphore, socket

Common Features of Windows processes and threads: semaphores and messages (events)

Difference between the critical section and mutex

1. The critical section can only be used for mutually exclusive access between objects in the same process. The mutex can be used for mutually exclusive access between object processes or threads.

2. The critical section is a non-kernel object. The lock operation is performed only in the user State, and the speed is fast. The mutex is the kernel object. The lock operation is performed in the core state, which is slow.

3. The critical section and mutex are available on windows. In Linux, only mutex is available.

Differences between Windows threads:

1. The mutex function is very similar to that of the critical zone, but the mutex can be named, that is, it can be used across processes. Therefore, creating mutex requires more resources. Therefore, if you only use it within a process, using the critical section will bring speed advantages and reduce resource occupation. Because the mutex is a cross-process mutex, once created, it can be opened by name. The mutex can not only achieve the security sharing of public resources for the same application, but also the security sharing of public resources for different applications.
. Mutex is more complex than that in the critical section.

2. mutex, semaphore, and event can all be used by a process to synchronize data. Other objects have nothing to do with data synchronization, but for the process and thread, if the process and thread are in the running status, there is no signal, and there is a signal after exiting. Therefore, you can use waitforsingleobject to wait for the process and thread to exit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.