Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Message queues


Published on

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

Published in: Education
  • Be the first to comment

Message queues

  1. 1. UNIX MESSAGE QUEUES Reference:Rohit Sinha Unix System Programming2BV07CS081 Using C++ -- Terrence Chan
  2. 2. MESSAGE QUEUES• Linked list of messages• Message queue table in kernel address space• Identified by message queue identifier
  3. 3. KERNEL DATA STRUCTURE •A name which Struct msqid_dsis anA MessageKey integer ID Queue {•A creator user ID and group ID •Assigned owner user ID and group ID struct ipc_perm msg_perm; •Read-write access permission msgqnum_t msg_qnum; of the queue for owners, msglen_t msg_qbytes; Message Record others group members and pid_t msg_lspid; •Last read time and process ID pid_t msg_lrpid; •Last write time and process ID time_t msg_stime •Pointer to a linked list of message time_t msg_rtime records. time_t msg_ctimeMessage Table : : }
  4. 4. Kernel Handling Message Queue Sender Process •Message type •No of bytes of message ReceiverMessage Table •Pointer to Process actual message data Actual Message
  5. 5. SYSTEM-IMPOSED LIMITS <sys/msg.h> SYSTEM LIMIT Meaning MSGMNI The maximum number of message queue that may exists at any given time in a system MSGMAX The maximum number of bytes of data allowed for a message MSGMNB The maximum number of bytes of all messages allowed in a queue MSGTQL The maximum number of messages in all queues allowed in a system
  6. 6. APIs FOR MESSAGE MANIPULATION #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h>MESSAGE API USES msgget Open and create if needed, a message queue for access msgsnd Sends a message to message queue msgrcv Receives a message from message queue msgctl Manipulate the control data of a message queue
  7. 7. MSGGET• Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgget (key_t key, int flag); Returns -1 when fails• 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.
  8. 8. MSGSND• Prototype is a pointer to an object that contains the actual messagemsgPtr: ittext and #include <sys/types.h> be sent and message type to #include <sys/ipc.h> #include <sys/message.h> int msgsnd (int msgfd, const void* msgPtr, int len, int flag); struct msgbuf Returns -1 when fails 0 on success {• Send a message pointed by msgPtr to a message long mtype queue designated message type // by the msgfd descriptor char mtext[MSGMAX]• The len value is the sizehold bytes of the message // buffer to in the message text }• 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)
  9. 9. MSGRCV • Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h>If there is message in queuemsgfd, const void* msgPtr, criteria but mtype, int flag); the int msgrcv (int that satisfies the selection int len, int is larger than len, Success: Returns number of bytes writtenfunction returns a failure status • Receive a message of type mtype from a message queueIf MSG_NOERROR is set in the flag value, a message-1 queue is selectable Fail: Returns in the designated by the msgfd descriptor in an object pointed by the msgPtrirrespective of length. • 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)
  10. 10. mtype Values (type of the message to be received) mtype values Meaning 0 Receive the oldest message of any type in the queuePositive integer Receive the oldest message of specified message typeNegative integer Receive a message whose message type is less than or equal to the absolute value of mytpe. If more than 1 receive the oldest and the smallest.
  11. 11. MSGCTL• Prototype #include <sys/types.h> #include <sys/ipc.h> #include <sys/message.h> int msgctl (int msgfd, int cmd, struct msqid_ds* mbufPtr); Fail: returns -1• Used to query the control data of a message queue designated by the msgfd:argument 0 Success returns• To change the information within the control data of the queue• To remove a message queue
  12. 12. cmd Values cmd value Meaning IPC_STAT Copy control data of the queue to the object pointed by the mbufPtr IPC_SET Change the control data of the queue by those specified in the object pointed by mbufPtr IPC_RMID Remove the queue from the systemNote: The calling process for IPC_SET and IPC_RMID must be superuser, creator or the assigned owner of the queue.
  14. 14. THANK YOU• Queries ? – Just Google it 