Learn-dsa..in 30 days!



























CC-17 : Simulate round robin CPU time allocator using a CircularSingleLinkedList.

Description:

Write and test code for round robin CPU time allocator simulator:

Round robin CPU timeslot allocator (Circular Single Link List) has a set of nodes that represent processes that need CPU time “10->6->3->9->4”. Data value of each nodes represents how many more seconds of CPU time is required by the process.
1 second CPU time should be allocated to each process, in a round robin manner, until the processes required CPU time becomes 0.
Each time CPU is allocated, required node’s required CPU time should be reduced by 1 second and data value of node should be updated with this new required time.

Test cases and expected outputs:

Input Parameters Expected outputs
Add these nodes one by one to CPU Time Allocator and check its contents after each add operation: 4->5->7->4->5 Nodes with CPU time required:4->3->7->4->5 (last node points to first node)
Nodes with CPU time required:3->3->7->4->5 (last node points to first node)
Nodes with CPU time required:3->2->7->4->5 (last node points to first node)
Nodes with CPU time required:3->2->6->4->5 (last node points to first node)
Nodes with CPU time required:3->2->6->3->5 (last node points to first node)
Nodes with CPU time required:3->2->6->3->4 (last node points to first node)
Nodes with CPU time required:2->2->6->3->4 (last node points to first node)
Nodes with CPU time required:2->1->6->3->4 (last node points to first node)
Nodes with CPU time required:2->1->5->3->4 (last node points to first node)
Nodes with CPU time required:2->1->5->2->4 (last node points to first node)
Nodes with CPU time required:2->1->5->2->3 (last node points to first node)
Nodes with CPU time required:1->1->5->2->3 (last node points to first node)
Nodes with CPU time required:1->5->2->3 (last node points to first node)
Nodes with CPU time required:1->4->2->3 (last node points to first node)
Nodes with CPU time required:1->4->1->3 (last node points to first node)
Nodes with CPU time required:1->4->1->2 (last node points to first node)
Nodes with CPU time required:4->1->2 (last node points to first node)
Nodes with CPU time required:3->1->2 (last node points to first node)
Nodes with CPU time required:3->2 (last node points to first node)
Nodes with CPU time required:3->1 (last node points to first node)
Nodes with CPU time required:2->1 (last node points to first node)
Nodes with CPU time required:2 (last node points to first node)
Nodes with CPU time required:1 (last node points to first node)
Circular Linked list empty

Pseudocode:

Initialize a CircularSingleLinkedList with list of processes shown above.
Use loops to iterate through the nodes list using next links.
Once node is reached, reduce its data value by -1 (i.e require cpu time is reduced).
Once data value reaches 0, remove node from Circular Single Linked List.
Once all nodes are removed from CircularSingleLinked List, exit the program.

Code:

public class CircularSingleLinkedListCPUAllocator {
	
CircularSingleLinkedList circularSingleLinkedList=this.new CircularSingleLinkedList();
		

public  void circularSingleLinkedListCpuAllocator() throws Exception{
	SingleLinkedListNode currentNode=circularSingleLinkedList.getFirstNode();
	while (circularSingleLinkedList.getFirstNode()!=null) {
		SingleLinkedListNode nextNode=currentNode.getNextNode();
		currentNode.setNodeData(currentNode.getNodeData()-1);
		if (currentNode.getNodeData()<=0) {
			circularSingleLinkedList.remove(0);
		}
		currentNode=nextNode;		
		circularSingleLinkedList.printSingleLinkList("Nodes with CPU time required:");
	}		
}
}

Click here to download and run code and test cases !