Monday, November 26, 2012

CPU Scheduling Algorithms.--- First come First serve(fcfs)

/* Simulate the following cpu scheduling algorithms.
    C. First come First serve(fcfs)    */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,bt[10],n,twt=0,z[20];
    int wt[10],sum=0,sum1=0;
    float avgt=0.00;
    clrscr();
    printf("\n Enter no. of Processes ::");
    scanf("%d",&n);
    printf("\n Enter the %d burst times::",n);
    for(i=0;i<n;i++)
    {
        printf("\n\n Enter time for process%d::",i+1);
        scanf("%d",&bt[i]);
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            wt[i]=0;
            sum=sum+bt[i];
        }
        else
        {
            wt[i]=sum;
            sum=sum+bt[i];
        }
        sum1=sum1+bt[i];
        z[i]=sum1;
    }
    printf("\n\n----------------------------------------------\n");
    printf("\n\tPNo\tbt\twt\n");
    printf("\n----------------------------------------------\n");
    for(i=0;i<n;i++)
    {
        twt+=wt[i];
        printf("\n\n\tP%d\t%d\t%d\n",i+1,bt[i],wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<n-1;i++)
        printf("%4d",z[i]);
    printf("\n\n Total waiting time is ::%d",twt);
    printf("\n\n Average waiting time is ::%f",avgt);
    getch();
}
 /*Input and OutPut:-

 Enter no. of Processes ::3
 Enter the 3 burst times::
 Enter time for process1::24
 Enter time for process2::3
 Enter time for process3::3
----------------------------------------------
    PNo     bt      wt
----------------------------------------------
    P1      24      0
    P2      3       24
    P3      3       27

----------------------------------------------
Gannt Chart ::

    0  24  27

 Total waiting time is ::51
 Average waiting time is ::17.000000  */


CPU Scheduling Algorithms. --- First come First serve(fcfs)

/* Simulate the following cpu scheduling algorithms.
    C. First come First serve(fcfs) when the arrival times are given    */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,bt[10],n,twt=0,z[20],at[10];
    int wt[10],sum=0,sum1=0;
    float avgt=0.00;
    clrscr();
    printf("\n Enter no. of Processes ::");
    scanf("%d",&n);
    printf("\n Enter the %d burst times::",n);
    for(i=0;i<n;i++)
    {
        printf("\n\n Enter burst and arrival times for process%d::",i+1);
        scanf("%d%d",&bt[i],&at[i]);
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            wt[i]=0;
            sum=sum+bt[i];
        }
        else
        {
            wt[i]=sum-at[i];
            sum=sum+bt[i];
        }
        sum1=sum1+bt[i];
        z[i]=sum1;
    }
    printf("\n\n----------------------------------------------\n");
    printf("\n\tPNo\tbt\tat\twt\n");
    printf("\n----------------------------------------------\n");
    for(i=0;i<n;i++)
    {
        twt+=wt[i];
        printf("\n\n\tP%d\t%d\t%d\t%d\n",i+1,bt[i],at[i],wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<n-1;i++)
        printf("%4d",z[i]);
    printf("\n\n Total waiting time is ::%d",twt);
    printf("\n\n Average waiting time is ::%f",avgt);
    getch();
}
 /*Input and OutPut:-
 Enter no. of Processes ::4
 Enter the 4 burst times::
 Enter burst and arrival times for process1::10 0
 Enter burst and arrival times for process2::4 1
 Enter burst and arrival times for process3::6 2
 Enter burst and arrival times for process4::3 4
----------------------------------------------
    PNo     bt      at      wt
----------------------------------------------
    P1      10      0       0
    P2      4       1       9
    P3      6       2       12
    P4      3       4       16
----------------------------------------------
Gannt Chart ::
    0  10  14  20

 Total waiting time is ::37
 Average waiting time is ::9.250000*/


Page Replacement algorithm using FIFO.

/* Simulate the page replacement algorith using FIFO.                */

#include<stdio.h>
#include<conio.h>
void main()
{
    int ref[50],i,j,fault=0,count=0,frame[5],n;
    int temp;
    clrscr();
    printf("\n Enter the no. of Frames :;");
    scanf("%d",&n);
    printf("\nEnter the reference string and end with -1 ::");
    scanf("%d",&temp);
    while(temp!=-1)
    {
        ref[count++]=temp;
        scanf("%d",&temp);
    }
    for(i=0;i<n;i++)
        frame[i]=-1;
    for(i=0;i<count;i++)
    {
        for(j=0;j<n;j++)
            if(frame[j]==ref[i])
                break;
        if(j==n)
        {
            frame[fault%n]=ref[i];
            fault++;
        }
        printf("\n\n After inserting %2d the Frame status is ::",ref[i]);
        for(j=0;j<n;j++)
            printf("%4d",frame[j]);
    }
    printf("\n\n\t Total no. of page faults ::%d",fault);
    getch();
}
/* Input and Output :-

 Enter the no. of Frames :;3

 Enter the reference string and end with -1 ::1 2 3 4 1 2 5 1 2 3 4 5 -1

 After inserting  1 the Frame status is ::   1  -1  -1

 After inserting  2 the Frame status is ::   1   2  -1

 After inserting  3 the Frame status is ::   1   2   3

 After inserting  4 the Frame status is ::   4   2   3

 After inserting  1 the Frame status is ::   4   1   3

 After inserting  2 the Frame status is ::   4   1   2

 After inserting  5 the Frame status is ::   5   1   2

 After inserting  1 the Frame status is ::   5   1   2

 After inserting  2 the Frame status is ::   5   1   2

 After inserting  3 the Frame status is ::   5   3   2

 After inserting  4 the Frame status is ::   5   3   4

 After inserting  5 the Frame status is ::   5   3   4

     Total no. of page faults ::9                    */