Sharing memory among processes Memory-mapped

Sharing memory among processes

Both segmentation and paging allow for portions of memory to be shared between processes. This can result in large savings in memory. Sharing memory among processes For example, on a mainframe supporting many users, it might be common for many users to be running a word processing program at the same time. With paging, the page tables for many processes can both point to the same frames in memory so that only one copy of the program code is actually resident. Similarly, with segmentation, the segment tables for many processes can point to the same physical memory segment. While this can be handy, it can also cause problems.

If the portions of memory that are being shared are data segments, then the individual processes will be changing some of the pages. This may or may not be desired. Several processes might be using shared memory to communicate among themselves. In this case, we would want each process to see all the changes to the pages, so they should be looking at the same frames in physical memory.

But consider the case where one process forks itself. Initially, it would be ideal to share the entire physical address space between the two processes. But as they run, changes made by one process should not be seen by the other process. In order to allow this to happen, an OS can use a mechanism known as copy on write. Initially, the two processes will be mapped to the same physical frames. But the page (or segment) tables will be set as read only.

If either process tries to write to a shared portion of the memory, then an interrupt will occur. When this happens the memory management subsystem will make a separate copy of the shared portion for each process and remove the write protection flag from the table, allowing each process to see only its own version of the data. Solaris supports access to a shared memory block (Solaris calls it a segment) using the shmget() routine.

One process creates a shared block with the first call. The block is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the block is called the shmid. Here is the call used to access a shared memory block in Solaris:

The key argument is either of type key t or is IPC PRIVATE. It is the numeric key to be assigned to the returned shared memory block. The size argument is the size in bytes of the requested block. The simple argument specifies the starting access permissions and creation control flags.

If the call succeeds, it returns an ID to identify the shared memory block. This call can also be used to get the ID of an existing shared block by another process. The following code illustrates shmget():

Server and clients can be created with a fork call or can be unrelated. For a child process, if a shared memory block is requested and attached prior to forking the child, then the server may want to use IPC_PRIVATE since the child has a copy of the server’s address space, Sharing memory among processes which includes the attached shared block. However, if the server and clients are separate processes, using IPC_PRIVATE is not a good idea since the clients will not know the key.

Memory mapped

files Most modern OSs allow a special mode of memory sharing referred to as memory-mapped files. In this mode, a process will ask the OS to open a file and associate all or part of the data in the file with a region of the logical address space of the process. Sharing memory among processes Then the process can refer to the information in that space as an array or through memory pointers. There are two main advantages of such a system.

The first is that the process does not have to use I/O statements to access the data the demand paging system takes care of accessing the right data from the file. The second advantage is that two or more processes can ask the OS for access to the same file at the same time.

The same memory frames will be mapped into the logical address spaces of both processes, allowing them to share access to the memory. This mechanism, therefore, provides a simple mechanism for sharing data between two processes. Of course, the processes may need to use synchronization techniques to avoid interfering with one another. In addition, Sharing memory among processes if the real purpose of the “shared file” is to provide a shared memory region between two or more processes, the shared file does not actually need to reside on the file system as a file. As an example, here is how memory-mapped objects (including files) can be created under the Windows Win32 libraries:

The meanings of some of the parameters are:

  • hFile—A handle to the file from which to create a mapping object. If hFile is 1, the call must also give a size for the object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters and a temporary file is created in the system paging file instead of mapping to a file system file.
  • A pointer to a security descriptor structure for the object that contains access control lists (ACL) and other security information.
  • flProtect—Protection to be applied to the object
  • PAGE_READONLY
  • PAGE_READWRITE
  • PAGE_WRITECOPY (copy on write)
  • PAGE_EXECUTE_READ
  • PAGE_EXECUTE_READWRITE
  • PAGE_EXECUTE_WRITECOPY Etc.
  • dwMaximumSizeHigh—High-order DWORD of max size of the object.
  • dwMaximumSizeLow—Low-order DWORD of max size of the object.
  • lpName—The name of the file to be mapped.

Also Read:-

Leave a Reply

Your email address will not be published. Required fields are marked *