Posts

LRU implementation in C

Program: #include<stdio.h> int main() {    int page_num,frame_num,i,j,k,c=0,min,l,loc;      printf("Enter # of pages and # of frames:\n");     scanf("%d%d",&page_num,&frame_num);     int ref[page_num];//reference string     int frm[frame_num];//frames array     printf("Enter the reference string :\n");     for (i = 0; i < page_num; ++i)         scanf("%d",&ref[i]);     for (i = 0; i < frame_num; ++i)         frm[i] = -1;          for(i=0;i<page_num;i++)    {       for(j=0;j<frame_num;j++)       {          if(ref[i]==frm[j])//check if element already present in frame,if true then no page fault.            break;          else            co...

Optimal Page Replacement Implementation in C

Program: #include<stdio.h> main() {    int i,j,k,frame_num,page_num,page_fault=0,max,loc;    printf("Enter # of pages and # of frames:\n");     scanf("%d%d",&page_num,&frame_num);     int ref[page_num];//reference string     int frm[frame_num];//frames array     printf("Enter the reference string :\n");     for (i = 0; i < page_num; ++i)         scanf("%d",&ref[i]);     for (i = 0; i < frame_num; ++i)         frm[i] = -1;          for(i=0;i<page_num;i++)    {         for(j=0;j<frame_num;j++) if(ref[j]==frm[i])     goto display; for(j=0;j<frame_num;j++){     if(frm[j]==-1)//check if element already present in frame,if true then no page fault.             {             ...

Linked file allocation program in C.

Program : #include<stdio.h> int  main(){ int f[50],i,st,len,op,ind,n; for(i=0;i<50;i++) f[i]=0; printf("Enter how many blocks are already allocated:\n"); scanf("%d",&n); printf("Enter blocks already allocated:\n"); for(i=0;i<n;i++){ scanf("%d",&ind); f[ind]=1; } x: printf("Enter the index of starting block and length:\n"); scanf("%d%d",&st,&len); if(f[st] == 0){ for(i=st;i<(st+len);i++){ if(f[i] == 0){ f[i] = 1; printf("%d --> %d \n",i,f[i]); }else{ printf("Block %d already allocated.\n",i); } } }else printf("%d starting block is already allocated.\n",st); printf("Press 1 to continue.....\n"); scanf("%d",&op); if(op == 1) goto x; return 0; } Output: Enter how many blocks are already allocated: 3 Enter blocks already alloca...

Indexed file access method in C.

Program : #include<stdio.h> int  main(){ int f[50],index[50],i,st,op,ind,n_o_f,count; for(i=0;i<50;i++) f[i]=0; x: printf("Enter the index block:\n"); scanf("%d",&ind); if(f[ind] != 1){ f[ind] = 1; printf("Enter the # of files on index:\n"); scanf("%d",&n_o_f); }else{ printf("Block already allocated\n"); goto x; } y: count=0; printf("\nEnter file's indexes:"); for(i=0;i<n_o_f;i++){ scanf("%d",&index[i]); if(f[index[i]] == 0) count++; } if( count == n_o_f ){ for(i=0;i<n_o_f;i++) f[index[i]] = 1; printf("Allocated file indexed"); for(i=0;i<n_o_f;i++) printf("%d --> %d : %d \n",ind,index[i],f[index[i]]); }else{ printf("\nFile in that location is already indexed\n"); goto y; } printf("Press 1 to continue.....\n"); scanf(...

Sequential access method in C.

Program: #include<stdio.h> int  main(){ int f[50],i,st,len,count,op; for(i=0;i<50;i++) f[i]=0; printf("Files allocated are:\n"); x: count = 0; printf("Enter starting block & length of files:\n"); scanf("%d%d",&st,&len); for(i=st;i<(st+len);i++) if(f[i] == 0) count++; if(len == count){ for(i=st;i<(st+len);i++) if(f[i]==0){ f[i]=1; printf("%d\t%d\n",i,f[i]); } if(i != (st+len -1)) printf("File allocated\n"); }else printf("File not allocated\n"); printf("Press 1 to continue.....\n"); scanf("%d",&op); if(op == 1) goto x; return 0; } Output: Files allocated are: Enter starting block & length of files: 4 4 4       1 5       1 6       1 7       1 File allocated Press 1 to continue..... 1 Enter starting block & length of files: 4 3 File not allocat...

Implementation of Banker's Algorithm for deadlock avoidance in C.

Program: #include<stdio.h> #define m 10 int need[m][m],maxm[m][m],allot[m][m],avail[m],P,R; void calculateNeed(){ int i,j;     for (i = 0 ; i < P ; i++)         for (j = 0 ; j < R ; j++)             need[i][j] = maxm[i][j] - allot[i][j]; } void isSafe() {     int i,j,p,count=0;     int finish[P]; calculateNeed(); for(i=0;i<P;i++)     finish[P] = 0; int safeSeq[P]; int work[R]; for (i = 0; i < P ; i++)         finish[i] = 0;        for (i = 0; i < R ; i++)         work[i] = avail[i];     while (count < P)     {     int found = 0;         for (p = 0; p < P; p++)         {             if (finish[p] == 0)             {         ...

FIFO Page Replacement Algorithm implementation in C

Program: #include<stdio.h> int main() { int page_num,frame_num,i,j,flag,page_fault=0; printf("Enter # of pages and # of frames:\n"); scanf("%d%d",&page_num,&frame_num); int ref[page_num];//reference string int frm[frame_num];//frames array printf("Enter the reference string :\n"); for (i = 0; i < page_num; ++i) scanf("%d",&ref[i]); for (i = 0; i < frame_num; ++i) frm[i] = -1; printf("\n"); for (i = 0; i < page_num; ++i) { flag = 0; printf("\nReference np:%d-->",ref[i]); for (j = 0; j < frame_num; ++j) { if (frm[j] == ref[i]) { flag++; page_fault--; } } page_fault++; if ((flag == 0) && (page_fault <= frame_num)) { frm[i] = ref[i]; } else if(flag == 0)     frm[(page_fault - 1) % frame_num] = ref[i]; for (j = 0; j < frame_num; ++j) { printf("%d\t",frm[j]); } } printf...