Communication between processes

Source: Internet
Author: User
Tags message queue mutex semaphore

The way and mechanism of process/thread synchronization, interprocess communication

One, process/thread synchronization mechanism.

Four ways of critical, mutex, event, Semaphore
Differences between critical sections (Critical section), mutexes (mutexes), semaphores (Semaphore), Events (event)
1, critical area: Through the serialization of multithreading to access public resources or a piece of code, fast, suitable for controlling data access. Only one thread is allowed to access the shared resource at any time, and if more than one thread attempts to access the public resource, the other threads that attempt to access the public resource will be suspended after one thread enters, and wait until the thread that enters the critical section leaves and the critical section is freed before other threads can preempt it.
2, Mutex: adopt mutually exclusive object mechanism. Only the thread that owns the mutex has access to the public resource, because there is only one mutex object, so that the public resources are not accessed by multiple threads at the same time. Mutual exclusion can not only realize the common resources security sharing of the same application, but also can realize the security sharing of common resources of different applications. The mutex is more complex than the critical section. Because using mutexes not only enables the secure sharing of resources in different threads of the same application, but also enables secure sharing of resources between threads of different applications.
3. Semaphore: It allows multiple threads to access the same resource at the same time, but it needs to limit the maximum number of threads that access this resource at the same time. Semaphore objects are synchronized to threads in a way that allows multiple threads to use shared resources simultaneously, which is the same as the PV operation in the operating system. It indicates the maximum number of threads concurrently accessing the shared resource. It allows multiple threads to access the same resource at the same time, but needs to limit the maximum number of threads that access this resource at the same time.

The concept of PV operation and signal volume is presented by Dutch scientist E.w.dijkstra. The semaphore S is an integer, s greater than or equals zero represents the number of resource entities available to the concurrent process, but s less than zero indicates the number of processes waiting to use the shared resource.
P Operation Request Resources:
(1) s minus 1;
(2) If s minus 1 is still greater than or equal to zero, then the process continues to execute;
(3) If s minus 1 is less than 0, then the process is blocked into the queue corresponding to the signal, and then transferred to the process scheduling.
  
V Operations Release Resources:
(1) s plus 1;
(2) If the sum result is greater than 0, the process will continue to execute;
(3) If the sum result is less than or equal to zero, a wait process is awakened from the waiting queue of the signal and then returned to the original process to continue or transfer to the process schedule.
4, Event: the way to maintain the synchronization of threads by notification operation, it is also convenient to achieve the priority comparison of multiple threads operations.

Summarize:
1. The mutex is very similar to the critical section, but the mutex can be named, which means it can be used across processes. So creating mutexes requires more resources, so using a critical section just to be used within a process can bring speed advantages and reduce resource usage. Because the mutex is a cross-process mutex once created, it can be opened by name.
2. Mutexes (mutexes), semaphores (Semaphore), events can be used across processes to synchronize data operations, while other objects are not related to data synchronization operations, but for processes and threads, if the process and thread are running in a state that is not signaled, signaled state after exiting. So you can use WaitForSingleObject to wait for processes and threads to exit.
3. The mutex can be used to specify that the resource is exclusive, but if one of the following cases can not be handled by mutual exclusion, for example, now a user buys a three concurrent Access License database system, depending on the number of access licenses purchased by the user to determine how many threads/processes can simultaneously perform database operations, At this time, if the use of mutual exclusion is no way to complete this requirement, the Beacon object can be said to be a resource counter.

II. modalities of communication between processes

Because it is easy to confuse, we have also listed the Interprocess communication method here for comparison.

Inter-process communication is the transmission or exchange of information between different processes, so what are the different processes between the two can access the media? The user space of the process is independent of each other and is generally inaccessible to each other, with the only exception being the shared memory area. However, the system space is a "public place", so the kernel can obviously provide such conditions. In addition, it is a peripheral that can be accessed by both parties. In this sense, two processes can of course also exchange information through ordinary files on disk, or through "registry" or some table entries and records in other databases. Broadly speaking, this is also a means of inter-process communication, but it is generally not counted as "interprocess communication". Because the efficiency of those communication means is too low, and people's requirements for inter-process communication is to have a certain real-time nature.

Inter-process communication mainly includes pipelines, system IPC (including message queue, semaphore, shared storage), SOCKET.

Pipelines are divided into well-known pipes and nameless pipes, which can only be used for communication between kinship processes, whereas well-known pipelines can be used between unrelated processes.

Message Queuing is used to run interprocess communication on the same machine, similar to pipelines;

Shared memory is typically created by one process, and the remaining processes read and write to the memory area. There are two ways of getting shared memory: mapping/dev/mem devices and memory image files. The former does not bring additional overhead to the system, but is not commonly used in reality because it controls access to actual physical memory;

Essentially, a semaphore is a counter that is used to record access to a resource, such as shared memory. In general, in order to get a shared resource, the process needs to do the following:

(1) test the amount of signal to control the resource;

(2) If the value of this semaphore is positive, the use of the resource is allowed, and the process will reduce the number of incoming signals by 1;

(3) If the semaphore is 0, then the resource is currently unavailable, the process goes to sleep until the semaphore value is greater than 0, the process is awakened, into the step (1);

(4) When the process no longer uses a semaphore-controlled resource, the semaphore value is added 1, and if there is a process waiting for this semaphore at this time, the process is awakened.
Socket communication is not proprietary to Linux, almost all of the operating systems that provide the TCP/IP stack are provided with sockets, and all such operating systems are almost identical to the socket programming method

Comparison of process/thread synchronization mechanism and inter-process communication mechanism

It's obvious that 2 of them are similar, but the difference is great.

Synchronization is mainly critical area, mutual exclusion, semaphore, event

Interprocess communication is a pipeline, memory share, message queue, semaphore, socket

In common, semaphores and messages (events)

Other information:

The inter-process communication (IPC) approach is mainly in the following ways:
Pipe/fifo/Shared memory/message queue/Signal

1. Pipelines also have named pipes and non-named pipes (that is, anonymous pipes), non-named pipes (that is, anonymous pipes) can only be used for parent-child process communication, named pipes can be used for non-parent-child processes, named Pipes are FIFO, pipeline is first-out communication mode

2. Message Queuing is used for communication between two processes, first creating a message queue in one process, then writing the data to the message queue, and the other process fetching data from that message queue. Note that Message Queuing is created by creating a file, and if one process writes data to a message queue, the other process does not take out the data, even though the process of writing data to the message queue has ended, the data saved in the message queue does not disappear. That is, the next time you read the data from this message queue is the last data!!!!

3. Semaphore, which is the same as the semaphore under Windows, so don't say more.

4. Shared memory, similar to a shared variable in a DLL under Windows, but the shared memory area under Linux does not require anything like a DLL, as long as a shared memory area is created first, other processes can access the data in this shared memory area at a certain step, of course readable and writable

Comparison of the above methods:

1. Pipeline: Slow speed, limited capacity, only the parent-child process can communicate

2.FIFO: can communicate between any processes, but is slow

3. Message Queuing: The capacity is limited by the system, and pay attention to the first reading, to consider the last time you did not read the data of the problem

4. Semaphore: cannot transmit complex messages, can only be used to synchronize

5. Shared memory Area: can easily control capacity, fast, but to maintain synchronization, such as a process in writing, another process to pay attention to read and write problems, the equivalent of thread security threads, of course, the shared memory area can also be used for inter-thread communication, but not necessary, A piece of memory in the same process is already shared between threads

Communication between processes

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.