How good is the circular memory buffer on Wikipedia?

I am trying to implement a circular buffer in C and have come across this example on Wikipedia (filmed in July 2014). This seems to provide a really nice interface for those reading from the buffer, as reading that flows from the end to the beginning of the buffer is processed automatically. Thus, all readings are contiguous.

However, I’m a little unsure that I will use it right away, since I don’t have much experience comparing memory or virtual memory, and I’m not sure that I fully understand what it does.

In my opinion, I understand that it compares the shared memory file twice with the size of the buffer in memory. Then, whenever data is written to the buffer, it appears in memory in two places at the same time. This allows all readings to be contiguous.

What would be great if someone who has more experience comparing POSIX memory could take a quick look at the code and tell if the mechanism used is really effective. Do I think correctly, for example, that the file in / dev / shm used for shared memory always remains in RAM or can be written to the hard disk (performance hit) at some point? Are there any errors that I should know about?

Be that as it may, I'm probably going to use a simpler method for my current project, but it would be nice to understand this so that it is in my toolbox for the future.

Thanks in advance for your time.

+3
source share
1 answer

I think that at first anonymous mmapis only performed to select some area of ​​the address in unused memory to store both mappings.
"/ dev / shm" is usually mounted with the tmpfs file system, which stores all the data in swap / memory. So in fact, this can lead to writing to the hard drive, but you have the same chance with your memory malloc.

"/etc/fstab" , "glibc 2.2 , tmpfs /dev/shm POSIX (shm_open, shm_unlink)". . , swap, .

man mkstemp - glibc 2.06 0666, , - mkstemp unlink.

+4

Source: https://habr.com/ru/post/1747342/


All Articles