Learn the code and experience of sharing operations

Source: Internet
Author: User

This small Code helps me understand the operations on shared memory.

Note that the specified key file must already exist, and ftok will not be created by myself. I initially thought that ftok will create a key value file, and an error will be reported when the program runs, now write the file creation function into the program

  1. //// // Twrite. c
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. # Include <pthread. h> // for Thread
  5. # Include <sys/SHM. h> // shmat
  6. # Include <sys/errno. h>
  7. /*
  8. 0666 indicates the permission, which is the same as the file permission settings.
  9. 4 2 1 indicates read/write execution, and 3 permissions.
  10. For example, the above 6 = 4 + 2 indicates read + write.
  11. If it is 7 = 4 + 2 + 1, it indicates read + write + execution.
  12. In addition, each digit of 0666 represents a type of permission. For example, the first 0 represents the UID, the first indicates the owner's permission, and the second 6 represents the same group of permissions, the third six represents others' permissions.
  13. */
  14. Int main ()
  15. {
  16. Int lishmid; // The initial address of the applied shared memory
  17. Key_t key; // The key applied for the shared memory.
  18. Char * p_map; // map the shared memory to your own program space.
  19. Char lsdata [1024];
  20. Char * lpname = "/home/report/lb/shmkey ";
  21. File * lpfp = fopen (lpname, "W ");
  22. Fclose (lpfp );
  23. Key = ftok (lpname, 0 );
  24. If (Key =-1)
  25. {
  26. Printf ("ftok Return Error: % d/N", errno );
  27. Return-1;
  28. }
  29. Printf ("ftok return: % 0x/N", key );
  30. /* First Use ftok to create a key, then call shmget to create a memory area */
  31. Lishmid = shmget (Key, 4096, ipc_creat | 0666 );
  32. If (lishmid =-1)
  33. {
  34. Printf ("shmget fail! Error code: % d/N ", lishmid );
  35. Return-1;
  36. }
  37. /* Attach this shared memory area to your memory block */
  38. P_map = (char *) shmat (lishmid, null, 0 );
  39. If (p_map = NULL)
  40. {
  41. Printf ("p_map: % 0x/N", p_map );
  42. Return-1;
  43. }
  44. Memset (lsdata, 0, sizeof (lsdata ));
  45. Strcpy (lsdata, "shared memory test program, this is test data ");
  46. Memcpy (p_map, lsdata, strlen (lsdata ));
  47. /* After writing data, delete it from its memory segment "*/
  48. If (shmdt (p_map) =-1)
  49. {
  50. Printf ("detach share memory/N ");
  51. Return-1;
  52. }
  53. /* Delete this memory from the system */
    If (shmctl (lishmid, ipc_rmid, 0) <0) // This line of code is not required if it is used for multiple times.
    {
    Printf ("shmctl error: % d/N", errno );
    Return-1;
    }
  54. Return 0;
  55. }

 

 

The program has created a shared memory segment, and 3099d is the returned key value.

 

  1. // Tread. c
  2. /* The read process first obtains the start address of the shared memory. ftok obtains the key created by the file name and ID at the time of creation */
  3. # Include <stdio. h>
  4. # Include <stdlib. h>
  5. # Include <pthread. h> // for Thread
  6. # Include <sys/SHM. h> // shmat/
  7. # Include <sys/errno. h>
  8. Int main ()
  9. {
  10. Int lishmid; // The initial address of the applied shared memory
  11. Key_t key; // The key applied for the shared memory.
  12. Char * p_map; // map the shared memory to your own program space.
  13. Char lsdata [1024];
  14. Char * lpname = "/home/report/lb/shmkey ";
  15. Key = ftok (lpname, 0 );
  16. If (Key =-1)
  17. {
  18. Printf ("ftok Return Error: % 0x/N", key );
  19. Return-1;
  20. }
  21. /* First Use ftok to create a key, then call shmget to create a memory area */
  22. Lishmid = shmget (Key, 4096, ipc_creat | 0666 );
  23. If (lishmid =-1)
  24. {
  25. Printf ("shmget fail! Error code: % d/N ", errno );
  26. Return-1;
  27. }
  28. /* Attach this shared memory area to your memory block */
  29. P_map = (char *) shmat (lishmid, null, 0 );
  30. Printf ("Shared Memory Data: % s/n", p_map );
  31. /* After writing data, delete it from its memory segment "*/
  32. If (shmdt (p_map) =-1)
  33. {
  34. Printf ("detach share memory/N ");
  35. Return-1;
  36. }
  37. Return 0;
  38. }

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.