Memory ing Diagram
Main functions used: shmget (), shmat (), shmctl (), shmdt ()
Shmget: generate a key through the file descriptor (usually absolute path name) and create a new shared memory zone (up to 4096kb)
Shmat: maps the shared memory area to the address space of the calling process (process a and process B.
Shmctl: Set (ipc_set), get (ipc_stat), and delete (ipc_rmid) shared memory zone.
Shmdt (): The process deletes the memory segment mapped to the shared memory area.
* ******************** Program-related information **************** *****/
# Include <sys/IPC. h>
# Include <sys/SHM. h>
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Int PID, shmid; // the latter is the shared memory recognition code
Char * write_address;
Char * read_address;
Struct shmid_ds dsbuf;
If (shmid = shmget (ipc_private, 32, 0) <0) // allocate shared memory
{
Printf ("An error occurred while allocating shmid shared memory. \ N ");
Exit (1 );
}
Else
Printf ("shmid shared memory allocated successfully. Shared Memory recognition code: % d. \ N ", shmid );
If (pid = fork () <0)
{
Printf ("fork function call error! \ N ");
Exit (2 );
}
Else if (pid> 0) // parent process, which writes data to the shared memory
{
Printf ("parent process ID: % d \ n", getpid ());
Write_address = (char *) shmat (shmid, null, 0); // connect to the shared memory
If (INT) write_address =-1)
{
Printf ("shmat connection shared memory error. \ N ");
Exit (3 );
}
Else
{
Printf ("shmat is successfully connected to the shared memory. \ N ");
Strcpy (write_address, "I am writing test data to the shared memory"); // write data to the shared memory
Printf ("the information written to the shared memory is" % s ". \ N ", write_address );
If (shmdt (void *) write_address) <0) // disconnect from the shared memory
Printf ("shmdt shared memory disconnection error. \ N ");
Else
Printf ("shmdt Shared Memory disconnected successfully. \ N ");
Sleep (2 );
Return;
}
}
Else // sub-process that reads data from the shared memory
{
Sleep (2); // wait until the parent process finishes writing data to the shared memory.
Printf ("sub-process ID: % d \ n", getpid ());
If (shmctl (shmid, ipc_stat, & dsbuf) <0)
{
Printf ("an error occurred when shmctl obtained the shared memory data structure. \ N ");
Exit (4 );
}
Else
{
Printf ("shmctl succeeded in obtaining the shared memory data structure. \ N: % d \ n ", dsbuf. shm_cpid );
Printf ("the size of the shared memory is % d \ n", dsbuf. shm_segsz );
If (read_address = (char *) shmat (shmid,) <0) // connect to the shared memory
{
Printf ("an error occurs when the shmat is connected to the shared memory. \ N ");
Exit (5 );
}
Else
{
Printf ("the information read from the shared memory is" % s ". \ N ", read_address );
Printf ("the last process that operates the shared memory is % d \ n", dsbuf. shm_lpid );
If (shmdt (void *) read_address) <0) // disconnect from the shared memory
{
Printf ("shmdt shared memory disconnection error. \ N ");
Exit (6 );
}
Else
Printf ("shmdt Shared Memory disconnected successfully. \ N ");
If (shmctl (shmid, ipc_rmid, null) <0) // Delete the shared memory and its Data Structure
{
Printf ("an error occurs when shmctl deletes the shared memory and its data structure. \ N ");
Exit (7 );
}
Else
Printf ("shmctl deleted the shared memory and its data structure successfully. \ N ");
Exit (0 );
}
}
}
}
**************** *****
Other reference learning: memory usage