Linux Process Communication--summary of signal and signal volume

Source: Internet
Author: User
Tags message queue semaphore signal handler

The most commonly used interprocess communication methods today are: signal, semaphore, message queue, shared memory.
The so-called process communication, is the different processes to carry out some "contact", this kind of contact is simple, also has the complexity. The mechanism is different and the complexity is not the same. Communication is a generalized meaning, not just a transfer of some massege. Their usage is basically the same, so just have to master one usage, and then remember the other usage can be.

1. Signal
In myLearningIn the content, the main contact with the signal to achieve the mechanism of synchronization, it is said that the signal can also be used to do other things
Love, but I don't know what to do yet.
Signals and semaphores are different, and although they can be used to achieve synchronization and mutual exclusion, the former uses a signal processor to
The latter is implemented using p,v*.
Use the signal to know what signal, under Linux there are 31 need to remember the general signal, is said to be the system
The most frequently used ones in V. Here a little.
1. 1 Signal correlation function:
#include
int sigaction (int signo, const struct sigaction *act, struct sigaction
*oact);
This function is used to install a signal processor for the process, and the struct sigaction data is used to hold the signal
The information is closed.

#include
int Sigemptyset (sigset_t *set);
Clears the signal collection.
int Sigfillset (sigset_t *set);
Sets the Signal collectionSetTo include all signals. The signal set must be initialized when the signal is made *.

int Sigaddset (sigset_t *set, int signo);
Add signo corresponding new signals to the signal set.
int Sigdelset (sigset_t *set, int signo);
Remove the signo corresponding signal from the signal set.
int Sigismember (const sigset_t *set, int signo);
Infer whether a signal is in the signal set. 1 is returned, and 0 is not.

#include
int sigprocmask (int how,const sigset_t *set, sigset_t *oset);
The signal mask used to set the process. The signal shielding code can be used to block some signals in a certain period of time
Signal is not in the signal set, you do not have to discuss it, because it is definitely not responding, can not be generated and not sure, I
No tests have been done.

1. 2 I understand how to use the signaling mechanism:
The main thing to do with the signal is to work with the signal processor, which is what you want to do. Just like interrupt processing
function is the same.
In the use of signals once, the first to initialize the signal set, only the signal set inside the signal will be considered.
There are two ways to initialize a signal set, one is to set an empty signal set, and one is to add all the signals to the signal set.
In Suppose you want a set of signals that is not both, and can be done by adding and removing signals after initialization
Custom.
Assuming that you do not want to respond to certain signals for a period of time that the process is running, you can use Sigprocmask for the current
Signals in the signal set are blocked and run later.
After you set the signal set, you need to install the signal processor before you let him work. The installation signal processor can
Now these fewfunction:
Specifies the entry of the signal processing function, specifies the signal mask set, and specifies some flags for the signal processor. The so-called signal office
Processor, the key is to install the signal processor, which is to make the correct signal
The processing key. When installing, be sure to assign the correct signal processing function to the specific signal.
I don't know if the signal processor between the different processes can be mixed, but how many signals are there in a particular process
The problem of the manager cannot be mentioned. Since the signal processor is a concept, he is aiming at the signal, that is to say,
You specify a data structure that is used to store processing information for a signal, then the installation signal processor is
Give this data structure some relevant information, using the signal processor is to use the information stored in this data structure to organize
A mechanism when this signal occurs, you do some processing that you have set up. But the hypothesis distinguishes between different processes
A different processor for the same signal? I think the processor may respond only to signals that are related to the process in which it belongs.
But assuming that's the case, how does that happen?
Just one thing to know is that every signal has a signal processor (OK) that can move
Install the signal processor to specify her behavior. The signal processor is stored by his own area of information (I don't know what
Location), but can be passed to the information storage area of the signal processor by the sigaction type of data structure
Information. This data structure can be made by one, because it is simply a carrier of data being transmitted temporarily.
However, the sigpromask and the signal processor inside the sigmask is not the same, the former is in the process of the current process settings letter
A signal that is required to be shielded when the signal processor is in effect. For example, when setting a specific signal
Signal processor, of course we can't let its signal handler work, because it's not set up yet, this is my
can use Sigprocmask to let the current process start plugging the signal, when the signal processor is set up, and then
Use Sigprocmask to recover blocked signals. When the signal is received later, the signal processor is able to work.
My idea is that the same signal can have different signal handlers in different processes (generally there should be a default
Treatment), whensystemWhen a signal is taken, all the accepted processes can receive the signal and use his
Their own signal processors to respond to this signal.

1. 3 How to use the signal for synchronization between processes
Synchronization is achieved mainly by suspending the process before accepting the signal and waiting for the relevant signal. So it involves an asynchronous signal security.
The concept of a full function.

Just how the signal is going to achieve inter-process repulsion, I don't understand much, I think the main use of the signal is still in the soft interrupt
Processing and process synchronization.


2. Signal Volume
Semaphores and signals are different things, can be understood in detail: the signal is to achieve the fixed value of the contract, and the signal
Volume is a variable that records certain specific information.
Semaphore this stuff we've been exposed to in the * system course, here's just a few more cursory remarks. Signal Volume
is divided into famous and nameless two kinds. Inter-process communication with a known semaphore, the same process internal communications generally with the nameless signal volume.
I'm not saying much about this.
2. 1 Semaphore correlation function
#include
#include
#include
int Semget (key_t key, int nsems, int semflg);
Create a new semaphore group or get a semaphore group that already exists.

#include
#include
#include
int semop (int semid, struct sembuf *sop, int nsops);
The SEMOP function is capable of making 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 information about the use of some semaphores or to control the amount of semaphores.

2. 2 My understanding of the semaphore mechanism
The amount of the semaphore is only two: P, v.
In order to logically facilitate the organization of semaphores, one concept in the semaphore mechanism is the semaphore group. We can put a letter
To create the relevant semaphore in the number group so that it is logically clear and easy toManagement。 You need to do the same for them before you use them.
Initialize: Generate or open a semaphore group to generate or delete the semaphore you specify.
The number of semaphores is only two, and he is specified by the SOPs in the SEMOP function, assuming this
is an array, then it is done with multiple semaphores. The Sem_op field in the SOPs parameter indicates the letter
Whether the number is p* or v*. You just have to specify, the specific * do not need you to implement, in the function
have been provided. With semaphores, you need to know the position of the semaphore Group ID and the semaphore in the Semaphore group (in fact
is another ID). A semaphore must belong to a semaphore group, otherwise it cannot be used by the system. Remember!
The semaphore and semaphore groups are notown initiativeClean up, so don't forget to clean up before your process exits
The semaphores that you generate.
Signal volume can be mutually exclusive, but also can achieve synchronization, here do not say, * system course is introduced.



3. Message Queuing
Message Queuing is one of the more advanced interprocess communication methods, because it really can transfer massege between processes, you
Send a "I seek you" both to be able.
A message queue can be shared by multiple processes (IPC is based on this); Suppose a process's
Too many messages a message queue cannot be placed, and more than one message queue can be used (it is possible to manage more complex
(Miscellaneous). The message sent by the process that shares Message Queuing except for the Massege itself is another flag, this flag
Be able to indicate which process or type of process the message will be accepted by. Each process that shares Message Queuing targets this
Queues also have their own flags that can be used to declare their identities.
For each message queue in the system, there is a data structure to represent it, and this data structure is msqid_ds,
It is omitted here and can be seen in the prototype.

3. 1 Message Queuing related functions
Before using Message Queuing, you either get this message queue or build one yourself, otherwise you can't use the message
Queue (I think it's like superfluous, please forgive me). When this message queue is no longer in use, there must also be
A process to delete Message Queuing, the system does not voluntarily clean up the message queue and Msgid_ds.

int Msgget (key_t key, int msgflg);
Gets the ID of an existing message queue, or creates a message queue based on the permissions that are set. But how to
To delete this message queue, I'm not quite sure yet.
int msgctl (int msqid, int cmd, struct msqid_ds *buf);
Information that is used to obtain a very many message queue itself 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, int
MSGFLG);
Receives a message from the queue.
I don't have much to deal with in this document for some critical situations in Message Queuing, as this is my summary, not an introduction.
In the GNU C libraryTechnologycan see its specific description in the

Linux Process Communication--summary of signal and signal volume

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.