NCHU System & Network LabNCHU System & Network Lab
Lab 10Lab 10Message Queue and Shared MemoryMessage Queue and Shared Memory
NCHU System & Network LabNCHU System & Network Lab
Message QueueMessage Queue
NCHU System & Network LabNCHU System & Network Lab
Message QueueMessage Queue
• provide a reasonably easy and efficient way of passing data between two unrelated processes
Process 1 process2
Message queue 1 message
NCHU System & Network LabNCHU System & Network Lab
Create and Access a Message Queue
#include <sys/msg.h>
int msgget(key_t key, int msgflg);key : names a particular message queue.msgflg: IPC_PRIVATE only used by process itself
IPC_CREAT create new message queue, if exist, ignore this flag
permission flags
r w x r w x r w x0 0 0
owner permission
group permission
others permission
Return queue identifier on success, -1 on failure
NCHU System & Network LabNCHU System & Network Lab
Send a Message to a message queue
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
msqid : message queue identifiermsg_ptr : a pointer to the message to be sent.msg_sz: size of the message pointed to by msg_ptr.
This size must not include the long int message type
msgflg: controls what happens if either the current message queue is full or the systemwide limit on queued messages has been reached
IPC_NOWAIT return -1 without sending message
If no IPC_NOWAIT, it will be suspended and waiting for space to become availablein the queue.
Return 0 on success, -1 on error.
NCHU System & Network LabNCHU System & Network Lab
MessageMessage
• The structure of the message must follow the following two rules.
struct my_message { long int message_type;/* The data you wish to transfer */}
1. smaller than the system limit.2. start with a long int.
NCHU System & Network LabNCHU System & Network Lab
retrieves messages from a message queue
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);
msqid : message queue identifier.msg_ptr: point to the message to be received.msg_sz: size of the message pointed to by msg_ptr.msgtype: a simple form of reception priority. 0 retrieves first available message. >0 retrieves first with the same type message. <0 retrieves the same type or less than it message.
msgflg: controls what happens when no message of the appropriate type is waiting to be received IPC_NOWAIT return -1 without sending messageIf no IPC_NOWAIT, it will be suspended and waiting for an appropriate type of message arrived in queue.
return number of bytes placed in the receive buffer, -1 on error.
NCHU System & Network LabNCHU System & Network Lab
Control functionControl function
int msgctl(int msqid, int command, struct msqid_ds *buf);
msqid: message queue identifier.command:
IPC_STAT Sets the data in the msqid_ds structure to reflect the values associated with the message queue.
IPC_SET If the process has permission to do so, this sets the values associated with the message queue to those provided in the msqid_ds data structure.
IPC_RMID Deletes the message queue.
buf: save the information of msqid.
return 0 on success, -1 on error.
NCHU System & Network LabNCHU System & Network Lab
msqid_dsmsqid_ds
struct msqid_ds { uid_t msg_perm.uid; uid_t msg_perm.gid; mode_t msg_perm.mode;}
NCHU System & Network LabNCHU System & Network Lab
Example: retrieve and showExample: retrieve and show
NCHU System & Network LabNCHU System & Network Lab
Lab ILab I• Use message queue write a program that
– Show the used message queue information• creator id and permission mode number
– Two processes can chat with each other– Just like…
uid:1 Pmode:0666P1:hello!!P2:Hi~!P2:how are you ?P1:so so.Input: _
uid:1 Pmode:0666P1:hello!!P2:Hi~!P2:how are you ?P1:so so.Input: _
Process 1 Process 2
NCHU System & Network LabNCHU System & Network Lab
Shared MemoryShared Memory
NCHU System & Network LabNCHU System & Network Lab
Shared MemoryShared Memory
• Two or multiple processes share the same memory region.
• The fastest form of IPC
Process 1 Process 2Shared memory
Two processes share the same memory region
NCHU System & Network LabNCHU System & Network Lab
Obtain a shared memory id.Obtain a shared memory id.
#include <sys/shm.h>
int shmget(key_t key, size_t size, int flag);
size : the size of the shared memory segment in bytes.flag :
IPC_PRIVATE only used by process itselfIPC_CREAT create new message queue, if exist, ignore this flag permission flags
return shared memory on success, -1 on error.
r w x r w x r w x0 0 0
owner permission
group permission
others permission
NCHU System & Network LabNCHU System & Network Lab
attachmentattachment
# include <sys/types.h># include <sys/ipc.h># include <sys/shm.h>char *shmat ( int shmid, char *shmaddr, int shmflg )
shmid: shared memory identifiershmaddr: the address at which the shared memory is to be attached to the current process. This should almost always be a null pointershmflg : SHM_RND indicates that the address specified for the second parameter
should be rounded down to a multiple of the page size. SHM_RDONLY indicates that the segment will be only read, not written.
return -1 on error, the address of the attached shared memory segment for success
NCHU System & Network LabNCHU System & Network Lab
detachmentdetachment# include <sys/types.h># include <sys/ipc.h># include <sys/shm.h>
int shmdt ( char *shmaddr)
return 0 on success, -1 on error.
NCHU System & Network LabNCHU System & Network Lab
Controlling and Deallocating Shared MControlling and Deallocating Shared Memoryemory
#include <sys/ipc.h>#include <sys/shm.h>int shmctl(int shm_id, int command, struct shmid_ds *buf);
shm_id : shared memory identifier
command:IPC_STAT copy the information about the shared memory
segment into the buffer bufIPC_SET apply the changes the user has made to the
uid, gid, or mode members of the shm_perms field.
IPC_RMID mark the segment as destroyed.
buf : save the information of shmid.Return 0 on success, -1 on error
NCHU System & Network LabNCHU System & Network Lab
shmid_ds structureshmid_ds structurestruct shmid_ds { struct ipc_perm shm_perm; /* operation perms */ int shm_segsz; /* size of segment(bytes) */ time_t shm_atime; /* last attach time */ time_t shm_dtime; /* last detach time */ time_t shm_ctime; /* last change time */ unsigned short shm_cpid; /* pid of creator */ unsigned short shm_lpid; /* pid of last operator */ short shm_nattch; /* no. of current attaches */ /* the following are private */ unsigned short shm_npages; /* size of segment (pages) */ unsigned long *shm_pages; struct shm_desc *attaches; /* descriptors for attaches */};
NCHU System & Network LabNCHU System & Network Lab
ipc_perm structureipc_perm structurestruct ipc_perm { key_t key; ushort uid; /* owner euid and egid */ ushort gid; ushort cuid; /* creator euid and egid */ ushort cgid; ushort mode; /* lower 9-bits of access modes */ ushort seq; /* sequence number */};
NCHU System & Network LabNCHU System & Network Lab
Lab ILab I
• Use shared memory write a program that– Show the information of shared memory
• pid of creator and last attach time.
– Two processes can chat with each other
pid:2 last: xxxxP1:hello!!P2:Hi~!P2:how are you ?P1:so so.
Input: _
pid:2 last: xxxxP1:hello!!P2:Hi~!P2:how are you ?P1:so so.
Input: _
Process 1 Process 2
NCHU System & Network LabNCHU System & Network Lab
ReferenceReference•Advanced Programming in the UNIX Environment 2nd
Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley
•Beginning Linux ProgrammingAuthor : Richard Stones, Neil Matthew Publisher : Wrox
•Operating System Concepts 6th edition Author : Abraham Silberschatz, Peter Baer Galvin, Greg Gagne Publisher : John Wiley & Sons, inc.
•Google•LINUX MAN PAGES ONLINE
Top Related