Sample Problems
Sample Problems
1. Discuss how you would implement a pool of threads. The idea is to have a queue of jobs that need to be executed and a pool of threads is created to execute those jobs. The user can limit the number of threads when they create the thread-pool. 2. Write a header le for a class to implement a thread-pool in C using PThreads. 3. How would you change your buddy system implementation such that a program can allocate multiple co-existing buddy systems? 4. How would you change your buddy system implementation such that it can manage multiple pools of memory instead of a single pool. Assume that we know the number of such pools at the time of creation of the buddy system. 5. Write pseudo-code for implementing a barrier using semaphores. Recall that a barrier implies that all participating threads reach the barrier before any thread can get past the barrier. Look at the example ch8/thread-barrier to see how to use a barrier. (Note: Use C.A.R. Hoare semantics for the monitor problem. That means that a process should return from the monitor routine immediately after doing a signal operation.) 6. A le is to be shared among dierent processes. The le can be accessed simultaneously by several processes, subject to the constraint that the total number of processes accessing the le should be less than n. Write a monitor to enforce the given constraint. Use the following template. monitor FileAccess { ... public: void StartAccess() { ... } void EndAccess() { ... } } 7. A le is to be shared among dierent processes, each of which has a unique process-id. The le can be accessed simultaneously by several processes, subject to the constraint that the sum of all unique process-ids associated with all the processes currently accessing the le must be less than a parameter n. Write a monitor to coordinate access to the le using the following template.
FileAccess {
void StartAccess(int id) { } void EndAccess(int id) { } 8. Consider a system consisting of n processes P1 , P2 , . . . , Pn . Write a monitor (in pseudocode) that allocates three printers printer[1..3] among these processes. Processes get printers in a rst-come-rst-served fashion (one per process). If there is more than one printer available, then each process wants printer[1] since it is the fastest. If it cant get printer[1], then a process wants printer[2] as it is the second fastest and printer[3] is the least favorite choice if the rst two are not available. For your solution use Hoare semantics (where a process must immediately leave the monitor after signaling). monitor PrintMonitor{ const int NUM=3; condition WaitForPrinter; int NumberOfPrintersLeft; boolean PrinterFree[1..NUM]; /* true implies free, false implies busy */ int GetPrinter() {
} void ReleasePrinter(int p) {
} // constructor to initialize the monitor PrintMonitor(void) { NumOfPrintersLeft = NUM; for (int i=1; i<= NUM; i++) 2
PrinterFree[i] = true; } }//monitor PrintMonitor // Code for each process void P(int pid); { int Printer; Printer = PrintMonitor.GetPrinter(); PrintFile(Printer); PrintMonitor.ReleasePrinter(Printer); goDoSomethingElse(); } void main(void) { // start the following n processes in parallel P(1); P(2); ... P(n); } 9. Rewrite the previous problem in Java.
public class PrintMonitor { public final int NUM=3; private int NumberOfPrintersLeft; private boolean PrinterFree[1..NUM]; /* true implies free, false implies busy public PrintMonitor(void) // constructor to initialize the monitor { NumOfPrintersLeft = NUM; for (int i=1; i<= NUM; i++) PrinterFree[i] = true; } public { /* add } public { /* add } int GetPrinter() code here */ void ReleasePrinter(int p) code here */
}//PrintMonitor // Code for each process public class P implements Runnable { int pid; PrintMonitor monitor; public P(int pid, PrintMonitor monitor) { this.pid = pid; this.monitor = monitor; } public void run() { int printer; while (true) { printer = monitor.GetPrinter(); printFile(printer); monitor.ReleasePrinter(printer); goDoSomethingElse(); } } /* other methods */ } pubic static void main(String [] args) { PrintMonitor monitor = new PrintMonitor(); // start the n processes in parallel for (int i=0; i<n; i++) { new Thread(new P(i, monitor)).start(); } }