操作系统课件ChapterIPC.ppt

操作系统课件ChapterIPC

Contents of this lecture 2.3 Interprocess Communication Race Conditions Critical Region and Mutual Exclusion Mutual Exclusion Using Busy Waiting Sleep and Wakeup Semaphores Monitors Message Passing 2.5 Classic IPC Problems Summary 2.3 Interprocess Communication 2.5 Classic IPC Problems Summary Inter-Process Communication Communication Pass information to each other UNIX pipe (special file) e.g. $ sort file1|grep scut Mutual Exclusion Keep each other’s hair Synchronization Proper sequencing The last one also applies to threads A Simple Example (1) A function to withdraw money from a bank account: int withdraw (account, amount) { balance = get_balance (account); balance = balance - amount; put_balance (account, balance); return balance; } Now suppose that you and your roommate share a bank account with a balance of ¥1500 (this is not necessarily a good idea...) What happens if you both go to separate ATM machines, and simultaneously withdraw ¥ 100 from the account? A Simple Example (2) We represent the situation by creating a separate thread for each ATM user doing a withdrawal Both threads run on the same bank server system What’s the problem with this? What are the possible balance values after each thread runs? A Simple Example (3) The execution of the two threads can be interleaved Assume preemptive scheduling Each thread can context switch after each instruction We need to worry about the worst-case scenario! What’s the account balance after this sequence? And whos happier, the bank or you??? A Simple Example (4) Which resources are shared? Local variables in a function are not shared They exist on the stack, and each thread has its own stack Global variables are shared Stored in static data portion of the address space Accessible by any thread Dynamically allocated data is shared Stored in the heap, accessible by any thread A Simple Example (5) The execution of the two threads can be interleaved Assume preemptive sched

文档评论(0)

1亿VIP精品文档

相关文档