Unformatted text preview:

Interprocess CommunicationSlide 2Messages Data StructureSlide 4Kernel msqid_ds structuremsgget system callmsgsnd system callmsgrcv system callmsgctl system callGet message queue stateChange queue modeSlide 12Slide 13msgsnd algorithmSlide 15msgrcv algorithmmsgsnd & msgrcv exampleShared Memoryshmget system callshmat system callshmdt system callshmctl system callShared memory exampleShared memory (continue)01/15/19 Mohamed M Khalil, Ph.D. 1Interprocess Communication01/15/19 Mohamed M Khalil, Ph.D. 2Interprocess CommunicationInteprocess communication mechanisms allow arbitrary processes to exchange data and synchronize execution.01/15/19 Mohamed M Khalil, Ph.D. 3Messages Data Structure•Pointer to the first and last messages on the linked list.•The number of messages.•The total number of bytes in the queue.•The maximum number of messages allowed in the queue.•Permission field for read and write01/15/19 Mohamed M Khalil, Ph.D. 4Messages Data StructureQueue HeadersMessage HeadersData Area01/15/19 Mohamed M Khalil, Ph.D. 5Kernel msqid_ds structure/* one msqid structure for each queue on the system */ struct msqid_ds { struct ipc_perm msg_perm; struct msg *msg_first; /* first message on queue */ struct msg *msg_last; /* last message in queue */ time_t msg_stime; /* last msgsnd time */ time_t msg_rtime; /* last msgrcv time */ time_t msg_ctime; /* last change time */ struct wait_queue *wwait; struct wait_queue *rwait; /* They are used when an operation on a message queue deems the process go into a sleep state*/ ushort msg_cbytes; /* sum of the sizes of all messages */ ushort msg_qnum; /* maximum number of messages in q*/ ushort msg_qbytes; /* max number of bytes on queue */ ushort msg_lspid; /* pid of last msgsnd */ ushort msg_lrpid; /* last receive pid */ };01/15/19 Mohamed M Khalil, Ph.D. 6msgget system call#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>A message queue is allocated by a msgget system call : msqid = msgget (key_t key, int msgflg); key: an integer or IPC_PRIVATE to assure the return of a a new entrymsgflg: IPC_CREAT : used to create a new resource if it does not already exist. IPC_EXCL | IPC_CREAT : used to ensure failure of the call if the resource already exists. rwxrwxrwx : access permissions. returns: msqid (an integer used for all further access) on success. -1 on failure.A message queue is allocated if there is no resource corresponding to the given key. The access permissions specified are then copied into the msg_perm struct and the fields in msqid_ds initialized. The user must use the IPC_CREAT flag or key = IPC_PRIVATE, if a new instance is to be allocated. If a resource corresponding to key already exists, the access permissions are verified.01/15/19 Mohamed M Khalil, Ph.D. 7msgsnd system call#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>msgsnd (int id, struct msgbuf *msgp, int size, int flag)Id: the message idsize: the size of the message msgp: it is of the following typestruct msgbuf{long msgtype;char mtext [];}flag: IPC_NOWAIT bit is of in flag. msgsend sleeps if the number of bytes in the message queue exceeds the maximum, or if the number of messages exceeds the maximum number of the system. msgsend returns immediately.01/15/19 Mohamed M Khalil, Ph.D. 8msgrcv system call#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>int msgrcv (int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg); msqid : id obtained by a call to msgget. msgsz : maximum size of message to receive. msgp : allocated by user to store the message in. msgtyp : = 0 => get first message on queue. > 0 => get first message of matching type. < 0 => get message with least type which is <= abs(msgtyp). msgflg : IPC_NOWAIT : Return immediately if message not found. MSG_NOERROR : The message is truncated if it is larger than msgsz. MSG_EXCEPT : Used with msgtyp > 0 to receive any msg except of specified type.returns : size of message if found. -1 on error. •.01/15/19 Mohamed M Khalil, Ph.D. 9msgctl system callint msgctl (int msqid, int cmd, struct msqid_ds *buf); msqid : id obtained by a call to msgget. buf : allocated by user for reading/writing info. cmd : IPC_STAT, IPC_SET, IPC_RMID. IPC_STAT, results in the copy of the queue data structure into the user supplied buffer. IPC_SET, the queue size (msg_qbytes) and the uid, gid, mode (low 9 bits) fields of the msg_perm struct are set from the user supplied values. msg_ctime is updated. Note that only the super user may increase the limit on the size of a message queue beyond MSGMNB. IPC_RMID, remove resource. The user must be the owner, creator or super-user. command results in immediate removal of a message queue01/15/19 Mohamed M Khalil, Ph.D. 10Get message queue state/* results in copying the message queue data structure into buffer */int get_queue_ds( int qid, struct msgqid_ds *qbuf ) { if( msgctl( qid, IPC_STAT, qbuf) == -1) { return(-1); } return(0); }01/15/19 Mohamed M Khalil, Ph.D. 11Change queue mode/* The only modifiable item in the data structure is the ipc_perm member. The mode must be passed in as a character array (i.e. ``660''). */int change_queue_mode( int qid, char *mode ) { struct msqid_ds tmpbuf; /* Retrieve a current copy of the internal data structure */ get_queue_ds( qid, &tmpbuf); /* Change the permissions using an old trick */ sscanf (mode, "%ho", &tmpbuf.msg_perm.mode); /* Update the internal data structure */ if( msgctl( qid, IPC_SET, &tmpbuf) == -1) { return(-1); } return(0); }01/15/19 Mohamed M Khalil, Ph.D. 12Get message queue stateint remove_queue( int qid ) { if( msgctl( qid, IPC_RMID, 0) == -1) { return(-1); } return(0); }01/15/19 Mohamed M Khalil, Ph.D. 13msgsnd system call1. Check that the sending process has the right permission for the message descriptor. 2. Check that the message length doesn’t exceed the system limits.3. The type is positive integer.4. The length of the queue doesn’t have to many bytes.5. If all test succeeds, the kernel allocates space for the message, and copy data from user space to system space.6. Kernel allocates a space for the header and place the message at the end of the queue. Sets the message header to point ot the message data.7. The kernel then awaken all processes waiting for the message to be available.01/15/19 Mohamed M Khalil, Ph.D. 14msgsnd algorithmmsgsndInput (1) message


View Full Document
Download Interprocess Communication
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Interprocess Communication and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Interprocess Communication 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?