c - What is the difference between writing to a file and a mapped memory? -


I have the following questions related to handling and mapping files ( mmap ):

  1. We know that if we make a file, and write that file, then we are writing memory then using the code Why map the map and
  2. If this is due to security that we mmap - PROT_NONE , PROT_READ , PROT_WRITE , then The same level of protection can be obtained by using the files O_RDONLY , O_RDWR etc. Then why mmap ?
  3. Is there any special advantage that we get to mapping files in memory, and then using it? Instead of just creating a file and writing it?
  4. Let's assume that we consider a file in the mmap memory, if we return that storage location by MMAP, then does it even simultaneously < / Ol>

    Please help me answer all the questions.

    Thank you very much earlier.

    * Edit: Sharing Files Between Thread *

    As far as I know, if we are between two threads (not process) If you share a file, then use it instead of using mmap in the archive and then instead of using the file directly.

    But we know that a file means, it's definitely in the main memory, then why does the thread need MMMAP?

    The disk access is slow, so when you type in a file, it will be switched to disk and will no longer be in RAM, which means that the next time you need the file, then You are probably going to get it from disk (slow), while in memory mapped files, you know that the file is in RAM and you can access it when the disk is on.

    In addition, memory-mapped files are often used as IPC mechanisms, so two or more processes can easily share the same file and read / write it (Using the required sycnh system)

    When you need to read the file frequently, and this file is very large, it can be useful to map it to memory so that you have it fast Used to Open it and get it from disk every time

    Edit:

    It depends on your needs , When you have a file through which multiple threads will need to be entered multiple times, I'm not sure that file meming mamming will be a good idea, from this view, you will need access to this MMMAP file if you If you want to write it, different If there are so many places with thread, then this resource may be a place of controversy.

    Just reading from the file, it can be a good solution, because you do not really need to log in, then read only from multiple threads. When you start to write, you can use a strong mechanism to .

    I suggest, if you have access to each thread in your thread locally, if you have to write the file, like you do with any other file like this This thread reduces the need for synchronization and it is difficult to find and debug the possibility of a bug.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -