L10: Shared Memory Flashcards
1
Q
Shared Memory
Overview
A
- Allows 2 or more processes to share a given region of memory
- Fastest form of IPC because there is no copying between a “client” and “server”
- The trick is synchronizing access
- Semaphores are used to synchronize shared memory access
2
Q
Shared Memory:
Important Functions (4)
A
- shmat()
- how a process attaches to shared memory
- shmdt()
- process detaches from shared memory
- shmctl()
- attempt a command on the shared memory
- mmap()
- memory mapped input/output
- alternative to shared memory
3
Q
Shared Memory Functions:
shmat()
- Full declaration
- Summary
A
void * shmat( int shmid, void *addr, int flag);
Used by a process to attach to shared memory
Returns a pointer to the shared memory
Leaves memory when the process dies. Potential for memory leak!
4
Q
Shared Memory Functions:
shmctl()
- Full Declaration
- Summary
A
int shmctl( int shmid, int cmd,
struct shmid_ds *buf );
- Performs various operations on shared memory
- A command is specified with “cmd”
- Returns 0 if ok, -1 on error
5
Q
Shared Memory:
Commond commands for
shmctl()
A
- IPC_STAT
- fills the “buf” data structure
- IPC_SET
- change uid, gid and mode fo the shmid
- IPC_RMID
- sets up the shared memory segment to be removed once all processes terminate or dettach from the segment
6
Q
Shared Memory Functions:
shmdt()
- Full Declaration
- Summary
A
void shmdt( void *addr)
- Used by a process to detach itself from a shared memory segment
- Sort of similar to using free() for heap memory
7
Q
Shared Memory Functions:
mmap()
A
- Memory Mapped Input/Output
- Alternative to shared memory
- Maps a file on DISK into buffer
- Contents are non-volatile
8
Q
Unix Commands
Dealing with
Shared Memory Segments
A
- ipcs
- used for listing shared memory segments
- ipcrm
- used to remove shared memory segments
9
Q
Shared Memory:
Steps for
Creating Shared Memory
A
- Use shmget()
- to obtain a segment of memory with an ID
- Use shmat()
- to get a pointer to the memory segment
- Use shmctl()
- to set up segment to be removed when processes are done with it
- Use the pointer as a pointer to a memory block
10
Q
A