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...

Multi-programming with fixed number of tasks(MFT) program in C

Program: #include<stdio.h> int main() { int tm,om,n,i,block_size,internal_frag; printf("Enter total memory size,memory for OS and # of processes:\n"); scanf("%d%d%d",&tm,&om,&n); int process[n]; tm = tm - om; block_size = tm / n; for (i = 0; i < n; ++i) { printf("Enter process %d size :\n",i+1); scanf("%d",&process[i]); if(process[i] <= block_size){ internal_frag = internal_frag + block_size - process[i]; printf("Allocated memory to process :%d\n",i+1); }else{ printf("Process %d is blocked\n",i+1); } } printf("Fragmentation is %d.\n",internal_frag); return 0; } Output: Enter total memory size,memory for OS and # of processes: 50 10 4 Enter process 1 size : Allocated memory to process :1 Enter process 2 size : Allocated memory to process :2 Enter process 3 size : Allocated memory to process :3 Enter process 4  size : Alloca...

Multi-programming with variable number of tasks(MVT) program in C

Program: #include<stdio.h> int main() { int tm,om,n,i; printf("Enter total memory size,memory for OS and # of processes:\n"); scanf("%d%d%d",&tm,&om,&n); int process[n]; for (i = 0; i < n; ++i) { printf("Enter process %d size :\n",i); scanf("%d",&process[i]); } tm = tm - om; for (i = 0; i < n; ++i) { if(tm >= process[i]){ printf("Allocated memory to process :%d\n",i+1); tm = tm - process[i]; }else{ printf("Process %d is blocked\n",i+1); } } printf("External fragmentation is %d.\n",tm); return 0; } Output: Enter total memory size,memory for OS and # of processes: 50 10 5 Enter process 0 size :6 Enter process 1 size :6 Enter process 2 size :6 Enter process 3 size :6 Enter process 4 size :6 Allocated memory to process :1 Allocated memory to process :2 Allocated memory to process :3 Allocated memory to process :4 Allocated mem...

Implementation of Round-Robin scheduling in C

Program: #include<stdio.h> int main(){ int bt[10],st[10],wt[10],tt[10]; int n,i,j,count=0,quantum,w1=0,t1=0,t,sq=0; float aw=0.0,at=0.0; printf("Enter Number of processes and time quantum:\n"); scanf("%d%d",&n,&quantum); for(i=0;i<n;i++){ printf("Enter burst time for process-%d:\n",i+1); scanf("%d",&bt[i]); st[i]=bt[i]; } // while(true){ for(i=0,count=0;i<n;i++){ t=quantum; if(st[i]==0){ count++; continue; } if(st[i] > quantum){ st[i] = st[i] - quantum; }else{ if(st[i] >= 0){ t = st[i]; st[i]=0; } } sq=sq+t; tt[i]=sq; } if(n == count){ break; } } // for(i=0;i<n;i++){ wt[i] = tt[i] - bt[i]; w1+=wt[i]; t1+=tt[i]; } aw = (float)w1/n; at = (float)t1/n; printf("\nBt\tWt\tTt\n"); for(i=0;i<n;i++){ printf("%d\t%d\t%d\n",bt[i],wt[i],tt[i]); } printf(...

Implementation of priority scheduling in C

Program: #include<stdio.h> void swap(int *a,int *b){ int temp;         temp = *a;         *a = *b;         *b = temp; } int main(){ int pno[10],pp[10],wt[10],bt[10],tt[10]; int n,i,j,w1=0,t1=0; float aw=0.0,at=0.0; printf("Enter number of processes :\n"); scanf("%d",&n); for(i=0;i<n;i++){ printf("Enter burst and priority of process-%d: \n",i+1); scanf("%d%d",&bt[i],&pp[i]); pno[i]=i+1; } for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(pp[i] < pp[j]){ swap(&pp[i],&pp[j]); swap(&bt[i],&bt[j]); swap(&pno[i],&pno[j]); } } } wt[0]=0; tt[0]=bt[0]; for(i=0;i<n;i++){ wt[i+1] = bt[i] + wt [i]; tt[i+1] = tt[i] + bt [i+1]; w1+=wt[i]; t1+=tt[i]; } aw = (float)w1/n; at = (float)t1/n; printf("\nP_no\tBt\tWt\tTt\n"); for(i=0;i<n;i++){ printf("%d\t%d\t%d\t%d\n",pno[i],bt[i],wt[i],tt[i]); } pri...

C program to copy contents of a file to another file

Program: #include<stdio.h> #include<fcntl.h> #include<sys/stat.h> int main(int argc, char * argv[]){ char block[1024]; int in,out,nread; in = open(argv[1],O_RDONLY); out = open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); if(in == -1 || out == -1){ printf("Unable to copy file\n"); return 0; } while((nread=read(in,block,1024)) > 0) write(out,block,nread); printf("File copied.\n"); close(in); close(out); return 0; }

Awk script to find number of characters,words and lines in a file

Script & Output: root@root:~/os# awk '{x+=length($0);y+=NF} END {print NR,x,y}' test1 10 385 95 root@root:~/os# awk '{x+=length($0);y+=NF} END {print NR,x,y}' 2b.sh 17 265 48

Shell script that displays number of line in given file

Script: if [ $# -eq 0 ]     then         echo No arguments else     for fname in $*         do             if [ -f $fname ]                 then                     echo $fname is a regular file                     count="wc -l $fname" #wc = word count                     echo Number of lines in $fname are                     echo $($count)             else                 echo $fname is a direct...

Shell script to display file permissions

Script: #displays a list a list of files in directory via arguments read,write& execute #echo Enter the directory name : read dir if [ -d $dir ]     then         cd $dir         ls>f         exec<f         while read line             do                 if [ -f $line ]                     then                         if [ -r $line ] && [ -w $line ] && [ -x $line ];                             then          ...

shell script that deletes all lines containing given word

Script: #deletes all lines containing a specific word in one or more files supplies via #arguments if [ $# -eq 0 ]     then         echo No arguments else     pattern=$1     shift #used when supplied arguments for loops as to not alter arguments     for fname in $*         do             if [ -f $fname ]                 then                     echo Deleting $pattern from $fname                     sed -e ' /'$pattern'/d ' $fname             else                 echo $fname not found      ...

Shell script to display given lines

Script : #accepts a file name , start and end line via arguments if [ $# -eq 0 ]     then         echo No arguments found elif [ $# -eq 1 ]     then         echo One argument found elif [ $# -eq 2 ]     then         echo Two arg8+uments found else     if [ ! -e $1 ]         then             echo File does not exixts.     else         sed -ne ' '$2','$3'p ' $1     fi fi Output: root@root:~/os# sh f_test_1.sh test1 2 3 this is a temp file line 2 this is a temp file line 3