Sunday, August 17, 2014

a). Program for error correction using CRC. b). Program for error detection using Hamming Code.


a). Program for error correction using CRC.
b). Program for error detection using Hamming Code.


cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data. Blocks of data entering these systems get a short check value attached, based on the remainder of apolynomial division of their contents; on retrieval the calculation is repeated, and corrective action can be taken against presumed data corruption if the check values do not match. 

Lets start...

#include<stdio.h>
#include<conio.h>
void main()

{
     int a[20],c[20],d[20],aux[20],r,i,j,l,in,flag,choice,key;
     clrscr();

     printf("\n ENTER THE SIZE OF DATA WORD : ");
     scanf("%d",&l);

     printf("\n ENTER THE DATA WORD : ");
     for(i=0; i<l; i++)
           scanf("%d",&a[i]);

     printf("\n ENTER THE NUMBER OF REDUNDANT BITS : ");
     scanf("%d",&r);

     printf("\n ENTER THE DIVISOR : ");
     for(i=0; i<(r+1); i++)
           scanf("%d",&d[i]);

     for(i=0; i<(l+r); i++){
           if(i<l) c[i]=a[i];
           else c[i]=0;
     }

     printf("\n\n ... GENERATOR MODULE ... \n");
     printf("\n\n THE INTERMEDIATE CODE WORD IS : ");
     for(i=0; i<(l+r); i++)
           printf("%d ",c[i]);

     for(i=0; i<(l+r); i++)
           aux[i]=c[i];

     for(i=0; i<l; i++){
           in=1;
           if(c[i]==1){
                for(j=i+1; j<(i+l); j++){
                     c[j]=c[j]^d[in];
                     in++;
                }
           }
           else{
                for(j=i+1; j<(i+l); j++)
                     c[j]=c[j]^0;
           }
     }

     printf("\n\n THE REMAINDER AFTER DIVISION IS : ");
     for(i=l; i<(l+r); i++)

           printf("%d ",c[i]);

     printf("\n\n SENDER CODE WORD : ");

     for(i=0; i<(l+r); i++){
           if(i<l) c[i]=aux[i];

           printf("%d ",c[i]);
     }

     printf("\n\n\n PRESS 1 IF YOU WANT TO CHANGE A BIT OR 0 TO CONTINUE : ");
     scanf("%d",&choice);

     if(choice==1){
           printf("\n\n ENTER THE BIT YOU WANT TO CHANGE :");
           scanf("%d",&key);
           for(i=0; i<(l+r); i++){
                if(i==(key-1)){
                     if(c[key-1]==0) c[key-1]=1;
                     else c[key-1]=0;
                }
           }

           printf("\n\n\n CODE WORD AFTER ERROR : ");
           for(i=0; i<(l+r); i++)
                printf("%d ",c[i]);
     }

     else printf("\n\n\n NO ERROR INSERTED IN CODE WORD... ");
     printf("\n\n\n\n ... CHECKER MODULE ... ");
     for(i=0; i<l; i++){
           in=1;
           if(c[i]==1){
                for(j=i+1; j<(i+l); j++){
                     c[j]=c[j]^d[in];
                     in++;
                }
           }

           else{
                for(j=i+1; j<(i+l); j++)
                     c[j]=c[j]^0;
           }
     }

     printf("\n\n\n THE SYNDROME ARRAY IS : ");
     for(i=l; i<(l+r); i++)
           printf("%d ",c[i]);
     flag=0;
     for(i=l; i<(l+r); i++){
           if(c[i]!=0){
                flag=1;
                break;
           }
     }

     if(flag==1) printf("\n\n\n ERROR DETECTED !!!");
     else printf("\n\n\n NO ERROR FOUND");

     getch();
}




Hamming Code Program 

(1 bit error correction)

Lab Exams are coming and this program is very vital for CSE Students. This program can run on any Linux distribution OS. so, if you want to run it on turbo C then give header file <conio.h> and getch(); at end (before } ). And also write clrscr(); to clear the screen(remove system("clear");)
lets start...


#include<stdio.h>
#include<math.h>
int main()
{
     int a[20],b[20],c[20],d[20],i,k,m,f,n,j,r,p,x,y,z,ch,key,q,v,sum=0;
     system("clear");
     printf("\n ENTER THE LENGTH OF DATA WORD :");
     scanf("%d",&k);
     printf("\n ENTER THE DATA WORD \n");
     for(i=1; i<=k; i++){
           scanf("%d",&a[i]);
     }
     m=1;
     while((k+m+1)>=pow(2,m)){
           m++;
     }
     printf("\n Value of m is : %d",m);
     n=k+m;
     j=1; r=0;
     for(i=1; i<=n; i++){
           p=pow(2,r);
           if(i==p){
                b[i]=0;
                r++;
           }
           else{
                b[i]=a[j];
                j++;
           }
     }
     printf("\n INTERMEDIATE CODE WORD IS \n");
     for(i=1; i<=n; i++)
           printf("%d",b[i]);
     p=0;
     for(i=1; i<=m; i++)
     {
           x=pow(2,p); r=1;
           for(j=x; j<=n; j=j+(x*2)){
                for(y=j; y<(j+x); y++){
                     c[r]=b[y];
                     r++;
                }
           }
           z=0;
           for(y=1; y<=(r-1); y++)
           {
                if(c[y]==1) z++;
           }
           if(z%2==0)
                b[x]=0;
           else
                b[x]=1;
           for(y=1; y<=20; y++)
                c[y]=0;
           p++;
     }
     printf("\n\n THE HAMMING CODE IS \n");
     for(i=1; i<=n; i++)
           printf("%d",b[i]);
     while(1){
                printf("\n\n PRESS 1 TO ALTER A BIT 0 to EXIT \n\n");
                scanf("%d",&ch);
                if(ch==1){
                        printf("\n ENTER THE BIT YOU WANT TO CHANGE \n");
                        scanf("%d",&key);
                        for(i=1; i<=n; i++){
                                if(i==key){
                                        if(b[key]==1) b[key]=0;
                                        else b[key]=1;
                                        break;
                                }
                        }
                        printf("\n THE NEW CODE IS \n\n");
                        for(i=1; i<=n; i++)
                                printf("%d",b[i]);
                }
                else
                        break;
        }
     p=0; q=0;
        for(i=1; i<=m; i++)
        {
                x=pow(2,p); r=1;
                for(j=x; j<=n; j=j+(x*2)){
                        for(y=j; y<(j+x); y++){
                                c[r]=b[y];
                                r++;
                        }
                }
                z=0;
                for(y=1; y<=(r-1); y++)
                {
                        if(c[y]==1) z++;
                }
                v=z%2;
           d[q]=v;
           sum=sum+(v*pow(2,q));
           q++;
                for(y=1; y<=20; y++)
                        c[y]=0;
                p++;
        }
     if(sum==0)
           printf("\n\n NO ERROR FOUND....... \n\n");
     else
           printf("\n\n ERROR AT POSITION %d",sum);
     printf("\n");
     return 0;
}

Tuesday, June 24, 2014

SIMULATE THE FOLLOWING FILE ORGANIZATION TECHNIQUES : A. SINGLE LEVEL DIRECTORY B) TWO LEVEL


SIMULATE THE FOLLOWING FILE ORGANIZATION TECHNIQUES :
A. SINGLE LEVEL DIRECTORY
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:/tc/bgi");
cleardevice();
setbkcolor(GREEN);
printf("enter number of files");
scanf("&d",&count);
if(i<count)
// for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("enter %d file name:",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"root directory");
setcolor(BLUE);
i++;
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[i]);
}
}
getch();

}
output:-


B.TWO LEVEL DIRECTORY:-
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev ,char *dname,int lx,int rx,int x)
{
int i, gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter the name of ir file name %s",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0 || lev==1)
(*root)-> ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx ;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0 || lev==1)
{
if((*root)->level==0)
printf("how many users");
else
printf(" how many files");
printf("(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20, root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}

}
output:-

SIMULATE BANKERS ALGORITHM FOR DEAD LOCK DETECTION


SIMULATE BANKERS ALGORITHM FOR DEAD LOCK DETECTION:-
#include<stdio.h>
#include<conio.h>
int max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0;
main( )
{
clrscr( );
printf("\n\nSIMULATION OF DEADLOCK PREVENTION");
printf("Enter no. of processes, resources");
scanf("%d%d",&p,&r);
printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++) /*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun(); /*calling function*/
if(flag==0)
{
if(finish[i]!=1)
{
printf("\n\n Failing :Mutual exclusion");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n By allocating required resources to process %d dead lock is prevented ",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n daed lock is prevented by allocating needed resources");
printf(" \n \n failing:Hold and Wait condition ");
for(j=0;j<r;j++)
{ /*checking hold and wait condition*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK");
}
}
getch( );
}
fun( )
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else
break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}
output:-

SIMULATE BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE


SIMULATE BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],c[10][10],r[10],av[10],ca[10][10],i,j,k,n,m,temp=0,tem,ch;
clrscr();
printf("enter no processes");
scanf("%d",&m);
printf("enter no of resources");
scanf("%d",&n);
printf("\nenter claim\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&c[i][j]);
printf("\nenter alocation matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nenter resourse vector\n");
for(i=0;i<n;i++)
scanf("%d",&r[i]);
k=0;
do
{
printf("claim\tallocation\tca\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",c[i][j]);printf("\t");
for(j=0;j<n;j++)
printf("%3d",a[i][j]);printf("\t\t");
for(j=0;j<n;j++)
{
ca[i][j]=c[i][j]-a[i][j];
printf("%3d",ca[i][j]);
}
printf("\n");
}
temp=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
temp=temp+a[j][i];
av[i]=r[i]-temp;temp=0;
}
printf("\nresource vector: ");
for(i=0;i<n;i++)
printf("%3d",r[i]);
printf("\navailable vector: ");
for(i=0;i<n;i++)
printf("%3d",av[i]);
if(k==0)
printf("\n****initial state****\n");
else
printf("\n***p%d runs to completion*****\n",tem+1);
if(k<n)
{
temp=0;
for(i=0;i<m;i++)
{
lab:for(j=0;j<n;j++)
{
if(ca[i][j]==0)
temp++;
}
if(temp==n)
{
i++; temp=0;goto lab;
}
else
{
for(j=0;j<n;j++)
{
if(ca[i][j]<=av[j])
tem=i;
else
{
if(i>m)
{
printf("\nunshafe state");goto end;
}
else
{i++;goto lab;}
}
}
}
for(j=0;j<n;j++)
{
a[tem][j]=0;c[tem][j]=0;ca[tem][j]=0;
}
break;
}
}
else
{
printf("\nprocesses are completed");
goto end;
}
k++;
printf("continue press ZERO");
scanf("%d",&ch);
}while(ch==0);
end: getch();

}
OUTPUT:-

Friday, June 20, 2014

CPU SCHEDULING ALGORITHMS:


Simulate the following CPU scheduling algorithms :
a) Round Robin    b) SJF    c) FCFS     d) Priority
-------------------------------------------------------------------------------------------------------------------------
A) ROUND ROBIN:
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,wait,ts2,ta;
float nta;
}p[20];
main()
{
int i,j,slice,n;
float tamean=0,ntamean=0;
clrscr();
printf("Enter Number of Processes :: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c : ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for process-%c : ",65+i);
scanf("%d",&p[i].ts);
}
printf("\nEnter Time Slice: ");
scanf("%d",&slice);
for(i=0;i<n+1;i++)
{
if(i==0)
p[i].ts2=n*slice;
else
p[i].ts2=p[i-1].ts2+(p[i-1].ts-slice);
if(i<n)
p[i].st=i*slice;
if(i>=1)
p[i-1].ft=p[i].ts2;
}
for(i=0;i<n;i++)
p[i].wait=(i*slice-p[i].at)+(p[i].ts2-(i+1)*slice);
for(i=0;i<n;i++)
{
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)tamean/n;
ntamean=(float)ntamean/n;
printf("\n Process AT ST StT FT WT TA NTA\n");
for(i=0;i<n;i++)
{
printf("Process-%c%9d%9d%12d%12d%10d%6d%10.4f",65+i,p[i].at,p[i].ts,p[i].st,p[i].ft,p[i].wait,p[i].ta,p[i].nta);
printf("\n");
}
printf("\nturn around mean is : %f",tamean);
printf("\nnorm.turn around mean is : %f",ntamean);
getch();
}
OUTPUT:-
Image
---------------------------------------------------------------------------------------------------------------------------------------------------
b) SJF:-
#include<stdio.h>
#include<conio.h>
main()
{
int sbt[10],swt[10],st[10],stt[10],sft[10];
int n,i,j,wt,tt,temp;
float avgwt,avgtt;
wt=0;tt=0;temp=0;st[1]=0;
clrscr();
printf("enter no.of jobs");
scanf("%d",&n);
printf("enter the burst times of jobs");
for(i=1;i<=n;i++)
scanf("%d",&sbt[i]);
for(i=1;i<=(n-1);i++)
for(j=i+1;j<=n;j++)
if((sbt[i]>sbt[j])&&(sbt[i]!=sbt[j]))
{
temp=sbt[i];
sbt[i]=sbt[j];
sbt[j]=temp;
}
for(i=1;i<=n;i++)
{ st[i+1]=st[i]+sbt[i];
sft[i]=st[i]+sbt[i];
if(i==1)
swt[i]=0;
else
swt[i]=swt[i-1]+sbt[i-1];
stt[i]=swt[i]+sbt[i];
wt=wt+swt[i];
tt=tt+stt[i];
} avgwt=((float)wt/(float)n);
avgtt=((float)tt/(float)n);
printf("\nJOB sert st wt ft turt");
for(i=1;i<=n;i++)
printf("\nJ%d\t%d\t%d\t %d\t%d\t%d\n",i,sbt[i],st[i],sft[i],swt[i],stt[i]);
printf("\navg waiting time=%0.2f,turnover total time=%0.2f",avgwt,avgtt);
getch();
}
output:-
Image
------------------------------------------------------------------------------------------------------------------------------------------------------
c) FCFS:-
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,ta;
float nta;
};
main()
{
struct process p[20];
int n,i,j;
float tamean=0,ntamean=0;
clrscr();
printf("\nEnter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c :: ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for Process-%c :: ",65+i);
scanf("%d",&p[i].ts);
}
for(i=0;i<n;i++)
{
if(i==0)
p[i].st=p[i].at;
else
{
p[i].st=0;
for(j=0;j<i;j++)
p[i].st=p[i].st+p[j].ts;
}
p[i].ft=p[i].ts+p[i].st;
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)(tamean/n);
ntamean=(float)(ntamean/n);
printf("\nProcess AT ST StT FT TA NTA");
for(i=0;i<n;i++)
printf("\n%3c%12d%10d%10d%10d%10d%15f",65+i,p[i].at,p[i].ts, p[i].st,p[i].ft,p[i].ta,p[i].nta);
printf("\n\n Mean of Turn-around time : %f",tamean);
printf("\n\n Mean of Normalized turn-around time : %f",ntamean);
getch();
}
output:-
Image
-----------------------------------------------------------------------------------------------------------------------------------------------
d) Priority:-
#include<stdio.h>
#include<conio.h>
struct process
{
int ts,pri,wait,ft;
}p[20];
main()
{
int n,pri1[20],i,j,temp,ft1[25];
clrscr();
printf("\n Enter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Service Time for Process-%c : ",65+i);
scanf("%d",&p[i].ts);
printf("\nEnter Priority for Process-%c : ",65+i);
scanf("%d",&p[i].pri);
}
for(i=0;i<n;i++)
pri1[i]=p[i].pri;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pri1[i]>pri1[j])
{
temp=pri1[i];
pri1[i]=pri1[j];
pri1[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri1[i]==p[j].pri)
{
if(i==0)
{
p[j].wait=0;
p[j].ft=p[j].ts;
ft1[i]=p[j].ft;
}
else
{
p[j].ft=ft1[i-1]+p[j].ts;
p[j].wait=ft1[i-1];
ft1[i]=p[j].ft;
}
}
}
}
printf("\nProcess ST PRI FT WT ");
for(i=0;i<n;i++)
{
printf("\nprocess-%c%10d%13d%14d%15d",65+i,p[i].ts,p[i].pri,p[i].ft,p[i].wait);
}
getch();
}
output:-
Image
----------------------------------------------------------------------------------------------------------------------------------------

SIMULATE MVT AND MFT


Simulation of Multiprogramming with Variable and Fixed number of Tasks:-
MVT:-
#include<stdio.h>
#include<conio.h>
main()
{
static int jobs[20][20],flag[10];
int ch;
static int i,k,nj,nb,tms;
clrscr();
printf("Enter time"); /* reading time */
scanf("%d",&tms);
printf("Enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("Enter job information 1.jobid 2.jobsize");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
for(i=0;i<nj;i++)
{
if(tms>=jobs[i][1])
{
tms=tms-jobs[i][1];
nb=nb+1;
flag[i]=1;
}
}
printf("Total memory space available which is not allocated is:%d\n",tms);
printf("Jobs which are not allocated:");
for(i=0;i<nj;i++)
if(flag[i]==0)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
if(nb!=nj)
{
while(1)
{
printf("enter jobid to deallocate:");
scanf("%d",&k);
for(i=0;i<nj;i++)
{
if(jobs[i][0]==k)
{
if(flag[i]==1)
{
tms=tms+jobs[i][1];
flag[i]=2;
printf("Deallocated job %d\t%d\n", jobs[i][0],jobs[i][1]);
}
}
}
for(i=0;i<nj;i++)
{
if (tms>=jobs[i][1])
{
if(flag[i] == 0)
{
tms=tms-jobs[i][1];
flag[i]=1;
}
}
}
printf("Remaining memory is: %d",tms);
printf("Jobs which are not allocated are:");
for( i=0;i<nj;i++) /* dellocating mamory*/
if(flag[i] ==0)
printf("%d\t%d\n", jobs[i][0],jobs[i][1]);
printf("Do you want to deallocate 1.Yes 2.No");
scanf("%d",&ch);
if(ch ==2)
break;
}
}
printf("Allocated jobs are:");
for(i=0;i<nj;i++)
if(flag[i]==1)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
getch();
}
output:-
Image
_________________________________________________________________________________________
MFT:-
#include<stdio.h>
#include<conio.h>
void main()
{
int tms,element,nb,i,j,t,index,frag,ch,count=0;
static int jobs[20][20],sz[20][20],nj,s;
clrscr();
printf("enter total memory space"); /* reading memory */
scanf("%d",&tms); /* reading choices */
printf("enter choice\n1.equal partition 2.unequal partition\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter size of each block");
scanf("%d",&s);
nb=tms/s;
for(i=0;i<nb;i++)
scanf("%d",&sz[i][0]);
}
else
{
printf("enter no. of blocks");
scanf("%d",&nb);
printf("enter size of %d blocks");
for(i=0;i<nb;i++)
scanf("%d",sz[i][0]);
}
printf("enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("enter job information 1.jobid 2.job size\n");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
frag=0;
for(j=0;j<nj;j++)
{
if(sz[j][0]>=element && sz[i][0]<=t)
{
if(sz[j][1]!=1)
{
t=sz[j][0];
index=j;
}
}
}
if(sz[index][1]!=1)
{
sz[index][1]=1;
jobs[i][2]=2;
frag=frag+(t-element);
count++;
}
printf("total internal fragmentation : %d", frag);
printf("no. of free blocks: %d" , nb-count);
printf("the jobs which are not allocated");
if(count==nj)
printf(" 0");
for(i=0;i<nj;i++)
{
if(jobs[i][2]!=2)
printf("jobid ------%d\tjob size-----%d\n",jobs[i][0],jobs[i][1]);
}
getch();
}
output:-
Image

____________________________________________________________________________________________

SIMULATE PAGE REPLACEMENT ALGORITHMS


Simulate the following Page Replacement algorithms :
a) FIFO b) LRU c) LFU
Page Replacement algorithm-FIFO:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i]){
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;
for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;
if(k==fs)
k=0; } }
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________
Page Replacement algorithm-LRU:-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;
}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;
flg1=1;
break;
} }
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;
break;
} }
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
} }
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________
Page Replacement algorithm-LFU:-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("\n ENTER THE PAGE REFERENCE STRING:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='X';
ru[i]=0;
} clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
(ru[j])++;
flg1=1;
break;
}}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=1;
flg2=1;
break;
}}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}}}}}
fp[min]=prs[i];
ru[min]=1;
}}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("-----");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c(%d)",fp[j],ru[j]);
}
y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n TOTAL PAGE FAULTS =%d",pfc);
getch();
}
output:-
Image
___________________________________________________________________________________________