Upload
ramasubbu-p
View
1.251
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Presentation
Citation preview
WashingtonWASHINGTON UNIVERSITY IN ST LOUIS
Inter-Process Communication
Fred Kuhns([email protected], http://www.arl.wustl.edu/~fredk)
Applied Research LaboratoryDepartment of Computer Science and Engineering
Washington University in St. Louis
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 2
IPC: Cooperating Processes
• Independent processes cannot affect or be affected by the execution of another process.
• Cooperating processes (and threads) can affect or be affected by the execution of another process
• Advantages of process/thread cooperation– Information sharing – Computation speed-up– Modularity– Convenience
• Dangers of process cooperation– Data corruption, deadlocks, increased complexity– Requires processes to synchronize their processing
• Purposes: – Data Transfer– Sharing Data– Event notification– Resource Sharing and Synchronization– Process Control
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 3
IPC Mechanisms
• Mechanisms used for communication and synchronization– Message Passing
• message passing interfaces, mailboxes and message queues• sockets, STREAMS, pipes
– Shared Memory• variables and buffers located in shared memory regions
• Common examples of IPC– Synchronization: various primitives from spin locks to higher
level mechanisms such as monitors.• Implemented using either shared memory or message passing.
– Debugging– Event Notification - condition variables, UNIX signals
• Here we want to focus on some common (and fundamental) IPC and event notification mechanisms
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 4
Message Passing
• In a Message passing system there are no shared variables. • Communication channel can be thought of as a queue of
messages that have been sent but not yet read (similar to a mail box)– chan cname(type1 id1, type2 id2, ..., type3 id3)
– chan array[n](type1, ..., typen)
• Message passing facility defines two operations on a communication channel:– send cname(expr1, ..., exprn)
– receive cname(var1, ..., varn)
• If processes P and Q wish to communicate, they need to:– establish a communication channel (any number of readers and
senders) or link (just one sender and receiver)– exchange messages via send and receive
• Implementation of communication link– physical (e.g., shared memory, hardware bus)– logical (e.g., syntax and semantics, abstractions)
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 5
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?
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 6
Message Passing Systems
•Methods for implementing a logical communication link and primitives (send/receive):
– Direct or Indirect communications (Naming)– Symmetric or Asymmetric communications– Automatic or Explicit buffering– Send-by-copy or send-by-reference– fixed or variable sized messages
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 7
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
• 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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 8
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.
• Question: – Does it matter if we have multiple receivers? – What about multiple senders?
• Ownership:– Process owns: only the owner may receive messages through this
mailbox. Other processes may only send. When process terminates any “owned” mailboxes are destroyed.
– System owns: OS provides mechanisms to create, delete, send and receive through mailboxes. Process that creates mailbox may receive messages but may transfer this right to another process.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 9
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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 10
Synchronizing Message Flow
• Message passing may be either blocking or non-blocking.
• Asynchronous and synchronous communications generally defined in terms of the sender– Asynchronous: sender resumes operation immediately,
does not wait for the message to be read by the receiver. Receiver may or may not block until a message is available.
– Synchronous: sender blocks until message received by mailbox or process
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 11
Buffering
• All messaging system require framework to temporarily buffer messages. These queues are implemented in one of three ways:
– Zero capacity – No messages may be queued within the link, requires sender to block until receives retrieves message.
– Bounded capacity – Link has finite number of message buffers. If no buffers are available then sender must block until one is freed up.
– Unbounded capacity – Link has unlimited buffer space, consequently send never needs to block.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 12
Purposes for IPC
• Data Transfer• Sharing Data
• Event notification• Resource Sharing• Process Control
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 13
Conventional View
pro
cess
1
pro
cess
2
pro
cess
n
kernel
user
Protection domains - (for example, virtual address space)
How can processes communicate with each other and the kernel?
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 14
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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 15
Signals - History
• Unreliable Signals - Orignal System V (SVR2 and earlier) implementation.– Handlers are not persistent– recurring instances of signal are not masked, can result
in race conditions (one signal delivered to handler the next may result in the default behavior)
• Reliable Signals - BSD and SVR3. Fixed problems but approaches differ.
• POSIX 1003.1 (POSIX.1) defined standard set of functions.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 16
Signals Overview• Divided into asynchronous (CTL-C) and synchronous
(illegal address)
• Three phases to processing signals: – generation: event occurs requiring process notification – pending: signal recorded in kernel process state object– delivery: process recognizes and takes appropriate action
• 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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 17
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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 18
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– can not be ignored, caught or blocked: SIGKILL and SIGSTOP
– change action at any time– block signal: signal remains pending until unblocked
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 19
Notes for BSD UNIX
• Signal action may only be taken within context of receiving process
• Process must be scheduled to run
• kernel calls issig() on behalf of process to check for pending signals. issig is called when:– before returning to user mode from a system call or
interrupt– before blocking in an interruptible system event– immediately after waking from an interruptible event
• if pending signal and process has handler, then kernel arranges to first run handler on returning to user mode, then resume the interrupted instruction.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 20
Signal Generation
• Exceptions - kernel notifies process with signal
• Other Process - signals sent to process or set of processes 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.
– Job control shells control foreground/background processes.
– Process terminate or suspends, kernel sends signal to parent
• Quotas - exceeding limits
• Notifications - event notification (device ready)
• Alarms - process notified of alarm via signal reception
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 21
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?
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 22
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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 23
UNIX Pipes
• Unidirectional, FIFO, unstructured data stream• Fixed maximum size• Simple flow control• pipe() system call creates two file descriptors.
– Why two?
• 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?
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 24
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
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 25
System V IPC Mechanisms
• Semaphores• Message queues• Shared memory
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 26
Common Elements
• Common Attributes– key - integer identifying a resource instance– Creator - usr and group id of resource creator– Owner - usr and group id of owner– Permissions - FS style read/write/execute
• shmget(key,…),semget(key,…),msgget(key,…)
• key can be generated from a filename and integer (ftok()) or IPC_PRIVATE.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 27
Common Facilities
• Resources are persistent, thus must be deleted when no longer needed - must be owner, creator or superuser.
• shmctl(shmid,…),semctl(semid,…),msgctl(msgid,…)
• Fixed size resource table: ipc_perm structure plus type specific data
• resource id = seq * table_size + index
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 28
System V Semaphores
Sem1semid_ds
Sem2
Sem3
Sem4
Semaphore set (semid, kernel)
semop(semid, sops, nsops)
Application
sem1+2, sem3+1, block until sem4 == 0
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 29
System V Message Queues
type
data
type
data
type
data
msgid_dsmsgcnt, bytesmaxbytes
Sendnew
message
msgrcv(msgqid, msgp, maxcnt, msgtype, flag)
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 30
System V Shared Memory
pro
cess
1
pro
cess
2
kernel
user
Pro
cess
3
0x00000000
sharedmemory
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 31
MACH IPC
• Message passing fundamental mechanism, – user-user and user-kernel
• avoid unnecessary data copies– provide copy-on-write mechanisms
• kernel must provide secure communications
• transparently extensible to distributed environment
• Tightly coupled with virtual memory
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 32
The Basics• Message - collection of typed data
– simple - contains ordinary data– complex - contains ordinary data, out-of-line data (uses copy-on-
write), send or receive rights to various ports. Kernel transforms complex data so meaningful for receive.
• Port - protected queue of messages– message can only be sent to a port– tasks own send and receive rights– several tasks may own send rights to a port, but only one task (the
owner) has receive rights
• Ports also represent kernel objects (threads, tasks etc). kernel hold receive rights.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 33
MACH Ports
• each thread and task have default ports, – send rights to task_self port, kernel receive– read rights to task_notify– send rights to a bootstrap port, access to nameserver– send rights to thread_self– receive rights to reply port, receive replies from system calls
and rpc to other tasks– read rights to exception port
• task owns rights to all port, so all threads can access any port
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 34
Message data structures
• Ordinary data - physically copied by kernel
• Out-of-line memory - copy-on-write
• Send or receive ports• name - type of data
typesize
reply_portdestination_port
message_idname
numbersizeflags
data
namenumber
sizeflags
data
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 35
Interface
• One-way send• Blocked read
– wait for unsolicited msgs
• Two-way asynchronous– send msg, receive reply asynchronously.
• Blocking two-way– send msg and wait for reply.
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 36
Example
DB server
I/O mngrTask mngr Memory mngr
microkernel
Client
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 37
MACH Message Passing
In-line(data)
Out-of-line(data)
Port right(local name)
Outgoing message
Copy ofdata
Holdingmap
Pointer toport obj
Internalmessage
Copy ofdata
Addressmaps
Port right(local name)
Receivedmessage
copy
copymaps
translate
copy
copymaps
translate
Sending Task Receiving Task
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 38
IPC In Action
client
nameserver
Server X
kernel
registerserver X
whereisserver X
Useport x
request
reply
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 39
Distributed messaging in MACH
netmsgserver netmsgserver
proxyport
proxyport
client
port
server
port
Host A Host B
Fred Kuhns (04/12/23) CSE522– Advanced Operating Systems 40
MACH IPC - Notes
• Handoff scheduling– support for a fast path when receiver is scheduled
immediately.
• Notification– asynchronous message from kernel to task.
• Port sets– group of ports with one receiver, and one receive queue
(i.e. one receive right)