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
- //// // Twrite. c
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <pthread. h> // for Thread
- # Include <sys/SHM. h> // shmat
- # Include <sys/errno. h>
- /*
- 0666 indicates the permission, which is the same as the file permission settings.
- 4 2 1 indicates read/write execution, and 3 permissions.
- For example, the above 6 = 4 + 2 indicates read + write.
- If it is 7 = 4 + 2 + 1, it indicates read + write + execution.
- 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.
- */
- Int main ()
- {
- Int lishmid; // The initial address of the applied shared memory
- Key_t key; // The key applied for the shared memory.
- Char * p_map; // map the shared memory to your own program space.
- Char lsdata [1024];
- Char * lpname = "/home/report/lb/shmkey ";
- File * lpfp = fopen (lpname, "W ");
- Fclose (lpfp );
- Key = ftok (lpname, 0 );
- If (Key =-1)
- {
- Printf ("ftok Return Error: % d/N", errno );
- Return-1;
- }
- Printf ("ftok return: % 0x/N", key );
- /* First Use ftok to create a key, then call shmget to create a memory area */
- Lishmid = shmget (Key, 4096, ipc_creat | 0666 );
- If (lishmid =-1)
- {
- Printf ("shmget fail! Error code: % d/N ", lishmid );
- Return-1;
- }
- /* Attach this shared memory area to your memory block */
- P_map = (char *) shmat (lishmid, null, 0 );
- If (p_map = NULL)
- {
- Printf ("p_map: % 0x/N", p_map );
- Return-1;
- }
- Memset (lsdata, 0, sizeof (lsdata ));
- Strcpy (lsdata, "shared memory test program, this is test data ");
- Memcpy (p_map, lsdata, strlen (lsdata ));
- /* After writing data, delete it from its memory segment "*/
- If (shmdt (p_map) =-1)
- {
- Printf ("detach share memory/N ");
- Return-1;
- }
- /* 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;
}
- Return 0;
- }
The program has created a shared memory segment, and 3099d is the returned key value.
- // Tread. c
- /* 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 */
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <pthread. h> // for Thread
- # Include <sys/SHM. h> // shmat/
- # Include <sys/errno. h>
- Int main ()
- {
- Int lishmid; // The initial address of the applied shared memory
- Key_t key; // The key applied for the shared memory.
- Char * p_map; // map the shared memory to your own program space.
- Char lsdata [1024];
- Char * lpname = "/home/report/lb/shmkey ";
- Key = ftok (lpname, 0 );
- If (Key =-1)
- {
- Printf ("ftok Return Error: % 0x/N", key );
- Return-1;
- }
- /* First Use ftok to create a key, then call shmget to create a memory area */
- Lishmid = shmget (Key, 4096, ipc_creat | 0666 );
- If (lishmid =-1)
- {
- Printf ("shmget fail! Error code: % d/N ", errno );
- Return-1;
- }
- /* Attach this shared memory area to your memory block */
- P_map = (char *) shmat (lishmid, null, 0 );
- Printf ("Shared Memory Data: % s/n", p_map );
- /* After writing data, delete it from its memory segment "*/
- If (shmdt (p_map) =-1)
- {
- Printf ("detach share memory/N ");
- Return-1;
- }
- Return 0;
- }