Article Title: Research on the Technology of shared memory and memory ing in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Linux provides a variety of internal process communication mechanisms, including shared memory, memory ing files, first-in-first-out (FIFO), interfaces (sockets), and a variety of identity for synchronization. In this article, we will mainly discuss the technology of shared memory and memory ing files.
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 implement synchronization in their own programs, but also need to develop their own communication protocols to use the IPC Mechanism.
Shared Memory
The use of shared memory is similar to the use of malloc to allocate memory areas. The method for using shared memory is:
1. Use shmget to allocate memory areas for a process/thread.
2. Use shmat to place one or more processes/threads in the shared memory. You can also use shmctl to obtain information or control the shared area.
3. Use shmdt to separate from the shared area.
4. Use shmctl to unallocate Space
The following is an example:
// Create the intshared_id; char * region; const intshm_size = 1024; shared_id = shmget (IPC_PRIVATE, // ensure that the unique ID shm_size is used, IPC_CREAT | IPC_EXCL | // create a new memory region S_IRUSR | S_IWUSR); // enable the current user to read and write this region // cross process or generate process. // Add the newly created memory area to the process/thread region = (char *) shmat (segment_id, 0, 0); // other program code... // separate various processes/threads from the shmdt (region); // destroy the shmctl (shared_id, IPC_RMID, 0) in the shared memory area );
|
Shared memory is the fastest IPC method in Linux. It is also a two-way process. Any process in the shared area can read and write the memory. 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.
Memory ing File
The memory 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 files to the memory to 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 );
|
The advantage of using memory ing to process IPC is that you don't need to process handles throughout the process: You just need to open the file and map it to the appropriate location. You can use a memory ing file between two unrelated processes.
The disadvantage of using memory ing is that the speed is not as fast as that of shared memory. If the file size is large, the required virtual memory size will be large, resulting in a reduction in overall performance.