All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Tuesday 26 August 2014

Round Robin Algorithm

#include<iostream>
using namespace std;
int main()
{
int n,i,ts,rp,t;
int twt=0,tat=0,f=0;
int bt[i],rt[i];
cout<<"Enter No of Process :";
cin>>n;
rp=n;
for(i=0;i<n;i++)
{
cout<<"Enter Burst time for Process"<<i<<": ";
cin>>bt[i];
rt[i]=bt[i];
}
cout<<"Enter Time Slice: ";
cin>>ts;
cout<<"\nP_NO\tB_Time\tW_time\tT_Time\n";
for(t=0,i=0;rp!=0;)
{
if(rt[i]>ts)
{
rt[i]-=ts;
t+=ts;
}
else if(rt[i]<=ts&&rt[i]>0)
{
t+=rt[i];
rt[i]=0;
f=1;
}

if(rt[i]==0&&f==1)
{
rp--;
cout<<"P"<<i<<"\t"<<bt[i]<<"\t"<<t-bt[i]<<"\t"<<t<<endl;
twt+=t-bt[i];

tat+=t;
f=0;
}
if(i==n-1)
i=0;
else
i++;
}
cout<<"\nTotal Waiting Time="<<twt<<"\t\t";
cout<<"Average Waiting time="<<twt/(float)n;
cout<<"\nTotal Turnaround Time="<<tat<<"\t";
cout<<"Average Turnaround Time="<<tat/(float)n;
return 0;
}

Friday 8 August 2014

Shortest Job First (Non-PreEmptive)

/*Shortest Job First Scheduling Progrmam (Non-PreEmptive) */
#include<iostream>
using namespace std;
int main()
{
    int i,j,n,total=0,temp,ptemp;
    int wtime[10],btime[10],process[10];
    float avg=0;
    cout<<"\nEnter number of Processes: ";
    cin>>n;
    for(i=0;i<n;i++)
    {
    process[i]=i;
        cout<<"Enter P"<<i+1<<" Burst Time:";
        cin>>btime[i];
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(btime[i]>btime[j])
            {
                temp=btime[i];
                btime[i]=btime[j];
                btime[j]=temp;
                ptemp=process[i];
                process[i]=process[j];
                process[j]=ptemp;
            }
        }
    }
        wtime[0]=0;
    for(i=1;i<n;i++)
    {
        wtime[i]=wtime[i-1]+btime[i-1];
        total=total+wtime[i];
    }
    avg=(float)total/n;
    cout<<"\nP_ID\tB_TIME\t W_TIME\n";
    for(i=0;i<n;i++)
        cout<<process[i]+1<<"\t"<<btime[i]<<"\t"<<wtime[i]<<endl;
    cout<<"\nTotal Waiting Time: "<<total;
cout<<"\nAverage Waiting Time: "<<avg;
    return 0;
}

Tuesday 5 August 2014

FCFS Program

#include<iostream.h>
#include<conio.h>
void main()
{
    int i, n, process[10], total=0, wtime[10], ptime[10],turntotal[10];
    float avg=0;
    clrscr();
    cout<<"\nEnter number of Processes:";
    cin>>n;
    for(i=0;i<n;i++)
    {
cout<<"Enter Process "<<i+1<<" ID:";
cin>>process[i];
cout<<"Enter Process "<<i+1<<" Burst Time:";
cin>>ptime[i];
    }
    wtime[0]=0;
    for(i=1;i<n;i++)
    {
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
    }
    for(i=0;i<n;i++)
    {
    turntotal[i]=ptime[i]+wtime[i];
    }
    avg=(float)total/n;
    cout<<"\nP_ID\t P_TIME\t W_TIME\t TURN_Time\n";
    for(i=0;i<n;i++)
cout<<process[i]<<"\t"<<ptime[i]<<"\t"<<wtime[i]<<"\t"<<turntotal[i]<<"\n";
    cout<<"\nTotal Waiting Time:"<<total;
    cout<<"\nAverage Waiting Time:"<<avg;
    getch();
}