suggested exercises 5. question #1 find a creative/funny example of synchronization that can...
TRANSCRIPT
![Page 1: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/1.jpg)
Suggested Exercises 5
![Page 2: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/2.jpg)
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.
![Page 3: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/3.jpg)
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
![Page 4: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/4.jpg)
Developing the Solution
• Global states:
activeConsumers = 0;
activeOwners = 0;
waitingConsumers = 0;
waitingOwners = 0;
Condition okToPump = NULL;
Condition okToChangePrice = NULL;
Lock lock = FREE;
![Page 5: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/5.jpg)
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();
}
![Page 6: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/6.jpg)
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.
![Page 7: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/7.jpg)
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!
![Page 8: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/8.jpg)
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
![Page 9: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/9.jpg)
Solutions
3. Allocate all resources at the beginning– They each either get both things or none
semaphore pole= 1semaphore bait= 1lock = 1;fisher(int j) {
while (TRUE) {P(s);
P(pole);P(bait);// fishV(bait);V(pole);
V(s);}
}
![Page 10: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/10.jpg)
Solutions
4. Make everyone use the same ordering– Fisherman 1 goes first, then fisherman 2 (round
robin)
![Page 11: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/11.jpg)
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.
![Page 12: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/12.jpg)
Kernel Steps
• Configuring a kernel– Download kernel source– Build new configuration from old configuration• Copy old configuration into kernel source• Issue ‘make oldconfig’
– Customize your kernel• Issue ‘make menuconfig’ and select/de-select options
as you see fit
![Page 13: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/13.jpg)
Kernel Steps
• Compiling a kernel– cd into kernel source, type ‘make’
![Page 14: Suggested Exercises 5. Question #1 Find a creative/funny example of synchronization that can demonstrate the difficulty of developing a monitor-based](https://reader036.vdocuments.site/reader036/viewer/2022082819/56649f255503460f94c3befc/html5/thumbnails/14.jpg)
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!