Unformatted text preview:

Shared Memory CSCI 8530 Advanced Operating Systems Shared Memory shm open name oflag mode name is a string identifying an existing shared memory object or a new one to be created It should begin with and contain only one slash In QNX 6 these objects will appear in a special directory oflag is similar to the flags for files O RDONLY read only O RDWR read write O CREAT create a new object if necessary O EXCL fail if O CREAT and object exists O TRUNC truncate to zero length if opened R W mode is the protection mode e g 0644 shm open returns a file descriptor or 1 in case of error mmap void addr size t len int prot int flags int fd off t off mmap is used to map a region of the shared memory object fd to the process address space The mapped region has the given len starting at the specified offset off Normally addr is 0 and allows the OS to decide where to map the region This can be explicitly specified if necessary more on next slide Sharing memory in POSIX and many other systems requires creating a persistent object associated with the shared memory and allowing processes to connect to the object Creating or connecting to the persistent object is done in a manner similar to that for a file but uses the shm open system call ftruncate int fd off t len This function inappropriately named causes the file referenced by fd to have the size specified by len If the file was previously longer than len bytes the excess is discarded If the file was previously shorter than len bytes it is extended by bytes containing zero mmap continued prot selected from the available protection settings PROT EXEC PROT NOCACHE PROT NONE PROT READ PROT WRITE flags one or more of the following interpret addr parameter exactly don t share changes to object MAP SHARED share changes to object MAP FIXED MAX PRIVATE mmap returns the mapped address or 1 on error 1 munmap void addr size t len This function removes mappings from the specified address range This is not a frequently used function as most processes will map a fixed sized region and use shm unlink at the end of execution to destroy the shared memory object which effectively removes the mappings Implementation 1 Shared memory is usually implemented as just described to take advantage of the sharing already present in the file system When multiple processes have the same file open only one copy of each block is allowed to be present in the buffer cache at any instant resulting in a shared memory area among the processes with the file open shm unlink char name This function much like a regular unlink system call removes a reference to the shared memory object If the are other outstanding links to the object the object itself continues to exist If the current link is the last link then the object is deleted as a result of this call File system Sharing Process A Note that each unique open of a file gets its own file descriptor Dup causes file descriptor sharing All instances of the same open file share the same in memory inode fd1 open a file O RDWR fd2 open a file O RDWR fd3 dup fd1 Process B fdx open a file O RDWR Process A File Descriptors In memory inode for a file fd1 Process B File Descriptor fdx fd2 fd3 Implementation 2 The next step in shared memory implementation is to map the block s in the buffer cache into the address spaces of the processes wanting to share the memory This is done by first requiring that the mapping be done in units that are equal to the size of a page This also has a direct effect on the the size of a logical disk block Even though the mapping is done in page sized units processes may map units in odd sizes but this may result in part of the virtual address space the difference between the odd size and the page size not being usable for any other purposes For example in the Intel x86 architecture pages are 4K bytes in length so mapping an object of less than 4K bytes will require the use of an entire page Implementation 3 Additional details like protection can be added without great effort The final implementation detail not needing a disk file is achieved by using a special system call to create the sharing object i e the earliest implementation of shared memory in FreeBSD required an existing disk file be used as the sharing object Still there must be some available disk space to back up the shared memory if buffer space is in short supply Some systems use swap space for this purpose 2 Demonstration of Shared Memory The program in home demos shm c on qnxcs gives a simple illustration of synchronizing two processes using shared memory A shared memory object named bolts is removed from the system if the program is invoked with more than one argument Note that this object is removed if the program terminates normally A new object is created if necessary for reading and writing the mode is set to 0777 so any process can access the object The result of the shm open call is checked for success shm c Demonstration cont d The shared memory object is next unlinked using shm unlink Since it is still open reducing the link count doesn t cause the object to be deleted However when all open instances of the object are closed it will be deleted unless a hard link to the object is made before that time Next the first byte of the object is initialized to contain the character 0 and an integer count is set to zero shm c Demonstration cont d Next the size of the object just created is set to 4096 bytes using ftruncate The program actually uses only one byte of the object but since we know QNX will use 4096 bytes on x86 we use 4096 The result of ftruncate is verified Next we map 4K bytes of the object all of it into memory for shared reading and writing this is done using mmap Note that the first argument is 0 which tells the system to place the object at any unused location The value returned is the address of e g a pointer to the mapped memory The mmap result is checked and the address at which the object was mapped is displayed for information purposes shm c Demonstration cont d Next a new process is created The processes will alternate changing 0 to 1 and 1 to 0 in the first byte of the shared memory terminating after 30 iterations Each change will be displayed as a single character on the display Note that each process checks to see if the byte in the shared memory is in the expected state If it is it changes it displays the changed value and increments count If the object is not in the …


View Full Document

UNO CSCI 8530 - Shared Memory

Loading Unlocking...
Login

Join to view Shared Memory and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shared Memory and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?