Download as pdf or txt
Download as pdf or txt
You are on page 1of 121

Ex no: 2.

A
IILUSTRATE UNIX COMMANDS
Date:

i)cat
The cat command is considered as one of the most frequently used commands on
Linux. It can be used for the following purposes under UNIX or Linux
Syntax
Display text files on screen - cat filename
Combine text files - cat file1 file2
Create the file - cat > filename
Display file with line number - cat –n filename

ii) mkdir - mkdir (make directory) command is used to create a directory


Syntax-mkdir directory name

KGISL institute of technology 711722104055


iii)rmdir - Removes a directory.
Syntax-rmdir directory name

iv)cd - Change directories. cd allows you to go to another directory, and thus, change your
current directory.
Syntax-cd path

v) ls - Lists the contents of the current directory. –l option is for long listing. -more
command is used to view page by page.
Syntax-ls directory name

vi) pwd-The pwd command (print working directory) is used to output the path of the
current working directory.
Syntax-pwd

vii) echo-echo command is used to send the output to file or a terminal.In this case, it
combines and print two strings
Syntax-echo hi

viii)cut-Cut command in unix (or linux) is used to select sections of text from each line of
files. You can use the cut command to select fields or columns from a line by specifying a
delimiter or you can select a portion of text by specifying the range or characters. Basically
the cut command slices a line and extracts the text
Syntax- cut –c1-2 filename

KGISL institute of technology 711722104055


ix) tr-Tr is an abbreviation of translate or transliterate, indicating its operation of replacing
or removing specific characters in its input data set.In our case, tr replaces lower case letters
to uppercase and stores the result in a file.
Syntax-tr '[a-z]' '[A-Z]' <filename

X) mv - Rename the two files.


Syntax- mv oldname newname

xi) cal command prints calendar of the given month or year. If the user does not specify any
command-line options, cal will print a calendar of the current month.
Syntax-cal month year

xii) sort-sort is a command that prints the lines of its input or concatenation of all files listed
in its argument list in sorted order. Sorting is done based on one or more sort keys extracted
from each line of input. By default, the entire input is taken as sort key. The "-r" flag will
reverse the sort order.

Syntax- sort(ascending),sort –r(reverse)

KGISL institute of technology 711722104055


xiii) paste- is a Unix command used to join files horizontally (parallel merging) by outputting
lines consisting of the sequentially corresponding lines of each file specified, separated by
tabs, to the standard output
Syntax- paste filename1 filename2

xiv) wc- is short for Word count. With various options turned on, you can count number or
bytes, characters, words, or lines from a file or standard input.
Syntax
wc -l <filename> print the line count
wc -c <filename> print the byte count
wc -m <filename> print the character count
wc -L <filename> print the length of longest line
wc -w <filename> print the word count

KGISL institute of technology 711722104055


xv) top - display top CPU processes,The top command is a very useful tool for quickly
showing processes sorted by various criteria.

Syntax- top

KGISL institute of technology 711722104055


xvi)Tail- prints the last 10 lines of each FILE to standard output. With more than one FILE, it
precedes each set of output with a header giving the file name. If no FILE is specified, or if
FILE is specified as a dash ("-"), tail reads from standard input
Syntax-tail filename

xvii)Uniq- prints the unique lines in a sorted file, retaining only one of a run of matching
lines. Optionally, it can show only lines that appear exactly once, or lines that appear more
than once. uniq requires sorted input since it compares only consecutive lines.
Syntax- uniq –u ,uniq –d

xviii) cmp-Compares two files and tells you what line numbers are different
Syntax-cmp filename1 filename2

xix) who- displays a list of users who are currently logged into a computer. who am i
displays information about the current user only.
Syntax-who

KGISL institute of technology 711722104055


xx) man -Show the Manual Page (documentation) for a command (or function)
Syntax-man ls – gives the documentation for ls command.

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim,algorithm and output for unix commands is verified and executed
successfully.

KGISL institute of technology 711722104055


Ex no: 2.B
IILUSTRATE SHELL PROGRAMMING
Date:

1.Write a Shell program to check the given number is even or odd

Algorithm:
Step 1: Start
Step 2: Read the number
Step 3: If modulus value of the number by 2 equals 0, print Number is Even, else print
Number is odd.
Step 4: Stop

Program:
echo "Enter the Number"
read a
b=`echo "$a % 2"|bc`
if [ $b -eq 0 ]
then
echo "Given Number is Even "
exit
fi
echo " Given Number is odd"

Output:

2.Write a shell program to find the sum and average of four integers

Algorithm:
Step 1: Start
Step 2: Read a , b , c , d
Step 3: Add a,b,c,d and store in sum.
Step 4: Find average for a,b,c,d and store in avg
Step 5: Display sum and avg
Step 6: Stop

Program:
echo Enter four integers with space between
read a b c d
sum=`expr $a + $b + $c + $d`
avg=`expr $sum / 4`

KGISL institute of technology 711722104055


echo Sum=$sum
echo Average=$avg
Output:

3.Write a shell program to compute simple interest and compound interest.

Algorithm:
Step1: start
Step2: read p,n,r
Step3: calculate simple interest for p,n,r and store in S.I
Step4: display S.I
Step5: stop

Program:
echo " Enter the principle value: "
read p
echo " Enter the rate of interest:"
read r
echo " Enter the time period:"
read n
s=`expr $p \* $n \* $r / 100`
echo " The simple interest is "
echo $s

Output:

4.Write a shell program to swap two numbers

Algorithm:
Step1: Start
Step2: Read x ,y
Step3: Swap the values of x and y with help of a temporary variable ‘z’
Step4: Display the swapped values of x and y
Step5: Stop

Program:

KGISL institute of technology 711722104055


echo "Enter value for x : "
read x
echo "Enter value for y : "
read y
echo "Before swap, x = $x and y = $y"
z=$x
x=$y
y=$z
echo "After swap, x = $x and y = $y

Output:

5.Write a Shell program to find the area and circumference of a circle


Algorithm
Step 1: Start
Step 2: Read the value of radius
Step 3: Calculate area of circle [πr2].Print area.
Step 4: Calculate circumference of circle [2πr].Print circumference.
Step 5: Stop
Program:

echo "Enter the radius:"


read r
area=`echo 3.14 \* $r \* $r | bc`
cir=`echo 2 \* 3.14 \* $r | bc`
echo "Area : $area"
echo "Circumference : $cir"

Output:

6.Write a Shell program to find the roots of a quadratic equation

Algorithm
Step 1: Start
Step 2: Read the value of quadratic equation constants a,b,c

KGISL institute of technology 711722104055


Step 3: Calculate d=b2-4ac
Step 4: Add and subtract b with discriminant value to print the roots of quadratic equation
Step 5: Print the roots value
Step 5: Stop
Program:
echo "Enter the value for a"
read a
echo "Enter the value for b"
read b
echo "Enter the value for c"
read c
d=`expr $b \* $b - 4 \* $a \* $c`
x1=`echo "scale=3; (-$b + sqrt($d)) / (2 * $a)" | bc`
x2=`echo "scale=3; (-$b - sqrt($d)) / (2 * $a)" | bc`
echo "Root 1 : $x1"
echo "Root 2 : $x2"
Output:

7.Write a shell program to check whether the given number is positive or negative.
Algorithm:
Step 1: Start
Step 2: Read a number n
Step 3: if n is greater than 0
Step 4: Display the result as positive number
Step 5: else display the result as negative number
Step 6: Stop.

Program:
echo "Enter the number:"
read n
if [ $n -gt 0 ]
then
echo $n "is positive"
elif [ $n -lt 0 ]
then
echo $n "is negative"
else
echo $n "is zero"
fi

KGISL institute of technology 711722104055


Output:

8.Write a shell program to find the greatest among three numbers


Algorithm:
Step 1: Start
Step 2: Read the value of a, b, c
Step 3: check the value of a with b & c
Step 4: if it is true, Print a is greater
Step 5: Else if the condition fails check the value of b with a & c
Step 6: if the condition is true, Print b is greater
Step 7: if both the condition fails, print c is greater
Step 8: Stop
Program:
echo Enter 3 numbers with spaces in between
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Largest of $a $b $c is $l

Output:

KGISL institute of technology 711722104055


9.Write a Shell program to find the sum of all numbers between 50 and 100, which are
divisible by 3 and not divisible by 5
Algorithm
Step 1: Start
Step 2: Read the number,n
Step 3: Initialize s to 9
Step 4: Repeat Until n value is greater than 0
i.Calculate n modulo 10 and store in r
ii.If r is less than s,assign r to s
iii.Divide the number by 10 to ignore last digit
Step 5: Print the smallest digit is, s
Step 6: Stop

Program:

sum=0
for((i = 50 ; i<= 100 ; i++))
do
if [ `expr $i % 3` -eq 0 -a `expr $i % 5` -ne 0 ]
then
sum=`expr $sum + $i`
fi
done
echo $sum
Output:

10.Write a Shell program to check and display 10 leap years


Algorithm
Step 1: Start
Step 2: Use a loop to iterate over the years in a range
Step 3: If year is divisible by 400,print year is a leap year.
Step 4: Also,If the year is divisible by 4 and not divisible by 100, print year is a leap year.
Step 5: Stop

Program:
for((i = 2020 ; i<= 2040 ; i++))
do if [ `expr $i % 400` = 0 ]
then echo "$i is a leap year"
elif [ `expr $i % 4` = 0 -a `expr $i % 100` != 0 ]
then echo "$i is a leap year"
fi
done

KGISL institute of technology 711722104055


Output:

11.Write a Shell program to display student grades


Algorithm
Step 1: Start
Step 2: Read the number of students
Step 3: Use a for loop to get rollno, name, marks
i.Add marks and store in tot
ii. Calculate average
iii. If average is greater than or equal to 75, assign grade as distinction
iv. Else If average is greater than or equal to 60, assign grade as First class
v. Else If average is greater than or equal to 50, assign grade as Second class
vi. Else assign grade as Fail
Step 4: Print rollno, name, Total Average Grade
Step 5: Stop
Program:

echo "Enter the number of students:"


read n
for((i = 1 ; i <= n ; i++))
do
echo "Enter roll no.:"
read rollno
echo "Enter name:"
read name
echo "Enter mark-1"
read m1
echo "Enter mark-2:"
read m2
echo "Enter mark-3:"
read m3
tot=`expr $m1 + $m2 + $m3`
avg=`expr $tot / 3`
if [ $avg -ge 75 ]
then
grade="Distinction"
elif [ $avg -ge 60 ]
then
grade="First class"
elif [ $avg -ge 50 ]
then grade="Second class"
else grade="Fail"

KGISL institute of technology 711722104055


fi
echo "Roll no. Name Total Average Grade $rollno $name $tot $avg $grade"
done

Output :

12.Write a Shell program to check the given number and its reverse are same

Algorithm:
Step 1: Start
Step 2: Read the number,n
Step 3: Repeat Until n value is greater than 0
i.Take n modulo 10 to extract the last digit from n
ii.use a temporary sum variable s which multiplies it’s content with 10,added with r
value.This is done to reverse the place value of digits.
iii.Divide n by 10 to ignore the final digit
Step 4: If s value is equal to input,print the number and it’s reverse are same,else print they
are not same
Step 5: Stop

Program:
echo "Enter a number:"
read n
t=$n
s=0
while [ $n -gt 0 ]
do

KGISL institute of technology 711722104055


r=`expr $n % 10`
s=`expr $r + $s \* 10`
n=`expr $n / 10`
done
if [ $s = $t ]
then
echo "The given number and its reverse are same"
else
echo "The given number and its reverse are not same"
fi

Output:

13.Write a shell program to find the factorial of a number


Algorithm:
Step 1: Start
Step 2: Read the value for x
Step 3: Initialize the values for I and f are 1
Step 4: Use do while condition for iterating the loop until it multiplies chronically
upto the given value to find factorial
Step 5: Print the factorial value
Step 6: Stop

Program:
echo "Enter the number:"
read x
i=1
f=1
while [ $i -le $x ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
c=`expr $i - 1`
done
echo "Factorial value is:" $f
Output:

KGISL institute of technology 711722104055


14.Write a shell program to find the sum of the digits of a given number
Algorithm:
Step 1: Start
Step 2: Read the value for n
Step 3: Initialize the values for s to 0
Step 4: Use while condition to repeat the loop until the value of n is greater than 0
Step 5: Split the numbers individually from n and store it in a temporary variable
Step 6: Every time when the loop is repeated the value which is splitted must be store
and added
Step 7: Print the value which has stored the sum of the value
Step 8: Stop
Program:
echo sum of digits of a number
echo enter a number
read n
s=0
while [ $n –gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r`
n=`expr $n / 10`
done
echo sum of the digits is $s
Output:

15.Write a Shell program to check the given string is palindrome or not


Algorithm:
Step 1: Start
Step 2: Read the string s
Step 3: Find string length and store in l
Step 3: Repeat Until c value is less than or equal to string length
i.Extract the first character from string using substr function
ii.Store the character in variable p
iii.Increment c count to extract the next character
Step 4: If p value is equal to input,print the given string is a palindrome,else print given string
is not a palindrome
Step 5: Stop
Program:
echo "Enter the string:"
read s
l=`expr length $s`
c=1
p=""
while [ $c -le $l ]

KGISL institute of technology 711722104055


do
e=`expr substr $s $c 1`
p=$e$p
c=`expr $c + 1`
done
if
[ $p = $s ]
then
echo "The given string $s is a palindrome"
else
echo "The given string $s is not a palindrome"
fi
Output:

16.Write a Shell program to check the given integer is Armstrong number or not
Algorithm
Step 1: Start
Step 2: Read the number,n
Step 3: Repeat Until n value is greater than 0
i.Take n modulo 10 to extract the last digit from n
ii.use a sum variable s which adds it’s content with cubic value of r each time
iii.Divide n by 10 to ignore the final digit
Step 4: If s value is equal to input,print the number is an amstrong number,else is not an
armstrong number
Step 5: Stop

Program:
echo "Enter a number:"
read n
t=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r \* $r \* $r`
n=`expr $n / 10`
done
if [ $s = $t ]
then
echo "$t is an armstrong number"
else

KGISL institute of technology 711722104055


echo "$t is not an armstrong number"
fi

Output:

17.Write a Shell program to check the given integer is prime or not


Algorithm
Step 1: Start
Step 2: Read the number,n
Step 3: Initialize flag value to 0
Step 3: Repeat Until loop counter value i is less than or equal to n/2
i.If n is divisible by i,set flag=1
Step 4: If flag=0,print the number is a prime number,else is not a prime number
Step 5: Stop
Program:
echo "Enter a number:"
read n
flag=0
for((i = 2 ; i <= n / 2 ; i++))
do
r=`expr $n % $i`
if [ $r = 0 ]
then
flag=1
break
fi
done
if
[ $flag = 0 ]
then
echo "$n is a prime number"
else
echo "$n is not a primenumber"
fi
Output:

KGISL institute of technology 711722104055


18.Write a Shell program to generate Fibonacci series.
Algorithm
Step 1: Start the program.
Step 2: Read the number.
Step 3: Initialize the value of num1 to 0,num2 to 1.
Step 4: Set the count value to two.
Step 5: Use do…while loop to check the number with the count.
Step 6: Compute num3 from num1 and num2.
Step 7: Compute the count value.
Step 8: Stop the program.

Program:

echo –n “Enter How many numbers:”


read num
num1=0
num2=1
echo –n “Fibonacci series: “
echo –n $num1
echo –n “ $num2”
count=2
while [ $count –le $num ]
do
num3=`expr $num1 + $num2`
echo –n “ $num3 “
num1=$num2
num2=$num3
count=`expr $count + 1`
done
echo –e “\nFinished…..”
Output:

19.Write a Shell program to execute various UNIX commands using case statements
Algorithm
Step 1: Start
Step 2: Display the options to execute different unix commands
Step 3: Get the choice from user
Step 4: Use a case statement that executes different commands based on choice given
Step 5: Stop

Program:

echo "1-who am I?"

KGISL institute of technology 711722104055


echo "2-who is logged on?"
echo "3-date"
echo "4-calendar"
echo "Enter your choice:"
read n
case $n in
1) whoami ;;
2) who ;;
3) date ;;
4) cal ;;
esac
Output:

20.Write a Shell program to find the smallest digit from a number


Algorithm
Step 1: Start
Step 2: Read the number,n
Step 3: Initialize s to 9
Step 4: Repeat Until n value is greater than 0
i.Calculate n modulo 10 and store in r
ii.If r is less than s,assign r to s

KGISL institute of technology 711722104055


iii.Divide the number by 10 to ignore last digit
Step 5: Print the smallest digit is, s
Step 6: Stop
Program:

echo "Enter a number:"


read n
s=9
while [ $n -gt 0 ]
do
r=`expr $n % 10`
if [ $r -lt $s ]
then
s=$r
fi
n=`expr $n / 10`
done
echo "The smallest digit is : $s"

Output:

21.Write a Shell program to count the number of vowels in a line of text


Algorithm
Step 1:Start the program.
Step 2:Read the line containing text.
Step 3: If the character is a vowel increment the vowel counter.
Step 4: Continue the process till the end.
Step 5: Stop the program.

Program:
clear
echo -n "Enter the Name / String:"
while :
do
read name
echo $name | awk '{print gsub(/[aeiou]/,"")}'
done

Output:

KGISL institute of technology 711722104055


22.Write a shell script to display the digits which are in odd
position in a given number
Algorithm
Step 1:Start the program.
Step 2:Read the number.
Step 3:Initialize the value of n to 1.
Step 3:Use do…while loop and split the number using cut
command.
Step 4:Sum the position of the digit with n.
Step 5:Stop the program.
Program:
echo Enter a 5 digit number
read num
n=1
while [ $n -le 5 ]
do
a=`echo $num | cut -c $n`
echo $a
n=`expr $n + 2`
done
Output:

23.Write a shell program to find the sum of two numbers


using function programming

Algorithm
Step 1:Start the program.
Step 2:Read the number
Step 3:Create a function add()
Step 4:Compute the addition value of A&B
Step 5:Cal the of function add()
Step 6:Stop the Program

Program:

#function to add two numbers


add()
{
x=$1
y=$2

KGISL institute of technology 711722104055


echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}
# main script
echo "enter first number"
read first
echo "enter second number"
read sec
#calling function
add $first $sec
echo "end of the script"

Output:

24.Write a shell program to find the largest number between


two numbers using function.
Algorithm
Step 1:Start the program.
Step 2:Read the number
Step 3:create function the largest()
Step 4: Check if the number a is greater than b
Step 5:cal the function
Step 6:Stop the program
Program:
read a
read b
largest()
{
if [ $a -gt $b ]
then
echo "$a is greater"
else
echo "$b is greater"
fi
}
largest

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:

Hence the aim,algorithm and output for shell programming is verified and executed
successfully.

KGISL institute of technology 711722104055


Ex no: 3
IMPLEMENTATION OF SYSTEM CALLS
Date:

Aim :
To write C programs for the fork, exec, getpid, exit, wait, close,stat, opendir, readdir
system calls of UNIX operating system.

1.Program to get process id

Algorithm:
1. Include necessary header files for using systems calls.
2. Make necessary declaration.
3. Get the process identification number and parent process identification number using
getpid() and getppid() system calls
4. Display the process id’s
Program:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
main()
{
int pid,ppid;
pid=getpid();
ppid=getppid();
printf("\n Process Id is %d\n",pid);
printf("\n Parent Process Id is %d\n",ppid);
}
Output:

2. Program using fork system call


Algorithm:
1. Include necessary header files for using systems calls.
2. Make necessary declaration.
3. Get the process identification number of child process created
4. Display the process ID and parent process ID using getpid() and getppid() system
calls
Program:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
main()

KGISL institute of technology 711722104055


{
int pid;
pid=fork();
if(pid==0)
{
printf("\n I am the child, my process ID is %d ",getpid());
printf("\n I am the child's parent process ID is %d ",getppid());
}
else
{
printf("\n I am the parent, my process ID is %d ",getpid());
printf("\n I am the parent's parent process ID is %d ",getppid());
}

Output:

3. Program using wait system call


Algorithm:
1. Include necessary header files for using systems calls.
2. Make necessary declaration.
3. Use wait system call to synchronize parent process and child process.
4. Display the details of process synchronization.

Program:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
int pid,i=0;
printf("\n Ready to fork");
pid=fork();
if(pid==0)
{
printf("\n Child starts ");
for(i=0;i <1000;i++);
printf("\n Child ends ");
}
else
{
wait(0); for(i=0;i<1000;i++);

KGISL institute of technology 711722104055


printf("\n Parent process ends ");
}
}
Output:

4. Program using exit system call


Algorithm:
1. Include necessary header files for using systems calls.
2. Make necessary declaration.
3. Use exit system call to ends a process and returns a value to it parent.
4. Display the result.
Program:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
int p;
p=fork();
if(p==0)
{
printf("\n Child created ");
exit(0);
printf("\n Process ended ");
}
if(p<0)
{
printf("\n Cannnot create child ");
exit(-1);
printf("\n Process ended ");
}
}

Output:

KGISL institute of technology 711722104055


5. Program using open,write and close system calls
Algorithm:
1. Include necessary header files for using systems calls.
2. Make necessary declaration.
3. Use open system call to open a file for reading, writing, or reading and writing.
4. Use write system call to write in a file by obtaining a file descriptor.
5. Close file descriptor
Program:
#include<fcntl.h>
#include<unistd.h>
main()
{
int fd,i;
fd=open("test1",O_CREAT|O_RDWR|O_APPEND,0666);
for(i=0;i<10;i++)
write(fd, "UNIX ",1);
close(fd);
}

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:

Hence the aim,algorithm and output for unix and shell programming is verified and
executed successfully.

KGISL institute of technology 711722104055


Ex no: 4.A
IMPLEMENTATION OF CPU SCHEDULING - FCFS
Date:

Aim:
To schedule snapshot of processes queued according to FCFS (First Come First
Serve)

Algorithm:

1. Get length of the ready queue, i.e., number of process (say n)


2. Obtain btime for each process.
3. The wtime for first process is 0.
4. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
5. Compute average waiting time awat and average turnaround time atur
6. Display the btime, ttime and wtime for each process.
7. Display average waiting time and average turn around time
8. Stop

Program:

#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time");
for(i=0;i<n;i++)
{
printf("\nP[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];

KGISL institute of technology 711722104055


printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:

Hence the aim,algorithm and output for FCFS scheduling is verified and executed
successfully.

KGISL institute of technology 711722104055


Ex no: 4.B
IMPLEMENTATION OF CPU SCHEDULING - SJF
Date:
Aim:
To schedule snapshot of processes queued according to SJF (Shortest Job First)
scheduling.
Algorithm:

1. Get length of the ready queue, i.e., number of process (say n)


2. Obtain btime for each process.
3. Sort the processes according to their btime in ascending order.
a. If two process have same btime, then FCFS is used to resolve the tie.
4. The wtime for first process is 0.
5. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turnaround time atur.
7. Display btime, ttime and wtime for each process.
8. Display awat and atur
9. Stop
Program:
#include<stdio.h>
void main()
{
int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
float avg=0;
printf("\nEnter number of Processes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Process %d ID:",i+1);
scanf("%d", &process[i]);
printf("\nEnter Process %d Time:",i+1);
scanf("%d",&ptime[i]);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
ptemp = process[i];
process[i] = process[j];
process[j] = ptemp;
} }}
wtime[0]=0;

KGISL institute of technology 711722104055


for(i=1;i<n;i++)
{
wtime[i]=wtime[i-1]+ptime[i-1];
total=total+wtime[i];
}
avg=(float)total/n;
printf("\nP_ID\t P_TIME\t W_TIME\n");
for(i=0;i<n;i++)

printf("\n%d\t\t %d\t\t %d",process[i],ptime[i],wtime[i]);


printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
}

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:

Hence the aim,algorithm and output for SJF scheduling is verified and executed
successfully.

KGISL institute of technology 711722104055


Ex no: 4.C
IMPLEMENTATION OF CPU SCHEDULING - PRIORITY
Date:

Aim :
To schedule snapshot of processes queued according to Priority scheduling.

Algorithm :
1. Get length of the ready queue, i.e., number of process (say n)
2. Obtain btime and pri for each process.
3. Sort the processes according to their pri in ascending order.
a. If two process have same pri, then FCFS is used to resolve the tie.
4. The wtime for first process is 0.
5. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turn around time atur
7. Display the btime, pri, ttime and wtime for each process.
8. Display awat and atur
9. Stop

Program:

#include<stdio.h>
void main()
{
int burst[10],pid[10],priority[10],wt[10],total, n, i, j, temp;
printf("\nPriority Scheduling");
printf("\nEnter Number of Processes:");
scanf("%d",&n);
printf("\nEnter %d Processes Details\n\n",n);
for(i=0;i<n;i++)
{
printf("\nEnter Process %d ID:",i+1);
scanf("%d",&pid[i]);
printf("\nEnter Process %d Burst Time:",i+1);
scanf("%d",&burst[i]);
printf("\nEnter Process %d Priority:",i+1);
scanf("%d",&priority[i]);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(priority[i]<priority[j])
{
//Swapping Processes Based on Priority
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;

KGISL institute of technology 711722104055


//Swapping Process ID Accordingly
temp=pid[i];
pid[i]=pid[j];
pid[j]=temp;
//Swapping Burst Time
temp=burst[i];
burst[i]=burst[j];
burst[j]=temp;
}
}
}
//Waiting time for the First Process is zero.
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+burst[i-1];
total=total+wt[i];
}
printf("\nPRIORITY\tP_ID\tP_TIME\tWT_TIME\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\t%d\n",priority[i],pid[i],burst[i],wt[i]);
}
}

Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded
Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:

Hence the aim,algorithm and output for priority scheduling is verified and executed
successfully.

KGISL institute of technology 711722104055


Ex no: 4.D
IMPLEMENTATION OF CPU SCHEDULING – ROUND ROBIN
Date:

Aim :
To schedule snapshot of processes queued according to Round robin scheduling.

Algorithm :
1. Get length of the ready queue, i.e., number of process (say n)
2. Obtain Arrrival time at Burst time bt for each processes Pi.
3. Get the time slice per round, say ts
4. Determine the number of rounds for each process.
5. The wait time for first process is 0.
6. If bt > ts then process takes more than one round. Therefore turnaround and
waiting time should include the time spent for other remaining processes in the
same round.
7. Calculate average waiting time and turnaround time
8. Display the burst time, turnaround time and wait time for each process (in order
of
rounds they were processed).
9. Display average wait time and turnaround time
10. Stop

Program :

#include<stdio.h>

int main()

int count,j,n,time,remain,flag=0,time_quantum;

int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];

printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

for(count=0;count<n;count++)

printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);

scanf("%d",&at[count]);

scanf("%d",&bt[count]);

rt[count]=bt[count];

KGISL institute of technology 711722104055


}

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

if(rt[count]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);

wait_time+=time-at[count]-bt[count];

turnaround_time+=time-at[count];

flag=0;

if(count==n-1)

count=0;

KGISL institute of technology 711722104055


else if(at[count+1]<=time)

count++;

else

count=0;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;

Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded

Aim and algorithm 20

Program 40

Output 20

Result 10

Viva voce 10

Total marks 100

Result:

Hence the aim , algorithm and output for the cpu scheduling round robin is verified
and executed successfully.

KGISL institute of technology 711722104055


Ex no: 5
INTER PROCESS COMMUNICATION STRATEGY
Date:

Aim :
To write a C Program to implement interprocess communication using shared memory.
Algorithm :
smserver.c
1. Start the program.
2. Declare the variables – shmid, key, size.
3. Assign a value for the variable ‘key’.
4. Create a shared memory segment (of size 27 bytes) and obtain access to it.
5. Attach the segment to the data space.
6. Write user input data directly to the segment.
7. Wait until the other process reads the segment and changes the first character of memory as
‘*’.
8. Stop the program

smclient.c
1. Start the program.
2. Declare the variables – shmid, key, size.
3. Assign a value for the variable ‘key’.
4. Locate the segment created by server.
5. Attach the segment to the data space.
6. Read the data available in the shared memory segment.
7. After reading, change the first character as ‘*’, to indicate the segment has been read.
8. Stop the program.

Program:
smserver.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#define SHMSIZE 27
int main()
{
char data;
int shmid,i;
key_t key;
char *shm,*s;

KGISL institute of technology 711722104055


key=5678;
if((shmid=shmget(key,SHMSIZE,IPC_CREAT | 0666))<0)

{
perror("shmget got failed");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char *) -1)

{
perror("shmat got failed");
exit(1);
}
s=shm;

for(i=0;i<60;i++){
scanf("%c",&data);
*s++=data;
*s=NULL;
}

while(*shm != '*')
sleep(1);
exit(0);
}
smclient.c

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdlib.h>
#define SHMSIZE 27
main()

KGISL institute of technology 711722104055


{
int shmid;
key_t key=5678;

char *shm, *s;


if((shmid=shmget(key,SHMSIZE,0666))<0)
{
perror("\n Shmget got failed");
exit(1);

}
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(1);

}
for(s=shm;*s!= NULL;s++)
putchar(*s);
putchar('\n');
*shm='*';

exit(0);
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result :
The program for inter process communication using shared memory was implemented
and verified successfully.

KGISL institute of technology 711722104055


Ex no: 6
IMPLEMENT MUTUAL EXCLUSION BY SEMAPHORE
Date:

Aim :
To implement producer and consumer problem using semaphore variable.

Algorithm :

1. Start the process.


2. Define structure to get items
3. Create two semaphore variables empty and full
4. Initilize empty semaphore with 1
5. Initilize full semaphore with 0
6. Create one mutual exclusion variable mutex
7. Initilize mutex with 1
8. Define wait() and signal() function
9. Use swith case statement to process through produce,consume,and display
definitions
10. Stop the process.

Program:

#include <stdio.h>
#include <string.h>
#define size 5
struct process
{
char item[10];
}p[10];
int flag=0, full=0, empty=size, mutex=1;

int wait(int s)
{
if(s==0)
flag=1;
else

s--;
return s;
}

KGISL institute of technology 711722104055


int signal(int s)
{
s++;

return s;
}
main()
{
int c, i;

printf("\nProducer-Consumer Problem\n");
while(1)
{
printf("\n1.Produce 2.Consume 3.Display 4.Exit\n");
printf("Enter your choice : ");

scanf("%d", &c);
switch(c)
{
case 1:
empty = wait(empty);

mutex = wait(mutex);
if(flag == 0)
{
printf("Enter the item to produce : ");
scanf("%s", p[full].item);

full = signal(full);
}
else
{
printf("\nBuffer is FULL\n");

flag = 0;

KGISL institute of technology 711722104055


}
mutex = signal(mutex);
break;

case 2:
full = wait(full);
mutex = wait(mutex);
if(flag == 0)
{

printf("Item %s is consumed\n",p[0].item);
for(i=0; i<size; i++)
strcpy(p[i].item, p[i+1].item);
flag=0;
}

else
{
printf("\nBuffer is EMPTY\n");
flag = 0;
}

mutex = signal(mutex);
empty = signal(empty);
break;
case 3:
if(full != 0)

{
printf("\nItems in the buffer : ");
for(i=0; i<full; i++)
printf("\n%s", p[i].item);
}

else

KGISL institute of technology 711722104055


{
printf("\nBuffer is EMPTY\n");
flag = 0;

}
break;
case 4:
exit(0);
break;

}
}
}
Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded

Aim and algorithm 20

Program 40

Output 20

Result 10

Viva voce 10

Total marks 100

Result:
Thus the Implementation of producer and consumer problem using semaphore done
successfully.

KGISL institute of technology 711722104055


Ex no: 7
IMPLEMENT BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
Date:

Aim :
To write a C program to implement the banker’s algorithm for deadlock avoidance.

Algorithm :
1. Start the program.
2. Declare the variables for processes, maximum, allocation, available, completed,
safe sequence matrices.
3. Get the number of processes, resources and their instances.
4. Get the maximum, allocation and available matrices for all processes.
i. Step 5: Calculate the need matrix for all process, and initialize the
completed matrix as ‘0’.
5. Repeat steps from 7 to 10, for all process till they complete.
6. Print the allocation and max matrix for all process.
7. For all process, such that completed[p]=0 then go to step 8 else step 9.
8. If available is less than need, break.
9. For the process,i
1. calculate available=available+allocation
2. allocation=0, max=0
3. add the process to safe sequence matrix
4. add this process to completed matrix
10. If all processes are completed print “system in safe state” and also print safe
sequence list, else print “system is not in safe state”.
11. End the program.

Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count;
count = 1;
printf("Enter the no of processes : ");
scanf("%d", &p);

printf("\nEnter the no of resources : ");


scanf("%d", &r);

for (i = 1; i <= p; i++)


completed[i] = 0;
printf("\nEnter the Max Matrix for each process : ");
for (i = 1; i <= p; i++) {
printf("\nFor process %d : ", i);
for (j = 1; j <= r; j++)
scanf("%d", &Max[i][j]);
}

KGISL institute of technology 711722104055


printf("\nEnter the allocation for each process : ");
for (i = 1; i <= p; i++) {
printf("\nFor process %d : ", i);
for (j = 1; j <= r; j++)
scanf("%d", &alloc[i][j]);}

printf("\nEnter the Available Resources : ");


for (i = 1; i <= r; i++)
scanf("%d", &avail[i]);

for (i = 1; i <= p; i++)


for (j = 1; j <= r; j++)
need[i][j] = Max[i][j] - alloc[i][j];

printf("\n Max matrix:\tAllocation matrix:\tNeed matrix\n");


for (i = 1; i <= p; i++) {
for (j = 1; j <= r; j++)
printf("%d ", Max[i][j]);
printf("\t\t\t");
for (j = 1; j <= r; j++)
printf("%d ", alloc[i][j]);
printf("\t\t\t");
for (j = 1; j <= r; j++)
printf("%d ", need[i][j]);
printf("\n");}
do {
for (i = 1; i <= p; i++) {
if (completed[i] == 0) {
process = i;
for (j = 1; j <= r; j++) {
if (avail[j] < need[i][j]) {
process = -1;
break;
}
}
if (process != -1) {
printf("\nProcess %d runs to completion!", process);
safeSequence[count] = process;
count++;
for (j = 1; j <= r; j++) {
avail[j] = avail[j] + alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
}
} while (count <= p && process != -1);
if (count == (p + 1)) {

KGISL institute of technology 711722104055


printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for (i = 1; i <= p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
} else
printf("\nThe system is in an unsafe state!!");
return 0;
}

Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded

Aim and algorithm 20

Program 40

Output 20

Result 10

Viva voce 10

Total marks 100

Result:
Thus the implementation of banker’s algorithm for deadlock avoidance is executed and
verified successfully.

KGISL institute of technology 711722104055


Ex no: 8
IMPLEMENTATION AN ALGORITHM FOR DEADLOCK DETECTION
Date:

Aim :
To write a C program to simulate an algorithm for Dead Lock detection.

Algorithm :
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail, allocation and need matrices.
4. for all process make finish as 0.
5. for all processes
a) If finish=0 and need<work then
i. work += allocation
ii. finish =1;
iii. set flag=1;
6. For all processes
7. If finish =0 then add to dead matrix
8. If flag =1 else go to step 8
9. Print System is in deadlock.
10. for all processes
i. i)If finish=0 and flag=1 then print the process name as deadlock
11. creating process
12. Print system is not in deadlock.
13. end
Program:
#include<stdio.h>
#include<conio.h>
int alloc[10][10];
int need[10][10];
int avail[10];
int n,r;
void input();
void show();
void deadlock_detect();
int main()
{
int i,j;
printf("********** Deadlock Detection Algorithm ************\n");
input();
show();
deadlock_detect();
getch();
return 0;
}
void input()

KGISL institute of technology 711722104055


{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the request matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
scanf("%d",&need[i][j]);
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Request \t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t\t");
for(j=0;j<r;j++)
{
printf("%d ",need[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);

KGISL institute of technology 711722104055


}
}
}
void deadlock_detect()
{
int finish[10],temp,flag=1,k,c1=0,work[10];
int dead[10];
int safe[10];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
work[i]=avail[i];
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=work[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
work[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;

KGISL institute of technology 711722104055


}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
if((finish[i]==0) && (flag==1))
printf("P%d\t",i);
}
}
else
{
printf("\nNo Deadlock Occur");
}}

Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded
Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Thus the implementation of deadlock detection algorithm is executed and verified
successfully.

KGISL institute of technology 711722104055


Ex no: 9
IMPLEMENTATION OF THREADING
Date:

Aim:
To write a c program for the implementation of threading
Algorithm:
1. Start Program
2. Initialize thread id of type pthread_t
3. Initialize counter
4. Create number of threads using pthread_create
5. Call the function that is defined to use thread
6. Check the synchronization of threads using pthread_join
7. Stop the program
Program :
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
void* doThings(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d finished\n", counter);
return NULL;
}
main()
{
int i = 0;
int err;

KGISL institute of technology 711722104055


while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doThings, NULL);
if (err != 0)
printf("\nCan't create thread : %s", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
}
Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Implementation of Threading and Synchronization of thread done sucessfully.

KGISL institute of technology 711722104055


Ex no: 10
IMPLEMENTATION OF PAGING TECHNIQUE
Date:

Aim:
To implement the c program for the paging technique
Algorithm:
1.Initialize System Parameters: Input total memory size and page size, then calculate the
number of pages in memory.
2.Input Process Information and Allocate Pages: Input the number of processes, and for each
process, input the required number of pages and allocate them if sufficient memory is
available.
3.Create Page Tables for Each Process: For each process, input the frame numbers to create
its page table.
4.Translate Logical Address to Physical Address: Input the process number, page number,
and offset, then validate the inputs.
5.Output the Physical Address: Calculate the physical address using the page table and
display it if inputs are valid, otherwise display an error message.
Program:
#include <stdio.h>
int main() {
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
printf("\nEnter the memory size -- ");
scanf("%d", &ms);
printf("\nEnter the page size -- ");
scanf("%d", &ps);
nop = ms / ps;
printf("\nThe number of pages available in memory are -- %d", nop);
printf("\nEnter number of processes -- ");
scanf("%d", &np);
rempages = nop;

for (i = 1; i <= np; i++) {

KGISL institute of technology 711722104055


printf("\nEnter number of pages required for p[%d] -- ", i);
scanf("%d", &s[i]);

if (s[i] > rempages) {


printf("\nMemory is Full");
break;
}

rempages -= s[i];
printf("\nEnter page table for p[%d] --- ", i);

for (j = 0; j < s[i]; j++) {


scanf("%d", &fno[i][j]);
}
}

printf("\nEnter Logical Address to find Physical Address ");


printf("\nEnter process number, page number, and offset -- ");
scanf("%d %d %d", &x, &y, &offset);

if (x > np || y >= s[x] || offset >= ps) {


printf("\nInvalid Process or Page Number or offset");
} else {
pa = fno[x][y] * ps + offset;
printf("\nThe Physical Address is -- %d", pa);
}

return 0;
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for the implementation for the paging techmique
is verified and executed successfully.

KGISL institute of technology 711722104055


Ex no: 11.A
IMPLEMENTATION OF FIRST FIT
Date:

Aim:
To implement the c program for the memory allocation method first fit.
Algorithm:
1. Initialization:Maintain a list of free memory blocks and processes with their memory
requirements.
2. Process Request:For each process that needs memory, determine its memory
requirement.
3. Search for Free Block:Traverse the list of free memory blocks from the beginning and
find the first block that is large enough.
4. Allocate Memory:Allocate the found memory block to the process, adjust the block
size or remove it if fully used, and mark the process as allocated.
5. Handle Allocation Failure:If no suitable block is found, mark the process as not
allocated and handle it according to system policy.
Program:
#include<stdio.h>

void main()

int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;

for(i = 0; i < 10; i++)

flags[i] = 0;

allocation[i] = -1;

printf("Enter no. of blocks: ");

scanf("%d", &bno);

printf("\nEnter size of each block: ");

for(i = 0; i < bno; i++)

scanf("%d", &bsize[i]);

printf("\nEnter no. of processes: ");

scanf("%d", &pno);

printf("\nEnter size of each process: ");

for(i = 0; i < pno; i++)

KGISL institute of technology 711722104055


scanf("%d", &psize[i]);

for(i = 0; i < pno; i++) //allocation as per first fit

for(j = 0; j < bno; j++)

if(flags[j] == 0 && bsize[j] >= psize[i])

allocation[j] = i;

flags[j] = 1;

break;

//display allocation details

printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");

for(i = 0; i < bno; i++)

printf("\n%d\t\t%d\t\t", i+1, bsize[i]);

if(flags[i] == 1)

printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);

else

printf("Not allocated");

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for the implementation for the memory
allocation method first fit is verified and executed successfully.

KGISL institute of technology 711722104055


Ex no: 11.B
IMPLEMENTATION OF WORST FIT
Date:

Aim:
To implement the c program for the memory allocation method worst fit
Algorithm:
1. Initialize:Mark all memory blocks as free,Set all process allocations to unallocated.
2. Input:Read the number of memory blocks and their sizes,Read the number of
processes and their sizes.
3. Allocate Memory:For each process:
4. Search: Find the largest free memory block that can accommodate the process.
5. Allocate:If such a block is found:Allocate the process to this block.
6. Mark this block as occupied or reduce the block size by the size of the allocated
process.
7. If no suitable block is found, the process remains unallocated.
8. Output:For each memory block, display the block number, its size, the process
allocated to it (if any), and the size of the allocated process.
Program:
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);

KGISL institute of technology 711722104055


}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for the implementation of memory allocation
method worst fit is verfired and executed successfully.

KGISL institute of technology 711722104055


Ex no: 11.C
IMPLEMENTATION OF BEST FIT
Date:

Aim:
To implement the c program for the memory allocation method best fit
Algorithm:
1. Initialize:Mark all memory blocks as free.Set all process allocations to unallocated.
2. Input:Read the number of memory blocks and their sizes.Read the number of
processes and their sizes.
3. Allocate Memory:For each process it wil Search and find the smallest free memory
block that is large enough to accommodate the process.
4. Allocate:If such a block is found then allocate the process to this block and mark this
block as occupied.
5. Calculate and record the fragmentation (unused memory) in the block after
allocation.If no suitable block is found, the process remains unallocated.
6. Output:For each memory block, display the block number, its size, the process
allocated to it (if any), and the fragmentation.
Program:
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}

KGISL institute of technology 711722104055


printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for the implementation of memory allocation
method best fit is verfired and executed successfully.

KGISL institute of technology 711722104055


Ex no: 12.A
IMPLEMENTATION OF PAGE REPLACMENT ALGORITHM-FIFO
Date:

Aim:
To implement the c program for the page replacement algorithm FIFO(First in First Out)
Algorithm:
Step 1: Start the program
Step 2: Declare reference string, frame .
Step 3: Get the reference string and its length.
Step 4: Get the number of frames and initialize them as -1.
Step 5: For each page in reference string
a) If page is not available in the frame.
i) Increment page fault
ii) Replace the oldest page by this current page.
Step 6: Display the reference string, frame contents.
Step 7: Stop the program.
Program:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);

KGISL institute of technology 711722104055


}
printf("\n");
}
printf("Page Fault Is %d",count);

return 0;
}

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Thus the implementation of replacement algorithm FIFO is executed and verified
successfully.

KGISL institute of technology 711722104055


Ex no: 12.B
IMPLEMENTATION OF PAGE REPLACMENT ALGORITHM-LRU
Date:
Aim:
To write a C program to implement the LRU page replacement algorithm.

Algorithm:
1. Start the program.
2. Get the number of pages, reference string and number of frames.
3. Declare count variable.
4. Copy the first page to the first frame and increment page fault count.
5. For the remaining pages, select the least recently used page by counter value
6. Replace them accordingly to the selection.
7. Display the values.
8. Display the number of page faults.
9. Stop the program

Program:
#include <stdio.h>
int arrmin(int[], int);
int main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;
int access[10], freq=0, dm;
printf("Length of Reference string : ");
scanf("%d", &len);
printf("Enter reference string :\n");
for(i=1; i<=len; i++)
scanf("%d", &rs[i]);
printf("Enter no. of frames : ");
scanf("%d", &nf);
for(i=0; i<nf; i++)
frame[i] = -1;
j = 0;
printf("\nRef. str Page frames");
for(i=1; i<=len; i++)
{

KGISL institute of technology 711722104055


printf("\n%4d\t", rs[i]);
avail = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == rs[i])
{
avail = 1;
access[k] = ++freq;
break; }
}
if(avail == 0)
{
dm = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == -1)
dm = 1;
break;
}
if(dm == 1)
{
frame[k] = rs[i];
access[k] = ++freq;
count++;
}
else
{
j = arrmin(access, nf);
frame[j] = rs[i];
access[j] = ++freq;

KGISL institute of technology 711722104055


count++;
}
for(k=0; k<nf; k++)
printf("%4d", frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n", count);
}
int arrmin(int a[], int n){
int i, min = a[0];
for(i=1; i<n; i++)
if (min > a[i])
min = a[i];
for(i=0; i<n; i++)
if (min == a[i])
return i;
}
Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded
Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Thus the implementation of replacement algorithm LRU is executed and verified
successfully.

KGISL institute of technology 711722104055


Ex no: 12.C
IMPLEMENTATION OF PAGE REPLACMENT ALGORITHM-LFU
Date:
Aim
To write a C program to implement the LFU page replacement algorithm.

Algorithm:
1. Start the program.
2. Declare and initialize total_frames, total_pages, hit = 0
3. Get the input for number of pages and frames
4. Set frame[]=-1 for all frames and count[]=0 for all pages.
5. Get the input for pages.
6. For each page in reference string repeat steps from 7 to 13
7. Increment the count for the page
8. Set the time for the page
9. Set flag to 1.
10. Set least to initial frame.
11. For each frame
If frame is either not empty or filled with current page then
If frame is not empty then
Increment hit and decrement pgfault
Set flag to 0
load frame with the current page
Compare the count of least and current frame and set least to the minimum
12.If flag is set to 1 then
Set mintime to 50
For each frame
If count of current frame is equal to least and time of current frame is less than mintime
Set temp as j
Set mintime as time[frame[]]
Set count for the new frame to 0
Load the page in to new frame
13. For each frames
Print frame[]
14. Print the hit and pagefault.
15. Stop the program.

Program:
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;

KGISL institute of technology 711722104055


printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)

KGISL institute of technology 711722104055


{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}

KGISL institute of technology 711722104055


printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}
Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Thus the implementation of replacement algorithms LFU is executed and verified
successfully.

KGISL institute of technology 711722104055


Ex no: 13.A
FILE ORGANIZATION TECHNIQUES - SINGLE LEVEL DIRECTORY STRUCTURE
Date:

Aim:
To implement the file organization technique single level directory structure
Algorithm:
1. Start the program.
2. Get the number of directories,names of dorectories
3. Get the size of directories and file names.
4. Display the directory names,size of directory and files under the directory
5. Stop the program

Program:

#include<stdio.h>
main()
{
int totaldir,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
printf("enter number of directories:");
scanf("%d",&totaldir);
printf("enter names of directories:");
for(i=0;i<totaldir;i++)
scanf("%s",&d[i]);
printf("enter size of directories:");
for(i=0;i<totaldir;i++)
scanf("%d",&s[i]);
printf("enter the file names :");
for(i=0;i<totaldir;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" DIRECTORIES\tSIZE\tFILE NAMES\n");
printf("*************************************************\n");
for(i=0;i<totaldir;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for the file organization technique single level
structure is verified and executed successfully

KGISL institute of technology 711722104055


Ex no: 13.B
FILE ORGANIZATION TECHNIQUES - TWO LEVEL DIRECTORY STRUCTURE
Date:

Aim:
To implement the file organization technique two level directory structure
Algorithm:
1. Start the program.
2. Define a structure to get directory names,sub directory names and file names
3. Get the number of directories,names of directories and size of directories
4. Get the sub directories name and size
5. Get the file names under sub directories
6. Display the directory names,size of directory and name of the file under sub
directories
7. Stop the program.

Program:
#include<stdio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d name:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directory:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\nDIRNAME\t\tSIZE\tSUB DIR. NAME\tSIZE\tFILES");
printf("\n******************************************************\n");

KGISL institute of technology 711722104055


for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}}
Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100
Result:
Hence the aim , algorithm and output for the file organization technique two level structure
is verified and executed successfully

KGISL institute of technology 711722104055


Ex no: 13.C
FILE ORGANIZATION TECHNIQUES - HIERARCHICAL DIRECTORY STRUCTURE
Date:

Aim:
To implement the file organization technique hierarchial directory structure
Algorithm:
1. Start the process.
2. Create root directrory of directory structure
3. Show the user options
1. Create directory under root.
2. Create directory under a directory
3. Create file under root directory.
4. Create file under directory.
4. Get the choice from user
1. Create directrory under root.
1. Call the createdirectory fuction with root directory.
2. Return directory.
2. Create directory under some directory.
1. Check the home directory that the user want create directory is preset or not
2. if directory found create directory under found
3. else print error message
3. Create file under root.
1. Call the createdirectory fuction with root directory.
2. Return file.
4. Create file under some directory.
Check the home directory that the user want create directory is
1. preset or not
2. if directory found create file under found
3. else print error message
5. repeat loop until user want to exit.
6. Stop the process.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct directory *hit;
//STRUCTE OF FILE
typedef struct file{
char f_name[20];
struct dir *home;

KGISL institute of technology 711722104055


}file;
//STRUCTRE OF DIRECTORY
typedef struct dir{
char d_name[20];
file *f[10];
struct dir *dirs[20];
struct dir *home;
int no_file;
int no_dir;
}directory;
/*THIS FUNCTION CREATES FILE UNDER DIRECTORY*/
file* create_file(directory *under)
{
file* new=(file*)malloc(sizeof(file));
printf("\nEnter file name:");
scanf("%s",new->f_name);
new->home=under;
printf("\nfile %s created under %s",new->f_name,under->d_name);
return new;
}
/*THIS FUNCTION CREATES DIRECTORY UNDER DIRECTORY*/
directory* createdirectory(directory *under)
{
directory* new =(directory*)malloc(sizeof(directory));
printf("\nEnter name of your directory:");
scanf("%s",new->d_name);
new->home=under;
int x=under->no_dir;
x++;
under->no_dir=x;

KGISL institute of technology 711722104055


under->dirs[x]=new;
return new;
}
directory *create_root()
{
directory* new =(directory*)malloc(sizeof(directory));
scanf("%s",new->d_name);
return new;
}
void putlines(int val)
{
int i;
printf("\n");
for(i=1;i<=val;i++)
printf("-");
printf("\n");
}
void displace(int val)
{
int i;
printf("\n");
for(i=1;i<val;i++){
printf(" ");
}
}
void display_dir(directory *under)
{
static int level=1;
int dir_no=under->no_dir;
int i;

KGISL institute of technology 711722104055


//putlines
printf("\nI am ---%s--- Directory",under->d_name);
displace(50/level);
printf("%s",under->d_name);
if(dir_no==0)
{
}
else{
//putlines(printf("Total numbers of Directory with me:%d",under->no_dir));
for(i=1;i<=dir_no;i++){
level++;
display_dir(under->dirs[i]);
}
}
}
directory* search(directory *root,char *name)
{
static int level;
printf("\n%d level\nSearch starts",level);
int i;
if(strcmp(root->d_name,name)==0)
{
hit=root;
printf("\nWe have found your directory");
return hit;
}
else if(root->no_dir>0)
{
for(i=1;i<=root->no_dir;i++)
{

KGISL institute of technology 711722104055


level++;
search(root->dirs[i],name);
}
}
return hit;
}
void dispaly_files_under_directory(directory *under)
{
int total_no_of_files=under->no_file;
int i;
putlines(printf("\nI am ---%s--- Directory",under->d_name));
putlines(printf("Total numbers of Directory with me:%d",under->no_dir));
putlines(printf("Total numbers of Files with me:%d",under->no_file));
for(i=1;i<=total_no_of_files;i++)
{
printf("%s",under->f[i]->f_name);
}
}
directory *root;
directory *found;
int main(void) {

root=create_root();
char name[20];
int choice;
do
{
printf("\n1.Create Directroy under ROOT\n2.Create directory under other
directory"
"\n3.Create file under ROOT\n4.Create file under other
directory\n5.EXIT");

KGISL institute of technology 711722104055


scanf("%d",&choice);
switch(choice)
{
case 1:
createdirectory(root);
display_dir(root);
break;
case 2:
printf("\nEnter name you want to search");
scanf("%s",name);
found=search(root,name);
if(found!=NULL)
createdirectory(found);
else
printf("Sorry Directory not found");
display_dir(root);
break;
case 3:
create_file(root);
display_dir(root);
break;
case 4:
printf("\nEnter name you want to search");
scanf("%s",name);
found=search(root,name);
if(found!=NULL)
create_file(found);
else
printf("Sorry Directory not found");
display_dir(root);

KGISL institute of technology 711722104055


break;
case 5:
exit(0);
break;
}
}while(choice<6);
return EXIT_SUCCESS;
}
Output:

KGISL institute of technology 711722104055


Content Max marks Marks awarded
Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output fro the file organization technique hireachial
structure is verified and executed successfully.

KGISL institute of technology 711722104055


Ex no: 14.A
IMPLEMENTATION OF SEQUENTIAL FILE ALLOCATION
Date:

Aim :
To implement file allocation on free disk space in a contiguous manner.

Algorithm :
1. Start the program
2. Get the no of files,no of blocks occufied by files and starting block of files
3. Display file name, start block and length
4. Display blocks occupied by the no of files
5. Stop program
Program:

#include<stdio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("blocks occupied are:\n");
for(i=0;i<n;i++)
{ printf("file no %d",i+1);
for(j=0;j<b[i];j++)
printf("\t%d",c[i][j]);
printf("\n");
}
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result :
Thus sequential allocation is done for files with the available free blocks.

KGISL institute of technology 711722104055


Ex no: 14.B
IMPLEMENTATION OF INDEXED FILE ALLOCATION
Date:

Aim :
To implement file allocation on free disk space using indexed file allocation method.

Algorithm :

1. Start the program.


2. Get the number of files,index block and block numbers occupied by each file.
3. Display the file name,index and length of the file
4. Display index number and block numbers occupied by files
5. Stop the program.

Program:

#include<stdio.h>
main()
{
int n,m[20],i,j,ib[20],b[20][20];
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter index block :",i+1);
scanf("%d",&ib[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",i+1,ib[i],m[i]);
printf("blocks occupied are:\n");
for(i=0;i<n;i++)
{ printf("fileno%d",i+1);
for(j=0;j<m[i];j++)
printf("\t%d--->%d\n",ib[i],b[i][j]);
printf("\n");
}
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result
Thus indexed file allocation is done for files with the available free blocks.

KGISL institute of technology 711722104055


Ex no: 14 .C
IMPLEMENTATION OF LINKED FILE ALLOCATION
Date:

Aim :
To implement file allocation on free disk space in a linked file memory allocation
manner.

Algorithm :

1. Start the program.


2. Define the structure to get file name,starting block,size of the file and actual block
numbers
3. Get the number of files,file name,starting block,number of blocks and actual block
numbers
4. Display the file name,starting block and size of the file
5. Display block numbers occupied by file as linked blocks
6. Stop the program.

Program
#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=0;j<f[i].size;j++)

KGISL institute of technology 711722104055


printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
} }

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result :

Thus Linked file allocation is done for files with the available free blocks.

KGISL institute of technology 711722104055


Ex no: 15.A
DISK SCHEDULING ALGORITHM - FCFS
Date:

Aim :

To write a C program for implementing disk scheduling algorithm FCFS


Algorithm:

1. Initialize:Read the initial position of the disk head,Read the sequence of disk requests.
2. Process Requests:Serve the disk requests in the order they arrive, one by one.
3. Calculate Metrics:For each request, calculate the distance (seek time) the disk head moves
from its current position to the position of the next request,Keep a running total of the seek
times tcalculate the total seek time.
4. Output:Display the order in which the disk requests were served,Display the total seek
time.

PROGRAM :

#include <stdio.h>
#include <stdlib.h>
void FCFS(int requests[], int n, int head) {
int seek_count = 0;
int distance, cur_track;
printf("Seek Sequence is:\n");
for (int i = 0; i < n; i++) {
cur_track = requests[i];
printf("%d ", cur_track);
distance = abs(cur_track - head);
// Increase the total count
seek_count += distance;
head = cur_track;
}
printf("\nTotal number of seek operations = %d\n", seek_count);
}
int main() {
int n, head;

KGISL institute of technology 711722104055


printf("Enter the number of requests: ");
scanf("%d", &n);
int *requests = (int *)malloc(n * sizeof(int));
printf("Enter the request sequence: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
FCFS(requests, n, head);
free(requests);
return 0;
}
Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for disk scheduling algorithm FCFS is verified and
executed successfully.

KGISL institute of technology 711722104055


Ex no: 15.B
DISK SCHEDULING ALGORITHM - SSTF
Date:

Aim :

To write a C program for implementing disk scheduling algorithm SSTF


Algorithm:
1. Initialize:Read the initial position of the disk head.Read the sequence of disk requests
2. Find Nearest Request:Calculate the distance from the current disk head position to
each unserviced request.
3. Select the request with the shortest distance (seek time) from the current position.
4. Move Disk Head:Move the disk head to the position of the nearest request.
5. Update Metrics:Add the seek time (distance) to the total seek time.
6. Mark the request as serviced and update the current position of the disk head.
7. Output:Display the order in which the disk requests were served.Display the total seek
time.
Program:
#include <stdio.h>
#include <stdlib.h>
void SSTF(int requests[], int n, int head) {
int seek_count = 0;
int distance, cur_track;
int *completed = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
completed[i] = 0;
}
printf("Seek Sequence is:\n");
for (int i = 0; i < n; i++) {
int min = 1000000;
int min_index = -1;
for (int j = 0; j < n; j++) {
if (!completed[j]) {
distance = abs(requests[j] - head);
if (distance < min) {

KGISL institute of technology 711722104055


min = distance;
min_index = j;
}
}
}
completed[min_index] = 1;
cur_track = requests[min_index];
printf("%d ", cur_track);
seek_count += min;
head = cur_track;
}
printf("\nTotal number of seek operations = %d\n", seek_count);
free(completed);
}
int main() {
int n, head;
printf("Enter the number of requests: ");
scanf("%d", &n);
int *requests = (int *)malloc(n * sizeof(int));
printf("Enter the request sequence: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
SSTF(requests, n, head);
free(requests);
return 0;
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for disk scheduling SSTF algorithm is verified and
executed successful

KGISL institute of technology 711722104055


Ex no: 15.C
DISK SCHEDULING ALGORITHM - SCAN SCHEDULING
Date:

Aim :

To write a C program for implementing disk scheduling algorithm scan scheduling algorithm

Algorithm:
1. Initialize:Read the initial position of the disk head.Read the sequence of disk
requests.Read the direction of the initial movement (towards the start or end of the
disk).
2. Sort Requests:Sort the requests in ascending order of their positions.
3. Process Requests:If the initial direction is towards the end of the disk:Service all
requests from the current position to the end of the disk in order.Reverse direction at
the end of the disk.
4. Service all remaining requests from the end of the disk back towards the start.
5. If the initial direction is towards the start of the disk:Service all requests from the
current position to the start of the disk in order.
6. Reverse direction at the start of the disk.
7. Service all remaining requests from the start of the disk back towards the end.
8. Calculate Metrics:For each request, calculate the distance (seek time) the disk head
moves.
9. Keep a running total of the seek times to calculate the total seek time.
10. Output:Display the order in which the disk requests were served.
11. Display the total seek time.

Program:
#include <stdio.h>
#include <stdlib.h>
void SCAN(int requests[], int n, int head, int direction) {
int seek_count = 0;
int distance, cur_track;
int size = 200; // size of the disk
int *seek_sequence = (int *)malloc((n + 2) * sizeof(int));
int index = 0;
printf("Seek Sequence is:\n");
if (direction == 1) { // Move towards the end
for (int i = 0; i < n; i++) {

KGISL institute of technology 711722104055


if (requests[i] >= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = size - 1;
for (int i = n - 1; i >= 0; i--) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
} else { // Move towards the beginning
for (int i = 0; i < n; i++) {
if (requests[i] <= head) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = 0;
for (int i = 0; i < n; i++) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
for (int i = 0; i < index; i++) {
cur_track = seek_sequence[i];
printf("%d ", cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}

KGISL institute of technology 711722104055


printf("\nTotal number of seek operations = %d\n", seek_count);
free(seek_sequence);}
int main() {
int n, head, direction;
printf("Enter the number of requests: ");
scanf("%d", &n);
int *requests = (int *)malloc(n * sizeof(int));
printf("Enter the request sequence: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the direction (1 for up, 0 for down): ");
scanf("%d", &direction);
SCAN(requests, n, head, direction);
free(requests);
return 0;
}

KGISL institute of technology 711722104055


Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for disk scheduling scan algorithm is verified and executed
successful

KGISL institute of technology 711722104055


Ex no: 15.D
DISK SCHEDULING ALGORITHM - C SCAN SCHEDULING
Date:

Aim :

To write a C program for implementing disk scheduling algorithm scan scheduling algorithm
Algorithm:
1. Initialize:Read the initial position of the disk head.Read the sequence of disk
requests.Assume the direction is always towards the end of the disk for servicing.
2. Sort Requests:Sort the requests in ascending order of their positions.
3. Process Requests:Service all requests from the current position to the end of the disk
in order.
4. Jump to the start of the disk without servicing any requests.
5. Service all remaining requests from the start of the disk to the initial position in
ascending order.
6. Calculate Metrics:For each request, calculate the distance (seek time) the disk head
moves.Keep a running total of the seek times to calculate the total seek time.
7. Output:Display the order in which the disk requests were served.Display the total seek
time.
Program:
#include <stdio.h>
#include <stdlib.h>
void CSCAN(int requests[], int n, int head) {
int seek_count = 0;
int distance, cur_track;
int size = 200; // size of the disk
int *seek_sequence = (int *)malloc((n + 2) * sizeof(int));
int index = 0;

printf("Seek Sequence is:\n");


for (int i = 0; i < n; i++) {
if (requests[i] >= head) {
seek_sequence[index++] = requests[i];
}
}

KGISL institute of technology 711722104055


seek_sequence[index++] = size - 1;
seek_sequence[index++] = 0;
for (int i = 0; i < n; i++) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
for (int i = 0; i < index; i++) {
cur_track = seek_sequence[i];
printf("%d ", cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
printf("\nTotal number of seek operations = %d\n", seek_count);
free(seek_sequence);
}
int main() {
int n, head;

printf("Enter the number of requests: ");


scanf("%d", &n);
int *requests = (int *)malloc(n * sizeof(int));
printf("Enter the request sequence: ");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
CSCAN(requests, n, head);

KGISL institute of technology 711722104055


free(requests);
return 0;
}

Output:

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

Result:
Hence the aim , algorithm and output for disk scheduling c scan algorithm is verified and
executed successful

KGISL institute of technology 711722104055


Ex no: 16
GUEST OPERATING SYSTEM LIKE LINUX USING VMWARE
Date:

AIM:
To Install Virtual Box/VMware Workstation with different flavours of Linux or Windows OS
on top of windows7 or 8
PROCEDURE:
Install Ubuntu as the guest operating system from an ISO image file. To create a new virtual
machine click on the New button in the Virtual Box Manager GUI window.

Give the VM a name and make sure that the operating system and version are Linux and
Ubuntu, respectively. Click Next.

KGISL institute of technology 711722104055


Next, select the amount of memory (RAM) to be allocated to the virtual machine:

Next, you must specify a virtual hard disk for your VM. There are multiple ways in which
Virtual Box can provide hard disk space to a VM, but the most common way is to use a large
image file on your real hard disk, whose contents Virtual Box presents to your VM as if it were
a complete hard disk. This file represents an entire hard disk then, so you can even copy it to
another host and use it with another Virtual Box installation:

Select VDI hard drive type and click Next:

KGISL institute of technology 711722104055


In this example we will be using dynamic disks, but fixed disks will work just as well:

Next, type the name of the new virtual hard drive file or select a different folder to create the
file in and click Create:

KGISL institute of technology 711722104055


When the VM creation thingy goes away, press Ctrl + s to open your VM settings.

Next, click Storage in the left pane and click the CD icon in the right pane to browse for the
Ubuntu ISO you downloaded in the first steps.

Click OK to close Settings and then start your new Ubuntu VM by clicking the green Start
arrow in the main Virtual Box window.

After a few seconds the Ubuntu welcome window happily shoots on the screen ready to install
Ubuntu.

Ubuntu 16.04 Installation Guide

1. Proceed by clicking the install Ubuntu button.

KGISL institute of technology 711722104055


Ubuntu 16.04 Installation
Choose the language as needed; and this one will be the default (once installed) throughout
the system.

2. Next up is your preparation screen

Preparing to Install Ubuntu 16.04

3. At this point, choose the installation type and the first screenshot is an automated process,
even if you have an operating system already installed, the installer will auto-detect it and allow
you partition the drive in the next screen with simple sliders that will auto-allocate your
assigned space for the Ubuntu partition.

KGISL institute of technology 711722104055


Select Ubuntu 16.04 Installation Type

4. Prompt to confirm that you want the changes to be made to your internal drive; click continue
to move onto the next screen.

Write Changes to Disk

5. This is where you select your current location;

KGISL institute of technology 711722104055


Select Current Location
6. Configure as needed — depending on your type of keyboard and default input language.

Select Keyboard Layout

7. This is where you enter your user details and clicks continue to proceed to the next screen.

Create User Account

8. Right up next, is the beginning of installation which (depending on your PC hardware), can
take a long or short time.

KGISL institute of technology 711722104055


Ubuntu 16.04 Installation Process

9. At this point, the installation is complete and now, you may restart your PC.

Ubuntu 16.04 Installation Complete

10. Once you’ve restarted, you are now greeted with the login screen where you input your
password.

KGISL institute of technology 711722104055


Ubuntu 16.04 Login Screen

11. Ubuntu 16.04 Desktop with Applications

Content Max marks Marks awarded


Aim and algorithm 20
Program 40
Output 20
Result 10
Viva voce 10
Total marks 100

RESULT:

Thus, the Virtual Box/VMware Workstation with different flavors of Linux or Windows OS
has been successfully installed.

KGISL institute of technology 711722104055

You might also like