Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

bullet implements the First_in_First_out algorithm bullet the Least Recently Use

ID: 3820081 • Letter: B

Question

bullet implements the First_in_First_out algorithm bullet the Least Recently Used algorithm. First, generate a random string(15 numbers) that represents the pages (numbers range from 0 to 9). In each program apply the algorithm on the random string and you should output the number of page faults. You should read in a command line argument from the command line that represents the number of pages(should work for numbers between 1 and 8) Assume that demand paging is used. Write a C program or java program using some combinations of mutex and semaphores to solve this problem. Provide copies of output that shows you checked all constraints(requires several runs). Provide instruction on running the program(must run on VirtualBox configuration). Santa is at the North Pole and can only be made awake by all eight reindeer arriving when 4 of his elves When four elves arc having their problems solved, any other elves wishing to visit Santa must wait for those elves to return. If Santa wakes up to find four elves waiting at the shop's door, along with the last reindeer arriving. Santa has decided that the elves can wait until after Christmas, because it is more important to get his sleigh ready. (It is assumed that the reindeer do not want to leave the tropics, and therefore they stay there until the last possible moment.) Here are some additional specifications: bullet When you have 8 reindeer Santa starts prepareTheSleigh and the reindeer thread invokes getSleighUp bullet After the fourth elf arrives, Santa must invoke helpElves. All four elves should invoke get Help. bullet All four elves must invoke getHelp before any additional elves enter (increment the elf counter), Santa should run in a loop so he can help many sets of elves. Assume there are 8 reindeer threads, 1 Santa thread and a command line number of elf threads

Explanation / Answer

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.System.out;

public class SantaClaus {

private volatile boolean kidsStillBelieveInSanta = true;
private final Semaphore disbelief = new Semaphore(0);
private final static int END_OF_FAITH = 2012;
private AtomicInteger year = new AtomicInteger(2006);
private static Random generator = new Random();


private final static int NUMBER_OF_REINDEER = 9;
private final static int NUMBER_OF_ELVES = 10;
private final static int ELVES_NEEDED_TO_WAKE_SANTA = 3;


private final Semaphore queueElves;
private final CyclicBarrier threeElves;
private final CyclicBarrier elvesAreInspired;
private final CyclicBarrier allReindeers;
private final Semaphore santasAttention;
private final static int LAST_REINDEER = 0;
private final static int THIRD_ELF = 0;

class Reindeer implements Runnable {
   int id;

   Reindeer(int id) { this.id = id; }

   public void run() {
       while (kidsStillBelieveInSanta) {
           try {

               Thread.sleep(900 + generator.nextInt(200));


               int reindeer = allReindeers.await();

               if (reindeer == LAST_REINDEER) {
                   santasAttention.acquire();
                   out.println("=== Delivery for Christmas " + year + " ===");
                   if (year.incrementAndGet() == END_OF_FAITH)
                   {
                       kidsStillBelieveInSanta = false;
                       disbelief.release();
                   }
                   santasAttention.release();
               }
           } catch (InterruptedException e) {

           } catch (BrokenBarrierException e) {

           }
       }
       out.println("Reindeer " + id + " retires");
   }
}

class Elf implements Runnable {
   int id;

   Elf(int id) { this.id = id; }

   public void run() {
       try {
           Thread.sleep(generator.nextInt(2000));

           while (kidsStillBelieveInSanta) {

               queueElves.acquire();
               out.println("Elf " + id + " ran out of ideas");


               int elf = threeElves.await();

               if (elf == THIRD_ELF)
                   santasAttention.acquire();

               Thread.sleep(generator.nextInt(500));
               out.println("Elf " + id + " got inspiration");
               elvesAreInspired.await();

               if (elf == THIRD_ELF)
                   santasAttention.release();

               queueElves.release();

               Thread.sleep(generator.nextInt(2000));
           }
       } catch (InterruptedException e) {

       } catch (BrokenBarrierException e) {

       }
       out.println("Elf " + id + " retires");
   }
}

class BarrierMessage implements Runnable {
   String msg;
   BarrierMessage(String msg) { this.msg = msg; }
   public void run() {
       out.println(msg);
   }
}

public SantaClaus() {
   santasAttention = new Semaphore(1, true);
   queueElves = new Semaphore(ELVES_NEEDED_TO_WAKE_SANTA, true);    // use a fair semaphore
   threeElves = new CyclicBarrier(ELVES_NEEDED_TO_WAKE_SANTA,
           new BarrierMessage("--- " + ELVES_NEEDED_TO_WAKE_SANTA + " elves are knocking ---"));
   elvesAreInspired = new CyclicBarrier(ELVES_NEEDED_TO_WAKE_SANTA,
           new BarrierMessage("--- Elves return to work ---"));
   allReindeers = new CyclicBarrier(NUMBER_OF_REINDEER, new Runnable() {
       public void run() {
           out.println("=== Reindeer reunion for Christmas " + year +" ===");
       }});

   ArrayList<Thread> threads = new ArrayList<Thread>();
   for (int i = 0; i < NUMBER_OF_ELVES; ++i)
       threads.add(new Thread(new Elf(i)));
   for (int i = 0; i < NUMBER_OF_REINDEER; ++i)
       threads.add(new Thread(new Reindeer(i)));
   out.println("Once upon in the year " + year + " :");
   for (Thread t : threads)
       t.start();

   try {
       disbelief.acquire();
       out.println("Faith has vanished from the world");
       for (Thread t : threads)
           t.interrupt();
       for (Thread t : threads)
           t.join();
   } catch (InterruptedException e) {
       e.printStackTrace();
   }
   out.println("The End of Santa Claus");
}

public static void main(String[] args) {
   new SantaClaus();
}
}