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