Inter-process communication-shared memory

Source: Internet
Author: User

Inter-process communication-shared memory

We are familiar with inter-process communication methods, such as pipelines (named pipelines), signals (signal), shared memory, message queues, sockets (sockets ), semaphores are classified as inter-process synchronization mechanisms.
Next we will talk about the communication mode of shared memory. It is the fastest in IPC. Once the memory zone is mapped to the address space of the process that shares it, data transmission between these processes does not involve the kernel. However, data must be synchronized during access. the authoritative reference for shared memory is unix Network Programming Volume 2. we have explained the mmap Kernel Mechanism in the previous sections. This is the way posix v shares memory. System v is similar, but the interface is encapsulated.
Let's start with the posix v method. Its common function interfaces are as follows:
Open/shm_open + mmap, munmap, msync, shm_unlink, etc.
Since the file is open, not all files can be mapped. The Supported file types are as follows:
1. Common Disk Files
2. Device Files (except some special files)
3. memory files (such as tmpfs)
4. Anonymous ing (mainly used between unrelated processes)
First, let's talk about the commonly used anonymous ing. open/dev/zero ensures that all the ing areas are initialized to an anonymous ing of 0. You can also use mmap to map objects without opening any files:

 
 
  1. mmap( NULL,size_of_map,PROT_READ|PROT_WRITE,MAP_SHARED | MAP_ANON, -1,0 );
The following figure shows the mmap function prototype:
 
 
  1. void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset);
The main difference between anonymous ing is that a MAP_ANON flag is added to flags, which is-1 passed by fd. For more mmap information, please query it by yourself.
Note the following when using mmap to share memory:
The size of the mapped memory area and file size. The size of the memory ing is an integer multiple of the page (PAGESIZE 4096 by default). For example, if the file size is 5000, It is mapped to 5000, therefore, you can access the memory area of 2 * PAGESIZE, but memory writes greater than 5000 are not synchronized to the file. The size of the accessible memory is also related to the file size. During the ing, the system automatically checks the underlying support, that is, the file size. however, we know that the advantage of mmap is that the file size can be dynamically changed. The function interfaces are ftruncate and fstat. for detailed usage, refer to unix Network Programming Volume 2.

The following briefly summarizes its features:
1. You can change the size at any time.
Operation Function interfaces: ftruncate and fstat
2. you must first create and open a file, which can be a disk file or a memory file, such as a file under tmpfs. If the shared memory configuration needs to be written to the disk, you can choose to open the disk file. If a flash file is used as a ing in the embedded system, shm is recommended since frequent file operations and disk read/write are required. After all, frequent flash reads and writes may affect the lifetime and lead to bad blocks.

System v does not require explicit open files, but there are many function operation interfaces.
Function interfaces: shmget, shmat, shmdt, and shmctl.
There are some restrictions on the shm method:
Shmax the maximum number of bytes in a shared memory area (the specific system is not the same)
Shmmnb the minimum number of bytes in a shared memory zone 1
The shmmni system has a maximum of 128 shared memory zone identifiers.
Shmseg the maximum number of shared memory zones attached to each process is 32.
This information can be viewed through the proc file system. Cat/proc/sys/kernel/shmmax and so on. You can view and operate the mapped memory zone using the ipcs command.

We also summarize its features:
1. No need to create or open a file
2. For multi-process communication, a key is required. Dynamic generation is not necessarily consistent.
3. The read/write speed is higher than that of mmap.
4. the shared memory space cannot be too large. After all, it directly occupies the memory space.
Either method involves communication between multiple processes and data interaction, so mutual exclusion and synchronization must be maintained. The most common method here is the semaphore method.
Semaphores: sem_init, sem_wait, and sem_post. Because semaphores are used for compilation, the thread library-lpthread needs to be linked.
The following code examples show their specific usage:
Posix v mode:
1. initialize the program, and write the program cyclically without interruption. The period is 1 s.
 
 
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include


  6. #define CLUSTER_SHARED_FILE "/tmp/cluster_share"

  7. typedef struct{
  8. char name[4];
  9. int age;
  10. }people;

  11. struct stu {

  12. sem_t mutex;
  13. people s[10];

  14. };

  15. struct stu t;

  16. main(int argc, char** argv) // map a normal file as shared mem:
  17. {
  18. int fd,i;
  19. struct stu *p_map;
  20. char temp;

  21. fd=open(CLUSTER_SHARED_FILE,O_CREAT|O_RDWR|O_TRUNC,00777);
  22. lseek(fd,sizeof(t)-1,SEEK_SET);
  23. write(fd,"",1);

  24. p_map = (struct stu *) mmap( NULL,sizeof(t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
  25. close(fd);
  26. temp = 'a';
  27. sem_init(&p_map->mutex,1,1);
  28. int j=0;
  29. while(1)
  30. {
  31. j++;
  32. printf("j 1\n");
  33. sem_wait(&p_map->mutex);
  34. printf("j 2\n");
  35. for(i=1; i<10; i++)
  36. {
  37. // temp += 1;

  38. memcpy(p_map->s[i].name, &temp,2 );
  39. p_map->s[i].age = p_map->s[i].age +20+i+j;
  40. }
  41. printf("j 3\n");
  42. sem_post(&p_map->mutex);
  43. printf("j 4\n");
  44. sleep(1);
  45. }
  46. printf(" initialize over \n ");
  47. sleep(50);
  48. munmap( p_map, sizeof(t) );
  49. printf( "umap ok \n" );
  50. }
2. Read operation:
 
 
  1. #include
  2. #include
  3. #include
  4. #include

  5. #include

  6. #include

  7. #define CLUSTER_SHARED_FILE "/tmp/cluster_share"

  8. typedef struct{
  9. char name[4];
  10. int age;
  11. }people;

  12. struct stu {

  13. sem_t mutex;
  14. people s[10];
  15. };

  16. struct stu t;

  17. main(int argc, char** argv) // map a normal file as shared mem:
  18. {
  19. int fd,i;
  20. struct stu *p_map;
  21. char temp;

  22. fd=open(CLUSTER_SHARED_FILE,O_CREAT|O_RDWR,00777);
  23. // lseek(fd,sizeof(people)*5-1,SEEK_SET);
  24. // write(fd,"",1);

  25. p_map = (struct stu *) mmap( NULL,sizeof(t),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
  26. close( fd );

  27. while(1)
  28. {
  29. sleep(1);
  30. printf("2.....s\n");
  31. sem_wait(&p_map->mutex);
  32. printf("2.....\n");
  33. for(i=0; i<10; i++)
  34. {
  35. printf("name:%s,age:%d\n",p_map->s[i].name,p_map->s[i].age);

  36. }
  37. printf("2.....0\n");
  38. sem_post(&p_map->mutex);
  39. printf("2.......1");
  40. }
  41. munmap( p_map, sizeof(t));
  42. printf( "umap ok \n" );
  43. }
As for the write operation, the program can be tested with a slight modification. There is no problem with multiple concurrency.
System V method, although the interface is not the same, but also very similar.
1. Initialization
 
 
  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. # Include
  6. # Include

  7. // # Include "shmdata. h"




  8. # Define TEXT_SZ 2048

  9. Struct shared_use_st
  10. {
  11. Sem_t mutex;
  12. Int written; // as a flag, non-0: Indicates readable, 0 indicates writable
  13. Char text [TEXT_SZ]; // record the text written and read
  14. Int cnt;
  15. };


  16. Int main ()
  17. {
  18. Int running = 1;
  19. Void * shm = NULL;
  20. Struct shared_use_st * shared = NULL;
  21. Char buffer [4096] = "hello world ";
  22. Int shmid;
  23. Int I = 0;
  24. // Create shared memory
  25. # If 0
  26. // Delete shared memory
  27. If (shmctl (shmid, IPC_RMID, 0) =-1)
  28. {
  29. Fprintf (stderr, "shmctl (IPC_RMID) failed \ n ");
  30. Exit (EXIT_FAILURE );
  31. }
  32. # Endif
  33. Shmid = shmget (key_t) 12345, sizeof (struct shared_use_st), 0666 | IPC_CREAT );
  34. If (shmid =-1)
  35. {
  36. Fprintf (stderr, "shmget failed \ n ");
  37. Exit (EXIT_FAILURE );
  38. }
  39. // Connect the shared memory to the address space of the current process
  40. Shm = shmat (shmid, (void *) 0, 0 );
  41. If (shm = (void *)-1)
  42. {
  43. Fprintf (stderr, "shmat failed \ n ");
  44. Exit (EXIT_FAILURE );
  45. }
  46. Printf ("Memory attached at % X \ n", (int) shm );
  47. // Set the shared memory
  48. Shared = (struct shared_use_st *) shm;
  49. Sem_init (& shared-> mutex, 1, 1 );

  50. While (running) // write data to the shared memory
  51. {
  52. Strncpy (shared-> text, buffer, TEXT_SZ );

  53. Sem_wait (& shared-> mutex );
  54. // Shared-> cnt = 0;
  55. Shared-> cnt = shared-> cnt + I;
  56. Sem_post (& shared-> mutex );
  57. I ++;
  58. Shared-> written = 1;

  59. Sleep (1 );
  60. }
  61. // Detach the shared memory from the current process
  62. If (shmdt (shm) =-1)
  63. {
  64. Fprintf (stderr, "shmdt failed \ n ");
  65. Exit (EXIT_FAILURE );
  66. }
  67. Sleep (2 );
  68. Exit (EXIT_SUCCESS );
  69. }
2. Read operations
 
 
  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. // # Include "shmdata. h"
  6. # Include




  7. # Define TEXT_SZ 2048

  8. Struct shared_use_st
  9. {
  10. Sem_t mutex;
  11. Int written; // as a flag, non-0: Indicates readable, 0 indicates writable
  12. Char text [TEXT_SZ]; // record the text written and read
  13. Int cnt;
  14. };

  15. Int main ()
  16. {
  17. Int running = 1; // indicates whether the program continues to run.
  18. Void * shm = NULL; // original first address of the allocated shared memory
  19. Struct shared_use_st * shared; // point to shm
  20. Int shmid; // shared memory identifier
  21. // Create shared memory
  22. Shmid = shmget (key_t) 12345, sizeof (struct shared_use_st), 0666 );
  23. If (shmid =-1)
  24. {
  25. Fprintf (stderr, "shmget failed \ n ");
  26. Exit (EXIT_FAILURE );
  27. }
  28. // Connect the shared memory to the address space of the current process
  29. Shm = shmat (shmid, 0, 0 );
  30. If (shm = (void *)-1)
  31. {
  32. Fprintf (stderr, "shmat failed \ n ");
  33. Exit (EXIT_FAILURE );
  34. }
  35. Printf ("\ nMemory attached at % X \ n", (int) shm );
  36. // Set the shared memory
  37. Shared = (struct shared_use_st *) shm;
  38. // Shared-> written = 0;
  39. While (running) // read data in the shared memory
  40. {

  41. Sem_wait (& shared-> mutex );
  42. Printf ("text: % s, cnt: % d \ n", shared-> text, shared-> cnt );
  43. Sem_post (& shared-> mutex );
  44. Sleep (1 );


  45. }
  46. // Detach the shared memory from the current process
  47. If (shmdt (shm) =-1)
  48. {
  49. Fprintf (stderr, "shmdt failed \ n ");
  50. Exit (EXIT_FAILURE );
  51. }
  52. # If 0
  53. // Delete shared memory
  54. If (shmctl (shmid, IPC_RMID, 0) =-1)
  55. {
  56. Fprintf (stderr, "shmctl (IPC_RMID) failed \ n ");
  57. Exit (EXIT_FAILURE );
  58. }
  59. # Endif
  60. Exit (EXIT_SUCCESS );
  61. }

The overall usage is relatively simple and efficient.








Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.