Shared memory and Message Queuing are also ways of communicating between processes.
Shared memory (similar to semaphore operation)
#include <sys/shm.h>
1, int shmget (key_t key,size_t size,int SHMFLG);
Function: Create a new piece of memory or return the built-in memory
Parameter: Key, which is used to indicate the opening of a memory, each process accesses the same piece of memory through this flag
Size, amount of memory
SHMFLG, which is exactly the same as the file operation, indicates that a bitwise or ipc_create creates a piece of memory, and if the memory that the key represents is already established, even if the addition of Ipc_create does not create a new chunk of memory, the memory associated with the key is returned.
Return value: Returns an identifier, other operations on shared memory, with the return value
2, void *shmat (int shm_id,const void * Shm_addr,int SHMFLG);
Role: Talk about a shared memory connection to the current process
Parameter: return value of Shm_id,shmget
SHM_ADDR is connected to the address location of the current process, which is generally null, indicating that the system chooses
Shmfig:shm_rnd, combined with shm_addr, to control the connection address
Shm_rdonly, read-only
Generally set to 0
Return value: Pointer to the first byte of shared memory
3, int shmdt (const void * shm_addr);
Role: Separating shared memory from the current process
Parameter: return value of Shmat
4, int shmctl (int shm_id,int command,struct shmid_ds * buf);
Structshmid_ds {
uid_t Shm_perm.uid;
uid_t Shm_perm.gid;
mode_t Shm_prem.mode;
}
Role: Control of shared memory
Parameter: return value of Shm_id,shm_get
Commond,ipc_stat: Sets the value in Shmid_ds to the current shared memory state value
Ipc_set: Set the shared memory state to a value in Shmid_ds
IPC_RMID: Deleting shared memory segments
Message Queuing
#include <sys/msg.h>
1, int msgget (key_t key,int MSGFLG);
Role: Create and access a message queue
Parameter: Key, which represents a message queue
MSGFLG, permission flag bit, similar to Shmget flag bit
Return value: Returns a descriptor for use in other Message Queuing functions
2, int msgsnd (int msqid,const void * msg_ptr,size_t msg_sz,int MSGFLG);
The message is generally expressed in the structure below.
struct my_message{
A long int message_type;//represents the data type
/*the data wish to transfer*/
}
Role: Adding messages to a message queue
Parameter: return value of Msqid,msgget
Msg_ptr, pointing to a pointer ready to send a message
MSG_SZ, message length does not include long shaping message type variables
MSGFLG controls what happens when the current queue is full or reaches the maximum system, typically set to 0
3, int MSGRCV (int msqid,void * msg_ptr,size_t msg_sz,long intmsgtype,int MSGFLG);
Role: Receive messages from Message Queuing
Parameter: The return value of the Msqid,msgget function
Msg_ptr, pointing to the pointer ready to receive the message
MSG_SZ, accepts the size of the message, does not include the first variable that represents a type
Msgtype, Acceptance type
MSGFLG, which controls what happens when no message is received, is typically set to 0
5, int msgctl (int msqid,int commond,struct msqid_ds * buf);
Structmsqid_ds {
uid_t Msg_perm.uid;
uid_t Msg_perm.gid;
mode_t Msg_perm.mode;
}
http://blog.csdn.net/coderlrx/article/details/8762017
Day 23rd: Shared memory. Message Queuing and MySQL database usage