shared_memory |
A shared memory segment is an area of memory that a process can attach to its own virtual address space. Attaching a memory area in this manner enables the process to treat the area as if it is local. Shared memory is often used as a fast way to share information among processes; however, access must be properly synchronized.
When a shared memory segment is
created, the segment is not attached to the process' address space. Use one of
the attach() family of functions to attach the
segment to the process' address space. To detach a process from its shared
memory region, send detach() to the segment or
terminate the process.
A process with superuser
privileges can use lock() to lock a shared memory
segment into main memory. Using lock() to lock a
memory segment protects the segment from the paging system. Similarly, a
superuser can use unlock() to unlock a segment.
Once created, a shared memory
segment persists in the system, even if no processes are attached to the
segment. To remove a shared memory segment, send remove()
, which schedules the segment for removal when the last process detaches from
the segment.
This section includes these examples of working with shared memory segments.
In the following example, one process creates a 1K shared memory segment and writes the alphabet into it; the other process attaches to the existing segment and reads it.
#include <iostream>
#include <ospace/unix.h>
int
reader()
{
os_shared_memory segment( os_key( 10 ) );
// Attach to my memory space.
char* ptr = segment.attach_at_first_available();
cout << segment.size() << " bytes attached to child at ";
cout << (void*) ptr << endl;
cout << "Read: ";
for ( int i = 0; i < 26; i++ ) // Read the alphabet.
cout << *ptr++;
cout << endl;
segment.remove(); // Remove the memory segment from the system.
return 0;
}
int
main()
{
os_unix_toolkit initialize;
// Create a 1K memory segment with key 10.
os_shared_memory segment( os_key( 10 ), O_CREAT, 1024, 0600 );
// Attach to my memory space.
char* ptr = segment.attach_at_first_available();
cout << segment.size() << " bytes attached to parent at ";
cout << (void*) ptr << endl << "Writing the alphabet." << endl;
for ( int i = 0; i < 26; i++ ) // Write the alphabet.
*ptr++ = 'a' + i;
segment.detach();
os_process child( reader ); // Spawn a child to read the segment.
return 0;
}
1024 bytes attached to parent at 0xef5f0000
Writing the alphabet.
1024 bytes attached to child at 0xef5f0000
Read: abcdefghijklmnopqrstuvwxyz
By default, a shared memory segment is attached for reading and writing. If you attempt to write to a memory segment that is attached for read-only, the process receives a SISSEGV signal, which causes the process to core dump.
#include <iostream>
#include <ospace/unix.h>
int
main ()
{
os_unix_toolkit initialize;
// Create a 1K memory segment with key 10.
os_shared_memory segment( os_key( 10 ), O_CREAT, 1024, 0600 );
// Attach for read-only.
char* ptr = segment.attach_at_first_available( O_RDONLY );
cout << segment.size () << " bytes attached to parent at ";
cout << (void*) ptr << endl;
// Attempt to write to read-only segment.
cout << "Attempting to write to read-only shared memory." << endl;
cout << "Expect a core." << endl;
*ptr = '0';
return 0; // Never executed.
}
1024 bytes attached to parent at 0xef5f0000
Attempting to write to read-only shared memory.
Expect a core.
Segmentation Fault (core dumped)
There are a variety of functions
in os_shared_memory that can determine
information about the memory segment status. The following example illustrates
many of these functions.
#include <iostream>
#include <ospace/security.h>
#include <ospace/unix.h>
int
main()
{
os_security_toolkit init_security;
os_unix_toolkit init_unix;
os_shared_memory segment( os_key( 10 ), O_CREAT, 1024, 0600 );
segment.attach_at_first_available();
cout << segment << endl;
cout << " size: " << segment.size() << endl;
cout << " number_attached: " << segment.number_attached() << endl;
cout << " creator_process: " << segment.creator_process() << endl;
cout << " last_op_process: " << segment.last_op_process() << endl;
cout << " last_attach_time: " << segment.last_attach_time() << endl;
cout << " last_detach_time: " << segment.last_detach_time() << endl;
cout << " last_change_time: " << segment.last_change_time() << endl;
cout << " creator_user: " << os_user( segment.creator_user() );
cout << endl;
cout << " creator_group: " << os_group( segment.creator_group() );
cout << endl;
cout << " owner_user: " << os_user( segment.owner_user() );
cout << endl;
cout << " owner_group: " << os_group( segment.owner_group() );
cout << endl;
cout << " mode: " << os_mode( segment.mode() ) << endl;
cout << " key: " << segment.key() << endl;
segment.remove();
return 0;
}
os_shared_memory( 800 )
size: 1024
number_attached: 1
creator_process: 9443
last_op_process: 9475
last_attach_time: 10/26/95 00:52:48
last_detach_time: 12/31/69 18:00:00
last_change_time: 10/26/95 00:52:00
creator_user: os_user( osi, 103 )
creator_group: os_group( cavemen, 21 )
owner_user: os_user( osi, 103 )
owner_group: os_group( cavemen, 21 )
mode: -rw-------
key: os_key( 10 )
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.