Linux kernel: process management - shared memory

1. Shared memory

In the system, two different processes will maintain their own address space. This address space is generally a virtual address, which will be mapped to the corresponding physical memory through the mmu and page table, because different processes will have different memory spaces. Therefore, two processes cannot see each other's data, and shared memory is to enable two processes to see the same address space, so as to realize data interaction between different processes.

It is worth pointing out that shared memory is the most efficient way of inter-process communication, because data is exchanged directly by accessing memory, eliminating the need for copying data in the message queue and performing P and V operations in the semaphore. time.

2. Functions in shared memory

1. Creation and destruction of shared memory

create:

In the function parameters,

Needless to say, the key value is created with the ftok function, and needs to use the path name and an integer of proj_id;

Size refers to how much space is needed. In Linux, data is divided into pages. One page is 4K, which is also 4096 bytes. The alignment number of this size is an integer multiple of 4096, that is, if you specify 4098 bytes, the system will allocate a size space of 4096*2;

shmflg is the same as message queue and semaphore. There are two options: IPC_CREAT and IPC_EXCL. Using these two values ​​at the same time will create a new shared memory. If the shared memory exists, it will return -1; and when IPC_CREAT is used alone If the shared memory exists, it will return the existing shared memory ID, otherwise it will create a new one;

destroy:

In the function parameters,

shmid is the ID of the shared memory created, and the option of cmd can be IPC_RMID, and the following buf is a structure pointer. Here, the shared memory needs to be destroyed, and buf can be set to NULL;

2. Association and disassociation of shared memory

After the shared memory is created, what is needed is to associate the process with the shared memory so that it can be used. Similarly, the association needs to be released after use:

In the figure above,

shmat is to associate the process with the shared memory to attach, shmid is the ID of the shared memory, and the following shmaddr is a buffer pointer, if it is NULL, the system will automatically select a suitable memory as the buffer, and shmflg can be SHM_RDONLY means that the process is read-only in shared memory, and when shmflg is 0, it means both reading and writing are possible, and there is no write-only option;

It can be seen that the return value of the shmat function is a void pointer, which can be understood as a pointer that has opened up a space in memory to return;

There is only one parameter in the shmdt function, and if the memory is opened up, it must be released, so shmaddr is the return value of shmat, which is used to cancel the association;

Chestnut time:

Same as the message queue and semaphore mentioned above, functions in shared memory can be encapsulated:

This creates two processes shm_client.c and shm_server.c:

Both processes are associated with the created shared memory, shm_client.c writes data into the shared memory, and shm_server.c takes out the data and prints it out, so as to realize the communication between processes, run the program:

What I want to say here is that the semaphore mentioned in the previous article can actually be used in combination with shared memory, because when two processes access a shared memory at the same time to write data or read data, conflicts will occur , At this time, the shared memory can be regarded as a critical resource and marked with a semaphore, which can complete the synchronization and mutual exclusion problems between processes.

Kernel information through train: Linux kernel source code technology learning route + video tutorial code information

Learning through train: Linux kernel source code/memory tuning/file system/process management/device driver/network protocol stack

Original Author: Proficient in Linux Kernel

Original address: Linux Kernel: Process Management - Shared Memory - Zhihu (Copyright belongs to the original author, contact to delete infringement message)

Guess you like

Origin blog.csdn.net/m0_74282605/article/details/130135432