1. First, call shmget to allocate a new * * * memory, where you can specify its size. If you want to allocate a plastic surgery, you can set the size parameter to 4. If you want to * * * enjoy a structure, set the size parameter to your structure size. The operating system doesn't care if you want to * * *. And in fact, the operating system will align the size you require according to the size of the memory page, which means that it may actually allocate several pages of physical storage space to you, as long as this space can accommodate the size you specify. Its third parameter is about some access rights settings, which is too long to talk about. I suggest you search for it yourself, or check the help with man. In a word, after calling shmget, the system will create a section of * * * shared memory for you, and then return to give you a shmid, which is the logo of this * * * shared memory, which you can understand as giving it a name.
2. Then call shmat to map this * * * shared memory to the virtual address space of your process. The first parameter of this function is the name shmid; of the * * * shared memory you created by calling shmget before; The second parameter is a pointer to an address in the virtual memory space of your process. You can force the operating system to map the * * * memory to the virtual memory address you specify by passing in a definite address (it may fail, if the virtual address space you specify has been mapped to other physical memory spaces), or you can let the system choose a suitable address for you by passing in a zero address (it will return the address to you by returning a value). The third parameter allows you to specify some special flag bits. Again, it is too complicated to search for yourself, and it is not needed for general applications.
As for the example, you can take a look at the following link:
/view/32596.htm
In addition, you should know that only * * * memory is not mutually exclusive, and you must use it together with semaphores to prevent mutual exclusion. If you * * * only enjoy a plastic variable, it may not be a big problem, because reading and writing plastic variables aligned with the page are atomic operations, but if you * * * enjoy a complex structure, you have to be careful.