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 content, I mainly come into contact with the signal mechanism for synchronization. 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 uses a signal processor
The latter is implemented using P, V.
Use signals to know which signals are there. in Linux, there are 31 general-purpose signals to be remembered, which are also said to be System
Which is the most common in v. 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 processes. struct sigaction data is used to save the phase of the signal processor.
Information.
# Include
Int sigemptyset (sigset_t * Set );
Clears the signal set.
Int sigfillset (sigset_t * Set );
Set the signal set to include all signals. You must initialize the signal set before performing * operations on the signal.
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 process signal shielding code. The signal shielding code can be used to block signals from certain signals within a certain period of time.
No. If the signal is not in the signal concentration, you don't have to discuss it, because it certainly does not respond, and it is not sure whether it can be generated. I
No tests have been performed.
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. Like Interrupt Processing
Functions are the same.
Before using signals, you must initialize the signal set. Only signals in the signal set are considered.
Two methods can be used to initialize a signal set. One is to set an empty signal set and the other is to add all signals to the signal set.
. If you do not want these two signal sets, you can add or delete signals after initialization.
Customization.
If you do not want to respond to certain signals within a period of time when the process is executed, you can use sigprocmask
Some signals in the signal set are blocked and executed 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
The following functions are available:
Specify the entry of the signal processing function, specify the signal shielding set, and specify some signals of the signal processor. So-called Signal Processing
The handler specifies some processing methods. The key lies in the installation of the signal processor, which makes the correct signal correct.
. 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 how many signals are there in a specific process?
A problem such as the handler cannot be raised. Because the signal processor is a concept, it targets signals, that is, if
If you specify a Data Structure to store processing information for a specific signal, then installing a signal processor is
Some information about the data structure is provided. The information stored in the data structure is organized by the signal processor.
A mechanism is used to handle the signal when it occurs. However
For different processors of the same signal? I think the processor may only respond to processes related to the core,
But if so, how can this be achieved?
But one thing we can know is that every signal has a signal processor (definite), which can be
Install a signal processor to specify her behavior. The signal processor is stored in his own information area (I don't know where it is
Location), but it can be transmitted to the information storage area of the signal processor through the sigaction data structure.
Information. This data structure can be changed from one to the other because it is only a carrier for temporary data transmission.
However, the sigpromask is different from the sigmask in the signal processor. The former is used to set messages in the current process.
Number shielding, which specifies the signal to be blocked when the signal processor is used. For example, when you set a specific signal
When the signal processor, we certainly cannot let its signal processor work, because it has not been set up yet, this is me
You can use sigprocmask to block the signal from the current process. After the signal processor is set up
Use sigprocmask to recover blocked signals. 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 it.
Their own signal processors make their respective responses to this signal.
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. Therefore, asynchronous signal security is involved.
The concept of full functions.
However, I don't know much about how signals are mutually exclusive between processes. I think the main use of signals is soft interruptions.
Process and process synchronization.
2. semaphores
Semaphores and signals are different. Think about them and you can understand that a signal is a fixed value to be agreed upon, while a signal
A variable records certain information.
Semaphores are something we have already touched in * system course. Here we just say a few more. Semaphores
There are two types: Name and name. 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 perform the * operation on one or more semaphores at a time.
Int semctl (INT sem_id, int semnum, int cmd,/* Union semun Arg */...);
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 send a message
Create related semaphores in the number group, which is logically clear and easy to manage. You also need
Initialization: generate or open a semaphore group to generate or delete the specified semaphore.
Only two types of * operations are used for semaphores. They are all specified through the SOPs parameter in the semop function. If this parameter
If it is an array, it is to perform * on multiple semaphores. The sem_op field in the SOPs parameter specifies
The number is P * or V. You only need to specify it. The specific * operation does not need to be implemented.
Are provided. Using semaphores, you need to know the position of the semaphores group ID and semaphores in the semaphores group (in fact
Is 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, so before your process exits, never forget to clear them.
The semaphores you generate.
Semaphores can be mutually exclusive or synchronized. We will not talk about them here. * This is introduced in the 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
Send an "I seek you" operation.
A Message Queue can be shared by multiple processes (IPC is based on this ).
Too many messages cannot be stored in one message queue, or more than one message queue can be used (however, the management may be more complex
Miscellaneous ). In addition to the massege, the message sent by the process that shares the message queue also has a flag.
You can specify the process or process in which the message will be accepted. Each process of the shared Message Queue
The queue also has its own logo, which can be used to declare its own identity.
Each message queue in the system has a Data Structure representing it. The data structure is msqid_ds,
Let's skip it here. We can see its prototype in the middle.
3.1 Message Queue Functions
Before using the message queue, you must either obtain the message queue or create one by yourself. Otherwise, you cannot use the message queue.
Queue (I think it seems redundant, please forgive me ). When this message queue is no longer used, it must also have
When a process deletes a message queue, the system does not automatically clear the message queue and msgid_ds.
Int msgget (key_t key, int msgflg );
Obtain the ID of an existing message queue, or create a Message Queue Based on the specified permissions. But how to proceed
I still don't know how to delete this message queue.
Int msgctl (INT msqid, int cmd, struct msqid_ds * BUF );
It is used to obtain 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 a queue.
Int msgrcv (INT msqid, void * msgp, size_t msgsz, long int msgtyp, int
Msgflg );
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.