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 ...

КОМЕНТАРІ • 51

  • @44r0n-9
    @44r0n-9 4 роки тому +43

    2 minutes for what I'd otherwise have to a watch a 2 hour lecture. Lmao thanks

    • @crosbytristen1153
      @crosbytristen1153 3 роки тому

      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 :)

    • @traviszahir2699
      @traviszahir2699 3 роки тому

      @Crosby Tristen Definitely, been watching on instaflixxer for since november myself :)

  • @giveaway4002
    @giveaway4002 Рік тому +1

    this trick saved me during one assignment, thanks

  • @RB-gi3zq
    @RB-gi3zq 4 роки тому +8

    Me : This video is only 2 minutes. It can't help me
    Also me after 2 minutes: OMG I GET EVERYTHING NOW

  • @arby8722
    @arby8722 6 років тому +17

    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).

    • @JacobSorber
      @JacobSorber  6 років тому +12

      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.

  • @ade5324
    @ade5324 2 місяці тому

    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.

  • @kaus05
    @kaus05 2 місяці тому

    Hi, what is the difference in using the shared memory of unix and mmap if there is any ?

  • @alexanderzikal7244
    @alexanderzikal7244 3 місяці тому

    That can be very useful! 😊

  • @DavidAlsh
    @DavidAlsh Рік тому

    Can you share memory with a process started via exec()?

  • @erbenton07
    @erbenton07 Рік тому

    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

  • @yashmanwani6232
    @yashmanwani6232 3 роки тому

    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.

  • @dakshays6375
    @dakshays6375 5 років тому +2

    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.

    • @JacobSorber
      @JacobSorber  5 років тому

      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?

  • @katecancer5200
    @katecancer5200 Рік тому

    Love the video

  • @leokiller123able
    @leokiller123able 3 роки тому +1

    Do we need mutexes or semaphores when editing shared memory?

    • @JacobSorber
      @JacobSorber  3 роки тому +3

      Often, yes. It depends on what your code is doing, but most programs that use shared memory need some sort of synchronization..

    • @leokiller123able
      @leokiller123able 3 роки тому

      @@JacobSorber OK thx, very good video btw 👍

  • @brooksburris8341
    @brooksburris8341 3 роки тому +7

    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.

    • @sverkeren
      @sverkeren 2 роки тому +5

      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.

    • @veggieboy1
      @veggieboy1 2 роки тому +2

      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).

  • @adrenochromeaddict4232
    @adrenochromeaddict4232 Рік тому

    I get a MAP_ANONYMOUS not defined error, how do I fix this?

  • @nutankumari6343
    @nutankumari6343 Рік тому

    very helpful 🙂

  • @rafalmichalski4893
    @rafalmichalski4893 4 роки тому +1

    What does mean MAP_ANONYMOUS ?

  • @aabdev
    @aabdev 3 роки тому

    How do ArtMoney program get access to another process memory in the OS?

    • @JacobSorber
      @JacobSorber  3 роки тому +1

      Probably using something like the ptrace API.

  • @chrisschneider7609
    @chrisschneider7609 3 роки тому

    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.

    • @d.sherman8563
      @d.sherman8563 Рік тому

      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.

  • @gr3ybrun1x66
    @gr3ybrun1x66 2 роки тому +1

    It would be helpful for my operating systems project if it weren't explicitly prohibited by the professors

  • @TheNata96
    @TheNata96 5 років тому

    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..

    • @JacobSorber
      @JacobSorber  5 років тому

      Need more info about your machine and how you're trying to build it.

    • @TheNata96
      @TheNata96 5 років тому

      @@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

    • @JacobSorber
      @JacobSorber  5 років тому +1

      @@TheNata96 Try adding "#define _GNU_SOURCE" at the top.

    • @TheNata96
      @TheNata96 5 років тому

      Thank you!!!
      @@JacobSorber

    • @MykhailoChynkov
      @MykhailoChynkov Рік тому

      @@JacobSorber Comment helped me even now)

  • @metehangul1164
    @metehangul1164 4 роки тому +1

    THATS CRAZY USEFULL

    • @JacobSorber
      @JacobSorber  4 роки тому +1

      I know, right?!?

    • @metehangul1164
      @metehangul1164 4 роки тому

      @@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?

  • @1nxpired
    @1nxpired 6 років тому +1

    very helpful :)

    • @JacobSorber
      @JacobSorber  6 років тому +1

      Thanks. Glad you enjoyed it.

  • @rouinihosni7108
    @rouinihosni7108 6 років тому +1

    Can u make a video about dlls plz.

    • @JacobSorber
      @JacobSorber  6 років тому +2

      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.

    • @rouinihosni7108
      @rouinihosni7108 6 років тому

      @@JacobSorber i use windows. but i have no probleme if u explain it on any platform and thx

    • @JacobSorber
      @JacobSorber  6 років тому +2

      Ok. Sounds good. I'll see what I can do.

  • @fallout__boy1130
    @fallout__boy1130 Рік тому

    Carefull with synchronization and race conditions tho

  • @NalamalaSaiTeja
    @NalamalaSaiTeja 4 роки тому

    Y so fast buddy.

  • @69k_gold
    @69k_gold Рік тому

    Mmappe

  • @livequality4578
    @livequality4578 3 місяці тому

    Dawg slow down a little, you skip past the results too fast.