Tuesday, March 2, 2010

Bankers algorithm

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);
}
}
}

Sunday, February 21, 2010

Job Sequencing

import java.util.*;

class job
{
int p,d,jid;
job()
{
Scanner src=new Scanner(System.in);
System.out.println("enter profit");
p=src.nextInt();
System.out.println("enter deadline");
d=src.nextInt();
}
}
class slot
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("enter the no.of jobs");
int n=src.nextInt();
job obj[]=new job[n];
for(int i=0;i<=n-1;i++)
{
    System.out.println("job"+(i+1));
    System.out.println("__________________");
obj[i]=new job();
obj[i].jid=i+1;
}
obj=sorting(obj);
seq(obj);
}
static job[] sorting(job 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)
{
job temp2=temp[i];
temp[i]=temp[j];
temp[j]=temp2;
}
}
return temp;
}
static void seq(job temp[])
{
int maxd=0,profit=0;
for(int i=0;i<=temp.length-1;i++)
{
if(temp[i].d>=maxd)
{
maxd=temp[i].d;
}
}
System.out.println("max deadline:"+maxd);
int slot[][]=new int[2][maxd];

for(int i=0;i<=maxd-1;i++)
slot[0][i]=0;

for(int i=0;i<=temp.length-1;i++)
{
    int fill=0,at=0;
for(int j=0;j<=maxd-1;j++)
{
if(slot[0][j]==0&&j<=temp[i].d-1)
{fill=1;
at=j;
}
}

if(fill==1)
{
slot[0][at]=1;
System.out.println("job "+temp[i].jid+"filled at "+at);
slot[1][at]=temp[i].jid;
profit=profit+temp[i].p;
}
}
System.out.println("max profit:"+profit);
}
}   

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);
}
}
}

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);
}
}
}

Saturday, February 13, 2010

Shortest job first(without preemption)

import java.util.*;

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

}
class test
{
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;
}
//shortest t[];
obj=sorting(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].bt>temp[j].bt)
{
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;
int w=0;
for(int i=1;i<=temp.length-1;i++)
{
w=w+temp[i-1].bt;
temp[i].st=w;
}

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

Monday, February 1, 2010

ps

#include<iostream.h>




int main()
{
int n,i;
cout<<"enter the no. of process";
cin>>n;
int at[n],bt[n],wt[n],st[n];
for(i=0;i<=n-1;i++)
{
cout<<"process:"<<i+1<<endl;
cout<<"-------------------"<<endl;
cout<<"enter the arrival time:";
cin>>at[i];
cout<<"enter the burst time:";
cin>>bt[i];
}
st[0]=0;
wt[0]=0;
int w=0;
for(i=0;i<=n-1;i++)
{
    w=w+bt[i];
    wt[i]=w-(at[i]+bt[i]);
st[i]=wt[i]+at[i];

}
double avg;
avg=(st[n-1]*1.0)/n;

cout<<"\n\nProcess\tArrival\tbusrt\twaiting\tstart\n";
for(i=0;i<=n-1;i++)
{
cout<<i<<"\t"<<at[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<st[i]<<"\n";
}
cout<<"\n\nAverage time:"<<avg;
return 0;
}

Wednesday, October 21, 2009

ram simulation

http://pastebin.com/m736f3373