|
|
UNIX MESSAGE QUEUES Reference: Unix System Programming Using C++ -- Terrence Chan Rohit Sinha 2BV07CS081 http://www.rohitrocks.com
MESSAGE QUEUES Linked list of messages Message queue table in kernel address space Identified by message queue identifier
KERNEL DATA STRUCTURE Message Record A Message Queue Message Table A name which is an integer ID Key A creator user ID and group ID Assigned owner user ID and group ID Read-write access permission of the queue for owners, group members and others Last read time and process ID Last write time and process ID Pointer to a linked list of message records. Struct msqid_ds { struct ipc_perm msg_perm; msgqnum_t msg_qnum; msglen_t msg_qbytes; pid_t msg_lspid; pid_t msg_lrpid; time_t msg_stime time_t msg_rtime time_t msg_ctime : : }
Kernel Handling Message Queue Message Table Sender Process Receiver Process Sends a message Retrieves a message Message type No of bytes of message Pointer to actual message data Actual Message
SYSTEM-IMPOSED LIMITS <sys/msg.h>
APIs FOR MESSAGE MANIPULATION #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h>
MSGGET Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgget (key_t key, int flag); Opens a message queue with ID as key If the key value is a manifested constant IPC_PRIVATE the API allocates a special message queue to be used exclusively by the calling process. Flag can take two values: 0: API aborts if there is no message queue whose key ID matches the given key value otherwise return a descriptor for that queue. IPC_CREAT: Creates a new queue (if none exists) with given key ID and read-write access permission. Ex: int msgfdesc = msgget(15, IPC_CREAT|0644); IPC_EXCL ensures the API succeeds only if it creates a new msg queue. Returns -1 when fails
Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgsnd (int msgfd, const void* msgPtr, int len, int flag); Send a message pointed by msgPtr to a message queue designated by the msgfd descriptor The len value is the size in bytes of the message Two values of flag: 0: Process is blocked if needed until the process call is completed. IPC_NOWAIT: Function abort if the process is to be blocked ----- (EAGAIN) MSGSND msgPtr: it is a pointer to an object that contains the actual message text and and message type to be sent struct msgbuf { long mtype // message type char mtext[MSGMAX] // buffer to hold the message text } Returns -1 when fails 0 on success
Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgrcv (int msgfd, const void* msgPtr, int len, int mtype, int flag); Receive a message of type mtype from a message queue designated by the msgfd descriptor in an object pointed by the msgPtr The len value is the max size in bytes of the message that can be received Two values of flag: 0: Process is blocked if no message in queue matched the selection criteria specified by mtype IPC_NOWAIT: The call is non blocking (returns -1 with errno set to ENOMSG) If there is message in queue that satisfies the selection criteria but is larger than len, the function returns a failure status If MSG_NOERROR is set in the flag value, a message in the queue is selectable irrespective of length. MSGRCV Success: Returns number of bytes written Fail: Returns -1
mtype Values (type of the message to be received)
MSGCTL Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgctl (int msgfd, int cmd, struct msqid_ds* mbufPtr); Used to query the control data of a message queue designated by the msgfd argument To change the information within the control data of the queue To remove a message queue Fail: returns -1 Success : returns 0
cmd Values Note: The calling process for IPC_SET and IPC_RMID must be superuser, creator or the assigned owner of the queue.
CLIENT/SERVER EXAMPLE
THANK YOU Queries ? Just Google it
by talktorohit54 | Added: 2 years ago
Language: English | Topic: Education
| 33 Views | 22 Downloads | 3 Embeds |
Summary: This presentation deals with message queues as a part of inter process communication. The presentation introduces to the basic of message queues, how message queues are handled my a unix kernel and the API' related to message queues
| URL: |
No comments posted yet
Comments