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                    */












                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

File allocation strategies --- Index file Allocation Method

/* 2.simulate all File allocation strategies
    a. Index file Allocation Method               */


#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
    int b[20],b1[20],i,j,blocks[20][20],sz[20];
    char F[20][20],S[20],ch;
    clrscr();
    printf("\n Enter no. of Files ::");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter file %d name ::",i+1);
        scanf("%s",&F[i]);
        printf("\n Enter file%d size(in kb)::",i+1);
        scanf("%d",&sz[i]);
        printf("\n Enter blocksize of File%d(in bytes)::",i+1);
        scanf("%d",&b[i]);
    }
    for(i=0;i<n;i++)
    {
        b1[i]=(sz[i]*1024)/b[i];
        printf("\n\nEnter blocks for file%d",i+1);
        for(j=0;j<b1[i];j++)
        {
            printf("\n Enter the %dblock ::",j+1);
            scanf("%d",&blocks[i][j]);
        }
    }
    do
    {
        printf("\nEnter the Filename ::");
        scanf("%s",&S);
        for(i=0;i<n;i++)
        {
            if(strcmp(F[i],S)==0)
            {
                printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
                printf("\n---------------------------------------------\n");
                printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
                for(j=0;j<b1[i];j++)
                    printf("%d->",blocks[i][j]);
            }
        }
        printf("\n---------------------------------------------\n");
        printf("\nDo U want to continue ::(Y:n)");
        scanf("%d",&ch);
    }while(ch!=0);
}
/*Input and Output;-

 Enter no. of Files ::2

 Enter file 1 name ::x.c

 Enter file1 size(in kb)::1

 Enter blocksize of File1(in bytes)::512

 Enter file 2 name ::y.c

 Enter file2 size(in kb)::1

 Enter blocksize of File2(in bytes)::512

 Enter blocks for file1

 Enter the 1block ::1000

 Enter the 2block ::1001

 Enter blocks for file2

 Enter the 1block ::2000

 Enter the 2block ::2001

 Enter the Filename ::x.c

Fname   Fsize   Bsize   Nblocks Blocks
---------------------------------------------
x.c     1       512     2       1000->1001->
---------------------------------------------

Do U want to continue ::(Y:n)1

Enter the Filename ::y.c

Fname   Fsize   Bsize   Nblocks Blocks
---------------------------------------------
y.c     1       512     2       2000->2001->
---------------------------------------------

Do U want to continue ::(Y:n)0
*/

FIFO PAGE REPLACEMENT ALGORITHM

/*A PROGRAM TO SIMULATE FIFO PAGE REPLACEMENT ALGORITHM */

#include<stdio.h>
#include<conio.h>
void main()
{
    int rs[30],pf[10],ctr[10]={0};
    int n,i,j,k,m,c=0,g,p,pos,count,count1=0,nr;
    clrscr();
    printf("\nEnter Howmany Frames The Page Have::");
    scanf("%d",&n);
    printf("\nEnter Howmany Numbers The Reference Strng Have::");
    scanf("%d",&nr);
    printf("\nEnter Reference String::");
    for(i=0;i<nr;i++)
    {
        scanf("%d",&rs[i]);
        for(k=0;k<10;k++)
            if(rs[i]==k)
            {
                ctr[k]++;
                break;
            }
    }
    printf("\nSIMULATION OF PAGE REPLACEMENT ALGORITHM::FIFO\n");
    printf("\n\nThe Pages Containing Page Faults One By One::\n\n");
    //pf[0]=0;
    for(i=0;i<n;i++)
    {

            for(m=0;m<=i&&i!=0;m++)
                if(pf[m]==rs[i])
                {
                    i++;
                    count=1;
                    break;
                }
            if(count==1)
                continue;
            pf[i]=rs[i];
            for(k=0;k<=i;k++)

                printf("\t%d\n",pf[k]);
            c++;


            printf("\t-------\n");



    }


    do
    {
        count1=0;
        for(m=0;m<n;m++)
            if(pf[m]==rs[i])
            {
                count1=1;
                break;
            }
        if(count1==0)
        {
            g=n;
            for(p=0;p<n-1;p++)
            {
                if(ctr[pf[p]]==ctr[pf[p+1]]&&g>ctr[pf[p]])

                    for(m=0;m<n;m++)
                        if(pf[m]==rs[i-n])
                            pos=m;
                if(ctr[pf[p]]<ctr[pf[p+1]])
                {    pos=p;
                    g=ctr[pf[p]];
                }
                else if(ctr[pf[p]]>ctr[pf[p+1]])
                {
                    pos=p+1;
                    g=ctr[pf[p+1]];
                }
            }

            pf[pos]=rs[i];
            for(k=0;k<n;k++)
                printf("\t%d\n",pf[k]);
            printf("\t--------\n");

            c++;
        }
        i++;
    }while(i!=nr)
    printf("\nNumber Of Page Faults::%d",c);
    getch();
}

File allocation strategies----- Linked file Allocation Method

/* 2.simulate all File allocation strategies
    a. Linked file Allocation Method               */


#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
    int b[20],b1[20],i,j,blocks[20][20],sz[20];
    char F[20][20],S[20],ch;
    int sb[20],eb[20],x;
    clrscr();
    printf("\n Enter no. of Files ::");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter file %d name ::",i+1);
        scanf("%s",&F[i]);
        printf("\n Enter file%d size(in kb)::",i+1);
        scanf("%d",&sz[i]);
        printf("\n Enter blocksize of File%d(in bytes)::",i+1);
        scanf("%d",&b[i]);
    }
    for(i=0;i<n;i++)
    {
        b1[i]=(sz[i]*1024)/b[i];
        printf("\n Enter Starting block of file%d::",i+1);
        scanf("%d",&sb[i]);
        printf("\n Enter Ending block of file%d::",i+1);
        scanf("%d",&eb[i]);
        printf("\nEnter blocks for file%d::\n",i+1);
        for(j=0;j<b1[i]-2;)
        {
            printf("\n Enter the %dblock ::",j+1);
            scanf("%d",&x);
            if(x>sb[i]&&x<eb[i])
            {
                blocks[i][j]=x;
                j++;
            }
            else
                printf("\n Invalid block::");
        }
    }
    do
    {
        printf("\nEnter the Filename ::");
        scanf("%s",&S);
        for(i=0;i<n;i++)
        {
            if(strcmp(F[i],S)==0)
            {
                printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
                printf("\n---------------------------------------------\n");
                printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
                printf("%d->",sb[i]);
                for(j=0;j<b1[i]-2;j++)
                    printf("%d->",blocks[i][j]);
                printf("%d->",eb[i]);
            }
        }
        printf("\n---------------------------------------------\n");
        printf("\nDo U want to continue (Y:n)::");
        scanf("%d",&ch);
    }while(ch!=0);
}
/*Input and Output;-

 Enter file1 size(in kb)::1

 Enter blocksize of File1(in bytes)::512

 Enter file 2 name ::sudee

 Enter file2 size(in kb)::1

 Enter blocksize of File2(in bytes)::1024

 Enter Starting block of file::1100

 Enter Ending block of file::1600

 Enter blocks for file1::

 Enter the 1block ::102

 Enter the 2block ::104

 Enter Starting block of file::2200

 Enter Ending block of file::2500

 Enter blocks for file2::

 Enter the 1block ::201

 Enter the Filename ::daya

Fname   Fsize   Bsize   Nblocks Blocks
---------------------------------------------
daya    1       512     2       100->102->104->600->
---------------------------------------------

Do U want to continue ::(Y:n)1

Enter the Filename ::sudee

Fname   Fsize   Bsize   Nblocks Blocks
---------------------------------------------
sudee   1       1024    1       200->201->500->
---------------------------------------------
Do U want to continue ::(Y:n)0
*/

Page Replacement algorithm using LRU.

/* Simulate the page replacement algorithm using LRU.                  */

#include<stdio.h>
#include<conio.h>
void main()
{
    int ref[50],i,j,k,fault=0,count=0,frame[5],n;
    int temp,m;
    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]=ref[i];
        fault++;
        printf("\n\n After inserting %2d the frame status is ::",ref[i]);
        for(j=0;j<=i;j++)
            printf("%4d",frame[j]);
    }
    for(i=n;i<count;i++)
    {
        for(j=0;j<n;j++)
        {
            if(frame[j]==ref[i])
                break;
            else
            {
                if(frame[j]==ref[i-n])
                {
                    frame[j]=ref[i];
                    fault++;
                }
            }
        }
        printf("\n\n After inserting %2d the frame status is ::",ref[i]);
        for(k=0;k<n;k++)
            printf("%4d",frame[k]);
    }
    printf("\n\n 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

 After inserting  2 the frame status is ::   1   2

 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 ::   3   1   2

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

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

 Total no. of page faults ::10                    */











                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

Sunday, November 25, 2012

Multiprogramming with variable Task --- MVT

/* simulate the mvt(multiprogramming with variable Task)*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=0;
void main()
{
    int tot,sz,b[20],ch,size[20];
    char p[20][20],pro[10];
    int j,k,status[10],tot1;
    clrscr();
    printf("\n Enter total Memory ::");
    scanf("%d",&tot);
    tot1=tot;
    printf("\n Enter the size of o.s::");
    scanf("%d",&sz);
    tot1=tot1-sz;
    do
    {
        printf("\n-----------------------------------------------------\n");
        printf("\n\t\tMulti Variable Task(MVT)");
        printf("\n-----------------------------------------------------\n");
        printf("\n\t\t1.Allocation\n\t\t2.Deletion\n\t\t3.Display\n\t\t4.Exit");
        printf("\n-----------------------------------------------------\n");
        printf("\n Enter Ur choice ::");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                printf("\n Enter process %d::",i+1);
                scanf("%s",&p[i]);
                printf("\n Enter memory size for process%d::",i+1);
                scanf("%d",&size[i]);
                if(tot1 >=size[i])
                {
                    b[i]=size[i];
                    tot1=tot1-size[i];
                    status[i]=1;
                    i++;
                    printf("\n Process is allocated ::");
                }
                else
                    printf("\n memory size is not Available ::");

                break;
            case 2:
                printf("\n Enter a process U want to delete ::");
                scanf("%s",&pro);
                j=0;
                while(j<i)
                {
                    if(strcmp(p[j],pro)==0)
                    {
                        status[j]=0;
                        tot1+=b[j];
                        b[j]-=size[j];
                    }
                    j++;
                }
                printf("\n Process is deleted ::");
                break;
               case 3:
                printf("\n\n Total Memory size ::%d",tot);
                printf("\n\n Memory size of o.s::%d",sz);
                printf("\n\n No.of processes   ::%d",i);
                printf("\n----------------------------------------------------------------------------\n");
                printf("\npname\tAllocated Memory\tStatus\n");
                printf("\n----------------------------------------------------------------------------\n");
                for(k=0;k<i;k++)
                {
                    if(b[k]!=0)
                    {
                        printf("\n%s\t\t%d\t\t",p[k],b[k]);
                        if(status[k]==1)
                            printf("Full");
                        else
                            printf("Available");
                    }
                }
                printf("\n----------------------------------------------------------------------------\n");
                printf("\n Total Available Space ::%d",tot1);
                break;
            case 4:
                exit(0);
            default:
                printf("\n Wrong Choice ::");
        }

    }while(1);
}

/* Input and output :-


 Enter total Memory ::600

 Enter the size of o.s::100

-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::1

 Enter process 1::p1

 Enter memory size for process1::250

 Process is allocated ::
-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::1

 Enter process 2::p2

 Enter memory size for process2::125

 Process is allocated ::
-----------------------------------------------------

                Multi Variable Task(MVT)
-----------------------------------------------------

                1.Allocation
                2.Deletion
                3.Display
                4.Exit
-----------------------------------------------------

 Enter Ur choice ::1

 Enter process 3::p3

 Enter memory size for process3::25

 Process is allocated ::
-----------------------------------------------------

                Multi Variable Task(MVT)
-----------------------------------------------------

                1.Allocation
                2.Deletion
                3.Display
                4.Exit
-----------------------------------------------------

 Enter Ur choice ::1

 Enter process 3::p4

 Enter memory size for process4::200

 Memory size is not Available::

-----------------------------------------------------

                Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::3

 Total memory size is::600

 Memory size for o.s::100

 No.of processes   ::3
----------------------------------------------------------------------------

pname   Allocated Memory        Status

----------------------------------------------------------------------------

p1              250             Full
p2              125             Full
p3              25              Full
----------------------------------------------------------------------------

 Total Available Space ::100
-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::

-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::1

 Enter process 4::p4

 Enter memory size for process4::100

  Process is allocated ::

  -----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::2

 Enter a process U want to delete ::p3

 Process is deleted ::
-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::3

 Total memory size is::600

 Memory size for o.s::100

 No.of processes   ::3
----------------------------------------------------------------------------

pname   Allocated Memory        Status

----------------------------------------------------------------------------

p1              250             Full
p2              125             Full
p4              100             Full
p3              0               Available
----------------------------------------------------------------------------

 Total Available Space ::25
-----------------------------------------------------

        Multi Variable Task(MVT)
-----------------------------------------------------

        1.Allocation
        2.Deletion
        3.Display
        4.Exit
-----------------------------------------------------

 Enter Ur choice ::4*/


Paging Program in OS with C language

#include"stdio.h"
#include"conio.h"
void main()
{
    int lmem,psize,pmem,i,j,k,c[10],str;
    int a[20],b[35],ch,page,index,abs,frame;
    clrscr();
    printf("\n enter page size::");
    scanf("%d",&psize);
    printf("\nenter logical memory size::");
    scanf("%d",&lmem);
    printf("\n%d",lmem);
    printf("\nenter phisical memory size::");
    scanf("%d",&pmem);
    printf("\n enter data::");
    for(i=0;i<lmem;i++)
        scanf("%d",&a[i]);
    for(i=0;i<32;i++)
        b[i]=-1;
    for(i=0;i<lmem/psize;i++)
    {
        printf("\nenter starting location for page::%d",i);
        scanf("%d",&str);
        if(str%4==0)
        {

            c[i]=str/psize;
            for(j=str,k=i*4;j<j+psize,k<i*4+psize;j++,k++)
            {
            b[j]=a[k];
            }
        }
        else
            printf("\n wrong entry for page the page address shud be multiples of 4");
    }
    printf("\n the page table is::");
    printf("\n page \t\t frame");

    for(i=0;i<lmem/psize;i++)
        printf("\n %d\t %d",i,c[i]);
    printf("\n enter for which data the mapping address to be found::");
    scanf("%d",&ch);
    for(i=0;i<lmem;i++)
    {
        if(ch==a[i])
        {
            index=i;
            page=index/4;
            frame=c[page];
            abs=(frame*psize)+(index%psize);
        }

    }

    printf("\n the physical address for %d is::%d",ch,abs);
    getch();
}

CPU scheduling algorithms.--- Priority.

/* Simulate the following cpu scheduling algorithms.
    d. Priority.   */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,sum=0,bt[20],wt[20],p[20];
    int twt=0,p1[20],n,j,temp,sum1=0,z[20];
    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 time and priority for process%d::",i+1);
        scanf("%d%d",&bt[i],&p[i]);
        p1[i]=i+1;
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(p[i]>p[j])
            {
                temp=bt[i];
                bt[i]=bt[j];
                bt[j]=temp;
                temp=p1[i];
                p1[i]=p1[j];
                p1[j]=temp;
            }
        }
    }
    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\tprt\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",p1[i],bt[i],i+1,wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<j-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 burst time and priority for process1::10 2
 Enter burst time and priority for process2::12 3
 Enter burst time and priority for process3::9 1
----------------------------------------------
    PNo     bt      prt     wt
----------------------------------------------
    P3      9       1       0
    P1      10      2       9
    P2      12      3       19
----------------------------------------------
Gannt Chart ::

    0   9  19

 Total waiting time is ::28
 Average waiting time is ::9.333333        */



                                    

CPU scheduling algorithms. --- Round Robin

/* Simulate the following cpu scheduling algorithms.
    A. Round Robin    */

#include<stdio.h>
#include<conio.h>
void main()
{
    int bt[30],wt[30],temp[10],p[20],i,t;
    int n,x=0,sum=0,twt=0,z[30],j=0;
    float avgt=0.00;
    clrscr();
    printf("\n Enter no. of Processes ::");
    scanf("%d",&n);
    printf("\n Enter time slice ::");
    scanf("%d",&t);
    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]);
        p[i]=0;
        temp[i]=bt[i];
        sum=sum+bt[i];
    }
    while(sum!=x)
    {
        for(i=0;i<n;i++)
        {
            if(bt[i]!=0)
            {
                if(bt[i]>t)
                {
                    bt[i]=bt[i]-t;
                    x=x+t;
                    z[j]=x;
                    j++;
                    p[i]++;
                }
                else
                {
                    wt[i]=x-(p[i]*t);
                    x=x+bt[i];
                    z[j]=x;
                    j++;
                    bt[i]=0;
                }
            }
        }
    }
    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,temp[i],wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<j-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 time slice ::2
 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      6
    P2      3       6
    P3      3       7
----------------------------------------------
Gannt Chart ::

    0   2   4   6   8   9  10  12  14  16  18  20  22  24  26  28

 Total waiting time is   ::19
 Average waiting time is ::6.333333        */


                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

File allocation strategies ---- Sequential file Allocation

/* 2.simulate all File allocation strategies
    a. Sequential file Allocation               */


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int st[20],b[20],b1[20],ch,i,j,n,blocks[20][20],sz[20];
    char F[20][20],S[20];
    clrscr();
    printf("\n Enter no. of Files ::");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter file %d name ::",i+1);

        scanf("%s",&F[i]);
        printf("\n Enter file%d size(in kb)::",i+1);
        scanf("%d",&sz[i]);
        printf("\n Enter Starting block of %d::",i+1);
        scanf("%d",&st[i]);
        printf("\n Enter blocksize of File%d(in bytes)::",i+1);
        scanf("%d",&b[i]);
    }
    for(i=0;i<n;i++)
        b1[i]=(sz[i]*1024)/b[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<b1[i];j++)
            blocks[i][j]=st[i]+j;
    }
    do
    {
        printf("\nEnter the Filename ::");
        scanf("%s",S);
        for(i=0;i<n;i++)
        {
            if(strcmp(S,F[i])==0)
            {
                printf("\nFname\tStart\tNblocks\tBlocks\n");
                printf("\n---------------------------------------------\n");
                printf("\n%s\t%d\t%d\t",F[i],st[i],b1[i]);
                for(j=0;j<b1[i];j++)
                    printf("%d->",blocks[i][j]);
            }

        }
        printf("\n---------------------------------------------\n");
        printf("\nDo U want to continue ::(Y:n)");
        scanf("%d",&ch);
        if(ch!=1)
            break;
    }while(1);
}
/*Input and Output;-

 Enter no. of Files ::2
 Enter file 1 name ::x.c
 Enter file1 size(in kb)::4
 Enter Starting block of 1::100
 Enter blocksize of File1(in bytes)::512
 Enter file 2 name ::y.c
 Enter file2 size(in kb)::2
 Enter Starting block of 2::500
 Enter blocksize of File2(in bytes)::256
    Enter the Filename ::y.c

Fname   Start   Nblocks Blocks
---------------------------------------------
y.c     500     8       500->501->502->503->504->505->506->507->
---------------------------------------------
Do U want to continue ::(Y:n) n             */

CPU scheduling algorithms. -- Short Job First(sjf)

/* Simulate the following cpu scheduling algorithms.
    B. Short Job First(sjf)     */
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,bt[10],n,twt=0,temp1,z[20],p[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 time for process%d::",i+1);
        scanf("%d",&bt[i]);
        p[i]=i+1;
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(bt[i]>bt[j])
            {
                temp1=bt[i];
                bt[i]=bt[j];
                bt[j]=temp1;
                temp1=p[i];
                p[i]=p[j];
                p[j]=temp1;
            }
        }
    }
    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;
    }
    for(i=0;i<n;i++)
        printf("%d",p[i]);
    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",p[i],bt[i],wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<j-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
  231
----------------------------------------------
    PNo     bt      wt
----------------------------------------------
    P2      3       0
    P3      3       3
    P1      24      6
----------------------------------------------

Gannt Chart ::

    0   3   6

 Total waiting time is ::9
 Average waiting time is ::3.000000        */

Short Job First(sjf) based on arrival time(non-preemptive)


/* Simulate the following cpu scheduling algorithms.             '
    B. Short Job First(sjf) based on arrival time(non-preemptive).    */

#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y)
{
    int *temp;
    *temp=*x;
    *x=*y;
    *y=*temp;
}
void main()
{
    int i,j,bt[10],bt1[10],n,twt=0,temp1,z[20],p[10];
    int wt[10],sum=0,sum1=0,at[10],k,l;
    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]);
        p[i]=i+1;
    }
    i=j=0;
    while(at[j+1]<bt[i])
        j++;

    for(k=0;k<j;k++)
        bt1[k]=bt[k+1];
    /*for(i=0;i<j;i++)
        printf("%6d",bt1[i]);*/
    for(i=1;i<j-1;i++)
    {
        for(k=i+1;k<j;k++)
        {
            if(bt[i]>bt[k])
                swap(&bt[i],&bt[k]);
        }
    }
/*    for(i=0;i<j;i++)
        printf("\n%4d",bt1[i]); */
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            wt[i]=0;
            sum=sum+bt[i];
            sum1=sum1+bt[i];
        }
        else
        {
            sum=sum+bt[i];
            wt[i]=sum-at[i];

        }
        sum1=sum1+bt[i];
        z[i]=sum1;

    }
    /*for(i=0;i<n;i++)
        printf("%d",p[i]);   */
    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",p[i],bt[i],at[i],wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=1;i<n;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
  231
----------------------------------------------
    PNo     bt      wt
----------------------------------------------
    P2      3       0
    P3      3       3
    P1      24      6
----------------------------------------------

Gannt Chart ::

    0   3   6

 Total waiting time is ::9
 Average waiting time is ::3.000000        */

Shortest Job First (Non-preemptive) SJF in OS

/* 2.simulate Shortest Job First (Non-preemptive)               */


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,j,n,Bt[10],wt[10],At[10],Tt=0;
    char c[10][10],pn[10][10],s[10];
    int z[50];
    float Twt=0.0,Awt;
    int w=0,temp,t;
    clrscr();
    printf("\n Enter no. of processes ::");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("\n Enter the processname ::");
        scanf("%s",&pn[i]);
        printf("\n Enter the Burst time for process P%d::",i+1);
        scanf("%d",&Bt[i]);
        printf("\n Enter the Arrival time for process P%d::",i+1);
        scanf("%d",&At[i]);
        s[i]='T';
        Tt+=Bt[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=2;j<n;j++)
        {
            if(Bt[j-1]>Bt[j])
            {
                temp=Bt[j];
                Bt[j]=Bt[j-1];
                Bt[j-1]=temp;
                temp=At[j];
                At[j]=At[j-1];
                At[j-1]=temp;
                strcpy(c[j-1],pn[j-1]);
                strcpy(pn[j-1],pn[j]);
                strcpy(pn[j],c[j-1]);
            }
        }
    }
    wt[0]=0;
    z[0]=0;
    w+=Bt[0];
    t=w;
    s[0]='F';
    while(w<Tt)
    {
        i=1;
        while(i<=n)
        {
            if(s[i]=='T' && At[i]<=t)
            {
                z[i]=w;
                wt[i]=w-At[i];
                s[i]='F';
                w+=Bt[i];
                t=w;
                i=1;
            }
            else
                i++;
        }
    }
    printf("\n..............................................\n");
    printf("\nPname\tBt\tAt\tWt");
    printf("\n..............................................\n");
    for(i=0;i<n;i++)
        printf("\n%s\t%d\t%d\t%d",pn[i],Bt[i],At[i],wt[i]);
    printf("\n..............................................\n");
    printf("\n\n Gannt Chart ::\n");
    for(i=0;i<n;i++)
        printf("%4d",z[i]);
    for(i=0;i<n;i++)
        Twt+=wt[i];
    printf("\n\n Total waiting Time ::%f",Twt);
    Awt=Twt/n;
    printf("\n\n Average waiting Time ::%f",Awt);
    getch();
}

/* Input and Output :-

 Enter no. of processes ::3
 Enter the processname ::dsk
 Enter the Burst time for process P1::24
 Enter the Arrival time for process P1::0
 Enter the processname ::pdn
 Enter the Burst time for process P2::3
 Enter the Arrival time for process P2::4
 Enter the processname ::mkk
 Enter the Burst time for process P3::3
 Enter the Arrival time for process P3::10
..............................................
Pname   Bt      At      Wt
..............................................

dsk     24      0       0
pdn     3       4       20
mkk     3       10      17
..............................................

 Gannt Chart ::

   0  24  27

 Total waiting Time ::37.000000

 Average waiting Time ::12.333333            */

Shortest Job First (preemptive) SJF in OS

/* 2.simulate Shortest Job First (preemptive)      */


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,j,k,n,Bt[10],wt[10],B[10],A[10],Tt=0;
    char s[20],start[20];
    int max=0,min,Time=0,z[50];
    float Twt=0.0,Awt;
    int w=0,flag=0,t=0;
    clrscr();
    printf("\n Enter no. of processes ::");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("\n Enter the Burst time for process P%d::",i);
        scanf("%d",&Bt[i]);
        printf("\n Enter the Arrival time for process P%d::",i);
        scanf("%d",&A[i]);
        B[i]=Bt[i];
        if(B[i]>max)
            max=B[i];
        if(A[i]>Time)
            Time=A[i];
        wt[i]=0;
        s[i]='T';
        start[i]='F';
        Tt+=B[i];
    }
    i=1;k=0;
    z[k]=0;
    k++;
    while(t<Time)
    {
        if(A[i]<=t && B[i]!=0)
        {
            if(flag==0)
                wt[i]=wt[i]+w;
            B[i]=B[i]-1;
            if(B[i]==0)
                s[i]='F';
            start[i]='T';
            t++;
            w=w+1;
            if(s[i]!='F')
            {
                j=1;
                flag=1;
                while(j<=n&&flag!=0)
                {
                    if(s[j]!='F'&&B[i]>B[j]&&A[i]<=t&&i!=j)
                    {
                        flag=0;
                        z[k]=w;
                        wt[i]=wt[i]-w;
                        i=j;
                        k++;
                    }
                    else
                        flag=1;
                    j++;
                }
            }
            else
            {
                i++;
                j=1;
                while(A[j]<=t && j<=n)
                {
                    if(B[i]>B[j] && s[j]!='F')
                    {
                        flag=0;
                        i=j;
                    }
                    j++;
                }
            }
        }
        else
        {
            if(flag==0)
                i++;
        }
    }
    while(w<Tt)
    {
        min=max+1;
        i=1;
        while(i<=n)
        {
            if(min>B[i]&&s[i]=='T')
            {
                min=B[i];
                j=i;
            }
            i++;
        }
        i=j;
        if(w==Time &&start[i]=='T')
        {
            w+=B[i];
            z[k]=w;
            k++;
            s[i]='F';
        }
        else
        {
            wt[i]+=w;
            w+=B[i];
            z[k]=w;
            k++;
            s[i]='F';
        }
    }
    printf("\n..............................................\n");
    printf("\nPno\tBt\tAt\tWt");
    printf("\n..............................................\n");
    for(i=1;i<=n;i++)
    {
        wt[i]=wt[i]-A[i];
        printf("\nP%d\t%d\t%d\t%d",i,Bt[i],A[i],wt[i]);
    }
    printf("\n..............................................\n");
    printf("\n\n Gannt Chart ::\n");
    for(i=0;i<k;i++)
        printf("%4d",z[i]);
    for(i=1;i<=n;i++)
        Twt+=wt[i];
    printf("\n\n Total waiting Time ::%f",Twt);
    Awt=Twt/n;
    printf("\n\n Average waiting Time ::%f",Awt);
    getch();
}
/* Input and Output :-

 Enter no. of processes ::4
 Enter the Burst time for process P1::8
 Enter the Arrival time for process P1::0
 Enter the Burst time for process P2::4
 Enter the Arrival time for process P2::1
 Enter the Burst time for process P3::9
 Enter the Arrival time for process P3::2
 Enter the Burst time for process P4::5
 Enter the Arrival time for process P4::3
..............................................
Pno     Bt      At      Wt
..............................................

P1      8       0       9
P2      4       1       0
P3      9       2       15
P4      5       3       2
..............................................

 Gannt Chart ::
   0   1   5  10  17  26

 Total waiting Time ::26.000000

 Average waiting Time ::6.500000
                                */