《操作系统20140416lecture15ipc_deadlock》由会员分享,可在线阅读,更多相关《操作系统20140416lecture15ipc_deadlock(93页珍藏版)》请在金锄头文库上搜索。
1、Operating Systems,Department of Computer Science & Technology Tsinghua University,Lecture 15 Inter-Process Communication and Deadlock,Outline,IPC Overview Communications Models Direct&Indirect Communication Blocking and Non-blocking Buffer of Communication Link Signal Pipe Message Queue Shared Memor
2、y,Deadlocks Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock,IPC Overview,Mechanism for processes to communicate and to synchronize their actions Processes communicate with each oth
3、er without resorting to shared variables IPC 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 Implementation of communication li
4、nk physical (e.g., shared memory, hardware bus) logical (e.g., logical properties),Communications Models,Implementation Questions,How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the c
5、apacity of a link? Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional?,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
6、Properties of communication link Links are established automatically 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,Indirect Communication,Messages are directed and re
7、ceived from message queues Each message queue has a unique id Processes can communicate only if they share a message queue Properties of communication link Link established only if processes share a common message queue A link may be associated with many processes Each pair of processes may share se
8、veral communication links Link may be unidirectional or bi-directional,Indirect Communication,Operations create a new message queue send and receive messages through message queue destroy a message queue Primitives are defined as: send(A, message) send a message to Queue A receive(A, message) receiv
9、e a message from Queue A,Indirect Communication,Message queue sharing P1, P2, and P3 share message queue 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 s
10、ystem to select arbitrarily the receiver. Sender is notified who the receiver was.,Blocking and Non-blocking,Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver bl
11、ock until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null,Buffering,Queue of messages attached to the link; implemented in one of three ways 1. Zero cap
12、acity 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity finite length of n messages Sender must wait if link full 3. Unbounded capacity infinite length Sender never waits,Outline,IPC Overview Signal Signal Generation Delivery Signal API Signal Example Pipe Message Queue Share
13、d Memory Solaris Doors (opt),Deadlocks Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock,Signals,Signal Software interrupt that notifies a process of an event Examples: SIGFPE, SIGKI
14、LL, SIGUSR1, SIGSTOP, SIGCONT What happens when a signal is received? Catch: Specify signal handler to be called Ignore: Rely on OS default action Example: Abort, memory dump, suspend or resume process Mask: Block signal so it is not delivered May be temporary (while handling signal of same type) Di
15、sadvantage Does not specify any data to be exchanged,Signals,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 gene
16、ration and delivery,Signals,Asynchronous signal: ctrl-C child process completes alarm scheduled by the process expires Unix: SIGALRM from alarm() or setitimer() resource limit exceeded (disk quota, CPU time.) Synchronous signal: programming errors, such as invalid data, divide by zero SIGTRAP: a condition arises that a debugger has requested to be informed of. SIGBUS: a bus error SIGSEGV: an invalid memory reference, or segmentation fault. SIGFPE: an erroneous arithmetic operatio