260 likes | 438 Views
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.
E N D
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 • 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?
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
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
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?
SysV shm • shm is system-wide • How do you identify which segment is yours? • “Key” – shared magic number unique to your program
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!
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
SysV shm • int shmdt(void *shmaddr) • shmaddr – address of attached shm segment • shm segments are automatically detached on process exit
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!
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
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)
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
Control facilities • Semaphores • semget() • semop() • semctl() • Semaphore with a start value of 1 acts like a mutex
Control facilities • Message queues • msgget() • msgsnd() • msgrcv() • msgctl() • Messages have a “type” and “content” • Content may be 0 length
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?