Posts

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