Unix/Linux Go Back    


Filesystems, Disks and Memory Discuss NAS, SAN, RAID, Robotic Libraries, backup devices, RAM, DRAM, SCSI, IDE, EIDE topics here.

How does memory mapping work?

Filesystems, Disks and Memory


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-26-2011   -   Original Discussion by theKbStockpiler
theKbStockpiler's Unix or Linux Image
theKbStockpiler theKbStockpiler is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 16 August 2014, 10:41 PM EDT
Posts: 56
Thanks: 30
Thanked 1 Time in 1 Post
How does memory mapping work?

I can't find a guide or tutorial that explains it at all. Its there a better search term than Memory Mapping? What's the magic inbetween having the hard drive behave like the memory?
Sponsored Links
    #2  
Old Unix and Linux 04-26-2011   -   Original Discussion by theKbStockpiler
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
The best 'guide' is man mmap and some experimentation.

The "magic" is virtual memory. If you don't understand that yet, you'll need to. the paging game is a pretty good starter, I think.

How it works, hardware-wise, is the CPU keeps a big table of what addresses are assigned to what processes, and what real memory -- if any -- is assigned to that address. The OS can configure it on the fly. A page can be marked as 'unavailable', meaning, potentially valid but not yet read in or assigned actual memory. If a process accesses it, the CPU signals the OS which freezes that process. It checks and finds that the page isn't marked as 'ready' in the table, and that by its own (separate) records, ought to be a chunk of bigfile.txt. The OS finds an empty page, reads the right chunk into it, marks it belonging to the process, and wakes it up, which tries to read again and this time succeeds with no delay.

IOW, the CPU can mark memory in such a way as to freeze a process when it tries to use it. The OS is told when this happens, so it can do something to the memory, then revive it when ready, making a convincing illusion of the file existing in contiguous memory.

Last edited by Corona688; 04-26-2011 at 01:46 AM..
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
theKbStockpiler (04-26-2011), usha rao (04-26-2011)
Sponsored Links
    #3  
Old Unix and Linux 04-26-2011   -   Original Discussion by theKbStockpiler
theKbStockpiler's Unix or Linux Image
theKbStockpiler theKbStockpiler is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 16 August 2014, 10:41 PM EDT
Posts: 56
Thanks: 30
Thanked 1 Time in 1 Post
Can anyone expand on the info contained in this link?

File Mapping - ????, ????! - CSDN??Linux


Or this link?http://www.ifm-services.com/people/j...orymapping.php

Last edited by theKbStockpiler; 04-26-2011 at 02:37 AM..
    #4  
Old Unix and Linux 04-26-2011   -   Original Discussion by theKbStockpiler
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
One of those refuses to load, and the other is in some odd computing language I've never seen before.

How about this?

To start you off:


Code:
char *mem;
int fd=open("filename", O_RDWR|O_CREAT, 0666); // Create the file
ftruncate(fd, getpagesize()); // Extend it to the length of the page size, probably 4K

// Map the file(MAP_SHARED) into memory with read and write permissions, at any available address(i.e. NULL)
mem=mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0L);
if(mem == MAP_FAILED) // Failure doesnot mean null!
{
        perror("Couldn't mmap");
        return(1);
}

// Fill file with Z's
memset(mem, 'Z', getpagesize());

The Following User Says Thank You to Corona688 For This Useful Post:
theKbStockpiler (04-26-2011)
Sponsored Links
    #5  
Old Unix and Linux 04-27-2011   -   Original Discussion by theKbStockpiler
theKbStockpiler's Unix or Linux Image
theKbStockpiler theKbStockpiler is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 16 August 2014, 10:41 PM EDT
Posts: 56
Thanks: 30
Thanked 1 Time in 1 Post
Some links I believe that should be shared.

Thank you Corona688 for the replies!Linux

Memory Mapped Files | Vladislav Online

Loading…

Memory Mapped Files Abdelrahman Al-Ogail's Blog

UNIX Tips - Memory Management
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bit Mapping readycpbala Shell Programming and Scripting 5 10-24-2010 09:08 AM
LUN mapping user23 UNIX for Dummies Questions & Answers 1 04-18-2008 12:28 PM
lvm_queryvg call does not work properly and results in a sudden memory rise. sandiworld AIX 0 11-15-2007 01:57 AM
C code for implementation of Memory Mapping raviviolet13 Programming 1 09-13-2007 02:56 AM
Script doesn't work, but commands inside work cheongww UNIX for Dummies Questions & Answers 2 11-14-2006 10:52 PM



All times are GMT -4. The time now is 09:05 AM.