How to Map Files into Memory in C (mmap, memory mapped file io)

preview_player
Показать описание
---

How to Map Files into Memory in C (mmap, memory mapped file io) // How to Map Files into Memory in C (mmap) // Want to read an entire file into an array in memory all at once, in C? Memory-mapped file I/O is one of those super useful tools that so many programmers don't know about. Let's fix that.

My recent, related videos:

Basic File IO in C

Get the size of files.

Easier working with file paths, with realpath

Using mmap to request more memory

Note that any Amazon links in my video descriptions are generated by Amazon. If you click one of them and then buy something it helps support this channel. Thanks.



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Want me to review your code?

You can also find more info about code reviews here.
Рекомендации по теме
Комментарии
Автор

The background music makes it sound like we're solving a murder case. Great video by the way

beegbraining
Автор

The background music caught me off guard at first. After you executed the code, I said wtf like ten times and the music really made me laugh.

Jonathondelemos
Автор

Thank you Jacob! However from the man page:
`if neither O_CREAT nor O_TMPFILE is specified, then mode is ignored.`
You don't have to use S_IRUSR and S_IWUSR as arguments. They should be ignored. Wanted to point out in case. Appreciate your great work!

djpolyester
Автор

Thank god someone finally made a video on mmap that made sense!
Thank you much <3

LinucNerd
Автор

This man is a boss! Your video has some excellent stuffs. Please keep sharing :)

topG
Автор

NO ONE explains like you do. Thank You!

kotravaijm
Автор

NIce quick to the point videos. Lectures and textbooks often go too in-depth and confuse more than is. Your videos give brief insight, and then shows an actual functioning demostration.

Автор

I tried to understand this whole day. You made this in few minutes :)

rafabaranowski
Автор

For my OS course one of our assignments is tbe classic consumer producer problem but with processes. One of the portions for this assignment is being able to write to a file from one of our consumer processes but I was having the hardest time understanding how to share a file in memory between processes. This definitely cleared it up! Thanks :D

officialsterlingarcher
Автор

Wow, this is amazing. My mouth was gaping when I saw this.

islandcave
Автор

ayyyy this is awesome very very good! you made mmap very easy to understand. craving for more !

tigerrabbit
Автор

This is exactly what I need to know for a project I'm working on. Thank you!

zrodger
Автор

Whoever reads this, spread Jacob Sorbers Channel as it has been done with Cherno. The quality is good, the insight is insane! Have a nice day

SolusPrimus
Автор

amazing video . simplicity and clearity. hatsoff

deepshankarjha
Автор

Put everything anyone background music in this is so intense.... actually I loved

MrUmang
Автор

Hey, great video ! I have one question though, how do you append characters at the end of the file this way ? Do we need a reallocation or something ?
EDIT: I found a way: First I tried allocating more space with mmap but surprisingly it doesn't append character that I `strcat` at the end, so after a little bit of digging I found that if you edit files with `mmap` the size of the file is not automatically updated like with `write`, so we need to call `truncate` (or `ftruncate`) to manually change the size

noctuss
Автор

you saved my life,
and i'm not overreacting !
Thank you !!!

thepickicool
Автор

Hi.
Is there any difference to memory acces time between MMAPed file, and just using MMAP to allocate memory?
What if I want a fast way of loading stuff, but then I also need a high performance runtime?

Artur-Matkowski
Автор

So freaking cool, thank you for this !

strayedaway
Автор

I am on Linux and can not access more than the 4 gig limit. I have 64 bit and 32 gig ram and when I do the mmap it appears to work (8 gig), but when I try to access past the 4 gig, it gives sig memory err. Now I am using gnuCOBOL and have the table defined in Linkage and get the pointer set all is well. It works for 4gig or less.
Is there something in my Linux system that I need to change as in the config ? (I use Ubuntu and PopOs) on two diff machines.... I should be able to mmap and access 90 gigs (should be no limit in linkage section. ?

justwanderin
visit shbcf.ru