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 6Code examples on csserver in directory /home/hwang/cs470/lecture06Reminder: Homework 1 due on WednesdayQuestions?Monday, January 24 CS 470 Operating Systems - Lecture 6 2OutlineMessage passingMessage queuesCross-network IPCMonday, January 24 CS 470 Operating Systems - Lecture 6 3Shared MemoryRecall 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 PassingThe message passing technique for providing IPC has the following characteristics:Messages are used to exchange informationEach send/receive is a system callSynchonization 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 PassingThere 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 PassingDirect - "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 PassingIf 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 PassingIs 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 messagesBounded - sender blocks when queue is fullUnbounded - sender never blocksMonday, January 24 CS 470 Operating Systems - Lecture 6 9Message PassingMach 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 ProcessesProducerItem 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 QueuesAn example implementation using System V (SysV) message queues is shown in file shm-prod-cons.cppSystem 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 QueuesSystem 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 QueuesThe 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 Queuesmsgsnd( ) 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 Queuesmsgrcv( ) 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 QueuesThe type parameter allows the receiver to read the next
View Full Document