Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Proper output for Non preemptive scheduling problem

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    int process[] = {
                    1, 2, 3, 4, 5
                };
                int arrival[] = {
                    0, 6, 8, 9, 12
                };
                int burst[] = {
                    6, 5, 3, 1, 2
                };


    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}.

    Example 1:

    Input
    int process[] = {1, 2, 3, 4};
    int arrival[] = {0, 2, 4, 5};
    int burst[] = {7, 4, 1, 4};

    Output: The function getWaitingList() returns {0,6,3,7}

    Explaination: Process 1 3 2 4
    0 7 8 12 16 starting time of process
    The waiting time should be : 0,6,3,7

    Example 2:

    Input
    int process[] = {1, 2, 3};
    int arrival[] = {0, 4, 8};
    int burst[] = {6, 2, 1};

    Output: The function getWaitingList() returns {0,2,0}

    Example 3:

    Input int process[] = {1, 2, 3, 4};
    int arrival[] = {2, 0, 4, 5};
    int burst[] = {7, 4, 1, 4};


    Output: The function getWaitingList() returns {0}

    Hint for the problem:

    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}


    Program for the above Question:

    public class NonPreemptiveScheduling
    {
    	public int[] getWaitingList(int[] process, int[] arrival, int[] burst)
            {      int n=process.length;
                    int s=arrival[0]+burst[0];int t,i,j; 
    	    	if(arrival[0]!=0)
                    {
                     return new int[]{0};
                    }
                     for(i=0;i<n-1;i++)
                    {
                       if(arrival[i+1]>arrival[i])
                       {
                       }
                        else
                       {
                           return new int[]{0};
                       } 
     
                    }
     
                     for(i=0;i<n;i++)
                    {
                      if(burst[i]>0 && burst[i]<10)
                      {
     
                      }
                      else
                      {
                        return new int[]{0};
                      }
                    }
                    int[] wait1=new int[n];
                    int[] wait=new int[n];
                    int[] tot=new int[n];
                    int[] ar1=new int[n];
                    int[] bu1=new int[n];
                    int[] pro=new int[n];
                    wait[0]=0;
                    for(i=0;i<n;i++)
                    {
                       tot[i]=arrival[i]+burst[i];
                    }
                    for(i=0;i<n;i++)
                    {
                       ar1[i]=arrival[i];
                    }
                    for(i=0;i<n;i++)
                    {
                       bu1[i]=burst[i];
                    }
                    for(i=0;i<n;i++)
                    {
                       pro[i]=process[i];
                    }
     
                    for(i=1;i<n;i++)
                    {
                     for(j=i;j<n;j++)
                    {  
                     if(tot[i]>tot[j])
                     {
                      t=tot[j];
                      tot[j]=tot[i];
                      tot[i]=t;
     
                      t=pro[j];
                      pro[j]=pro[i];
                      pro[i]=t;
     
                      t=ar1[j];
                      ar1[j]=ar1[i];
                      ar1[i]=t;
     
                      t=bu1[j];
                      bu1[j]=bu1[i];
                      bu1[i]=t;
     
                     }
                    }
                  }
                     for(i=1;i<n;i++)
                    {
                      wait[i]=s-ar1[i];
                      s=s+bu1[i];
                    }
                    for(i=0;i<n;i++)
                    {
                      wait1[pro[i]-1]=wait[i];
                    }
     
    		return  wait1;
    	}
     
    	public static void main(String args[])
    	{
    		//testcase 1:
    		try
    		{
    			int process[] = {1, 2, 3, 4};
    			int arrival[] = {0, 2, 4, 5};
    			int burst[] = {7, 4, 1, 4};
    			int[] output = new NonPreemptiveScheduling().getWaitingList(process, arrival, burst);
    			if(output!=null && output.length>0)
    			for (int i = 0; i < output.length; i++)
    			{
    				System.out.print(output[i]+", ");
    			}
    			System.out.println();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
     
    		//testcase 2:
    		try
    		{
    			int process[] = {1, 2, 3};
    			int arrival[] = {0, 4, 8};
    			int burst[] = {6, 2, 1};
    			int[] output = new NonPreemptiveScheduling().getWaitingList(process, arrival, burst);
    			if(output!=null && output.length>0)
    			for (int i = 0; i < output.length; i++)
    			{
    				System.out.print(output[i]+", ");
    			}
    			System.out.println();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
     
    		//testcase 3:
    		try
    		{
    			int process[] = {1, 2, 3, 4};
    			int arrival[] = {2, 0, 4, 5};
    			int burst[] = {7, 4, 1, 4};
    			int[] output = new NonPreemptiveScheduling().getWaitingList(process, arrival, burst);
    			if(output!=null && output.length>0)
    			for (int i = 0; i < output.length; i++)
    			{
    				System.out.print(output[i]+", ");
    			}
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Last edited by Deep_4; November 7th, 2012 at 01:11 PM.


Similar Threads

  1. How to solve NullPointerException in runtime?
    By Koren3 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 20th, 2017, 11:25 PM
  2. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  3. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM
  4. [SOLVED] Interesting error cipher while sending message
    By Koren3 in forum Java Networking
    Replies: 0
    Last Post: April 29th, 2009, 09:54 AM
  5. SOAP Message Factory response error
    By Buglish in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 6th, 2008, 01:42 PM