1 / 19

Shared Memory Overview

Shared Memory Overview. What is shared memory?. IPC construct Used to communicate information between processes Pros Very fast: accessed just like regular memory Cons Processes must be on same machine Programmer must define semantics for communication. shm semantics.

jafari
Download Presentation

Shared Memory Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shared Memory Overview

  2. What is shared memory? • IPC construct • Used to communicate information between processes • Pros • Very fast: accessed just like regular memory • Cons • Processes must be on same machine • Programmer must define semantics for communication

  3. shm semantics • shm gives you a block of memory; you have to decide how to use it • One user at a time, or split into multiple regions? • What do its contents represent? • How do you know how much data a process wrote? • How do you know when the data is there? • How do you know when it’s safe to write to?

  4. shm semantics • Often paired with other IPC mechanisms • Mutexes/semaphores • Signals/conditions/messages • Use these to implement control • Limit number of simultaneous writers/readers • Indicate that data is ready for consumption

  5. SysV shm • shmget() – Create a shm segment • shmat() – Attach to a shm segment • shmdt() – Detach from shm segment • shmctl() – shm controls (e.g. delete) • ftok() – Generate key to identify shm segments

  6. SysV shm • intshmget(key_tkey, size_tsize, intshmflg) • size – duh • shmflg • IPC_CREATE – Create a segment • IPC_EXCL – Fail if segment already exists • mode • Lower 9 bits • Read/write/execute mask for segment • Execute ignored • returns: shmid • key?

  7. SysV shm • shm is system-wide • How do you identify which segment is yours? • “Key” – shared magic number unique to your program

  8. SysV shm • key_tftok(char *pathname, intproj_id) • Creates a hash on pathname and proj_id to provide a unique key • pathname – path to a file • Create a special file (e.g. “.shmfile”) • Or point to your executable • proj_id – Programmer-provided uniqueness • pathname and proj_id must be the same on both sides to get the same key!

  9. SysV shm • void *shmat(int shmid, void *shmaddr, int shmflg) • shmid – Returned from shmget() • shmaddr – Address you’d prefer it to be mapped at • can be NULL – system will choose location for you • shmflg – Typically 0; see man page • Returns pointer to shm segment

  10. SysV shm • int shmdt(void *shmaddr) • shmaddr – address of attached shm segment • shm segments are automatically detached on process exit

  11. BIG FAT WARNING • shm is system-wide • Segments could be idle and then attached to later • How does the kernel know when you’re done with a segment? • IT DOESN’T!

  12. SysV shm • int shmctl(int shmid, int cmd, struct shmid_ds *buf) • Valid values for cmd • IPC_STAT – Copy info into buf • IPC_SET – Set info from buf • IPC_RMID – Mark segment for deletion • May do this before everyone has detached

  13. BOTTOM LINE • YOU are responsible for cleaning up shm • Unlike malloc()ed memory, shm will not disappear on program exit • It stays around FOREVER (until system reboot)

  14. Command-line tools • ipcs – List IPC facilities created • ipcs –m – shm segments • ipcrm – Delete IPC facilities • ipcrm –m shmid – Delete shm segment with given id

  15. Control facilities • Semaphores • semget() • semop() • semctl() • Semaphore with a start value of 1 acts like a mutex

  16. Control facilities • Message queues • msgget() • msgsnd() • msgrcv() • msgctl() • Messages have a “type” and “content” • Content may be 0 length

  17. How will you use your shm? • Multiple processes want to read and write • Create several shm segments? • Create 1 shm segment and subdivide? • What’s the data format? How will the writer indicate how much data he wrote? • What if you have to write more data than fits in the segment? • What semantics will you use for • locking? • signaling when data is written? • signaling when data has been read and can be discarded?

  18. Questions?

More Related