1 / 40

Inter-Process Communication

Inter-Process Communication. Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Applied Research Laboratory Department of Computer Science and Engineering Washington University in St. Louis. IPC: Cooperating Processes.

kolina
Download Presentation

Inter-Process Communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inter-Process Communication Fred Kuhns (fredk@arl.wustl.edu, http://www.arl.wustl.edu/~fredk) Applied Research Laboratory Department of Computer Science and Engineering Washington University in St. Louis

  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 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 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) 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? 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 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 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. 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 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 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. CSE522– Advanced Operating Systems

  12. Purposes for IPC • Data Transfer • Sharing Data • Event notification • Resource Sharing • Process Control CSE522– Advanced Operating Systems

  13. 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? CSE522– Advanced Operating Systems

  14. process 1 pipe Universal IPC Facilities handler user process 2 dbx kernel stop handle event • 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 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. 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 CSE522– Advanced Operating Systems

  17. System call interface {read(), write(), sigaltstack() … } Signals - Virtual Machine Model signal handler stack Process X (Signal handles) instruction set register handlers dispatch to handler kernel (restartable system calls) deliver signal I/O facilities filesystem scheduler 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 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. 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 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? 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 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? 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 CSE522– Advanced Operating Systems

  25. System V IPC Mechanisms • Semaphores • Message queues • Shared memory 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. 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 CSE522– Advanced Operating Systems

  28. 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) CSE522– Advanced Operating Systems

  29. msgid_ds msgcnt, bytes maxbytes System V Message Queues msgrcv(msgqid, msgp, maxcnt, msgtype, flag) Send new message type data data data type type FIFO CSE522– Advanced Operating Systems

  30. shared memory System V Shared Memory 0x00000000 user Process 3 process 1 process 2 kernel 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 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. 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 replyport, 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 CSE522– Advanced Operating Systems

  34. 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 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. CSE522– Advanced Operating Systems

  36. DB server microkernel Memory mngr Task mngr I/O mngr Example Client CSE522– Advanced Operating Systems

  37. 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 CSE522– Advanced Operating Systems

  38. nameserver client Server X IPC In Action whereis server X Use port x register server X request reply kernel CSE522– Advanced Operating Systems

  39. proxy port proxy port Distributed messaging in MACH Host B Host A netmsgserver netmsgserver port port server client 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) CSE522– Advanced Operating Systems

More Related