CS34 2013-05-19 CS 134: Operating Systems More Synchronization CS 134: Operating Systems More Synchronization 1 / 21
Overview CS34 Overview 2013-05-19 More Synchronization Overview Monitors Simpler Mechanisms More Synchronization Monitors Simpler Mechanisms 2 / 21
More Synchronization The Story So Far. . . CS34 The Story So Far. . . 2013-05-19 More Synchronization Mutual Exclusion ◮ Basic idea? Semaphores The Story So Far. . . ◮ Basic idea? Mutual Exclusion ◮ Basic idea? Semaphores ◮ Basic idea? 3 / 21
More Synchronization Fairness CS34 Fairness 2013-05-19 More Synchronization Just how fair do we need to be. . . ? Our Take. . . Fairness Just how fair do we need to be. . . ? Our Take. . . 4 / 21
More Synchronization Fairness CS34 Fairness 2013-05-19 More Synchronization Just how fair do we need to be. . . ? Our Take. . . No one likes semaphores! Fairness ◮ Too low-level ◮ Too much freedom (& too strange) ◮ Too hard to get right Need an alternative. . . Just how fair do we need to be. . . ? Our Take. . . No one likes semaphores! ◮ Too low-level ◮ Too much freedom (& too strange) ◮ Too hard to get right Need an alternative. . . 4 / 21
More Synchronization Monitors Monitors CS34 Monitors 2013-05-19 More Synchronization Monitors were devised as an alternative to semaphores ◮ High-level synchronization construct, based on classes ◮ Only one task can be running “inside” the class at a time Monitors Declare classes like this: monitor class MyClass { Monitors public: /* method declarations only private: /* private data and private methods */ }; Monitors were devised as an alternative to semaphores ◮ High-level synchronization construct, based on classes ◮ Only one task can be running “inside” the class at a time Declare classes like this: monitor class MyClass { public: /* method declarations only private: /* private data and private methods */ }; 5 / 21
More Synchronization Monitors Monitors CS34 Monitors 2013-05-19 queue of More Synchronization entering processes Basic idea: Entrance ◮ Only one process can be MONI TOR in the monitor at a time local data Monitors But what about waiting? method 1 Monitors • queue of • • method k entering processes initialization code Basic idea: Exit Entrance ◮ Only one process can be MONI TOR in the monitor at a time local data But what about waiting? method 1 • • • method k initialization code Exit 6 / 21
More Synchronization Monitors Monitors CS34 Monitors 2013-05-19 More Synchronization queue of Basic idea entering processes ◮ Only one process can monitor waiting area Entrance MONI TOR be in the monitor at a Monitors condition c 1 local data time cwait(c 1 ) condition variables ◮ cwait(beer) waits for beer • • method 1 Monitors ◮ csignal(beer) signals beer • • condition c n • • cwait(c n ) method k initialization code queue of Exit entering Basic idea processes Entrance monitor waiting area ◮ Only one process can MONI TOR be in the monitor at a local data condition c 1 time cwait(c 1 ) condition variables ◮ cwait(beer) waits for beer • • method 1 ◮ csignal(beer) signals beer • • condition c n • • cwait(c n ) method k initialization code Exit 7 / 21
More Synchronization Monitors Monitors CS34 Monitors 2013-05-19 More Synchronization queue of Basic idea entering processes ◮ Only one process can monitor waiting area Entrance MONI TOR be in the monitor at a Monitors condition c 1 local data time cwait(c 1 ) condition variables ◮ cwait(beer) waits for beer • • method 1 Monitors ◮ csignal(beer) signals beer • • condition c n • • cwait(c n ) method k urgent queue csignal initialization code queue of Exit entering Basic idea processes Entrance monitor waiting area ◮ Only one process can MONI TOR be in the monitor at a local data condition c 1 time cwait(c 1 ) condition variables ◮ cwait(beer) waits for beer • • method 1 ◮ csignal(beer) signals beer • • condition c n • • cwait(c n ) method k urgent queue csignal initialization code Exit 8 / 21
More Synchronization Monitors Equivalence Claims CS34 Equivalence Claims 2013-05-19 More Synchronization Monitors How could we show that ◮ Semaphores aren’t “more powerful” than monitors? ◮ Monitors aren’t “more powerful” than semaphores? Equivalence Claims How could we show that ◮ Semaphores aren’t “more powerful” than monitors? ◮ Monitors aren’t “more powerful” than semaphores? 9 / 21
More Synchronization Simpler Mechanisms Minimalism. . . CS34 Minimalism. . . 2013-05-19 More Synchronization Simpler Mechanisms In NP-completeness, you learn SAT , and then the simpler 3-SAT , which is equivalent. Can we imagine something “less” than semaphores? Minimalism. . . In NP-completeness, you learn SAT , and then the simpler 3-SAT , which is equivalent. Can we imagine something “less” than semaphores? 10 / 21
More Synchronization Simpler Mechanisms Binary Semaphores CS34 Binary Semaphores 2013-05-19 More Synchronization Simpler Mechanisms Basic idea? Binary Semaphores A binary semaphore is similar to test-and-set. If it’s nonzero, one one process can set it to zero and continue past bsem_dec . If it’s zero, bsem_inc sets it nonzero and wakes at least one process waiting on it. Multiple calls to bsem_inc with no intervening bsem_dec will have Basic idea? no effect. However, it is illegal to do that: you can’t call bsem_inc unless the semaphore value is currently zero. 11 / 21
More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores CS34 Semaphores from Binary Semaphores 2013-05-19 Assume the following binary semaphore operations: More Synchronization struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); Simpler Mechanisms void bsem_inc (struct bsem* s); Data to implement semaphores...? Semaphores from Binary Semaphores Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? 12 / 21
More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores CS34 Semaphores from Binary Semaphores 2013-05-19 Assume the following binary semaphore operations: More Synchronization struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); Simpler Mechanisms void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { Semaphores from Binary Semaphores volatile int count; // Semaphore count Assume the following binary semaphore operations: struct bsem* wait; // Wait here... struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... 12 / 21
More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores CS34 Semaphores from Binary Semaphores 2013-05-19 Assume the following binary semaphore operations: More Synchronization struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); Simpler Mechanisms void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { Semaphores from Binary Semaphores volatile int count; // Semaphore count Assume the following binary semaphore operations: struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count 12 / 21
More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores CS34 Semaphores from Binary Semaphores 2013-05-19 Assume the following binary semaphore operations: More Synchronization struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); Simpler Mechanisms void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { Semaphores from Binary Semaphores volatile int count; // Semaphore count Assume the following binary semaphore operations: struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count volatile int waiting; // How many waiting struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count volatile int waiting; // How many waiting 12 / 21
Recommend
More recommend