3b-Interprocess Communication
3b-Interprocess Communication
1
Inter-process Communication
• Processes within a system may be independent or cooperating.
• The communication is under the control of the users’ process and not the OS.
4
IPC – Message Passing Model
• Processes communicate with each other without resorting to shared variables
• Control Information
• What to do if run out of buffer space
• Sequence numbers
• Priority
6
Implementation Questions
• How are links established?
• How many links can there be between every pair of communicating processes?
• Is the size of a message that the link can accommodate fixed or variable?
8
Indirect Communication
• Messages are directed and received from mailboxes (also referred to as ports)
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
• Operations:
• create a new mailbox
• send and receive messages through mailbox
9
• destroy a mailbox
Indirect Communication
• Primitives are defined as:
• send (A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
12
Examples of IPC Systems – POSIX
• POSIX Shared Memory
• Process first creates shared memory segment
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
• Reading and writing to shared memory is done by using the pointer returned by
mmap(). 13
IPC POSIX Producer and Consumer
14
Examples of IPC Systems – Mach
• Mach communication is message based:
• Even system calls are messages.
• Each task gets two mailboxes at creation: Kernel and Notify.
• Messages are sent and received using the mach_msg() function
• Ports needed for communication, created via mach_port_allocate()
• Send and receive are flexible; for example, four options if mailbox full:
• Wait indefinitely
• Wait at most n milliseconds #include<mach/mach.h>
• Return immediately
• Temporarily cache a message struct message {
mach_msg_header_t header;
int data;
};
mach port t client; 15
mach port t server;
Mach Messages
Mach Message Passing – Client and
Server
16
Examples of IPC Systems – Windows
• Message-passing centric via advanced local procedure call (LPC) facility:
• Only works between processes on the same system.
• Uses ports (like mailboxes) to establish and maintain communication channels.
• Communication works as follows:
• The client opens a handle to the
subsystem’s connection port object.
• The client sends a connection request.
• The server creates two private
communication ports and returns the
handle to one of them to the client.
• The client and server use the
corresponding port handle to send
messages or callbacks and to listen for
replies. 17
• Some issues:
• Is communication unidirectional or bidirectional?
• In the case of two-way communication, is it half or full-duplex?
• Must there exist a relationship (i.e., parent-child) between the communicating
processes?
• Can the pipes be used over a network?
• Ordinary pipes – cannot be accessed from outside the process that created it.
Typically, a parent process creates a pipe and uses it to communicate with a
child process that it created.
18
• Named pipes – can be accessed without a parent-child relationship.
Ordinary Pipes
• Ordinary Pipes allow communication in standard producer-consumer style
• Consumer reads from the other end (the read-end of the pipe)
19
• Communication is bidirectional
20
Communication in Client-Server Systems
• Various mechanisms for communication between client-server systems
• Sockets
• Remote Procedure Calls (RPCs)
21
Sockets
• A socket is defined as an endpoint for communication.
• Concatenation of IP address and port.
• The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
• Communication consists between a pair of sockets
• All ports below 1024 are well known, used for standard services
• Special IP address 127.0.0.1 (loopback) to refer to system on which process is
running
22
Sockets in Java
• Three types of sockets
• Connection-oriented (TCP)
• Connectionless (UDP)
• MulticastSocket class– data can be sent to multiple recipients
23
Remote Procedure Calls (RPCs)
• RPC abstracts procedure calls between processes on a networked system.
• Uses ports for service differentiation
• Stubs – client-side proxy for the actual procedure on the server.
• The client-side stub locates the server and marshals the parameters.
• The server-side stub receives this message, unpacks the marshalled parameters,
and performs the procedure on the server.
• On Windows, stub code compile from specification written in Microsoft Interface
Definition Language (MIDL)
• Data representation handled via External Data Representation (XDL) format to
account for different architectures (Big-endian and little-endian)
• Remote communication has more failure scenarios than local
24
• Messages can be delivered exactly once rather than at most once
• OS typically provides a matchmaker service to connect client and server
Remote Procedure Call Mechanism
25
Execution of Remote Procedure Call
26