osys 6.slides

12
1 urbino worldwide campus applied computer science Operating Systems 02.04 Lesson 6 emanuele lattanzi isti information science and technology institute 1/23 Operating Systems 02.04 Communication between processes urbino worldwide campus applied computer science Operating Systems 02.04 Lesson 6 emanuele lattanzi isti information science and technology institute 2/23 Cooperating Processes Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up – Convenience

Upload: nadia-baker

Post on 29-Jan-2016

212 views

Category:

Documents


0 download

DESCRIPTION

Operating Systems

TRANSCRIPT

Page 1: Osys 6.Slides

1

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 1/23

Operating Systems

02.04 Communication between processes

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 2/23

Cooperating Processes

• Independent process cannot affect or be affected by the execution of another process

• Cooperating process can affect or be affected by the execution of another process

• Advantages of process cooperation– Information sharing – Computation speed-up– Convenience

Page 2: Osys 6.Slides

2

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 3/23

Interprocess Communication (IPC)

• Mechanism for processes to communicate and to synchronize their actions– Shared memory

• Establish a shared memory region• Exchange information by reading/writing in the shared memory

region• Fast, convenient

– Message passing• Exchange messages between processes• Communication links must be established• Useful to exchange small amounts of data• Slower (implemented by means of system calls)

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 4/23

Communications Models

Page 3: Osys 6.Slides

3

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 5/23

Producer-Consumer Problem

• Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process– unbounded-buffer places no practical limit

on the size of the buffer– bounded-buffer assumes that there is a

fixed buffer size

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 6/23

Bounded-Buffer – Shared-Memory Solution

• Shared data#define BUFFER_SIZE 10typedef struct {. . .

} item;

item buffer[BUFFER_SIZE];int in = 0;int out = 0;

• Solution is correct, but can only use BUFFER_SIZE-1 elements

Page 4: Osys 6.Slides

4

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 7/23

Bounded-Buffer – Insert() Method

while (true) {/* Produce an item */

while ((in + 1) % BUFFER_SIZE) == out); /* do nothing -- no free buffers */buffer[in] = item;in = (in + 1) % BUFFER_SIZE;

}

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 8/23

Bounded Buffer – Remove() Method

while (true) {while (in == out);

// do nothing -- nothing to consume

// remove an item from the bufferitem = buffer[out];out = (out + 1) % BUFFER_SIZE;

}

Page 5: Osys 6.Slides

5

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 9/23

Bounded Buffer – Remove() Method

while (true) {while (in == out);

// do nothing -- nothing to consume

// remove an item from the bufferitem = buffer[out];out = (out + 1) % BUFFER_SIZE;

}

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 10/23

Message passingMessage system – processes communicate with each other without resorting to shared variablesIPC facility provides two operations:

• send(message) – message size fixed or variable• receive(message)

If P and Q wish to communicate, they need to:• establish a communication link between them• exchange messages via send/receive

Communication models:• Direct or indirect • Synchronous or asynchronous• Automatic or explicit buffering

Page 6: Osys 6.Slides

6

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 11/23

Direct Communication• Processes must name each other explicitly:

– send (P, message) – send a message to process P– receive(Q, message) – receive a message from

process Q• Properties of communication link

– Links are established automatically (each process must know the identity of the other process)

– A link is associated with exactly one pair of communicating processes

– Between each pair there exists exactly one link– The link may be unidirectional, but is usually bi-

directional

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 12/23

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• Properties of communication link

– Link established only if processes share a common mailbox

– A link may be associated with many processes– Each pair of processes may share several

communication links– Link may be unidirectional or bi-directional

Page 7: Osys 6.Slides

7

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 13/23

Indirect Communication

• Mailbox sharing– P1, P2, and P3 share mailbox A– P1, sends; P2 and P3 receive– Who gets the message?

• Solutions– Allow a link to be associated with at most two

processes– Allow only one process at a time to execute a receive

operation– Allow the system to select arbitrarily the receiver.

Sender is notified who the receiver was.

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 14/23

Indirect Communication

• Operations– create a new mailbox– send and receive messages through mailbox– destroy a mailbox

• Primitives are defined as:send(A, message) – send a message to mailbox Areceive(A, message) – receive a message from mailbox A

Page 8: Osys 6.Slides

8

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 15/23

Synchronization• Message passing may be either blocking or

non-blocking• Blocking is considered synchronous

– Blocking send: the sender blocks until the message is received

– Blocking receive: the receiver blocks until a message is available (rendezvous)

• Non-blocking is considered asynchronous– Non-blocking send: the sender sends the message

and continue– Non-blocking receive: the receiver receives a valid

message or null

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 16/23

Buffering

• Queue of messages attached to the link; implemented in one of three ways1. Zero capacity – 0 messages

Sender must wait for receiver (rendezvous)2. Bounded capacity – finite length of n

messagesSender must wait if link full

3. Unbounded capacity – infinite length Sender never waits

Page 9: Osys 6.Slides

9

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 17/23

Client-Server Communication

• Sockets• Remote Procedure Calls• Remote Method Invocation (Java)

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 18/23

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

Page 10: Osys 6.Slides

10

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 19/23

Socket Communication

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 20/23

Page 11: Osys 6.Slides

11

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 21/23

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 22/23

Remote Procedure Calls

• Remote procedure call (RPC) abstracts procedure calls between processes on networked systems.

• Stubs – client-side proxy for the actual procedure on the server.

• The client-side stub locates the server and marshalls the parameters.

• The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the server.

Page 12: Osys 6.Slides

12

urbino worldwide campusapplied computer scienceOperating Systems

02.04 Lesson 6

emanuele lattanzi isti information science and technology institute 23/23

Remote Method Invocation

• Remote Method Invocation (RMI) is a Java mechanism similar to RPCs.

• RMI allows a Java program on one machine to invoke a method on a remote object.