Thursday, 2 October 2014

Banker's Algoithm - C Program

#include<stdio.h>
int main()
{

    int n,p,i,j,complete=0,c,cnt=0,mx[10][3],all[10][3],need[10][3],av[3],done[10];
    char safe[10],name[10];
    printf("\nEnter the num of process:");
    scanf("%d",&n);
    printf("\nEnter the num of resources:");
    scanf("%d",&p);
    for(i=0;i<n;i++)
    {
        printf("\nEnter the name of process:");
        fflush(stdin);
        scanf("%c",&name[i]);
        done[i]=0;
        printf("\nEnter details of process %d",i+1);
     for(j=0;j<p;j++)
    {
        printf("\nEnter max resource %c:",65+j);
        scanf("%d",&mx[i][j]);
        printf("\nEnter allocated resource %c:",65+j);
        scanf("%d",&all[i][j]);
        need[i][j]=mx[i][j]-all[i][j];
    }
    }

    for(i=0;i<p;i++)
    {
      printf("\nEnter the available resource %c:",65+i);
      scanf("%d",&av[i]);
    }

    while(complete!=n && cnt!=n)
    {
        for(i=0;i<n;i++)
        {
            cnt++;
            if(done[i]!=2)
            {
            c=0;
            for(j=0;j<p;j++)
            {
              if(need[i][j]<=av[j])
              {c++;}
            }
              if(c==p)
              {
                  for(j=0;j<p;j++)
                  av[j]+=all[i][j];
                  safe[complete]=name[i];
                  complete++;
                  done[i]=2;
              }
            }
        }
    }

    if(cnt!=n)
    {
    printf("\n\nSafe Sequence-->> ");
    for(i=0;i<n;i++)
    printf("| %c |",safe[i]);
    }
    else
    {
        printf("\nNo safe sequence!!");
    }
    return 0;


}

Disk Scheduling Algorithm [FCFS,SSTF,SCAN,LOOK] - C Program

#include<stdio.h>

int main()
{
    int len,start,que[20],i,j,dist=0,k,l,temp[20],tque[20],cnt=0,ends,ch;

    printf("\nEnter the length of queue:");
    scanf("%d",&len);
    for(i=1;i<len+1;i++)
    {
        printf("\nEnter the cylinder block %d:",i);
        scanf("%d",&que[i]);
        tque[i]=que[i];
    }
    printf("\nEnter the starting block value:");
    scanf("%d",&que[0]);
    tque[0]=que[0];
    printf("\nEnter the ends of disc block value:");
    scanf("%d",&ends);
    printf("\nHow u want SCAN Algorithm to start from?\n1:Back from start block\n2:Front from start block\nYour choice:");
    scanf("%d",&ch);

    //FCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFS
    printf("\n****************FCFS*************\n\n");
    printf("\nThe blocks visited are as follows->\n\n");

    for(i=0;i<len+1;i++)
    {
        printf("%d ->> ",que[i]);
        if(que[i+1]>que[i] && i+1!=len+1)
        dist+=que[i+1]-que[i];
        else if(tque[i+1]<=tque[i] && (i+1)!=k)
        dist+=que[i]-que[i+1];

    }
    printf("\n\nTotal head movement=%d\n",dist);
    //FCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFSFCFS

    //SSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTF
    dist=0;
    printf("\n****************SSTF*************\n\n");
    printf("\nThe blocks visited are as follows->\n\n");
    for(i=0;i<len+1;i++)
    {
        if(que[i]>que[0])
        temp[i]=que[i]-que[0];
        else
        temp[i]=que[0]-que[i];
    }
    //selection sort
    for(i=0;i<len+1;i++)
    {
        for(j=1;j<len+1-i;j++)
        if(temp[j-1]>temp[j])
        {
            k=temp[j-1];
            l=tque[j-1];
            temp[j-1]=temp[j];
            tque[j-1]=tque[j];
            temp[j]=k;
            tque[j]=l;
        }
    }
    //end of selection sort
     for(i=0;i<len+1;i++)
    {
        printf("%d ->> ",tque[i]);
        if(tque[i+1]>tque[i] && i+1!=len+1)
        dist+=tque[i+1]-tque[i];
        else if(i+1!=len+1)
        dist+=tque[i]-tque[i+1];
        temp[i]=0;

    }
    printf("\n\nTotal head movement=%d\n",dist);
    //SSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTFSSTF

    //SCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCAN
    dist=0;

    //restore value of tque
    for(i=0;i<len+1;i++)
    tque[i]=que[i];

    printf("\n****************SCAN*************\n\n");
    printf("\nThe blocks visited are as follows->\n\n");
    //selection sort
    for(i=0;i<len+1;i++)
    for(j=1;j<len+1-i;j++)
    {
       if(tque[j-1]>tque[j])
       {
         k=tque[j-1];
         tque[j-1]=tque[j];
         tque[j]=k;
       }
    }
    //end of selection sort
    //Reversing que before start block and merging in main tque
    for(i=0;i<len+1;i++)
    {if(tque[i]==que[0]) break;}
    tque[i]=tque[0];
    for(j=1;j<i;j++)
    {
       k=tque[i-j];
       tque[i-j]=tque[j];
       tque[j]=k;
    }
    tque[0]=que[0];
    k=i;
    //end of reverse process
    if(ch==1){
    for(i=0;i<len+1;i++)
    {
        printf("%d ->> ",tque[i]);
        if(tque[i+1]>tque[i] && i+1!=len+1)
        dist+=tque[i+1]-tque[i];
        else if(tque[i+1]<=tque[i] && (i+1)!=k)
        dist+=tque[i]-tque[i+1];
        if(k==i)
        {printf("0 ->> ");dist+=2*tque[i];}

    }
    }
    else
    {
       dist=0;
       printf("%d ->> ",tque[0]);
       dist+=tque[k+1]-tque[0];
       for(i=k+1;i<len+1;i++)
       {
        printf("%d ->> ",tque[i]);
        if(tque[i+1]>tque[i] && i+1!=len+1)
        dist+=tque[i+1]-tque[i];
        else if((i+1)!=len+1)
        dist+=tque[i]-tque[i+1];
       }
       printf("%d ->> ",ends);
       dist+=(ends-tque[len]);
       for(i=1;i<=k;i++)
       {
        dist+=ends-tque[i];
        printf("%d ->> ",tque[i]);
        if(tque[i+1]>tque[i] && (i+1)!=k+1)
        dist+=tque[i+1]-tque[i];
        else if(tque[i+1]<=tque[i] && (i+1)!=k)
        dist+=tque[i]-tque[i+1];
       }
    }
    printf("\n\nTotal head movement=%d\n",dist);
    //SCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCANSCAN

    //LOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOK

    dist=0;
    printf("\n****************LOOK*************\n\n");
    printf("\nThe blocks visited are as follows->\n\n");
    for(i=0;i<len+1;i++)
    {
        printf("%d ->> ",tque[i]);
        if(tque[i+1]>tque[i] && i+1!=len+1)
        dist+=tque[i+1]-tque[i];
        else if(tque[i+1]<=tque[i] && (i+1)!=k)
        dist+=tque[i]-tque[i+1];

    }
    printf("\n\nTotal head movement=%d\n",dist);
    //LOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOKLOOK
    return 0;


}