Posts

Showing posts from August, 2018

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