A brief analysis of the signal message queue shared memory of communication between operating system processes

Source: Internet
Author: User
Tags message queue mutex semaphore

"Several confusing related concepts"

    • Process mutex: In a multi-channel program environment, only one process is allowed to access the critical resource at a time.
    • Process synchronization: Refers to the coordination of multiple related processes in the order of execution.
    • Critical resource: A resource that allows only one process to access it over a period of time.
    • Critical section: The code that accesses critical resources in each process.


"Process Communication"

The commonly used inter-process communication methods are signal, semaphore, message queue, shared memory, and so on. Communication, is a generalized meaning, not only refers to passing some message. Process communication refers to the process data sharing and data exchange between different processes.


"Signal and signal volume"

Signal and semaphore are different, although they can be used to achieve synchronization and mutual exclusion, but the signal is used by the signal processor, the semaphore is the use of P, v operation to achieve.


"Signal Volume"

The semaphore is also called the semaphore, and the data structure of the semaphore (semaphore) is a value and a pointer pointing to the next process waiting for that semaphore. It is used to coordinate data objects between different processes, and the most important application is inter-process communication in shared memory mode. 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 semaphore that controls the resource.
(2) If the value of this semaphore is positive, the resource is allowed to be used. The process will reduce the semaphore by 1.
(3) If the semaphore is 0, the resource is currently unavailable, the process goes to sleep until the semaphore value is greater than 0, the process is awakened, and the step is transferred (1).
(4) When the process no longer uses a semaphore-controlled resource, the semaphore value is added 1. If there is a process waiting for this semaphore at this time, the process is awakened.
The value of the semaphore is related to the usage of the corresponding resource. When its value is greater than 0 o'clock, it represents the number of resources currently available, and when its value is less than 0 o'clock, its absolute values indicate the number of processes waiting to use the resource. Note that the value of the semaphore can only be changed by the PV operation.
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/21/2602015.html

"PV Operation"
    • Definition of PV operation

The PV operation consists of the P operation Primitive and the V Operation Primitive (the primitive is a non-interruptible process) that operates on the semaphore.


P:passeren (via)

is the application waiting for available resources S, which means the request is allocated a unitresource, s--, wait (S)

V: virjgeren (release)

is to use the resource s after releasing the signal, which means releasing a unit resource, s++,signal (S)


P (s): ① reduce the value of the semaphore S by 1, i.e. s=s-1;

② If s>0, the process continues execution, otherwise the process is set to wait, queued to wait.
V (S): ① adds 1 to the value of the semaphore S, i.e. s=s+1;

② if s>0, the process continues; otherwise, the first process waiting on the semaphore in the queue is freed.


    • The meaning of PV operation

We use semaphores and PV operations to achieve synchronization and mutual exclusion of processes. The PV operation belongs to the low level communication of the process.



"PV operations enable synchronization and mutual exclusion between processes"

    • Process mutex using semaphores and P and V operations

The general model for realizing process mutex using semaphores and PV operations is:


Process P1 Process P2 ...... Process PN
...... ...... ...... ......
P (S) P (S) ...... P (S)
Critical section Critical section ...... Critical section
V (S) V (S) ...... V (S)


The implementation of the process mutex using the PV operation should be noted:
(1) Each program with the user to achieve mutual exclusion of p, v operation must be in pairs appear, first do p operation, into the critical area, after doing v operation, out of the critical area. If you have more than one branch, carefully check its alignment.
(2) P, v operation should be close to the critical section of the head of the tail, the critical section of the code should be as short as possible, there can not be a dead cycle.

(3) The initial value of the mutex signal S is generally 1.


    • Process synchronization with semaphores and PV operations

The PV operation is one of the typical synchronization mechanisms. A semaphore is associated with a message when the value of the semaphore is 0 o'clock, indicating that the expected message has not been generated, and that the expected message already exists when the semaphore value is not 0 o'clock. When the PV operation is used to implement the process synchronization, the P operation is called to test whether the message arrives and the V operation sends the message.

When using the PV operation to achieve process synchronization, it should be noted that:

(1) Analysis of the constraints between the process, determine the type of signal. In cases where there is a correct synchronization relationship between processes, which process executes first, which processes are executed, and what resources (semaphores) are reconciled to each other, thus identifying which semaphores to set.
(2) The initial value of the semaphore is related to the amount of the corresponding resource, and also to the position where P and V operations appear in the program code.
(3) The P and V operations of the same semaphore appear in pairs, but they are in different process codes.

http://blog.csdn.net/Jesse621/article/details/8039071


semaphore mechanism is a kind of effective process synchronization tool, which is widely used in single processor and multiprocessor system, and computer network.


"Message Queuing"

Message Queuing is a more advanced method of interprocess communication that can transfer messages between processes, and can also send an "I seek You".

Message Queuing is also known as the message queue, which is persistent with the kernel, and only when the kernel restarts or displays the deletion of a message queue does the message queue actually delete the data structure that records Message Queuing in the system struct Ipc_ids Msg_ids is located in the kernel. All message queues in the system can find access portals in the structure Msg_ids

Message Queuing is actually a list of messages, each message queue has a queue header, called a struct Msg_queue, which describes the message queue key value, user ID, group ID and other information, but it is stored in the kernel and struct struct MSQID_ DS can return or set Message Queuing information, which is in user space, similar to the MSG_QUEUE structure Message Queuing allows one or more processes to write to it or read messages, and Message Queuing is a linked list of messages.
Messages are accessed by message type, and the process must specify the message type to read the message, as well as the type of message that must be given when the message is written to the message queue, or read the first message in the queue if the read queue uses a message type of 0.
The structure of the kernel space Msg_queue describes the case of the message queue corresponding to the key value, and corresponds to the msqid_ds of the user space, so the structure of the msgid_ds can be manipulated to operate the message queue.
"Shared Memory"
Shared memory is the fastest way to communicate between processes running on the same machine because the data does not need to replicate between different processes. A shared memory area is typically created by a process, and the remaining processes read and write to the memory area. Shared memory is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
Description of function prototypes see: http://www.cnblogs.com/biyeymyhjob/archive/2012/08/04/2623323.html
Program examples: see http://blog.csdn.net/yangzhongxuan/article/details/7925750

http://blog.csdn.net/pp0xx0ww0/article/details/8701735

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A brief analysis of the signal message queue shared memory of communication between operating system 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.