Sunday, February 14, 2010

Shortest Job First(with preemption)

import java.util.*;

class shortest
{
int at,bt,st,wt,pid,flag=0,used=0;
shortest()
{
Scanner src=new Scanner(System.in);
System.out.println("enter arrival time:");
at=src.nextInt();
System.out.println("enter Burst time:");
bt=src.nextInt();

}

}
class test2
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("enter the no.of process");
int n=src.nextInt();
shortest obj[]=new shortest[n];
for(int i=0;i<=n-1;i++)
{
    System.out.println("process"+(i+1));
    System.out.println("__________________");
obj[i]=new shortest();
obj[i].pid=i+1;
}

obj=sorting(obj);
obj=sorting1(obj);
obj=sorting2(obj);
obj=sorting3(obj);
display(obj);
}

static shortest[] sorting(shortest temp[])
{
for(int i=0;i<=temp.length-2;i++)
for(int j=i+1;j<=temp.length-1;j++)
{
if(temp[i].at>temp[j].at)
{
shortest temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}

static shortest[] sorting1(shortest temp[])
{
for(int i=0;i<=temp.length-2;i++)
for(int j=i+1;j<=temp.length-1;j++)
{
if(temp[i].bt>temp[j].bt)
{
shortest temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}
static shortest[] sorting2(shortest temp[])
{
    int time=0,i=0;
    while(time<=25)
    {
        int flag2=0;
        for(i=0;i<=temp.length-1;i++)
        {
            if(temp[i].at<=time&&temp[i].used==0)
            {
                temp[i].flag=1;
                flag2=1;
                break;
            }
        }
        if(flag2==1)
        {
            temp[i].used=1;
            temp[i].st=time;
            time=time+temp[i].bt;
        }
        else
        time++;
    }
    return temp;
}
    static shortest[] sorting3(shortest temp[])
{
for(int i=0;i<=temp.length-2;i++)
for(int j=i+1;j<=temp.length-1;j++)
{
if(temp[i].st>temp[j].st)
{
shortest temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}    
static void display(shortest temp[])
{
temp[0].st=0;
temp[0].wt=0;

for(int i=1;i<=temp.length-1;i++)
{


temp[i].wt=temp[i].st-temp[i].at;
}

System.out.println("pid\tarrival\tburst\tstart\twait");
for(int i=0;i<=temp.length-1;i++)
{
System.out.println(temp[i].pid+"\t"+temp[i].at+"\t"+temp[i].bt+"\t"+temp[i].st+"\t"+temp[i].wt);
}
}
}

1 comment:

  1. This is not Shortest Job First with preemption
    this is without preemption. ?!

    ReplyDelete