Linux learning record - 이십 inter-process communication (2) shared memory


1. System V shared memory

system is a set of standards, independent of the file system, and is a kernel module specially designed for communication by the system. It is called the IPC communication mechanism of system V.

The main method of sharing memory is to let two unrelated processes see the same resource.

1. Principle

In the address space of the process, there is a shared area between the stack and the heap area. The heap grows upward, and the stack grows downward. The overlapping area is the shared area, which is where the dynamic library is stored. Open up a space in physical memory, map this space to the shared area, and then return the starting address of this space to the user; similarly, another process does the same, so that two processes can access the same space in physical memory.

If it is no longer shared, the process can cancel the mapping relationship, that is, modify the page table, then release the shared memory of the address space, and finally release the shared memory of the physical memory, so that it will no longer be shared.

2. Simulation implementation

overall code

Analyze the code against the following words, starting from the server.cc file.

insert image description here

shmget, apply for a System V shared memory, is a system call interface, size is the size of the requested memory block, shmflg is a capital letter, used for actual operation, written in the code. After this function is successfully created, it will return an identifier of the shared memory. This subscript is different from the file subscript, so it is actually less used, and the method of using files will be more general. The key parameter needs to be set with the ftok function, and ftok combines the passed project id and path to generate the key.

The principle of shared memory is written above. There is not only one shared memory in the system, nor more than two processes sharing memory, so there will be many memories in the system at the same time. The management of them by the system is to describe and organize them first, and create a Structure to store all attributes of shared memory.

For the bottom layer, the management of shared memory becomes the management of structures. As long as two processes can get the same data, they can access the same shared memory. But the system has a lot of shared memory, how can two processes ensure that they access one memory? Here is the key of the ftok function. A process creates a shared memory, specifies a path, and passes in an id to form a key. The key will be filled in the structure of the shared memory, and another process also calls the ftok function. If it uses the same path and id, it will also generate a key, and then the two keys will be matched, which must be equal, so the same resource will be accessed.

The process related to the shared memory has exited, but the memory is still there. How to determine the existence of shared memory?

ipcs

Look at the Shared Memory Segments column, or you can add -m to just look at this column. ipcrm can be used to delete shared memory, but should it be deleted according to key or shmid?

shmid is the identifier of the shared memory returned by the shmget function, which is equivalent to fd. The key is used in the system, which is equivalent to the inode of shared memory. Shmid is used at the user level. Instructions are at the user layer, above the system interface layer. Delete the command ipcrm -m shmid.

shmctl

This also reflects a problem. The shared memory does not follow the process. If the written simulation file is generated into an executable program, the call is cleared, and the call is made again. The server will fail because there is no need to create it again, and the client can obtain and upload it. Same time. Shared memory depends on the operating system. In addition to command deletion, the system interface can also be called.

Although shared memory is created, it cannot be used directly, and the shmat function is used.

insert image description here

The address of the shared memory obtained by the user is a virtual address. The shmaddr in the shmat parameter refers to where it is placed in the address space, so it is generally set to None and let the system do it by itself. shmflg can set the attributes of the memory block, if it is 0, it is readable and writable.

3. Shared memory size

In the above code, gsize = 4096 is set. If you use ipcs -m to view the process while running, you can see that the size of the shared memory we opened is 4096. This size can be changed. The size of the shared memory is based on the PAGE page, and it is aligned upwards to the PAGE. The system allocates memory in units of pages, and this page is 4kb in size, which is the file and disk data block. The unit of IO. But if we set gsize to 4097, the size of the shared memory should be 4096 * 2, because 4097 exceeds 4096, according to the rule of aligning to PAGE, it should be 4096 times twice, but now it is 4097 by command. In fact, the capacity has been expanded, but the system is indeed aligned to double the 4096, but I only use 4097, and the shared memory size is 4097.

------------------------------------------------------------------------------------------------------------------------------------------------

4. Features of shared memory

The shared memory does not use the system call interface, because the shared memory is mapped to the process address space, it has been seen, and no user needs to do anything. It is precisely because of this feature that the number of copies can be reduced when processes communicate, so shared memory is the fastest communication between all processes.

According to the above code, if the client does not call, there is no content in the shared memory, and only the server is called, the server reader will always read and print empty, and the pipeline will block the reader, and wait until the writer starts writing before continuing Read, so there is no protection mechanism for shared memory (synchronous mutual exclusion).

Why does shared memory have this seemingly fatal shortcoming? The pipeline communicates through the system interface, and the system interface manages the data when it is called inside the system; the shared memory communicates directly.

Guess you like

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