Inter-process communication in Linux: shared memory
Source: Internet
Author: User
Inter-process communication in Linux: Shared memory Linux provides a wide range of internal process communication mechanisms, including shared memory, memory ing files, first-in-first-out (FIFO), and interfaces (sockets) and multiple identifiers for synchronization. Www.2cto.com in general, internal process communication (inter...
Inter-process communication in Linux: Shared memory Linux provides a wide range of internal process communication mechanisms, including shared memory, memory ing files, first-in-first-out (FIFO), and interfaces (sockets) and multiple identifiers for synchronization. Generally, 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 ----------------------------------------------------------- it is very similar to using shared memory and using malloc () to allocate memory areas. The method of using shared memory is: 1 pair of processes/threads use shmget () to allocate memory areas. 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. using shmctl () to unallocate space 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, and it is necessary to ensure that the handle is passed to sub-processes and threads. Routine ----------------------------------------------------------- # include # Include # Include # Include # Include # Include # Include # Include # Include # Define PERM S_IRUSR | S_IWUSR int main (int argc, char ** argv) {key_t shmid; char * p_addr, * c_addr; pid_t pid; if (argc! = 2) {fprintf (stderr, "Usage: % s/n/a", argv [0]); exit (1) ;}if (shmid = shmget (IPC_PRIVATE, 1024, PERM) =-1) {// (1) fprintf (stderr, "Create Share Memory Error: % s/n/a", strerror (errno )); exit (1) ;}pid = fork (); if (pid> 0) {p_addr = shmat (shmid, 0, 0); memset (p_addr, '/0 ', (1024); strncpy (p_addr, argv [1], 1024); wait (NULL); exit (0);} else if (pid = 0) {sleep (1 ); c_addr = shmat (shmid, 0, 0); printf ("Client get % s/n", c_addr); exit (0) ;}} (1) IPC_PRIVATE ensures that the unique IDS_IRUSR | S_IWUSR enables the current user to read and write data to run in this region ------------------------------------------------- $. /. out zengxiaolongClient get zengxiaolong $. /. out zengxiaolong postscript: process communication (IPC) is the foundation of network programs. in many network programs, the concept and knowledge of process communication are widely used. in fact, process communication is a very complicated thing. I will just give a brief introduction here. if you want to learn more about process communication, the best way is to constantly write programs and read online manuals.
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