Today we continue to study POSIX IPC objects, this time by learning how to use POSIX shared memory, starting with:
The following program is written to create a shared memory:
Compile run:
Where is POSIX's shared memory stored? The POSIX message queue of the upper secondary school is to create a message queue in the virtual file, which requires us to manually mount it to a directory to see, and the same,POSIX shared memory needs to be mounted , but this mount operation is done by the system , without our manual to operate, has been mounted to the/DEV/SHM under the following:
The next function to describe is to modify the size of the shared memory:
description: Actually the Ftruncate function can also modify the size of the file.
Modify the program below to use it:
Compile run:
where struct stat is structured as:
Modify the program as follows:
Compile run:
This is a relatively simple experiment:
Compile run:
Now that we have created a shared memory object, how do we use it? You need to use the following function:
To use the following, after the mapping succeeds, the data is written to memory and then read from memory:
Next, do a fault-tolerant processing:
In fact, the mapping failure has a special macro definition, which can be learned from the man help:
So, instead of the-1 program will be more readable:
Compile run:
What is this for? Or from the man help to find the answer:
So the cause of the problem is found, then modify the open mode to either:
Compile and run again:
There is no successful write, you need to write a read program to verify:
Compile run:
You can actually use the shell command to view the contents of the shared memory directly:
OK, this time to learn something relatively simple, next time to continue ~
POSIX shared memory for Linux network programming