Skip to content
Advertisement

Implement the `sleep()` and `awake()` in java

I need to develop the sleep() and awake() functions of the OS. I have runningJobs queue, and sleepingJobs queue. The sleep method should receive a Job and insert it in the sleeping queue in the right place, and the awake() method should move the sleeping jobs that are ready to run, to the runningJobs queue.

  1. In not sure how to implement my queues – should I use PriorityQueue / LinkedList queue?
  2. What is the most efficient way to order my list / find the right job in my queue?
  3. How the awake() method would know the time without having access to the current time

Advertisement

Answer

  1. Either option (linked list/priority queue) is fine. The decision depends on what result you want.

Linked list is a “first in first out” queue. The first job to sleep is the one that has been running the longest. The first job to awake is the one that has been asleep the longest.

Priority queue will let you have different execution priorities: the lowest priority process will sleep first, the highest priority process will awake first.

  1. Don’t reorder the queues externally, trust the data structure to do it itself.

  2. Presumably your os simulator has a clock of its own, which you can use instead of the real world clock time.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement