Inter-process communication --- LinuxIPC Summary

Source: Internet
Author: User
Methods of inter-process communication --- LinuxIPC summary -- Linux general technology-Linux programming and kernel information. The following is a detailed description. Currently, the most common methods of inter-process communication include signal, semaphore, message queue, and shared memory.
  
The so-called process communication involves some "Contact" between different processes, which is simple and complex. The complexity varies depending on different mechanisms. Communication is a broad sense, not just the transfer of some massege.
  
Their usage methods are basically the same, so you only need to master one method, and then remember other usage methods.
  
1. Signal
In my learning, I am mainly engaged in the signal synchronization mechanism. It is said that signals can also be used for other tasks, but I still don't know what to do.
  
Signals and semaphores are different. Although they can be used for synchronization and mutex, the former is implemented by a signal processor, and the latter is implemented by P and V Operations.
  
Use signals to know which signals are there. in Linux, there are 31 common signals to be remembered, which are said to be the most common ones in systemV. Here.
  
1. 1 signal-related functions:
# Include
Int sigaction (int signo, const struct sigaction * act, struct sigaction
* Oact );

This function is used to install a signal processor for a process. struct sigaction data is used to store information related to the signal processor.
  
# Include
Int sigemptyset (sigset_t * set );

Clears the signal set.
Int sigfillset (sigset_t * set );

Set the signal set to include all signals. Prior to signal operations, you must initialize the signal set.
  
Int sigaddset (sigset_t * set, int signo );

Add a new signal corresponding to signo to the signal set.
Int sigdelset (sigset_t * set, int signo );

Delete a signal corresponding to signo from the signal set.
Int sigismember (const sigset_t * set, int signo );

Determines whether a signal is in a signal set. 1 is returned, and 0 is not.
  
# Include
Int sigprocmask (int how, const sigset_t * set, sigset_t * oset); used to set the signal shielding code of a process. The signal shielding code can be used to block signals in certain signal sets within a certain period of time. If the signal is not in the signal set, you do not need to discuss it because it is certainly not responsive and is not sure whether it can be generated, I have not performed any tests.
  
1.2 I understand how to use the signal mechanism:
The main task of using signals is the work of the signal processor, which is what you want to do. Just like an interrupt handler.
  
Before using signals, you must initialize the signal set. Only signals in the signal set are considered.
  
There are two ways to initialize the signal set. One is to set the empty signal set, and the other is to add all the signals to the signal set. If you do not want these two signal sets, you can customize them by adding or deleting signals after initialization.
  
If you do not want to respond to certain signals within a period of time when the process is executed, you can use sigprocmask to block some signals in the current signal set and execute them later.
  
After you set up the signal set, you need to install the signal processor before letting him work. The signal processor can be installed to implement these functions:
  
Specify the entry of the signal processing function, specify the signal shielding set, and specify some signals of the signal processor. The so-called signal processor specifies some processing methods. The key lies in the installation of the signal processor, which is the key to correct the signal processing. During installation, you must assign correct signal processing functions to specific signals.
  
I don't know whether the signal processors of different processes can be mixed, but questions like how many signal processors of a specific process cannot be raised. Because a signal processor is a concept, it targets signals. That is to say, if you specify a Data Structure and use it to store processing information for a specific signal, then, to install a signal processor, give the data structure some relevant information, the signal processor uses the information stored in this data structure to organize a mechanism for processing the configured signal. But what if we want to differentiate different processors for the same signal in different processes? I think the processor may only respond to processes related to the core. But if so, how can this be implemented?
  
But one thing we can know is that each signal has a signal processor (identified), which can be installed manually to specify her behavior. A signal processor is stored in his own information area (I don't know where it is), but information can be transferred to the Information Storage Area of the signal processor by sending a sigaction-type data structure. This data structure can be changed from one to the other because it is only a carrier for temporary data transmission.
  
However, sigpromask is different from sigmask in the signal processor. The former sets signal shielding in the current process, and the latter specifies the signal to be shielded when the signal processor is used. For example, when we set a signal processor for a specific signal, we certainly cannot let its signal processor work, because it has not been set up yet, this is where we can use sigprocmask to block the current process. After the signal processor is set up, use sigprocmask to recover the blocked signal. The signal processor will work when the signal is received later.
  
My idea is that the same signal can have different signal processors in different processes (generally there should be a default Processing). When a signal occurs in the system, all processes that can be connected can receive the signal and use their own signal processor to respond to the signal separately.
  
1.3 How to Use signals to synchronize Processes
Synchronization is implemented mainly by suspending the process before receiving the signal and waiting for the relevant signal. So it involves the concept of asynchronous signal security functions.
  
However, I don't know much about how signals are mutually exclusive between processes. I think the main use of signals is Soft Interrupt Processing and process synchronization.
  
2. semaphores
Semaphores and signals are different. If you think about them carefully, you can understand that a signal is a fixed value to be agreed upon, while semaphores are a variable that records certain information.
  
Semaphores have already been used in operating system courses. Here we just say a few more. Semaphores are classified into two types. Inter-process communication uses a famous semaphore, while internal communication within a process generally uses an unknown semaphore. I will not talk more about this.

2.1 semaphores related functions
# Include
# Include
# Include
Int semget (key_t key, int nsems, int semflg );
Create a new semaphore group or obtain an existing semaphore group.
  
# Include
# Include
# Include
Int semop (int semid, struct sembuf * sop, int nsops );

The semop function can operate on one or more semaphores at a time.
Int semctl (int sem_id, int semnum, int cmd ,...);

This function can be used to obtain the usage information of semaphores or to control semaphores.
  
2.2 my understanding of the semaphore mechanism
There are only two operations on semaphores: P and V. To logically facilitate the organization of semaphores, The semaphores mechanism involves a semaphores group. We can create related semaphores in a semaphores group, which is logically clear and easy to manage. Before using it, you also need to initialize them: generate or open the semaphore group, generate or delete the specified semaphore to it.
  
There are only two types of operations on semaphores. They are all specified through the sops parameter in the semop function. If this parameter is an array, multiple semaphores are operated. The sem_op field in the Sops parameter specifies whether the P operation or V operation is performed on the semaphore. You only need to specify the parameters. You do not need to implement the specific operations. All functions are provided. When using semaphores, you need to know the position of the semaphores group id and semaphores in the semaphores group (which is actually another id ). A semaphore must belong to a semaphore group; otherwise, it cannot be used by the system. Remember! Semaphores and semaphores are not automatically cleared by the system. Therefore, do not forget to clear the semaphores generated before your process exits.
  
Semaphores can be mutually exclusive or synchronized. We will not talk about them here. This is introduced in the operating system course.
  
3. Message Queue
Message Queue is a relatively advanced method for inter-process communication, because it can really transfer massege between processes, you can send an "I seek you.
  
A Message Queue can be shared by multiple processes (IPC is based on this). If there are too many messages in a process and one message queue cannot be placed, you can also use more than one message queue (but the management may be complicated ). In addition to the massege, a message sent by a process that shares a Message Queue can indicate the process or process in which the message will be accepted. Each process of the shared message queue also has its own identifier for this queue, which can be used to declare its own identity.
  
For each message queue in the system, there is a data structure to represent it. The data structure is msqid_ds, Which is omitted here. We can see its prototype in.
  
3.1 Message Queue Functions
Before using a message queue, you can either obtain the message queue or create one by yourself. Otherwise, you cannot use the Message Queue (I think This is redundant, please forgive me ). When this message queue is no longer used, you must have a process to delete the message queue. The system will not automatically clear the message queue and msgid_ds.
  
Int msgget (key_t key, int msgflg); obtains the ID of an existing message queue, or creates a Message Queue Based on the specified permissions. However, I still don't know how to delete this message queue.
Int msgctl (int msqid, int cmd, struct msqid_ds * buf); used to get a lot of Message Queue information from msqid_ds.
Int msgsnd (int msqid, void * msgp, size_t msgsz, int msgflg); used to send messages to the queue.
Int msgrcv (int msqid, void * msgp, size_t msgsz, long int msgtyp, intmsgflg );

Receives messages from the queue.

There are not many critical conditions in the message queue in this document, because this is my summary, not an introduction. You can see its detailed introduction in the gnu c library technology.
Related Article

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.