Sunday, 17 June 2012

Write a program to calculate total waiting and turn around time of n processes with ROUND ROBIN Scheduling algorithm


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,tot_wt=0,t,n=5,found=0,m;
float avg_wt;
clrscr();
for(i=0;i<n;i++)
{
printf("enter the process name : ");
scanf("%s",&p[i]);
printf("enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);

count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{

wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
tot_wt+=wt[i];
}
avg_wt=(float)tot_wt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\ntotal waiting time %d\n",tot_wt);
printf("total avgtime %f",avg_wt);
}

Write a program to calculate total waiting and turn around time of n processes with FCFS CPU Scheduling algorithm


#include<stdio.h>
#include<conio.h>
void main()
{
int st[5],wt[5]={0,0,0,0,0},trt[5]={0,0,0,0,0},i;
float tot_trt=0,tot_wt=0;
clrscr();
printf("Enter the service time of each process\n");
for(i=0;i<5;i++)
{
scanf("%d",&st[i]);
}
//trt[0]=0;
//wt[0]=0;
for(i=0;i<5;i++)
{
trt[i]=wt[i]+st[i];
wt[i+1]=trt[i];
}
for(i=0;i<5;i++)
{
tot_trt=tot_trt+trt[i];
tot_wt=tot_wt+wt[i];
}
tot_trt=tot_trt/5;
tot_wt=tot_wt/5;
for(i=0;i<5;i++)
{
printf("\nTurn Around Time of each process is %d and waiting time of each process %d\n",trt[i],wt[i]);
}
printf("\nTotal turn around time is %d and Total Waiting time is %d\n",tot_trt,tot_wt);
getch();
}

Write a program to calculate number of page faults with LRU page replacement


#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frame_size=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frame_size-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

Write a program to calculate number of page faults with OPTIMAL page replacement


#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}

if(flag2==0)
{
for(i=0;i<3;i++)
lg[i]=0;
for(i=0;i<frsize;i++)
{
for(k=j+1;k<12;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

Write a program to calculate number of page faults with FIFO page replacement


#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frame_size=3,top=0;

for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<12;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<frame_size;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=frame_size)
top=0;
}
display();
}
printf("Number of page faults  : %d ",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}


Write a shell script to convert the entered upper case string to lower case string


clear
echo enter the string in uppercase
read s
echo $s|tr '[:upper:]' '[:lower:]'

Write a shell script to reverse an array


clear
echo enter the size of array...
read s
echo enter the elements of array
for ((i=0; $i<$s; i++))
do
            read a[$i]
done
j=`expr $s - 1`
for ((i=0; $i<$j; i++))
do
            temp=${a[$i]}
            a[$i]=${a[$j]}
            a[$j]=$temp
            j=`expr $j - 1` 
done

echo The reverse array is
for ((i=0; $i<$s; i++))
do
echo ${a[$i]}
done

Write a shell script to add two arrays


clear
echo enter the size of array..
read s
echo enter the elements of array A...
for ((i=0; $i<$s; i++))
do
            read a[$i]
done
echo enter the elements of Array B
for ((i=0; $i<$s; i++))
do
            read b[$i]
done
echo The sum of array is...
for ((i=0; $i<$s; i++))
do
            c[$i]=`expr ${a[$i]} + ${b[$i]}`
            echo ${c[$i]}
done

Write a shell script to remove duplicates values from an array


clear
echo enter the size of array..
read s
echo enter elements of Array
for (( i=0; $i<$s; i++ ))
do
            read a[$i]
done
for (( i=0; $i<$s; i++ ))
do
            for ((j=$i+1; $j<$s; j++))
            do
                        if [ ${a[$i]} -eq ${a[$j]} ]
                        then
                                    a[$j]=0
                        fi
            done
done
j=0
echo The elements are
for ((i=0; i<$s; i++))
do
            if [ ${a[$i]} != 0 ]
            then
                        b[$j]=${a[$i]}
                        j=`expr $j + 1`
            fi
done
for (( i=0; i<$j; i++ ))
do
            echo ${b[$i]}
done

Size of array A is 10 while size of B is 30. Scan 10 integers in both the array and concat array A to B. Then apply sorting algorithm according to the user choice


clear
echo enter the elements for array A..
for (( i=0; i<10; i++ ))
do
            echo enter `expr $i + 1` element.
            read a[$i]
done
echo enter the elements for array B..
for (( j=0; j<10; j++ ))
do
            echo enter `expr $j + 1` element.
            read b[$j]
done
for (( k=10; k<20; k++ ))
do
            z=`expr $k - 10`
            b[$k]=${a[$z]}
done
for (( i=0; i<20; i++ ))
do
            for (( j=`expr $i + 1`; j<20; j++ ))
            do
                        if [ ${b[$i]} -gt ${b[$j]} ]
                        then
                                    x=${b[$i]}
                                    b[$i]=${b[$j]}
                                    b[$j]=$x
                        fi
            done
done
clear
echo 1.Ascending  2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
            echo the ascending order is....
            for (( i=0; i<20; i++ ))
            do
                        echo ${b[$i]}
            done
elif [ $c = 2 ]
then
            echo the descending order is...
            for (( i=20; i>0; i-- ))
            do
                        echo ${b[$i]}
            done
else
            echo wrong choice......
fi

Write a shell script to check whether the entered number is Armstrong or not


clear
echo enter any number..
read n
m=$n
sum=0
while [ $m != 0 ]
do
            y=`expr $m % 10`
            x=`expr $y \* $y \* $y`
            sum=`expr $sum + $x`
            m=`expr $m / 10`
done
if [ $sum -eq $n ]
then
            echo the number is armstrong...
else
            echo the number is not armstrong...
fi

Write a shell script to arrange numbers in ascending or descending order as per the user choice



echo enter the no of element
read n1
for (( i=0; i<$n1; i++ ))
do
            echo enter `expr $i + 1` the element.
            read a[$i]
done
for (( i=0; i<$n1; i++ ))
do
            for (( j=`expr $i + 1`; j<$n1; j++ ))
            do
                        if [ ${a[$i]} -gt ${a[$j]} ]
                        then
                                    x=${a[$i]}
                                    a[$i]=${a[$j]}
                                    a[$j]=$x
                        fi
            done
done
echo 1.Ascending  2.Descending
echo enter your choice...
read c
if [ $c = 1 ]
then
            echo the ascending order is....
            for (( i=0; i<$n1; i++ ))
            do
                        echo ${a[$i]}
            done
elif [ $c = 2 ]
then
            echo the descending order is...
            for (( i=$n1; i>0; i-- ))
            do
                        echo ${a[`expr $i - 1`]}
            done
else
            echo wrong choice......
fi

Write a shell script to accept two filenames and check if both exist. If the second filename exists, then the contents of the first filename should be appended to it. If the second filename does not exist then create a newfile with the contents of the first file


echo enter the file name
read first
echo enter the second file name
read second
if [ -e $first ]
then
if [ -e $second ]
then
cat $first>>$second
else
cat $first>$second
fi
fi

Write a shell script to accept an alphabet from the user and list all the files/directory starting with that alphabet in the current directory


echo enter character
read c
for i in `ls -l|tr -s " "|cut -d " " -f 9`
do
 x=`echo $i|cut -c 1`
if [ $x = $c ]
then echo $i
fi
done

Write a shell script to find the number of ordinary files and directory files in the current directory


f=0
d=0
for i in `ls -l|tr -s " "|cut -c 1`
do
if [ $i = "-" ]
then
f=`expr $f + 1`
elif [ $i = "d" ]
then
d=`expr $d + 1`
fi
done
echo no of ordinary files are $f
echo no of directories are $d

Write a shell script to find the file or directory with the maximum size in the current directory


max=0
for i in `ls -l|tr -s " "|cut -f 5 -d " "`
do
if [ $max -le $i ]
then
max=$i
fi
done
echo maximum size is: $max

Write a shell script to accept a number and a word as command line arguments and print the word the given number of times on each line


i=1
f=""
temp=$1" "
while [ $i -le $3 ]
do
f=$f$temp
i=`expr $i + 1`
done
i=1
while [ $i -le $2 ]
do
echo $f
i=`expr $i + 1`
done