SlideServe
Menu
Préférences
© SlideServe · v2026

Shared Memory Overview

jafari avatar
19 diapositives · 494 Views · Sep 08, 2014

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.

Shared Memory Overview - slide 1
01 / 19Shared Memory Overview
1 / 17

Télécharger la présentation

Shared Memory Overview
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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?