DOC PREVIEW
SMU CSE 8343 - Interprocess Communication

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Interprocess Communication 01 15 19 Mohamed M Khalil Ph D 1 Interprocess Communication Inteprocess communication mechanisms allow arbitrary processes to exchange data and synchronize execution 01 15 19 Mohamed M Khalil Ph D 2 Messages 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 write 01 15 19 Mohamed M Khalil Ph D 3 Messages Data Structure Queue Headers 01 15 19 Message Headers Mohamed M Khalil Ph D Data Area 4 Kernel 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 5 msgget 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 entry msgflg 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 6 msgsnd 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 id size the size of the message msgp it is of the following type struct 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 7 msgrcv 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 8 msgctl system call int 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 queue 01 15 19 Mohamed M Khalil Ph D 9 Get 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 10 Change 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 11 Get message queue state int remove queue int qid if msgctl qid IPC RMID 0 1 return 1 return 0 01 15 19 Mohamed M Khalil Ph D 12 msgsnd system call 1 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 13 msgsnd algorithm msgsnd Input 1 message queue descriptor 2 Address of message structure 3 Size of message 4 Flags Output number of bytes sent Check legality of descriptor permission While not enough space to store message If flags specify not to wait return sleep until event enough space is available Allocate space for data and header copy data from user space to kernel space wakeup all processes waiting waiting to read message from queue 01 15 19 Mohamed M Khalil Ph D 14 msgrcv system call 1 Check that the user has the necessary access rights to the message queue 2 If the requested message type is 0 the kernel finds the first message on the linked list 3 If the size of the message is less than or equal to the size requested by the user the kernel copies the message data to the user data structure …


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 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?