Principles of Operating Systems: Lecture 5 - Interprocess Communication Ardalan Amiri Sani
Principles of Operating Systems: Lecture 5 - Interprocess Communication Ardalan Amiri Sani
Operating Systems
Lecture 5 - Interprocess Communication
Ardalan Amiri Sani (ardalan@uci.edu)
[lecture slides contains some content adapted from : Silberschatz textbook authors]
Interprocess Communication
● Processes within a system may be independent or cooperating
● Cooperating process can affect or be affected by other processes,
including sharing data
● Reasons for cooperating processes:
● Information sharing
● Computation speedup
● Modularity
● Convenience
● Cooperating processes need interprocess communication (IPC)
● Two models of IPC
● Shared memory
● Message passing
Interprocess Communication – Shared Memory
● Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Bounded-Buffer – Producer
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
● How many elements in the buffer can be used at most at a given time?
Producer Consumer
Bounded-Buffer – Shared-Memory Solution
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced); Producer
}
● Sockets
● Remote Procedure Calls
● Pipes
● Remote Method Invocation (Java)
Sockets
● All ports below 1024 are well known, used for standard
services
if (pipe(fd) == -1) {
/* handle error */
}
pid = fork();
if (pid < 0) {
/* handle error */
}