Several ways of interprocess communication and communication between threads

Source: Internet
Author: User
Tags data structures semaphore

(i) communication between threads: Because multithreading shares address space and data space, communication between multiple threads is a thread of data that can be directly provided to other threads for use,

Without having to go through the operating system.


So the communication between the threads and synchronization of the main way is lock, signal, signal volume

The communication between processes is different, and the independence of its data space determines that its communication is relatively complicated and needs to pass through the operating system.

Communication mechanisms include: pipelines, well-known pipelines, message queues, semaphores, shared space, signals, sockets (socket).


(ii) The following brief description of several modes of communication between processes:

Pipeline: It transmits data is one-way, can only flow from one side to the other, that is, a Half-duplex communication mode, only for the relationship between the process of communication, affinity

The system is either a parent-child process or a sibling process; there is no name and limited size, the transmission is unformatted flow, so the two process communication must be agreed to the format of the data communication. Pipeline

It's like a special file, but the file exists in memory, and when you create the pipe, the system assigns a page to the pipeline as a data buffer, and the process

Buffer to read and write to complete the communication. One process can only read one can only write, so called Half-duplex communication, why one can only read one can only write it? Because it's written in

The process is written at the end of the buffer, the read processes are read in the head of the buffer, and their respective data structures are different, so the function is different.


Famous pipe: See the name can know a ballpark, it is different from the pipe is that it has a name. This differs from the pipeline being able to communicate only between processes that have affinity.

It provides a path name associated with it and has its own transmission format. The difference between a famous pipe and a pipe is that a well-known pipe is a device file, stored in a file system

The process of unrelated processes can also be accessed, but it should be read in terms of the FIFO principle. The same is also single duplex.


Shared memory: Is allocating a piece of memory that can be accessed by another process. Shared memory can be said to be the most useful way to communicate between processes and the fastest form of IPC. First of all, in making

Before using a shared memory area, it must be attached to the address space of the process through a system function or as mapped to a process space. Two different processes A, B shared memory means that the same

Block physical memory is mapped to process A, B's respective process address space. Process A can instantly see Process b update of the data in shared memory, and vice versa. Because multiple processes are altogether

To enjoy the same area of memory, there must be some kind of synchronization mechanism, mutual exclusion lock and semaphore. One obvious benefit of using shared memory communication is that it is efficient because the process

You can read and write memory directly, without requiring a copy of any data. For communications such as pipelines and Message Queuing, four copies of data are required in the kernel and user space, and

Shared memory copies only two data [1]: one time from the input file to the shared memory area, and the other from the shared memory area to the output file. In fact, between processes in shared memory, and

You do not always read and write a small amount of data and then unlock it, and then re-establish the shared memory area when there is new traffic. Instead, keep the shared area until the communication is complete so that the data inside

The volume has been saved in shared memory and has not been written back to the file. Content in shared memory is often written back to the file when the mapping is released. Therefore, the use of shared memory communication mode effect

The rate is very high.


Signal: Signal is a software level on the interrupt mechanism of a simulation, in principle, a process received a signal and the processor received an interrupt request can be said to be the same.

The signal is asynchronous, a process does not have to do anything to wait for the signal to arrive, in fact, the process does not know exactly when the signal arrived. Signal is the interprocess communication machine

The only asynchronous communication mechanism in the system, can be seen as asynchronous notification, notification of the process of receiving signals what happened. The signal mechanism after POSIX real-time expansion, the function is more

Enhanced, in addition to the basic notification function, you can also pass additional information. There are two sources of signal events: hardware sources (e.g. we pressed the keyboard or other hardware failure);

Software source. The signals are divided into reliable and unreliable signals, real-time and non-real-time signals. The process has three responses to signal 1. Ignore signal 2. Capture Signal 3. Perform the default action.


Semaphore: can also be said to be a counter, commonly used to deal with process or thread synchronization problems, especially for the critical resource access synchronization problem. Critical resources: For a moment only

A resource that can be manipulated by a process or thread, indicating that the number of critical resources that can be accessed by the concurrent process, when the semaphore value is greater than or equal to 0 o'clock, indicates that it is waiting if it is less than 0 o'clock

The number of processes using the critical resource. More importantly, the value of the semaphore can only be changed by the PV operation.


Socket: Socket communication, UPD or TCP.

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.