Saturday, February 20, 2010

Priority Scheduling

import java.util.*;

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

}

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

obj=sorting(obj);
obj=sorting1(obj);
obj=sorting2(obj);
obj=sorting3(obj);
display(obj);
}
static process[] sorting(process 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)
{
process temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}
static process[] sorting1(process temp[])
{
for(int i=0;i<=temp.length-2;i++)
for(int j=i+1;j<=temp.length-1;j++)
{
if(temp[i].p<temp[j].p)
{
process temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}
static process[] sorting2(process temp[])
{
    int time=0,i=0;
    while(time<=30)
    {
        int flag=0;
        for(i=0;i<=temp.length-1;i++)
        {
            if(temp[i].at<=time&&temp[i].used==0)
            {
               
                flag=1;
                break;
            }
        }
        if(flag==1)
        {
            temp[i].used=1;
            temp[i].st=time;
            time=time+temp[i].bt;
        }
        else
        time++;
    }
    return temp;
}
static process[] sorting3(process 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)
{
process temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}

static void display(process 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\tpriority\tarrival\tburst\tstart\twait");
for(int i=0;i<=temp.length-1;i++)
{
System.out.println(temp[i].pid+"\t"+temp[i].p+"\t\t"+temp[i].at+"\t"+temp[i].bt+"\t"+temp[i].st+"\t"+temp[i].wt);
}
}
}

No comments:

Post a Comment