Showing posts with label C Code. Show all posts
Showing posts with label C Code. Show all posts

Monday, 3 November 2014

Operating Systems Page Replacement Algorithm FIFO, LRU and Optimal C code

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

    int data[15],page[6],n,s,c,hit=0,i,j,index=0,cnt,k,arr[5],entry,x;
    printf("\nEnter the frame size:");
    scanf("%d",&n);
    printf("\nEnter the string size:");
    scanf("%d",&s);
    printf("\nEnter the data bits:>");
    for(i=0;i<s;i++)
    {
        printf("\nEnter data %d:",i+1);
        scanf("%d",&data[i]);
    }
    for(i=0;i<n;i++)
    {
        page[i]=999;
    }
//FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
     printf("\n**************FIFO******************");
    for(i=0;i<s;i++)
    {
        c=0;
        if(index==0)
        index=n;
        for(j=0;j<n;j++)
        {
            if(page[j]==data[i])
            {
                c++;
                break;
            }
        }
        if(c==1)
        hit++;
        else
        page[n-index--]=data[i];
    }
    printf("\nPage Fault=%d\n",s-hit);
    printf("\nHit ratio=%f\n",hit/(float)s);
////FIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFOFIFO
////LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
hit=0;
printf("\n**************LRU******************");
    for(i=0;i<n;i++)
    {
        page[i]=999;
    }
    index=0;
    for(i=0;i<s;i++)
    {
        cnt=0;
        c=0;
        for(j=0;j<n;j++)
        {
            if(page[j]==data[i])
            {
                c++;
                break;
            }
        }
        if(c==1)
        hit++;
        else
        {
            if(index<n)
            page[index++]=data[i];
            else
            {
              for(j=i-n;j>0;j--)
              {
                  for(k=j+1;k<i;k++)
                  {
                      if(data[j]==data[k])
                      cnt++;
                  }
                  j=cnt==0?j:(j-cnt+1);
                      for(k=0;k<n;k++)
                      {
                          if(data[j]==page[k])
                          {page[k]=data[i];break;}
                      }
              }
            }
        }

    }
    printf("\nPage Fault=%d\n",s-hit);
    printf("\nHit ratio=%f\n",hit/(float)s);
//LRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRULRU
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
index=0;
hit=0;
printf("\n**************OPTIMAL******************");
 for(i=0;i<n;i++)
    {
        page[i]=999;
    }
for(i=0;i<s;i++)
{
    c=0;
    cnt=0;
   for(j=0;j<n;j++)
        {
            if(page[j]==data[i])
            {
                c++;
                break;
            }
        }
        if(c==1)
        hit++;
        else
        {
            if(index<n)
            page[index++]=data[i];
            else
            {
                entry=0;
                x=0;
                for(k=i+1;k<s;k++)
                {
                    if(cnt==n)
                    break;
                    for(j=0;j<n;j++)
                    {
                        if(data[k]==page[j])
                        {
                            if(cnt++==0)
                            arr[cnt-1]=k;
                            else
                            {
                                for(x=0;x<cnt;x++)
                                {
                                    if(data[arr[x]]==data[k])
                                    entry++;
                                }
                                if(entry==0)
                                {arr[cnt++]=k;}
                            }
                        break;
                        }
                    }
                }
                if(cnt==n)
                page[cnt]=data[i];
                else
                page[0]=data[i];
            }
        }
}
printf("\nPage Fault=%d\n",s-hit);
printf("\nHit ratio=%f\n",hit/(float)s);
//OPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMALOPTIMAL
return 0;
}

Operating Systems Memory allocation methods First Fit,Best Fit and Worst Fit C code

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

    int arr[10]={5,6,7,4,9,8,2,3,1,4};//input memory block size;change as per your requirement
int i,j,n,bs,cnt=0,m=0,temp;
    printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
    scanf("%d",&n);
    printf("\n*************************\n");
        for(i=0;i<10;i++)
        printf("|%d| ",arr[i]);
        printf("\n*************************\n");
    while(n!=4)
    {

    if(n==1)
    {
        printf("\nEnter the block size:");
        scanf("%d",&bs);
        cnt=0;
        while(++cnt!=10)
        {
            if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
            {
                break;
            }
        }
        if(cnt<10){
        if(arr[cnt-1]>bs){
        printf("\nSize of block allocated=%d",bs);
        arr[cnt-1]-=bs;}
        else{
        printf("\nSize of block allocated=%d",arr[cnt-1]);
        arr[cnt-1]=999;}
        }
        else
        printf("\nNo memory blocks available!!\n");
        printf("\n*************************\n");
        for(i=0;i<10;i++)
        printf("|%d| ",arr[i]);
        printf("\n*************************\n");

    }
    else if(n==2)
    {
        printf("\nEnter the block size:");
        scanf("%d",&bs);
        cnt=0;
        m=0;
        while(++cnt!=11)
        {
            if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
            {
                if(m++==0)
                temp=cnt-1;
                else if(arr[temp]>arr[cnt-1])
                temp=cnt-1;
            }
        }
        if(m!=0){
        if(arr[temp]>bs){
        printf("\nSize of block allocated=%d",bs);
        arr[temp]-=bs;}
        else{
        printf("\nSize of block allocated=%d",arr[temp]);
        arr[temp]=999;}
        }
        else
        printf("\nNo free block available!\n");
        printf("\n*************************\n");
        for(i=0;i<10;i++)
        printf("|%d| ",arr[i]);
        printf("\n*************************\n");

    }

    else if(n==3)
    {
        printf("\nEnter the block size:");
        scanf("%d",&bs);
        cnt=0;
        m=0;
        while(++cnt!=11)
        {
            if(arr[cnt-1]!=999 && arr[cnt-1]>=bs)
            {
                if(m++==0)
                temp=cnt-1;
                else if(arr[temp]<arr[cnt-1])
                temp=cnt-1;
            }
        }
        if(m!=0){
        if(arr[temp]>bs){
        printf("\nSize of block allocated=%d",bs);
        arr[temp]-=bs;}
        else{
        printf("\nSize of block allocated=%d",arr[temp]);
        arr[temp]=999;}
        }
        else
        printf("\nNo free block available!\n");
        printf("\n*************************\n");
        for(i=0;i<10;i++)
        printf("|%d| ",arr[i]);
        printf("\n*************************\n");

    }
    printf("\nEnter:\n1:First Fit\n2:Best Fit\n3:Worst Fit\n4:Exit\nYour Choice:");
    scanf("%d",&n);
    }


    return 0;
}

C Program to perform all base conversions in Binary, Decimal, Hexadecimal & Octal number systems

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
void func1(int);
void func2(int);
void func3(int);
void func4(int);
void func5(int);
void func6(int);
void func7(char ar[],int);
void func8(char ar[],int);
void func9(char ar[],int);
void func10(int);
void func11(int);
void func12(int);
int a,c,sum=0,i,u,l;
main()
{     int ch,k,g=0;
      char cho='Y';
      printf("Welcome!!\n\a");
      while(toupper(cho)=='Y')
      {
      printf("\a\nPress->\n\n1:Binary to Decimal\n2:Decimal to Binary\n3:Octal to Decimal\n4:Decimal to Octal\n5:Octal to Binary\n6:Binary to Octal\n7:Hexadecimal to Decimal\n8:Hexadecimal to Binary\n9:Hexadecimal to Octal\n10:Binary to Hexadecimal\n11:Decimal to Hexadecimal\n12:Octal to Hexadecimal\n\n");
      printf("You pressed : ");
      scanf("%d",&ch);
      printf("\a");
      switch(ch)
      {
                case 1:{
                     printf("Enter Binary Number:");
                     scanf("%d",&a);
                     k=a;
                     while(k!=0)
                     {
                     if(k!=0)
                     {
                     if((k%10)!=0 && (k%10)!=1)
                     {
                     printf("\n\nYou entered a non Binary number!\n\a\a\a");
                     l=1;
                     break;
                     }
                     k/=10;
                     }
                     }
                     if(l!=1)
                     {
                     printf("\a");
                     func1(a);
                     }
                     }
                break;
                case 2:{
                     printf("Enter Decimal Number:");
                     scanf("%d",&a);
                     printf("\a");
                     func2(a);
                     }
                break;
                case 3:{
                     printf("Enter Octal Number:");
                     scanf("%d",&a);
                     k=a;
                     while(k!=0)
                     {
                     if(k!=0)
                     {
                     if((k%10)>7)
                     {
                     printf("\n\nYou entered a non Octal number!\n\a\a\a");
                     break;
                     }
                     k/=10;
                     }
                     else
                     {
                     printf("\a");
                     func3(a);
                     }
                     }
                     }
                break;
                case 4:{
                     printf("Enter Decimal Number:");
                     scanf("%d",&a);
                     printf("\a");
                     func4(a);
                     }
                break;
                case 5:{
                     printf("Enter Octal Number:");
                     scanf("%d",&a);
                     k=a;
                     while(k!=0)
                     {
                     if(k!=0)
                     {
                     if((k%10)>7)
                     {
                     printf("\n\nYou entered a non Octal number!\n\a\a\a");
                     break;
                     }
                     k/=10;
                     }
                     else
                     {
                     printf("\a");
                     func5(a);
                     }
                     }
                     }
                break;
                case 6:{
                     printf("Enter Binary Number:");
                     scanf("%d",&a);
                      k=a;
                     while(k!=0)
                     {
                     if(k!=0)
                     {
                     if((k%10)!=0 && (k%10)!=1)
                     {
                     printf("\n\nYou entered a non Binary number!\n\a\a\a");
                     l=1;
                     break;
                     }
                     k/=10;
                     }
                     }
                     if(l!=1)
                     {
                     printf("\a");
                     func6(a);
                     }
                     }
                break;
                case 7:{
                     char ar[5];
                     int n=0,j;
                     printf("Enter Hexadecimal Number:");
                     printf("\nEnter number of characters in Hexadecimal Number:");
                     scanf("%d",&n);
                     printf("\a");
                     fflush(stdin);
                     printf("\nEnter Hexadecimal characters separated with spaces:");
                     for(j=0;j<n;j++)
                     {
                     scanf("%s",&ar[j]);
                     printf("\a");
                     }
                     fflush(stdin);
                     u=-1;
                     while(ar[++u]!='\0')
                     {
                     if(toupper(ar[u])!='A' && toupper(ar[u])!='B' && toupper(ar[u])!='C' && toupper(ar[u])!='D' && toupper(ar[u])!='E' && toupper(ar[u])!='F' && ar[u]!='0' && ar[u]!='1' && ar[u]!='2' && ar[u]!='3' && ar[u]!='4' && ar[u]!='5' && ar[u]!='6' && ar[u]!='7' && ar[u]!='8' && ar[u]!='9')
                     {
                     printf("\n\nYou entered a non Hexadecimal number!\nProgram will now EXIT...\a\a\a");
                     getch();
                     return 0;
                     }
                     }
                     printf("\a");
                     func7(ar,n);
                     }
                break;
                case 8:{
                     char ar[5];
                     int n=0,j;
                     printf("Enter Hexadecimal Number:");
                     printf("\nEnter number of characters in Hexadecimal Number:");
                     scanf("%d",&n);
                     printf("\a");
                     printf("\nEnter Hexadecimal characters separated with spaces:");
                     for(j=0;j<n;j++)
                     {
                     scanf("%s",&ar[j]);
                     }
                     fflush(stdin);
                     u=-1;
                     while(ar[++u]!='\0')
                     {
                     if(toupper(ar[u])!='A' && toupper(ar[u])!='B' && toupper(ar[u])!='C' && toupper(ar[u])!='D' && toupper(ar[u])!='E' && toupper(ar[u])!='F' && ar[u]!='0' && ar[u]!='1' && ar[u]!='2' && ar[u]!='3' && ar[u]!='4' && ar[u]!='5' && ar[u]!='6' && ar[u]!='7' && ar[u]!='8' && ar[u]!='9')
                     {
                     printf("\n\nYou entered a non Hexadecimal number!\nProgram will now EXIT...\a\a\a");
                     getch();
                     return 0;
                     }
                     }
                     printf("\a");
                     func8(ar,n);
                     }
                break;
                case 9:{
                     char ar[5];
                     int n=0,j;
                     printf("Enter Hexadecimal Number:");
                     printf("\nEnter number of characters in Hexadecimal Number:");
                     scanf("%d",&n);
                     printf("\a");
                     printf("\nEnter Hexadecimal characters separated with spaces:");
                     for(j=0;j<n;j++)
                     {
                     scanf("%s",&ar[j]);
                     }
                     u=-1;
                     while(ar[++u]!='\0')
                     {
                     if(toupper(ar[u])!='A' && toupper(ar[u])!='B' && toupper(ar[u])!='C' && toupper(ar[u])!='D' && toupper(ar[u])!='E' && toupper(ar[u])!='F' && ar[u]!='0' && ar[u]!='1' && ar[u]!='2' && ar[u]!='3' && ar[u]!='4' && ar[u]!='5' && ar[u]!='6' && ar[u]!='7' && ar[u]!='8' && ar[u]!='9')
                     {
                     printf("\n\nYou entered a non Hexadecimal number!\nProgram will now EXIT...\a\a\a");
                     getch();
                     return 0;
                     }
                     }
                     printf("\a");
                     func9(ar,n);
                     }
                break;
                case 10:{
                     printf("Enter Binary Number:");
                     scanf("%d",&a);
                     k=a;
                      k=a;
                     while(k!=0)
                     {
                     if(k!=0)
                     {
                     if((k%10)!=0 && (k%10)!=1)
                     {
                     printf("\n\nYou entered a non Binary number!\n\a\a\a");
                     l=1;
                     break;
                     }
                     k/=10;
                     }
                     }
                     if(l!=1)
                     {
                     printf("\a");
                     func10(a);
                     }
                     }
                break;
                case 11:{
                     printf("Enter Decimal Number:");
                     scanf("%d",&a);
                     printf("\a");
                     func11(a);
                     }
                break;
                case 12:{
                     printf("Enter Octal Number:");
                     scanf("%d",&a);
                     k=a;
                     while(k!=0)
                     {
                     if((k%10)>7)
                     {
                     printf("\n\nYou entered a non Octal number!\n\a\a\a");
                     l++;
                     break;
                     }
                     k/=10;
                     }
                     if(l==0)
                     {
                     printf("\a");
                     func12(a);
                     }
                     }
                break;
                default:printf("\nYou Enetred A Wrong Choice!");
                }

               printf("\n\n\aWant to perform more conversions?(Y/N):");
               scanf("%s",&cho);
               printf("\a");
               system("cls");
               }
               return 0;
      }

void func1(a)
{
     while(a!=0)
     {
     sum+=pow(2,c++)*(a%10);
     a/=10;
     }
     printf("Decimal Equivalent is:%d",sum);
     }

void func2(a)
{
     int b[15],i,c=0;
     printf("Binary Equivalent:");
     while(a!=1)
     {
                b[c++]=a%2;
                a/=2;
                }
                b[c]=1;
                for(i=c;i>-1;i--)
                printf("%d",b[i]);
                }
void func3(a)
{
     while(a!=0)
     {
     sum+=pow(8,c++)*(a%10);
     a/=10;
     }
     printf("Decimal Equivalent is:%d",sum);
     }

void func4(a)
{
     int b[15],i,c=0;
     printf("Octal Equivalent:");
     while(a>7)
     {
                b[c++]=a%8;
                a/=8;
                }
                b[c]=a;
                for(i=c;i>-1;i--)
                printf("%d",b[i]);
                }

void func5(a)
{    int b[15],i;
     while(a!=0)
     {
     sum+=pow(8,c++)*(a%10);
     a/=10;
     }
     func2(sum);
     }

void func6(a)
{
     while(a!=0)
     {
     sum+=pow(2,c++)*(a%10);
     a/=10;
     }
    func4(sum);
     }

void func7(char ar[],int nr)
{    int i=0,sum=0;
     int c=-1;
     for(i=nr-1;i>-1;i--)
     {c++;
     if(toupper(ar[i])=='A')
     sum+=pow(16,c)*10;
     else if(toupper(ar[i])=='B')
     sum=pow(16,c)*11;
     else if(toupper(ar[i])=='C')
     sum=pow(16,c)*12;
     else if(toupper(ar[i])=='D')
     sum=pow(16,c)*13;
     else if(toupper(ar[i])=='E')
     sum=pow(16,c)*14;
     else if(toupper(ar[i])=='F')
     sum=pow(16,c)*15;
     else if(ar[i]=='1')
     sum+=pow(16,c)*1;
     else if(ar[i]=='2')
     sum+=pow(16,c)*2;
     else if(ar[i]=='3')
     sum+=pow(16,c)*3;
     else if(ar[i]=='4')
     sum+=pow(16,c)*4;
     else if(ar[i]=='5')
     sum+=pow(16,c)*5;
     else if(ar[i]=='6')
     sum+=pow(16,c)*6;
     else if(ar[i]=='7')
     sum+=pow(16,c)*7;
     else if(ar[i]=='8')
     sum+=pow(16,c)*8;
     else if(ar[i]=='9')
     sum+=pow(16,c)*9;
     }
     printf("Decimal Equivalent is:%d",sum);
     }

void func8(char ar[],int nr)
{    int i=0,sum=0;
     int c=-1;
     for(i=nr-1;i>-1;i--)
     {c++;
     if(toupper(ar[i])=='A')
     sum+=pow(16,c)*10;
     else if(toupper(ar[i])=='B')
     sum=pow(16,c)*11;
     else if(toupper(ar[i])=='C')
     sum=pow(16,c)*12;
     else if(toupper(ar[i])=='D')
     sum=pow(16,c)*13;
     else if(toupper(ar[i])=='E')
     sum=pow(16,c)*14;
     else if(toupper(ar[i])=='F')
     sum=pow(16,c)*15;
     else if(ar[i]=='1')
     sum+=pow(16,c)*1;
     else if(ar[i]=='2')
     sum+=pow(16,c)*2;
     else if(ar[i]=='3')
     sum+=pow(16,c)*3;
     else if(ar[i]=='4')
     sum+=pow(16,c)*4;
     else if(ar[i]=='5')
     sum+=pow(16,c)*5;
     else if(ar[i]=='6')
     sum+=pow(16,c)*6;
     else if(ar[i]=='7')
     sum+=pow(16,c)*7;
     else if(ar[i]=='8')
     sum+=pow(16,c)*8;
     else if(ar[i]=='9')
     sum+=pow(16,c)*9;
     }
     func2(sum);
     }

void func9(char ar[],int nr)
{    int i=0,sum=0;
     int c=-1;
     for(i=nr-1;i>-1;i--)
     {c++;
     if(toupper(ar[i])=='A')
     sum+=pow(16,c)*10;
     else if(toupper(ar[i])=='B')
     sum=pow(16,c)*11;
     else if(toupper(ar[i])=='C')
     sum=pow(16,c)*12;
     else if(toupper(ar[i])=='D')
     sum=pow(16,c)*13;
     else if(toupper(ar[i])=='E')
     sum=pow(16,c)*14;
     else if(toupper(ar[i])=='F')
     sum=pow(16,c)*15;
     else if(ar[i]=='1')
     sum+=pow(16,c)*1;
     else if(ar[i]=='2')
     sum+=pow(16,c)*2;
     else if(ar[i]=='3')
     sum+=pow(16,c)*3;
     else if(ar[i]=='4')
     sum+=pow(16,c)*4;
     else if(ar[i]=='5')
     sum+=pow(16,c)*5;
     else if(ar[i]=='6')
     sum+=pow(16,c)*6;
     else if(ar[i]=='7')
     sum+=pow(16,c)*7;
     else if(ar[i]=='8')
     sum+=pow(16,c)*8;
     else if(ar[i]=='9')
     sum+=pow(16,c)*9;
     }
     func4(sum);
     }
void func10(a)
{    char b[10];
     int k,i=0,c=0;
     while(a!=0)
     {
     k=a%10000;
     if(k==0)
     b[i++]='0';
     else if(k==1)
     b[i++]='1';
     else if(k==10)
     b[i++]='2';
     else if(k==11)
     b[i++]='3';
     else if(k==100)
     b[i++]='4';
     else if(k==101)
     b[i++]='5';
     else if(k==110)
     b[i++]='6';
     else if(k==111)
     b[i++]='7';
     else if(k==1000)
     b[i++]='8';
     else if(k==1001)
     b[i++]='9';
     else if(k==1010)
     b[i++]='A';
     else if(k==1011)
     b[i++]='B';
     else if(k==1100)
     b[i++]='C';
     else if(k==1101)
     b[i++]='D';
     else if(k==1110)
     b[i++]='E';
     else if(k==1111)
     b[i++]='F';
     a/=10000;
     }
     printf("Hexadecimal Equivalent:");
     for(i=i-1;i>-1;i--)
     {
     printf("\a");
     printf("%c",b[i]);
     }
     }

void func11(a)
{
     int b[15],i,c=0;
     while(a!=1)
     {
                b[c++]=a%2;
                a/=2;
                }
                b[c]=1;
                for(i=c;i>-1;i--)
                sum=sum*10+b[i];
                func10(sum);
                }

void func12(a)
{
     int b[15],m[15],i,c=0,sum=0,sum1=0;
     {
     while(a!=0)
     {
     sum+=pow(8,c++)*(a%10);
     a/=10;
     }
                c=0;
                while(sum!=1)
                {
                m[c++]=sum%2;
                sum/=2;
                }
                m[c]=1;
                for(i=c;i>-1;i--)
                sum1=(sum1*10)+m[i];
                func10(sum1);
                }

                }

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;


}