Implement internal process Communication in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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 a shared memory area
Intshared_id;
Char * region;
Const intshm_size = 1024;
Shared_id = shmget (IPC_PRIVATE, // ensure that a unique ID is used
Shm_size,
IPC_CREAT | IPC_EXCL | // create a new memory area
S_IRUSR | S_IWUSR); // enables the current user to read and write data to 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 processes/threads
Shmdt (region );
// Destroy the shared memory area
Shmctl (shared_id, IPC_RMID, 0 );
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:
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service