Linux learning records - 이십일 inter-process communication (3) semaphore and message queue


1. Message queue

The system maintains a queue structure between two processes ab, process a puts information in the queue, the information number is 1, process b puts information in the queue, the information number is 2; when it starts to read data later, a will read 2 data, b will read 1 data. There are many, many message queues in the system, and the system will create a structure to manage these data.

The interface to create a message queue is msgget

insert image description here

The msgflg parameter is IPC_CREAT IPC _EXCL, the key has been written before, here is a shared memory number. Check the command ipcs -q and delete ipcrm -q. Interface deletion is like this

insert image description here

insert image description here

Use man to check. The message queue has msgsnd, msgrcv sending and receiving interfaces. The message sent by the message queue is a structure type, a long variable, and a flexible array. The array is the message to be sent, and the long variable is the data type

Message queue is relatively old knowledge, it is not recommended to use, and it is not very comfortable to use. There are better communication solutions to use.

2, signal amount

1. Understand the concept

The resources that everyone can see are public resources. In order to protect this resource, the system only allows one process to access the shared resource at any time. This is mutual exclusion.

At any one time, only one execution flow is allowed to access shared resources called critical resources. Like pipes.

Critical resources are accessed through code, and any area of ​​code that accesses critical resources is called a critical section.

Atomicity refers to the property of two definite states of either not doing or done.

2. Semaphore understanding

A semaphore is also called a semaphore. It is equivalent to a counter count, which is used to describe the number of resources. This resource is the resource that can be stored in the critical section. For any execution flow, when you want to access a resource in the critical resource, you cannot directly access it. You need to apply for the semaphore resource first, that is, to see how many places can be stored in the critical area. If the application is successful, the count will be reduced by 1; When leaving the critical section, the corresponding critical resource must be released, and the count is incremented by 1. If the count is 0, the process of applying for the semaphore will be suspended. The count is always 0, and the process keeps blocking until the count is greater than 0 again.

Each process will apply for a semaphore before entering the critical section, and release the semaphore when it goes out.

No matter what process needs to see the semaphore first. The system makes the semaphore a shared resource, and the +±- of the semaphore is also atomic.

Applying for a semaphore resource is regarded as a P operation, and releasing a semaphore resource is regarded as a V operation. The related operations of the semaphore are actually PV operations.

Since processes need to see the same counter resource, semaphores are relegated to interprocess communication.

If the counter is 1, when a process has applied for the semaphore resource, and the count is reduced to 0, other processes cannot get the resource, which forms a binary semaphore, which is a mutual exclusion function.

3. Interface

Semget interface, it has key, semflg parameters, semflg has two options IPC_CREAT IPC_EXCL, but there is a parameter nsems in the middle, this parameter means the number of semaphores, that is to say, a process can apply for multiple semaphores at one time , to view the semaphore is ipcs -s, to kill the semaphore is ipcrm -s + semaphore number.

The semctl interface can also be used to delete. The semctl interface has a semnum parameter, which represents which semaphore to operate on. The cmd parameter corresponds to some options, and it has other parameters, which are some variable parameters. Use man to view them. See more details.

The semop interface is to operate the semaphore. The semid in the parameter indicates the semaphore to be operated; the struct sembuf* sops parameter is a structure that needs to be explicitly defined, which semaphore should be in it, the operation to be done, and the flag has some Your own options, you can choose the default.

4. Understand IPC

Shared memory has its own data structures.

insert image description here

This is the data structure that the user layer can see, and the bottom layer of the system has more information, it just gives the information that the user can see.

Semaphores, shared memory, and message queue structures have many things in common, such as the first variable struct ipc_perm shm/sem/msg_perm.

The system will define an array, which is of type struct ipc_perm* ipc_id_arr[], so it is an array of pointers, the elements of which point to variables of type struct ipc_perm, that is, the first variable of a structure such as a semaphore, pointing to the first variable One also points to the entire structure. So in this way, all ipc resources in the kernel can be managed in an array. If you want to access other elements of the shared memory, just cast the array type to the structure type of the shared memory. This can be optimized for the system.

What is the perm variable written above? In fact, it is polymorphism. The semaphore, the variable in the message queue structure is the base class, the element of the pointer array points to a subclass object, and the corresponding perm is used.

Guess you like

Origin blog.csdn.net/kongqizyd146/article/details/130268444