Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Specification of your program 1. Your program should be named as: RoundRobin.java 2. The input and output of your program. There are two inputs to

Specification of your program 1. Your program should be named as: RoundRobin.java 2. The input and output of your program. There are two inputs to your program. One is a text file, that contains a list of hprocess id, processs CPU time needs tuples. Each line of the text file is one tuple. The two fileds of each tuple is separated by the comma symbol. You can assume all process ids are distinct. The file name should be supplied to your program as a command line parameter. The other input, which is also supplied as a parameter to your program, is a positive integer number representing the length of each CPU service. The output should be a sequence of process ids that are in the ascending order of their termination time, separated by the comma symbol.

We require all processes be served at the order of their ids in each round. Implementation of your program You need to construct a circular doubly linked list to organize all the processes from the input file, where each node represents a process. The nodes in the circular doubly linked list are sorted on their process ids, so that in each round of service, the processes will be served at the ascending order of their ids. Note that it is NOT allowed that you first store and sort the processes in a temporary array and then load them into the circular doubly linked list. You must insert the given processes into the correct positions in the circular doubly linked list when they are read from the input file.

Your program will then scan the circular linked list round by round starting from the process with the smallest id. Every time after a process is served for the specified amount of time, if the process has no more CPU time needs meaning the process has terminated, then the linked list node representing that process must be deleted from the linked list and the corresponding process id should be printed on the screen. The deletion of any terminated process must be an O(1)-time operation.

Code so far

package prog3; import java.io.File; import java.util.Arrays; import java.util.Scanner;

public class RoundRobin { private static File inFile; public static void main(String[]args)throws Exception { File inf = new File(args[0]); if (args.length<2) throw new IllegalArgumentException ("you need three arguments"); Scanner fin = new Scanner(inf); int count = 0; while(fin.hasNext()) { fin.nextLine(); count++; } //putting all the numbers into an array fin = new Scanner(inf); int[] array = new int[count]; for(int x = 0; x

---------------------------------------------

package prog3;

class Node { protected int data; protected Node next, prev; public Node() { next = null; prev = null; data = 0; } public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } public void setLinkNext(Node n) { next = n; } public void setLinkPrev(Node p) { prev = p; } public Node getLinkNext() { return next; } public Node getLinkPrev() { return prev; } public void setData(int d) { data = d; } public int getData() { return data; } }

-------------------------------------------------------------------------------------

package prog3;

class linkedList { protected Node start; protected Node end ; public int size; /* Constructor */ public linkedList() { start = null; end = null; size = 0; } /* Function to check if list is empty */ public boolean isEmpty() { return start == null; } /* Function to get size of list */ public int getSize() { return size; } /* Function to insert element at begining */ public void insertAtStart(int val) { Node nptr = new Node(val, null, null); if (start == null) { nptr.setLinkNext(nptr); nptr.setLinkPrev(nptr); start = nptr; end = start; } else { nptr.setLinkPrev(end); end.setLinkNext(nptr); start.setLinkPrev(nptr); nptr.setLinkNext(start); start = nptr; } size++ ; } /*Function to insert element at end */ public void insertAtEnd(int val) { Node nptr = new Node(val, null, null); if (start == null) { nptr.setLinkNext(nptr); nptr.setLinkPrev(nptr); start = nptr; end = start; } else { nptr.setLinkPrev(end); end.setLinkNext(nptr); start.setLinkPrev(nptr); nptr.setLinkNext(start); end = nptr; } size++; } /* Function to insert element at position */ public void insertAtPos(int val , int pos) { Node nptr = new Node(val, null, null); if (pos == 1) { insertAtStart(val); return; } Node ptr = start; for (int i = 2; i <= size; i++) { if (i == pos) { Node tmp = ptr.getLinkNext(); ptr.setLinkNext(nptr); nptr.setLinkPrev(ptr); nptr.setLinkNext(tmp); tmp.setLinkPrev(nptr); } ptr = ptr.getLinkNext(); } size++ ; } /* Function to delete node at position */ public void deleteAtPos(int pos) { if (pos == 1) { if (size == 1) { start = null; end = null; size = 0; return; } start = start.getLinkNext(); start.setLinkPrev(end); end.setLinkNext(start); size--; return ; } if (pos == size) { end = end.getLinkPrev(); end.setLinkNext(start); start.setLinkPrev(end); size-- ; } Node ptr = start.getLinkNext(); for (int i = 2; i <= size; i++) { if (i == pos) { Node p = ptr.getLinkPrev(); Node n = ptr.getLinkNext(); p.setLinkNext(n); n.setLinkPrev(p); size-- ; return; } ptr = ptr.getLinkNext(); } } /* Function to display status of list */ public void display() { System.out.print(" Circular Doubly Linked List = "); Node ptr = start; if (size == 0) { System.out.print("empty "); return; } if (start.getLinkNext() == start) { System.out.print(start.getData()+ " <-> "+ptr.getData()+ " "); return; } System.out.print(start.getData()+ " <-> "); ptr = start.getLinkNext(); while (ptr.getLinkNext() != start) { System.out.print(ptr.getData()+ " <-> "); ptr = ptr.getLinkNext(); } System.out.print(ptr.getData()+ " <-> "); ptr = ptr.getLinkNext(); System.out.print(ptr.getData()+ " "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions