To use shared memory, follow these steps:
1. Open up a shared memory shmget ()
2. allow this process to use a shared memory shmat ()
3. Write/read
4. prohibit this process from using this shared memory shmdt ()
5. Delete this shared memory shmctl () or ipcrm under the command line
Ftok (). It has two parameters: a string and a character. The string is generally used by the current processProgramThe name and character are generally used to mark the shared memory identified by this identifier as the first shared memory opened by this process. Ftok () returns a value of the key_t type, that is, the calculated Identifier value.
Shmkey = ftok ("mcut", 'A'); // calculate the identifier
The following function is used to operate the shared memory.
# Include <sys/types. h>
# Include <sys/IPC. h>
# Include <sys/SHM. h>
Int shmget (key_t shmkey, int shmsiz, int flag );
Void * shmat (INT shmid, char * shmaddr, int shmflag );
Int shmdt (char * shmaddr );
Shmget () is a function used to open/point to a shared memory. Parameters are defined as follows:
Key_t shmkey is the identifier of the shared memory. For inter-process communication of the parent-child relationship, this identifier is replaced by ipc_private. However, our two processes have nothing to do with each other, so we use ftok () to calculate an identifier.
Int shmsiz is the memory size.
Int flag is the memory mode and permission ID.
The mode can be set to ipc_creat.
Use the opened memory: ipc_alloc
If the identifier already exists, the error value ipc_excl is returned.
Then, the "Mode" and "permission ID" are calculated as "or" and used as the third parameter.
For example, ipc_creat | ipc_excl | 0666
If this function succeeds, the ID of the shared memory is returned. If the function fails, the value-1 is returned.
// Open shared memory in shmid
Shmid = shmget (shmkey, sizeof (in_data), ipc_creat | 0666 );
Shmat () is a function used to allow the process to access a shared memory.
Int shmid is the ID of the shared memory.
Char * shmaddr is the starting address of the shared memory.
Int shmflag is the memory operation mode of the current process. If it is shm_rdonly, it is read-only mode. The other is the read/write mode.
If successful, this function returns the starting address of the shared memory. -1 is returned if the request fails.
Char * head, * POs,
Head = Pos = shmat (shmid, 0, 0 );
// Allow this process to use this shared memory
Shmdt (), in contrast to shmat (), is a function used to prohibit the process from accessing a shared memory.
The char * shmaddr parameter is the starting address of the shared memory.
If the call succeeds, 0 is returned. -1 is returned if the request fails.
Shmdt (head); // disable this process from using this memory.
In addition, there is a shmctl () function used to control the shared memory as follows:
# Include <sys/types. h>
# Include <sys/IPC. h>
# Include <sys/SHM. h>
Int shmctl (INT shmid, int cmd, struct shmid_ds * BUF );
Int shmid is the ID of the shared memory.
Int CMD is a control command with the following values:
Ipc_stat obtains the shared memory status.
Ipc_set changes the shared memory status
Ipc_rmid: delete shared memory
Struct shmid_ds * Buf is a struct pointer. In ipc_stat, the obtained status is placed in this struct. To change the shared memory status, use this struct.
Returned value: Success: 0
Failed:-1
Shmctl (shmid, ipc_rmid, null );
Just now, we can add a few more words in our mpaste. C program.
Struct shmid_ds Buf;
......
Shmctl (shmid, ipc_stat, & BUF); // gets the shared memory status
......
Shmctl (shmid, ipc_rmid, & BUF); // Delete shared memory
Note !!!!!!!!! : When shared memory is used, the program exits. If you have not used shmctl () in the program to delete the shared memory, you must use the ipcrm command to delete the shared memory. If you don't care, it's always there.
The IPCS command and ipcrm command are briefly explained.
Obtain IPC information:
IPCS [-M |-q |-S]
-M outputs information about shared memory.
-Q: output information about Message Queue
-S outputs information about the semaphore.
% IPCS-m
Delete IPC
Ipcrm-M |-q |-s shm_id
% Ipcrm-M 105