DOC PREVIEW
UE CS 470 - Lecture 6

This preview shows page 1-2-20-21 out of 21 pages.

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

Unformatted text preview:

Lecture 6OutlineShared MemoryMessage Passing 1Message Passing 2Message Passing 3Message Passing 4Message Passing 5Message Passing 6Producer/Consumer ProcessesSystem V Message Queues 1System V Message Queues 2System V Message Queues 3System V Message Queues 4System V Message Queues 5System V Message Queues 6System V Message Queues 7System V Message Queues 8Cross-Network IPC 1Cross-Network IPC 2Cross-Network IPC 3Monday, January 24 CS 470 Operating Systems - Lecture 6 1Lecture 6Code examples on csserver in directory /home/hwang/cs470/lecture06Reminder: Homework 1 due on WednesdayQuestions?Monday, January 24 CS 470 Operating Systems - Lecture 6 2OutlineMessage passingMessage queuesCross-network IPCMonday, January 24 CS 470 Operating Systems - Lecture 6 3Shared MemoryRecall that in shared memory, cooperating processes map same physical shared memory segment into their logical memory.This is very efficient, since access is the same as for non-shared memory. But using shared memory requires synchronization (Chapter 6).Like all software engineering problems, we can solve this problem by adding a layer of abstraction.Monday, January 24 CS 470 Operating Systems - Lecture 6 4Message PassingThe message passing technique for providing IPC has the following characteristics:Messages are used to exchange informationEach send/receive is a system callSynchonization is provided automatically, since processes do not share space.Since messages are copied, they are mostly used for small amounts of data exchange.They are especially good for distributed communication. E.g., a chat programMonday, January 24 CS 470 Operating Systems - Lecture 6 5Message PassingThere are at least two operations: send and receive. Lots of design issues in implementing message passing.Are the messages fixed size or variable sized? Advantage/disadvantages of each? Is communication direct or indirect? Direct is like sending mail to a specific address. Indirect is like sending mail to a post office box.Monday, January 24 CS 470 Operating Systems - Lecture 6 6Message PassingDirect - "name" is given in calls: send (P, msg) or receive (Q, msg). Why might this be useful?Indirect - "mailbox" (or "port") is created that is used in calls: send (M, msg) or receive (M, msg). How does this behave differently than using names?Monday, January 24 CS 470 Operating Systems - Lecture 6 7Message PassingIf indirect, are mailboxes are stored/owned in process space or in OS space? What are the differences?Process AProcess B M OS KernelProcess AProcess B OS Kernel MMonday, January 24 CS 470 Operating Systems - Lecture 6 8Message PassingIs communication synchronous or asynchronous? Either for send or receive. Can be any of the four possible combinations. In particular, if both send and receive block, have a rendevous (implemented directly by Ada tasks).How is message storage handled? Is there a fixed size buffer? Is buffering automatic?Zero capacity - no waiting messagesBounded - sender blocks when queue is fullUnbounded - sender never blocksMonday, January 24 CS 470 Operating Systems - Lecture 6 9Message PassingMach OS uses message passing for all IPC including system calls to the OS. The main issue is that local communication is slow because data must be transferred from user space to kernel space and back.Solve this by using shared memory to implement mailboxes. The sender's message area is mapped into the receiver's message area.Monday, January 24 CS 470 Operating Systems - Lecture 6 10Producer/Consumer ProcessesProducerItem nextP;while (true) { nextP = MakeItem(); while(numItems == BUFSIZE) ; // do nothing buffer[in] = nextP; in = (in+1) % BUFSIZE; numItems++;}ConsumerItem nextC;while (true) { while(numItems == 0) ; // do nothing nextC = buffer[out]; out = (out+1) % BUFSIZE; numItems--; UseItem(nextC);}Monday, January 24 CS 470 Operating Systems - Lecture 6 11System V Message QueuesAn example implementation using System V (SysV) message queues is shown in file shm-prod-cons.cppSystem V message queue routines are defined in library <sys/msg.h>. They have syntax similar to the shared memory routines.Both the producer (parent process) and consumer (child process) are in this file, and it uses signal handlers to clean up before exit.Message queue is used as the buffer.Monday, January 24 CS 470 Operating Systems - Lecture 6 12System V Message QueuesSystem V message queues are owned by the OS, and have automatic bounded buffering. Maximum queue size is set by the system, but can be modified by the superuser. Default is synchronous, but can be made asynchronous.Programmer defines the format of a message as a struct. It must start with a long that stores the type of the message followed by the message data which must consist of plain types. E.g., the Message struct.Monday, January 24 CS 470 Operating Systems - Lecture 6 13System V Message QueuesThe msgget( ) routine is used to create a message queue and has prototype:int msgget(key_t key, int flags);The routine returns a queue id. Examples:// Create a named queueid=msgget(19,IPC_CREAT|IPC_EXCL|0660);// Access existing queue by nameid=msgget(19, 0);// Create unnamed queueid=msgget(IPC_PRIVATE, 0660);Monday, January 24 CS 470 Operating Systems - Lecture 6 14System V Message Queuesmsgsnd( ) adds a message to the queue; its prototype is:void msgsnd(int id, void *msg, size_t len, int flg);id is the queue identifier. msg must be a pointer to a message struct. len is the size of the data portion of the msg struct in bytes. Note that len does not include the size of the type field. flg is usually 0 or IPC_NOWAIT. By default msgsnd( ) will block if the queue is full unless the IPC_NOWAIT flag is set.Monday, January 24 CS 470 Operating Systems - Lecture 6 15System V Message Queuesmsgrcv( ) is used to get a message and has prototype:void msgrcv(int id, void *msg, size_t len, size_t type, int flg);id is the queue identifier. msg must be a pointer to a message struct just as for msgsnd( ). len is the size of the data area in the receiving struct. If the message is longer than len, the message is removed from the queue and the call fails. (This can be modified via msgctl( )).Monday, January 24 CS 470 Operating Systems - Lecture 6 16System V Message QueuesThe type parameter allows the receiver to read the next


View Full Document
Download Lecture 6
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 Lecture 6 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 Lecture 6 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?