Hello, crossing, the last time we were talking about the example of the SYSTEMV IPC structure overview, this time we say the example is: using shared memory for interprocess communication . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Shared memory is a concrete object of the abstract concept of the SYSTEMV IPC structure. Just like its name, it provides a memory space for different processes to use, and the process can pass data through that memory space, thus enabling interprocess communication.
Before we introduce the use of shared memory, let's introduce a few functions that are used to manipulate shared memory.
Shmget function
intsize,int shmflag)
The function is used to get the shared memory, the function returns the identifier of the shared memory, we can use the shared memory through the identifier;
- The first parameter is the key value, which operates the structure of the IPC in the kernel, that is, the structure of the kernel within the share;
- The second parameter is the capacity of the shared memory, in bytes;
- The third parameter is the permission token for shared memory, which is the same as the file permission;
- Returns the shared memory identifier when the function runs successfully, or 1;
When using this function, we need to define a type of shared memory and calculate the memory space of that type. The type of shared memory can be defined according to the needs of the program, and it is common to define a struct type.
Shmat function
void * shmat(intconstvoid *shm_addr,int shmflg)
This function is used to connect shared memory to the address space of the process, so that the process can use shared memory;
- The first parameter is the identifier of the shared memory, which can be obtained by the Shmget function;
- The second parameter is an address that represents the location where the shared memory is connected to the process;
- The third parameter is a bit marker and only three values are used: shm_rnd,shm_rdonly 0;
- Returns a pointer to the first byte of shared memory if the function succeeds, otherwise returns-1;
When using this function, the second parameter usually uses a null pointer, and a null pointer indicates that the system chooses the location of the shared memory connection to the process address space, when the third parameter can use Shm_rdonly or 0. If you specify an address for the second parameter, then the third parameter requires the use of shm_rnd.
SHMDT function
int shmdt(constvoid *shm_addr)
This function is used to separate shared memory from the address space of the process, and the process cannot use shared memory after separation.
- The first parameter is an address, which is a pointer to the first byte of the shared memory, which is the return value of the Shmat function;
- Returns 0 if the function succeeds, otherwise returns-1;
Shmctl function
intshmctl(intint*buf)
This function is used to connect shared memory to the address space of the process, so that the process can use shared memory;
- The first parameter is the identifier of the shared memory, which can be obtained by the Shmget function;
- The second parameter is a command that represents the operation of shared memory, with only three commands available: Ipc_stat,ipc_set and Ipc_rmid;
- The third parameter is a struct pointer, which has shared memory permissions and owner information;
- Returns 0 if the function succeeds, otherwise returns-1;
We usually use this function to delete shared memory, when we need to assign a value of Ipc_rmid to the second parameter, which means to delete shared memory, the third parameter can be a null pointer. Two additional commands for the second parameter:
- Ipc_stat means associating the contents of the third parameter with the shared memory;
- Ipc_set indicates that the contents of the third parameter are set to the value of shared memory.
The type of the third parameter, as we mentioned in the previous one, is similar to the structure of the SYSTEMV IPC, which has its own unique members, in addition to the necessary members.
I found the type of the third parameter from the source code, which is defined in detail as follows: (in the linux-4.0.3/include/linux/shm.h file)
structShmid_kernel/ * Private to the kernel * /{structKern_ipc_perm Shm_perm;structFile *shm_file;unsigned LongShm_nattch;unsigned LongShm_segsz; time_t Shm_atim; time_t Shm_dtim; time_t Shm_ctim; pid_t Shm_cprid; pid_t Shm_lprid;structUser_struct *mlock_user;/ * The task created the SHM object. NULL If the task is dead. */ structTask_struct *shm_creator;structList_head shm_clist;/ * List by creator * /};
Crossing, for examples of interprocess communication using shared memory Let's talk about this. I want to know what the following example, and listen to tell.
Talk C chestnuts Together (95th: C-language instance-interprocess communication using shared memory)