import java.util.*;
class process
{
int pid,allocated,total,req,used=0;
process()
{
Scanner src=new Scanner(System.in);
System.out.println("enter the resources allocated");
allocated=src.nextInt();
System.out.println("enter the total required resources");
total=src.nextInt();
req=total-allocated;
}}
class test
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("Enter no. of processes");
int n=src.nextInt();
process obj[]= new process[n];
for(int i=0;i<=n-1;i++)
{
obj[i]=new process();
obj[i].pid=i+1;
}
System.out.println("enter free resources");
int free=src.nextInt();
obj=sort(obj);
display(obj);
int safe[]=new int[n];
int j=0,flag=0;
for(int i=0;i<=n-1;i++)
{
if(obj[i].req<=free&&obj[i].used==0)
{
free=free+obj[i].allocated;
obj[i].used=1;
safe[j]=obj[i].pid;
j++;
}
else
{
flag=1;
System.out.println("\n\nunsafe state");
break;
}
}
System.out.println("process executed as:");
for(int i=0;i<=j-1;i++)
System.out.print(safe[i]+" ");
}
static process[] sort(process temp[])
{
for(int i=0;i<=temp.length-2;i++)
for(int j=i+1;j<=temp.length-1;j++)
if(temp[i].req>temp[j].req)
{
process temp1=temp[i];
temp[i]=temp[j];
temp[j]=temp1;
}
return temp;
}
static void display(process temp[])
{
for(int i=0;i<=temp.length-1;i++)
{
System.out.println();
System.out.println("pid\tallocated\ttotalreq\treq");
System.out.println(temp[i].pid+"\t\t"+temp[i].allocated+"\t\t"+temp[i].total+"\t\t"+temp[i].req);
}
}
}
No comments:
Post a Comment