Suggested Exercises 5 Question #1 • Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based solution (in pseudocode) similar to the “Readers-Writers.” • Make sure that you vigorously discuss the correctness and pitfalls of your solution. Gas Station Problem • There is a common price for gas – Consumers never modify the price – they just pump gas – Owners read modify the price • We want one owner at a time, but many consumers at the same time Developing the Solution • Global states: activeConsumers = 0; activeOwners = 0; waitingConsumers = 0; waitingOwners = 0; Condition okToPump = NULL; Condition okToChangePrice = NULL; Lock lock = FREE; Sample Code Solution – up to you to prove correctness Consumer() { lock.Acquire(); while (activeOwners > 0 || waitingOwners > 0) { ++waitingConsumers; okToPump.Wait(&lock); --waitingConsumers; } ++activeConsumers; lock.Release(); // access price and pumps lock.Acquire(); --activeConsumers; if (activeConsumers == 0 && waitingOwners > 0) { okToChangePrice.Signal(&lock); } lock.Release(); } Writer() { lock.Acquire(); while (activeOwners > 0 || activeConsumers > 0) { ++waitingOwners; okToChangePrice.Wait(&lock); --waitingOwners; } ++activeOwners; lock.Release(); // access database lock.Acquire(); --activeOwners; if (waitingOwners > 0) { okToChangePrice.Signal(&lock); } else if (waitingConsumers > 0) { okToPump.Broadcast(&lock); } lock.Release(); } Question #2 • Find a creative/funny example of deadlock, and apply various deadlock prevention approaches to the live example. • Describe the advantages and disadvantages of each approach. A Tale of Two Fisherman • Two fisherman are out fishing, but between them they only have one fishing pole and one bait. – If one takes the pole, and the other takes the bait… • They deadlock! Solutions 1. Infinite resources – Buy more poles and bait 2. No sharing – They each have their own poles and bait, fish alone 3. Allocate all resources at the beginning – They each either get both things or none Solutions 3. Allocate all resources at the beginning – They each either get both things or none semaphore pole= 1 semaphore bait= 1 lock = 1; fisher(int j) { while (TRUE) { P(s); } } V(s); P(pole); P(bait); // fish V(bait); V(pole); Solutions 4. Make everyone use the same ordering – Fisherman 1 goes first, then fisherman 2 (round robin) Question #3 • Please summarize the steps to configuring, compiling, and installing a kernel and its modules. • Include the step of rebooting into the new kernel. Kernel Steps • Configuring a kernel – Download kernel source – Build new configuration • Make the default configuration (smaller than the distro configuration) • Issue ‘make defconfig’ – Customize your kernel • Issue ‘make menuconfig’ and select/de-select options as you see fit Kernel Steps • Compiling a kernel – cd into kernel source, type ‘make’ Kernel Steps • Installing a kernel – cd into kernel source – Issue ‘make modules_install’ to copy the modules to installation location for new kernel – Issue ‘make install’ to install the kernel executable to /boot – Make an initrd image • Issue ‘makeinitramfs –o [output file] [kernel version] – Update the boot loader • Issue ‘update-grub’, make sure grub finds both kernel image and initrd image • Reboot!