Professional Documents
Culture Documents
03b Ch3 Process IPC v.5
03b Ch3 Process IPC v.5
Inter Process
Communication (IPC)
Chapter 3: Processes - IPC
● Process Concept
● Interrupts and Processes
● Process Data
● Process Scheduling
● Operations on Processes
● Interprocess Communication
● Examples of IPC Systems
● Communication in Client-Server Systems
In order to explain the principles behind Processes and the manner in which they are
dealt with by the OS - we restrict ourselves in this presentation to:
• Using a single CPU system. The issue of running processes in a multiprocessor
systems will be dealt with later in the course.
• Dealing with single-threaded processes. The issue of multi-threaded processes
will be dealt with later in the course.
Signal(s
em)
Communication
Client Service
Medium
Shared
memory
Shared Memory
IPC
void *ptr; /* pointer to shared memory object */ A successful call to shm open()
returns an integer file descriptor
for the shared-memory object.
/* create the shared memory object */
shm_fd = shm_open(name, O_CREAT | O_RDRW, 0666);
/* memory map the shared memory object */ mmap() function establishes a memory-mapped file
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd,
containing the0);
shared-memory object.
It also returns a pointer to the memory-mapped file
/* write to the shared memory object */ that is used for accessing the shared-memory object.
sprintf(ptr,"%s",message_0);
ptr += strlen(message_0);
sprintf(ptr,"%s",message_1);
© Dr Yigal Hoffner 4 May 2020 12
Consumer process illustrating POSIX shared-memory API
int main() { // CONSUMER side
/* the size (in bytes) of shared memory object */
const int SIZE 4096;
● Advantages:
● No need to involve OS – no system calls, except during setup
● Transfer can be uni or bi-directional
● Can be used for data transfer or synchronization
● Can pass any type of data
● Disadvantages:
● Lack of in-built synchronisation: no synchronisation is offered by the
mechanism
● If synchronisation is needed – it can be provided by using the shared memory,
but it will be based on spinning (busy-waiting)
● So anything based on suspension will require a proper mechanism, such as
semaphores or locks, for example
network
message next_produced;
while (true) {
message next_consumed;
while (true) {
receive(next_consumed);
BUFFER_SIZE-1 y z
0 1
2
buffer
Flag
Buffer
Producer Consumer
● Sockets
● Remote Procedure Calls (example: Remote Method Invocation in Java)
● All ports below 1024 are well known, used for standard services
● The steps involved in establishing a socket on the server side are as follows:
● Create a socket with the socket() system call
● Bind the socket to an address using the bind() system call. For a server socket on the
Internet, an address consists of a port number on the host machine.
● Listen for connections with the listen() system call
● Accept a connection with the accept() system call. This call typically blocks until a client
connects with the server.
● Receive and Send data
import java.net.*;
import java.io.*;
public class DateClient IP address
{ Por
public static void main(String[] args) { t
try {
/* make connection to server socket */ client
Socket sock = new Socket("127.0.0.1", 6013); request
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
s s
t t
u u
b b
CPU
CPU CPU 43
© Dr Yigal Hoffner 4 May 2020
Remote Procedure Calls (Cont.)
● 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
● Messages can be delivered exactly once rather than at most once
● OS typically provides a rendezvous (or matchmaker) service to connect
client and server
s s
Java t t C++
u u
b b
CPU CPU
(64 bit) (32 bit)
Flag
item next_consumed;
while (true) {
while (in == out); /* wait */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}