LinuxMediumShareMemoryAndMemoryResearch on ing technology
| CCID-1-28 save this article and recommend it to friends QQ to view this site favorites site |
LinuxProvides us with a wide range of internal process communication mechanisms, includingShareMemory,MemoryIng files, first-in-first-out (FIFO), interfaces (sockets), and multiple identifiers for synchronization. In this article, we will mainly discussShareMemoryAndMemoryIng file technology.
In general, interprocess communication (IPC) refers to communication between two or more processes and two or more threads. Each IPC Mechanism has different strengths or weaknesses, but no IPC Mechanism contains built-in Synchronization Methods. Therefore, programmers not only need to be in the programImplementationIn addition, you also need to develop the communication protocol to use the IPC Mechanism.
ShareMemory
UseShareMemoryAnd use malloc to allocateMemoryAreas are similar. UseShareMemoryThe method is:
1. Use shmget to allocate a process/threadMemoryRegion.
2. Use shmat to place one or more processes/threads inShareMemory, You can also use shmctl to obtain information or controlShareRegion.
3. Use shmdtShareSeparated from each other.
4. Use shmctl to unallocate Space
The following is an example:
// CreateShareMemoryRegion intshared_id; char * region; const intshm_size = 1024; shared_id = shmget (ipc_private, // ensure that the unique ID shm_size, ipc_creat | ipc_excl | // create a newMemoryRegion s_irusr | s_iwusr); // enables the current user to read and write this region // cross process or generate process. //MemoryAdd process/thread region = (char *) shmat (segment_id, 0, 0); // other program code... // separate various processes/threads from shmdt (region); // destroyShareMemoryRegion shmctl (shared_id, ipc_rmid, 0 ); |
ShareMemoryYesLinuxThe fastest IPC method. It is also a two-way process,ShareAny process in the region can read and writeMemory. The disadvantage of this mechanism is that its synchronization and Protocol are not controlled by programmers. You must ensure that the handle is passed to sub-processes and threads.
MemoryIng File
MemoryThe ing file is not only used for IPC, but also plays a major role in other processes. If you need to initialize an allocated buffer to zero, just remember/dev/zero. You can also map filesMemoryTo improve its performance. It allows you to read and write files like reading and writing strings. The following is an example:
Const char filename [] = "testfile"; intfd; char * mapped_mem; const intflength = 1024; FD = open (filename, o_rdwr | o_creat, s_irusr | s_iwusr); lseek (FD, flength + 1, seek_set); write (FD, "/0", 1); lseek (FD, 0, seek_set); mapped_mem = MMAP (0, flength, prot_write, // allow write map_shared, // The written content is immediately written to the file FD, 0); close (FD); // use the ing area .... munmap (file_memory, flength ); |
ExploitationMemoryThe advantage of IPC ing to process IPC is that you don't need to process the handle throughout the process: You just need to open the file and map it to the appropriate location. You can useMemoryIng file.
UseMemoryThe disadvantage of ing is that it is not as fastShareMemoryFast. The required virtualMemoryIt will be very large, which will cause the overall performance to decline.