330 likes | 478 Views
Inter-Process Communication. Purposes for IPC. Data Transfer Sharing Data Event notification Resource Sharing Process Control. Conventional View. Protection domains - (for example, virtual address space). user. process 2. process n. process 1. kernel.
E N D
Inter-Process Communication CS523S: Operating Systems
Purposes for IPC • Data Transfer • Sharing Data • Event notification • Resource Sharing • Process Control CS523S: Operating Systems
Conventional View Protection domains - (for example, virtual address space) user process 2 process n process 1 kernel How can processes communicate with each other and the kernel? CS523S: Operating Systems
process 1 pipe Universal IPC Facilities handler user process 2 dbx kernel stop handle event CS523S: Operating Systems
Universal Facilities • Signals - asynchronous or synchronous event notification. • Pipes - unidirectional, FIFO, unstructured data stream. • Process tracing - used by debuggers to control control target process CS523S: Operating Systems
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. • Reliable Signals - BSD and SVR3. Fixed problems but approaches differ. • POSIX 1003.1 (POSIX.1) defined standard set of functions. CS523S: Operating Systems
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 CS523S: Operating Systems
Handling Signals (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 CS523S: Operating Systems
User Specified Actions • User specified actions • Default action, • Ignore signal, • Catch signal - invoke user specified signal handler • User may not ignore, catch or block SIGKILL and SIGSTOP • User may change action at any time • User may block signal • signal remains pending until unblocked CS523S: Operating Systems
Note • 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 processes has handler, then kernel arranges to first run handler on returning to user mode, then resume the interrupted instruction. CS523S: Operating Systems
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, kernels sends signal to parent • Quotas - exceeding limits • Notifications - event notification (device ready) • Alarms - process notified of alarm via signal reception CS523S: Operating Systems
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? CS523S: Operating Systems
System call interface {read(), write(), sigaltstack() … } Signals - Virtual Machine Model signal handler stack Process X (Signal handles) dispatch to handler instruction set register handles kernel (restartable system calls) deliver signal I/O facilities filesystem scheduler CS523S: Operating Systems
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 CS523S: Operating Systems
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). CS523S: Operating Systems
Named Pipes • Lives in the filesystem - 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? CS523S: Operating Systems
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 CS523S: Operating Systems
System V IPC Mechanisms • Semaphores • Message queues • Shared memory CS523S: Operating Systems
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. CS523S: Operating Systems
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 CS523S: Operating Systems
Sem1 semid_ds Sem2 Sem3 Sem4 System V Semaphores Application semop(semid, sops, nsops) sem1+2, sem3+1, block until sem4 == 0 Semaphore set (semid, kernel) CS523S: Operating Systems
msgid_ds msgcnt, bytes maxbytes System V Message Queues Send new message msgrcv(msgqid, msgp, maxcnt, msgtype, flag) type data data data type type FIFO CS523S: Operating Systems
shared memory System V Shared Memory 0x00000000 user Process 3 process 1 process 2 kernel CS523S: Operating Systems
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 CS523S: Operating Systems
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. CS523S: Operating Systems
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 CS523S: Operating Systems
size flags name number size flags type size reply_port destination_port message_id data data name number Message data structures • Ordinary data - physically copied by kernel • Out-of-line memory - copy-on-write • Send or receive ports • name - type of data CS523S: Operating Systems
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. CS523S: Operating Systems
DB server microkernel Memory mngr Task mngr I/O mngr Example Client CS523S: Operating Systems
MACH Message Passing Sending Task Receiving Task Copy of data In-line (data) Copy of data copy copy copy maps Address maps Out-of-line (data) copy maps Holding map Port right (local name) Port right (local name) Pointer to port obj translate translate Received message Outgoing message Internal message CS523S: Operating Systems
nameserver client Server X IPC In Action whereis server X Use port x register server X request reply kernel CS523S: Operating Systems
proxy port proxy port Distributed messaging in MACH Host B Host A netmsgserver netmsgserver port port server client CS523S: Operating Systems
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) CS523S: Operating Systems