The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Proper output for Non preemptive scheduling problem

Dear sir,

following is the question of the assignment provided to me from my college.i hav developed the program for the same question. My problem is that i am getting exact o/p as mentioned in the examples provided with question.But my lecturer is saying that it is not providing the exact o/p according to i/p's

The expected output should be : {0, 0, 4, 2, 3 }
The waiting time should not be negative.

But my program is providing the o/p {0,4,-3,-1,3}

Please help me for it. Please see me program below the question and guide me what i should do for it.

Question:Write a program to list the waiting time of the processes based on the arrival time and the burst time under non preemptive scheduling. In the Non preemptive Scheduling,the current process keeps the CPU until it releases the CPU by terminating. the operating system never initiates a context switch from a running process to another process.

The Prototype of the function is

int[] getWaitingList(int[] process,int[] arrival,int[] burst)

where process represents list of processes,arrival represents the arrival time of the processes and burst represents the execution time of the processes.

the function returns the waiting time of the processes based on the arrival and burst time.

Constraints

the arrival time of the first process should be started with 0, it never contain duplicate, the current process arrival time should be greater than previous process else return{0}.

the burst time should be greater than 0 and less than 10 else return{0}.

For the input,
int process[] = {1,2,3,4,5,6,7};
int arrival[] = {0,2,4,6,8,10,12};
int burst[] = {6,2,5,4,1,3,5};
The correct output is {0, 4, 4, 8, 5, 8, 9}
Explanation:
1) The arrival time of the first process = 0, hence waiting time = 0 and its burst time = 6. Hence time taken by CPU = 6 unit.
2) In the mean time, the processes 2, 3 and 4 arrived. CPU will allocate the process, which has the least (arrival time + burst time). Hence, for process 2 it is (2+ 2) = 4, for process 3 it is (4 + 5) = 9 and for process 4 it is (6 + 4) = 10. Hence CPU will allocate the process 2.
3) The arrival time of the 2nd process = 2, hence waiting time = (time taken by the previous process - arrival time of the 2nd process ) = (6-2) = 4 and its burst time = 2. Hence time taken by CPU = (6 + 2) = 8 unit.
4) In the mean time, the processes 5 arrived. CPU will allocate the process, which has the least (arrival time + burst time). Here (arrival time + burst time) for the process 3, 4 and 5 are 9, 10 and respectively. The process 3 and 5 has same (arrival time + burst time), but CPU will allocate process 3 because of it arrived before 5th one.
5) The arrival time of the 3rd process = 4, hence waiting time = (time taken by the previous process - arrival time of this process ) = (8-4) = 4 and its burst time = 5. Hence time taken by CPU = (8 + 5) = 13 unit.
6) In the mean time, the processes 6 and 7 arrived. But the 5th process has least (arrival time + burst time) = 9. CPU will allocate it before 4th and other process.
7) The arrival time of the 5th process = 8, hence waiting time = (time taken by the previous process - arrival time of this process ) = (13-8) = 5 and its burst time = 1. Hence time taken by CPU = (13 + 1) = 14 unit.
8) Then the 4th process allocated. After that 6th and 7th process allocated by CPU.
9) The arrival time of the 4th process = 6, hence waiting time = (time taken by the previous process - arrival time of this process ) = (14-6) = 8 and its burst time = 4. Hence time taken by CPU = (14 + 4) = 18 unit.
10) The arrival time of the 6th process = 10, hence waiting time = (time taken by the previous process - arrival time of this process ) = (18-10) = 8 and its burst time = 3. Hence time taken by CPU = (18 + 3) = 21 unit.
11) The arrival time of the 7th process = 12, hence waiting time = (time taken by the previous process - arrival time of this process ) = (21-12) = 9 and its burst time = 5. Hence time taken by CPU = (21 + 5) = 26 unit.
12) Now arrange the waiting time according to the arrived process.
Hence the answer is : {0, 4, 4, 8, 5, 8, 9}