Lecture 18 ● ● ● Log into Linux. Copy directory /home/hwang/cs375/lecture19/ Reminder: Project 5 due on Tuesday. Questions? Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 1 Outline ● System V Shared Memory ● Shared Memory via mmap( ) ● POSIX Shared Memory Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 2 System V Shared Memory ● ● ● The System V shared memory facility allows unrelated processes to access the same segment of memory. It is the fastest means of transferring data between two processes. The shared memory routines map the physical address space of the shared memory segment into the logical address space of each process. As noted in last class, the shared memory routines are similar in format to the semaphore routines. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 3 System V Shared Memory ● The shmget( ) routine is used to create a shared memory segment: shmget(key_t key, size_t size, int flags); ● An id is returned that is used in other calls: // Create named shm block id = shmget(123, 2048, IPC_CREAT|0660); // Access existing block by name id = shmget(123, 0, 0); // Create unnamed shm block id = shmget(IPC_PRIVATE, 2048, 0660); Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 4 System V Shared Memory ● The shmat( ) routine maps the shared segment to the process address space: void *shmat(id, addr, flags); ● The addr argument allows us to specify a local address at which the shared memory can be accessed, it should normally be NULL to allow the system to choose an address. flags is usually 0. shmat( ) returns a pointer to the first byte of shared memory. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 5 System V Shared Memory ● The shmdt( ) routine detaches the shared memory segment from the current process. Detaching the segment does not delete the segment or clear the segment contents. The segment can be reattached later: // Attach shared memory segment shmaddr = shmat(shmid, (void *)0, 0); // Detach shared memory segment ret = shmdt(shmaddr); Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 6 System V Shared Memory ● The shmctl( ) routine is used to change permissions on a shared memory segment, get info about a segment and delete a segment: // Remove the segment from the system ret = shmctl(shmid, IPC_RMID, 0); ● The segment may not be deleted until sometime later, after all processes using the segment have detached it. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 7 System V Shared Memory ● ● As noted last time, the ipcs command can be used to display information (key, id, owner, permissions, size, and number of attaching processes) about System V shared memory segments. System V shared memory segments have kernel persistence. The server program should take responsibility for deleting them. They can be deleted from the command line via the ipcrm command, if necessary. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 8 In-class Exercise ● ● The program in shm_srvr.cpp demonstrates a server program that creates, attaches, detaches, and removes a System V shared memory segment. It displays the shared memory id so that it can be used as an argument when starting the client program Complete the unfinished client program in shm_clnt.cpp, then run it with the id given by the server. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 9 Shared Memory Via mmap( ) ● ● The mmap( ) routine has traditionally been used to map a file into memory. It can be used by a single process for fast file I/O. mmap( ) also can be used to share memory between processes. It typically is preferred over System V shared memory in modern operating systems. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 10 Shared Memory Via mmap( ) ● ● mmap( ) supports two types of mappings: file mappings and anonymous mappings. A file mapping maps a file (or a portion of a file) into memory. The file must first be opened and the file descriptor passed to mmap( ). Other (related) processes that inherit the file descriptor can use mmap( ) to map to the same memory segment. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 11 Shared Memory Via mmap( ) ● ● An anonymous mapping is not associated with a file. Anonymous mappings are preserved across a fork( ) (as are file mappings), but (of course) not across an exec( ) call. Anonymous mappings are similar to System V shared memory segments, but they can not be used by unrelated processes. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 12 POSIX Shared Memory ● ● POSIX defines memory objects that can be mapped into a processes' memory space by mmap( ). Objects are created with shm_open( ): int shm_open(const char *name, int oflag, mode_t mode); ● name is the name of the object. It should have a leading '/'. shm_open( ) returns a file descriptor. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 13 POSIX Shared Memory ● ● ● When a POSIX shared memory object is first created (oflag contains O_CREAT), the file descriptor returned from shm_open( ) should be first passed to ftruncate( ) to set the memory size and then to mmap( ). The file descriptor of an existing memory segment may be passed directly to mmap( ). POSIX memory objects can be shared by unrelated processes Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 14 POSIX Shared Memory ● ● An mmap( ) memory segment can be unmapped using munmap( ). A POSIX shared memory object should be deleted using shm_unlink( ). The segments will “live” until the next kernel reboot if they are not deleted. As noted last time, they can be deleted from the command line using “rm”. They have corresponding file names in the /dev/shm directory. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 15 In-Class Exercise ● The programs in the posix subdirectory are versions of the example programs modified to use POSIX shared memory. Complete the unfinished client program. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 16 Shared Memory Synchronization ● ● ● When using shared memory segments you need to take precautions to synchronize access to the segment. The example programs demonstrate using a shared boolean variable for exclusive access. This works only for two processes sharing a segment. Other methods (e.g., semaphores) are more efficient and must be used when more than two processes are involved. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 17 In-class Exercise ● ● ● Modify the example programs to use a pair of semaphores for synchronization, instead of polling the written_by_you flag. (System V semaphores and POSIX semaphores, respectively.) The semaphores are initialized to 1 and 0. The producer acquires the first before writing data and releases the second when it is done, while the consumer does the opposite. Try running multiple clients. Thursday, October 29 CS 375 UNIX System Programming - Lecture 18 18