23
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns ([email protected], http://www.arl.wustl.edu/~fredk) Department of Computer Science and Engineering Washington University in St. Louis

Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns ([email protected], fredk)

Embed Size (px)

Citation preview

Page 1: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

WashingtonWASHINGTON UNIVERSITY IN ST LOUIS

Core Inter-Process Communication Mechanisms

(Historically Important)

Fred Kuhns([email protected], http://www.arl.wustl.edu/~fredk)

Department of Computer Science and EngineeringWashington University in St. Louis

Page 2: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

2Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

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– Modularity– Convenience

• Dangers of process cooperation– Data corruption, deadlocks, increased complexity– Requires processes to synchronize their processing

Page 3: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

3Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Purposes for IPC

• Data Transfer• Sharing Data• Event notification• Resource Sharing and

Synchronization• Process Control

Page 4: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

4Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

IPC Mechanisms• Mechanisms used for communication and synchronization

– Message Passing• message passing interfaces, mailboxes and message queues• sockets, STREAMS, pipes

– Shared Memory: Non-message passing systems

• Common examples of IPC– Synchronization using primitives such as semaphores to higher level

mechanisms such as monitors. Implemented using either shared memoru or message passing.

– Debugging– Event Notification - UNIX signals

• We will defer a detailed discussion of synchronization mechanisms and concurrency until a later class

• Here we want to focus on some common (and fundamental) IPC and event notification mechanisms

Page 5: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

5Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Message Passing

• In a Message passing system there are no shared variables. IPC facility provides two operations for fixed or variable sized message:– send(message)– receive(message)

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

• Implementation of communication link– physical (e.g., shared memory, hardware bus)– logical (e.g., syntax and semantics, abstractions)

Page 6: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

6Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Implementation Questions• How are links established?

• Can a link be associated with more than two processes?

• How are links made known to processes?

• How many links can there be between every pair/group of communicating processes?

• What is the capacity of a link?

• Is the size of a message that the link can accommodate fixed or variable?

• Is a link unidirectional or bi-directional?

Page 7: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

7Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Message Passing Systems

•Exchange messages over a communication link

•Methods for implementing the communication link and primitives (send/receive):1.Direct or Indirect communications (Naming)2.Symmetric or Asymmetric communications3.Automatic or Explicit buffering4.Send-by-copy or send-by-reference5.fixed or variable sized messages

Page 8: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

8Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Direct Communication – Internet and Sockets

• Processes must name each other explicitly:– Symmetric Addressing

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

– Asymmetric Addressing• send (P, message) – send to process P• receive(id, message) – rx from any; system sets id = sender

• Primitives:– send(A, message) – send a message to mailbox A– receive(A, message) – receive a message from mailbox A

• Properties of communication link– Links established automatically between pairs– processes must know each others ID– Exactly one link per pair of communicating processes

• Disadvantage: a process must know the name or ID of the process(es) it wishes to communicate with

Page 9: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

9Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Indirect Communication - Pipes

• Messages are sent to or 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 more than 2 processes.– Each pair of processes may share several communication

links.• Ownership:

– process owns (i.e. mailbox is implemented in user space): only the owner may receive messages through this mailbox. Other processes may only send. When process terminates any “owned” mailboxes are destroyed.

– system owns – then mechanisms provided to create, delete, send and receive through mailboxes. Process that creates mailbox owns it (and so may receive through it) but may transfer ownership to another process.

Page 10: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

10Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

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

Page 11: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

11Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Synchronizing Message Flow

• Message passing may be either blocking or non-blocking.– blocking send: sender blocked until message

received by mailbox or process– nonblocking send: sender resumes operation

immediately after sending– blocking receive: receiver blocks until a

message is available– nonblocking receive: receiver returns

immediately with either a valid or null message.

Page 12: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

12Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Buffering

•All messaging system require framework to temporarily buffer messages. These queues are implemented in one of three ways:

1. Zero capacity – No messages may be queued within the link, requires sender to block until receives retrieves message.

2. Bounded capacity – Link has finite number of message buffers. If no buffers are available then sender must block until one is freed up.

3. Unbounded capacity – Link has unlimited buffer space, consequently send never needs to block.

Page 13: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

13Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Lets Get Practical

Notes to help you with the first project

Page 14: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

14Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Conventional View

pro

cess

1

pro

cess

2

pro

cess

n

kernel

user

Protection domains - (virtual address space)

How can processes communicate with each other and the kernel?

Page 15: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

15Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Universal IPC Facilities

pro

cess

1

pro

cess

2

kerneluser

pipe

handler

handle event

stop

db

x

• Universal Facilities in UNIX– Signals - asynchronous or synchronous event notification.– Pipes - unidirectional, FIFO, unstructured data stream.– Process tracing - used by debuggers to control control target process

Page 16: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

17Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Signals Overview• Divided into asynchronous (CTL-C) and

synchronous (illegal address)• Three phases to processing signals:

– generation: event occurs requiring process notification

– delivery: process recognizes and takes appropriate action

– pending: between generation and delivery

• SVR4 and 4.4BSD define 31 signals, original had 15. Some commercial system support > 32.

• Signal to integer mappings differ between BSD and System V implementations

Page 17: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

18Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Signals - Virtual Machine Model

System call interface{read(), write(), sigaltstack() … }kernel

scheduler I/O facilities filesystem

instruction set

(restartable system calls)

Process X

(Signal handles)

register handlers

deliver signal

dispatch to handler

signal handlerstack

Page 18: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

19Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Actions• Handling, default actions

– Abort: terminate process, generate core dump– Exit: terminate without generating core dump– Ignore: ignore signal– Stop: suspend process– Continue: resume process

• User specified actions– Default action, – Ignore signal, – Catch signal - invoke user specified signal handler

• User may– not ignore, catch or block SIGKILL and SIGSTOP– change action at any time– block signal: signal remains pending until unblocked

Page 19: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

21Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Signal Generation

• Exceptions - kernel notifies process with signal• Other Process - using kill or sigsend.• Terminal interrupts - stty allows binding of

signals to specific keys, sent to foreground process

• Job control - background processes attempt to read/write to terminal. Process terminate or suspends, kernels sends signal to parent

• Quotas - exceeding limits• Notifications - event notification (device ready)• Alarms - process notified of alarm via signal

reception

Page 20: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

22Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Reliable Signals - BSD

• Persistent handlers• Masking signals

– signals masked (blocked) temporarily– user can specify mask set for each signal– current signal is masked when handler invoked

• Interruptible sleeps• Restartable system calls• Allocate separate stack for handling signals

– why is this important?

Page 21: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

23Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Signals - A Few Details

• Any process or interrupt can post a signal– set bit in pending signal bit mask– perform default action or setup for delivery

• Signal typically delivered in context of receiving process.– exception is sending SIGSTOP, kernel may

perform action directly– Pending signals are checked before

returning to user mode and just before/after certain sleep calls.

– Produce core dump or invoke signal handler

Page 22: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

24Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

UNIX Pipes

• Unidirectional, FIFO, unstructured data stream• Fixed maximum size• Simple flow control• pipe() system call creates two file descriptors. Why?• Implemented using filesystem, sockets or STREAMS

(bidirectional pipe).• Named Pipes:

– Lives in the filesystem - that is, a file is created of type S_IFIFO (use mknod() or mkfifo())

– may be accessed by unrelated processes– persistent– less secure than regular Pipes. Why?

Page 23: Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns (fredk@arl.wustl.edu, fredk)

25Fred Kuhns (04/18/23) CS422 – Operating Systems Concepts

Process Tracing

• ptrace()• used by debuggers such as dbx and gdb.• Parent process controls execution of child

– child must notify kernel that it will be traced by parent

• Modern systems use the /proc file system.– Allows process to trace unrelated processes