Simple Shared Memory in C (mmap)
Вставка
- Опубліковано 2 гру 2024
- Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.th...
Website ➤ www.jacobsorbe...
---
Simple Shared Memory in C (mmap) // How do you share memory between two computer programs? This question has baffled many programmers over the years, but it's not that hard, and this video shows you one way to set it up using mmap.
Related Videos:
How processes get memory
• How processes get more...
Memory Mapped File IO (mmap)
• How to Map Files into ...
Cloning Processes (fork)
• Creating new processes...
***
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:
people.cs.clem...
persist.cs.clem...
***
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:
www.jacobsorbe...
people.cs.clem...
persist.cs.clem...
To Support the Channel:
like, subscribe, spread the word
contribute via Patreon --- [ / jacobsorber ]
rep the channel with nerdy merch --- [teespring.com/...]
Source code is also available to Patreon supporters. --- [jsorber-youtub...]
Want me to review your code?
Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
You can also find more info about code reviews here.
• I want to review your ...
2 minutes for what I'd otherwise have to a watch a 2 hour lecture. Lmao thanks
dont know if anyone gives a damn but if you're bored like me during the covid times you can stream pretty much all of the latest series on InstaFlixxer. I've been watching with my girlfriend during the lockdown :)
@Crosby Tristen Definitely, been watching on instaflixxer for since november myself :)
this trick saved me during one assignment, thanks
Me : This video is only 2 minutes. It can't help me
Also me after 2 minutes: OMG I GET EVERYTHING NOW
Man I always love watching these videos, they're great! Any plans on delving into the Linux or Unix kernels? I realise they're obscenely massive, but it might be nice to have a look into perhaps how a small portion of the kernel works (maybe elaborate on Linux device drivers or something, it can be a bit overwhelming).
I'm glad you like them. As for the kernel question. Sure. Maybe. Time is always my biggest constraint. So, I tend to make videos about stuff that I'm already playing with while working on other projects. The kernel is a bigger investment with a higher startup cost. But, I really like hacking around in the kernel. So, I'll see what I can do. Probably not in the next few weeks, though.
I just wanted you to know that this
"Programming with Processes" series
Covered a HUGE knowledge gap in such a short amount of time
I'm extremely grateful!
Also question: is series complete? does it cover about IPC, Processes, Threads extensively, or is there still some topic to be covered.
Please let me know, thanks.
Hi, what is the difference in using the shared memory of unix and mmap if there is any ?
That can be very useful! 😊
Can you share memory with a process started via exec()?
I can't get this to play, it always results in "An error occurred. Please try again later", been trying for 2 days - linux firefox 110.0
Is there a way to have a shared memory between processes that aren't parent/child and yet have it persisted on disk (i.e across reboots)? shm_open shared memories created in /dev/shm get wiped out on reboot.
You thinking about making videos about File Systems ? I am sure a lot of people that watch your videos are the students that are taking Operating System course. File System is one of major topics in OS course.
It's definitely on my list of possible future topics. File systems is a pretty big space. Are there specific things you would like to hear about?
Love the video
Do we need mutexes or semaphores when editing shared memory?
Often, yes. It depends on what your code is doing, but most programs that use shared memory need some sort of synchronization..
@@JacobSorber OK thx, very good video btw 👍
Hey Jacob, thanks for these videos. I had a noob question about this shared memory. How is this different from just allocating a normal block of memory with malloc before the fork? All pointers should have the same values and so can access the same block of memory. What is the advantage to using mmap over this? Is it simply that you don't have to use pointers? Thanks for your time.
Modern hardware and operating systems support virtual vs physical addresses. The same virtual address value in two different processes does not mean they map to the same physical memory address.
Just to add on to the other comment, the address might appear to be the same, but upon writing to the memory block in the child process you will notice that it is unchanged in the parent process (if the memory was malloc’ed, as you said).
I get a MAP_ANONYMOUS not defined error, how do I fix this?
very helpful 🙂
What does mean MAP_ANONYMOUS ?
How do ArtMoney program get access to another process memory in the OS?
Probably using something like the ptrace API.
Is this shared memory available to other processes? Example one C program and one PHP program.
I'm looking at creating something that writes to something other than a file and then is picked up by PHP (or another C program) for use in an API. This will happen once a second so don't want to write to a file and the device has eMMC so definitely don't want to write to that file.
Once a second is nothing, there is no reason not to write to file. Another solution would be to send it via a socket if you really need to avoid the file. But again your computer can do billions of operations a second, something done every second is nothing.
It would be helpful for my operating systems project if it weren't explicitly prohibited by the professors
error: ‘MAP_ANONYMOUS’ undeclared (first use in this function)
A have this problem.
Can you tell me how to fix it?
I try
gcc -std=c99
gcc -std=gnu99 ..
thenks..
Need more info about your machine and how you're trying to build it.
@@JacobSorber
Linux nata-MS-7721 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
#define _XOPEN_SOURCE 700
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PAGESIZE (1023)
#define osAssers(cond,msg) osErrorFatal(cond,msg,__FILE__,__LINE__);
void osErrorFatal(bool cond,const char * msg,const char * file,int line){
if(!cond){
perror(msg);
fprintf(stderr, "%s:%d
",file,line );
exit(EXIT_FAILURE);
}
}
int main(int argc, char const *argv[])
{
int v=5;
uint8_t * shared_memory = mmap(NULL ,PAGESIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS ,-1,0);
osAssers(MAP_FAILED!=shared_memory,"mmap failed");
shared_memory[1]=34;
pid_t ChildPid=fork();
osAssers(ChildPid!=-1,"fork failed");
if(ChildPid==0){
v=10;
shared_memory[1]=15;
}
else{
wait(NULL);
}
printf("Not shered: %d
",v );
printf("Shared memory: %i
",shared_memory[1] );
return 0;
}
gcc -std=c99 -o mmap mmap.c
@@TheNata96 Try adding "#define _GNU_SOURCE" at the top.
Thank you!!!
@@JacobSorber
@@JacobSorber Comment helped me even now)
THATS CRAZY USEFULL
I know, right?!?
@@JacobSorber yes but what about the speed? is mmap faster than the pipes or another interprocessing techniques? İs that the best way to do that? What do you think?
very helpful :)
Thanks. Glad you enjoyed it.
Can u make a video about dlls plz.
For a specific OS or just in general? A general one or one for Linux/Unix/MacOS would be easy. If you want a windows one, you might have to wait a bit longer. I'm not doing much with Windows, these days.
@@JacobSorber i use windows. but i have no probleme if u explain it on any platform and thx
Ok. Sounds good. I'll see what I can do.
Carefull with synchronization and race conditions tho
Yep.
Y so fast buddy.
Mmappe
Dawg slow down a little, you skip past the results too fast.