Lecture 4 Code examples on csserver in directory /home/hwang/cs470/lecture04 Homework 1 posted, due next Wednesday Questions? Wednesday, January 18 CS 470 Operating Systems - Lecture 4 1 Outline Message passing Message queues Cross-network IPC Wednesday, January 18 CS 470 Operating Systems - Lecture 4 2 Shared 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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 3 Message 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 Synchronization 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 program Wednesday, January 18 CS 470 Operating Systems - Lecture 4 4 Message 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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 5 Message 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? Wednesday, January 18 CS 470 Operating Systems - Lecture 4 6 Message Passing If indirect, are mailboxes stored/owned in process space or in OS space? What are the differences? Process A Process B OS Kernel Wednesday, January 18 Process A M Process B OS Kernel CS 470 Operating Systems - Lecture 4 M 7 Message 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 rendezvous (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 blocks Wednesday, January 18 CS 470 Operating Systems - Lecture 4 8 Message 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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 9 Producer/Consumer Processes Producer Consumer Item nextP; Item nextC; while (true) { nextP = MakeItem(); while(numItems == BUFSIZE) ; // do nothing buffer[in] = nextP; in = (in+1) % BUFSIZE; numItems++; } while (true) { while(numItems == 0) ; // do nothing nextC = buffer[out]; out = (out+1) % BUFSIZE; numItems­­; UseItem(nextC); } Wednesday, January 18 CS 470 Operating Systems - Lecture 4 10 System V Message Queues An example implementation using System V (SysV) message queues is shown in file shmprod-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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 11 System 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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 12 System 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 queue id=msgget(19,IPC_CREAT|IPC_EXCL|0660); // Access existing queue by name id=msgget(19, 0); // Create unnamed queue id=msgget(IPC_PRIVATE, 0660); Wednesday, January 18 CS 470 Operating Systems - Lecture 4 13 System 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. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 14 System 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( )). Wednesday, January 18 CS 470 Operating Systems - Lecture 4 15 System V Message Queues The type parameter allows the receiver to read the next message of a particular type. If type is 0, the first message is read. If type is greater than 0, the first message in the queue of that type is read. (Unless flg contains MSG_EXCEPT, then the first message NOT of that type is read.) Wednesday, January 18 CS 470 Operating Systems - Lecture 4 16 System V Message Queues If type is less than 0, the first message in the queue with the lowest type less than or equal to the absolute value of type is read. The flg parameter is usually either 0 or IPC_NOWAIT (if you do not want msgrcv( ) to block until a message becomes available). Wednesday, January 18 CS 470 Operating Systems - Lecture 4 17 System V Message Queues The msgctl( ) routine is used to change permissions on a queue, get information about the queue, and to delete the queue: // Remove the queue ret = msgctl(id, IPC_RMID, 0); Unlike shared memory, the queue is removed immediately. Any processes that are waiting on the queue are awakened and receive an error return. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 18 Cross-Network IPC Most system implement the POSIX socket interface to provide the low-level mechanism for connecting two processes and transferring data. Sockets are tricky to use. Again abstract up a level and provide remote procedure calls (RPCs) or remote method invocation (RMI). Wednesday, January 18 CS 470 Operating Systems - Lecture 4 19 Remote Procedure Call (RPC) An RPC looks like a regular call, but is just a stub that wrap system calls. Some issues: How to marshal/unmarshal (value) parameters /return values, that is, convert data from local representation to an external data representation (XDR). In particular, linked structures need to be "flattened" and reassembled. How to find the port of the remote procedure. Fixed names or a matchmaker process. How to handle failure, both of procedure and network. At most once vs. exactly once semantics. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 20 Remote Procedure Call (RPC) Client Server P(arg1, ...) void P (int arg1,...) Arguments Arguments Client stub Server stub Marshalled arguments Marshalled arguments RPC system call RPC system call Data Wednesday, January 18 CS 470 Operating Systems - Lecture 4 21 Remote Method Invocation (RMI) An RMI (used in Java) is similar to an RPC. In addition, references to non-local objects can be sent as parameters. Wednesday, January 18 CS 470 Operating Systems - Lecture 4 22