New version page

UNO CSCI 8530 - Shared Memory

Upgrade to remove ads

This preview shows page 1 out of 3 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

1CSCI 8530Advanced Operating SystemsShared MemoryShared MemorySharing memory in POSIX (and many other systems) requirescreating a persistent “object” associated with the shared memory, andallowing 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.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 onlyO_RDWR –read/writeO_CREAT – create a new object if necessaryO_EXCL – fail if O_CREAT and object existsO_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.ftruncate(int fd, off_t len)This function (inappropriately named) causes the file referenced by fdto have the size specified by len.If the file was previously longer than lenbytes, the excess is discarded.If the file was previously shorter than lenbytes, it is extended by bytes containing zero.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)mmap, continuedprot – selected from the available protection settings:PROT_EXECPROT_NOCACHEPROT_NONEPROT_READPROT_WRITEflags – one or more of the following:MAP_FIXED –interpret addr parameter exactlyMAX_PRIVATE – don’t share changes to objectMAP_SHARED – share changes to objectmmap returns the mapped address, or –1 on error.2munmap (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).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.Implementation-1Shared 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.File-system SharingProcess A: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:fd1fd2fd3File DescriptorsProcess B:fdxFile DescriptorIn-memory inode for “a.file”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.Implementation-2The 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-3Additional 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.3Demonstration of Shared MemoryThe 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.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.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, 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


View Full Document
Download Shared Memory
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?