Lecture 9 - Unix Message Queues and Remote Procedure Call

## Unix Interprocess Communication Mechanisms ### Introduction to Sockets Sockets are commonly employed for communication between independent processes and are extensively used across the Internet in scenarios where shared data structures or files are not an option. One can visualize a communication channel as a pair of interconnected endpoints. ### Program Abstraction for Endpoint Binding Sockets function as abstractions that applications create to serve as connection resources within a communication subsystem. For data exchange to occur from the application's address space to the communication subsystem's address space, sockets must be associated with an endpoint in the communication system. The socket interface API plays a crucial role in facilitating data transfer among processes. ### TCP/IP Communication Protocol To establish reliable communication over the Internet, all network points must utilize a compatible messaging protocol, specifically TCP/IP in this context. This protocol suite supports a variety of applications and guarantees that communication is appropriately initiated. ### Network Subsystem The communication service, such as TCP/IP, connects sockets belonging to different processes, possibly across diverse machines, and routes messages throughout the network. Identification mechanisms, involving the use of IP addresses and port numbers, are utilized to accurately identify connection endpoints. ### Reliability Levels in Socket Communication When establishing sockets, varying levels of reliability can be selected, enabling preferences such as favoring speed over reliability. The two primary types of sockets include datagram, characterized as connectionless for less dependable transmission, and connection-oriented, which establishes links for assured data transfer. ### Socket-based Client/Server Communication In the context of socket-based communication, a socket is designated as passive to indicate its role as an entity ready to accept incoming connection requests. Communication requires that the socket be tied to a specific named IP port. ### Remote Procedure Call (RPC) RPC is considered a higher-level abstraction that streamlines client-server interaction by allowing remote function calls that closely resemble local calls. This process employs a stub mechanism to automate the creation of communication stubs derived from an RPC interface that outlines data exchange procedures. During this process, client RPC stubs encapsulate arguments within messages directed to the server, which decodes them and calls the corresponding procedure before sending the results back to the client. ### RPC Mechanisms and Considerations RPC mechanisms typically involve port addressing, requiring RPC stubs to track the server's port address by often utilizing a directory service for assistance. However, complications can arise when transmitting pointer-based structures across distinct address spaces, as they cannot be correctly evaluated in a different context. Moreover, discrepancies in data formats may surface when clients and servers are built on different architectures or programming languages, necessitating translation of data formats for accurate interpretation. ### RPC and Messaging Failure Semantics In terms of fault management for RPC, different execution semantics are implemented. Available options include "at least once," which necessitates clients to preserve requests for potential retransmission; "at most once," which utilizes sequence numbers to oversee request-response pairings allowing for a single retransmission after acknowledgment; and the "exactly once" approach, which entails maintaining stable storage of requests and responses to ensure that effects are preserved even after server crashes. ### Message Queues in IPC Message queues allow for asynchronous message exchange without requiring a rendezvous, thereby enabling tasks to be arranged according to priority. The operating system oversees the persistence of message queues until they are intentionally removed by a user or deleted upon system shutdown. ### Message Queue API Functions Key APIs for managing message queues include `mq_open()` for creating or accessing a queue, `mq_send()` for transmitting messages, `mq_receive()` for reading messages, `mq_close()` for shutting down the use of a queue, and `mq_unlink()` for marking a queue for deletion once all processes have been closed. ### Internal Structure of Message Queues Each message queue comprises characteristics that include maximum message size and the current count of messages, which can be queried using `mq_getattr()`. ### Creating Message Queues in C To establish message queues in C, implementation typically involves using `strcpy()` for assigning the queue name while defining permission and attribute flags during the queue opening process. ### Sending Messages to Queues Messages are dispatched as data buffers to a designated opened message queue, which entails defining both the size and priority of the message. ### Receiving Messages from Queues The receiving process can utilize `mq_receive()` to extract messages and ascertain the total number of bytes retrieved. ### Closing and Unlinking Queues The closure of a queue can be achieved by invoking `mq_close()` to cease usage, followed by `mq_unlink()` for complete destruction when conditions allow. ### Required Headers for Message Queues in C In the C programming language, essential headers to be included comprise ``, ``, and ``. It is critical to compile using the `-lrt` option to ensure proper linking of the real-time library. ### Summary of IPC Mechanisms In summary, different interprocess communication mechanisms such as pipes, named pipes, sockets, remote procedure calls, and message queues offer various functionalities and advantages. Pipes provide rapid and memory-based communication for related processes, while named pipes facilitate interaction for unrelated processes using shared file structure. Sockets serve as low-level communication tools via TCP/IP, RPC simplifies intricate client-server programming tasks, and message queues allow for asynchronous communication organized by priority.