0% found this document useful (0 votes)
141 views4 pages

Sample Problems

The document contains 9 practice problems related to implementing thread pools, buddy systems, monitors, and shared resources among processes. The problems cover topics like implementing a thread pool with a job queue, designing a C header file for a thread pool class, managing multiple buddy system pools, using semaphores for barriers, using monitors to limit the number of processes accessing a shared file, and allocating printers among processes in a first-come-first-served manner. The problems are presented in both C and Java and involve concepts like monitors, semaphores, and shared resources.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views4 pages

Sample Problems

The document contains 9 practice problems related to implementing thread pools, buddy systems, monitors, and shared resources among processes. The problems cover topics like implementing a thread pool with a job queue, designing a C header file for a thread pool class, managing multiple buddy system pools, using semaphores for barriers, using monitors to limit the number of processes accessing a shared file, and allocating printers among processes in a first-come-first-served manner. The problems are presented in both C and Java and involve concepts like monitors, semaphores, and shared resources.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practice 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.

monitor ... public:

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(); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy