You are on page 1of 97

Sri Vidya College of Engineering & Technology Lab Manual

1. BASIC UNIX COMMANDS


Aim

To Study about basic Unix Commands.

FILE MANIPULATION COMMANDS

Command : Cat
Purpose : It is used to display the contents of the file as well as used to create a new
file.
Syntax : cat <file name >
Example : $ cat devi

Command : More

Purpose : It is used to display the contents of the file on the screen at a time.
Syntax : more <file name >
Example : $ more devi

Command : Wc
Purpose : It is used to count the number of lines ,words and characters in a file or
group of files.
Syntax : wc [options] <file name >
Example : $ wc –l devi

Command : File
Purpose : It is used to determine the type of the file.
Syntax : file <file name >
Example : $ file devi

Command : Spell
Purpose : It is used to find the spelling errors in the file.
Syntax : spell [options] <file name >
Example : $ spell -b devi

Command : Split
Purpose : It is used to split the given file into smaller pieces of given size.
Syntax : split –size <file name > < splitted file name >
Example : $ split –2 devi de

Command : CP
Purpose : It is used to copy one or more files.
Syntax : cat <source file name > <destination file name>
Example : $ cp devi latha

CS8461 OPERATING SYSTEMS LABORATORY Page 1


Sri Vidya College of Engineering & Technology Lab Manual

Command : MV
Purpose : It is used to move a file within a directory with different names and also
used to move a file to different directory with its original name.
Syntax : mv <source file name > <destination file name>
Example : $ mv devi jeya

Command : RM
Purpose : It is used to remove a file from the disk.
Syntax : rm <file name >
Example : $ rm devi

1. a) GENERAL PURPOSE COMMANDS

Command : Banner
Purpose : It is used to display its argument in large letters.
Syntax : banner < string >
Example : $ banner BOOM

Command : Who
Purpose : It is used to get the information about all the users currently working in
the system.
Syntax : who
Example : $ who

Command : Who am i
Purpose : It is used to know in which terminal the user is currently logged on.
Syntax : who am i
Example : $ who am I

Command : Date
Purpose : It is used to display the system date and time.
Syntax : date
Example : $ date

Command : Cal
Purpose : It prints the calender for the specified year and month.
Syntax : cal <month> <year>
Example : $ cal 05 2003

Command : Id
Purpose : It is used to display the login name.
Syntax : id
Example : $ id

Command : Clear
Purpose : It is used to clear the screen.

CS8461 OPERATING SYSTEMS LABORATORY Page 2


Sri Vidya College of Engineering & Technology Lab Manual

Syntax : clear
Example : $ clear

Command : Tput
Purpose : It is used to manipulate the screen.
Syntax : tput < attributes >
Example : $ tput rmso

Command : Uname
Purpose : It is used to display the details about the OS in which we are working.
Syntax : uname [options]
Example : $ uname –n

Command : Tty
Purpose : It is used to know the terminal name on which we work.
Syntax : tty
Example : $ tty

Command : Pwd
Purpose : It is used to display the absolute pathname of current working directory.
Syntax : pwd
Example : $ pwd

Command : Bc
Purpose : It is used to perform simple mathematical calculations.
Syntax : bc <operation>
Example : $ bc 3+5 8 ^d

Command : Ls
Purpose : It is used to display the files in the current working directory.
Syntax : ls [options] <arguments>
Example : $ ls –p

Command : Echo
Purpose : It echoes the argument on the standard output device.
Syntax : echo [options] <string>
Example : $ echo „BOOM‟
Command : Man
Purpose : It gives details about the unix commands.
Syntax : man < command name >
Example : $ man echo

CS8461 OPERATING SYSTEMS LABORATORY Page 3


Sri Vidya College of Engineering & Technology Lab Manual

1. b) COMMAND GROUPING & FILTER COMMANDS

Command : Head
Purpose : It is used to display the top portion of the file.
Syntax : head [options] <file name>
Example : $ head -5 devi

Command : Tail
Purpose : It is used to display the bottom portion of the file.
Syntax : tail [options] <file name >
Example : $ tail –5 devi

Command : Pr
Purpose : It is used to display the contents of the file by separating them into pages
and each page begins with the header information.
Syntax : pr [options] <file name >
Example : $ pr devi

Command : Cut
Purpose : It is used to extract selected fields or columns from each line of one or
more files and display them on the standard output device.
Syntax : cut [options] <file name >
Example : $ cut –c5 devi

Command : Paste
Purpose : It concatenates the line from each input file column by column with tab
characters in between them.
Syntax : paste [options] <file name >
Example : $ paste f1 f2

Command : Join
Purpose : It is used to extracts common lines from two sorted files and there should
be the common field in both file.
Syntax : join [options] <file name1 > <file name 2>
Example : $ join –a1 f1 f2

Command : Uniq
Purpose : It compares adjacent lines in the file and displays the output by
eliminating duplicate adjacent lines in it.
Syntax : uniq [options] <file name >
Example : $ uniq -c devi
Command : Sort
Purpose : It sorts one or more files based on ASCII sequence and also to merge the
file.
Syntax : sort [options] <file name >
Example : $ sort -r devi

CS8461 OPERATING SYSTEMS LABORATORY Page 4


Sri Vidya College of Engineering & Technology Lab Manual

Command : Nl
Purpose : It is used to add the line numbers to the file.
Syntax : nl [options] [filename]
Example : $ nl devi

Command : Tr
Purpose : It is used to translate or delete a character or a string from the standard
input to produce the required output.
Syntax : tr [options] <string1> <string2>
Example : $ tr –t „a‟ „b‟ < devi>

Command : Tee
Purpose : It is used to read the contents from standard input or from output of another
command and reproduces the output to boyh in standard output and direct into
output to one or more files.
Syntax : tee [options] <file name >
Example : $ tee date dat.txt

Command : grep
Purpose : It is used to search the specified pattern from one or more files.
Syntax : grep [options] <pattern> <file name >
Example : $ grep “anand” devi

1. c) DIRECTORY COMMANDS AND PROCESS MANAGEMENT


COMMANDS

Aim

To Study about directory handling and Process Management Commands

Command : mkdir
Purpose : It is used to create new directory or more than one directory.
Syntax : mkdir <directory name >
Example : $ mkdir riya

Command : rmdir
Purpose : It is used to remove the directory if it is empty.
Syntax : rmdir <directory name >
Example : $ rmdir riya

Command : cd
Purpose : It is used to change the control from one working directory to another
specified directory.
Syntax : cd <directory name >
Example : $ cd riya

CS8461 OPERATING SYSTEMS LABORATORY Page 5


Sri Vidya College of Engineering & Technology Lab Manual

Command : cd ..
Purpose : It is used to quit from current directory and move to the previous
directory.
Syntax : cd ..
Example : $ cd ..

Process Commands

Command : echo $$
Purpose : It is used to display the process number of the current shell.
Syntax : echo $$
Example : $ echo $$

Command : ps
Purpose : It is used to display the attributes of a process.
Syntax : ps
Example : $ ps
$ ps –f ( Display the ancestry of a process )
$ ps –u ( Display the activities of a user )
$ ps –a ( Lists processes of all users but not the system processes )

Command :&
Purpose : It is shell operator which is used to run a process in the background.
Syntax : <command> &
Example : $ sort emp.txt &

Command : nohup
Purpose : It permits the execution of the process even after the user has logged out.
Syntax : nohup <command>
Example : $ nohup sort emp.txt ( result is available on nohup.out )

Command : kill
Purpose : It is used to terminate the process.
Syntax : kill <PID>
Example : $ kill 105

Command : kill $!
Purpose : $! is the system variable which stores the process id of the last background
job. The command kill $! is used to kill the last process.
Syntax : kill $!
Example : $ kill $!

Command : at
Purpose : It is used to execute the process at the time specified.
Syntax : echo <time>
Example : $ at 14:08 (or) $ at 3 PM (or) $ at 4 :50 AM

CS8461 OPERATING SYSTEMS LABORATORY Page 6


Sri Vidya College of Engineering & Technology Lab Manual

Ex.No. 2 SIMPLE SHELL PROGRAMS

Ex.No. 2a Getting and displaying the academic and personal details

Aim
To get the input and display the academic, personal details.

Algorithm

1.Get name, age, and address from the user.


2.Print that message as similar.
3.Get mark1, mark2, and mark3 from the user.
4.Print that message as similar.

Program

#display.sh
echo –n “Enter the name” read s
echo –n “Enter the age” read a
echo –n “Enter the address” read adr
echo –n “The name is $s” echo –n “The age is $a”
echo –n “The address is $adr” echo –n “Enter the mark1” read m1
echo –n “Enter the mark2” read m2
echo –n “Enter the mark3” read m3
echo –n “The mark1 is $m1” echo –n “The mark2 is $m2” echo –n “The mark3 is $m3”

Sample Input & Output


$ Enter the name john Enter the age 17
Enter the address madurai
The name is john
The age is 17
The address is madurai
Enter the mark1 90
Enter the mark2 90
Enter the mark3 80
The mark1 is 90
The mark2 is 90
The mark3 is 80

CS8461 OPERATING SYSTEMS LABORATORY Page 7


Sri Vidya College of Engineering & Technology Lab Manual

Ex No. 2b Arithmetic Operations

Aim
To perform all arithmetic operations
Algorithm

1.Read two numbers from the user.


2.Add both the numbers.
3.Print the summation of two numbers.
4.Subtract the second number from the first number
5.Print the result.
6.Multiply the two numbers
7.Print the result.
8.Divide the first number by second number.

Program

echo enter the numbers


read n1n2
n3=`expr $n1 + $n2`
n4=`expr $n1 - $n2`
n5=`expr $n1 \* $n2`
n6=`expr $n1 / $n2`

echo summation of $n1 and $n2 is $n3


echo difference of $n1 and $n2 is $n4
echo multiplication of $n1 and $n2 is $n5
echo division of $n1 by $n2 is $n6

Sample Input & Output


$ enter the numbers 105

summation of 10 and 5 is 15
difference of 10 and 5 is 5
multiplication of 10 and 5 is 50
division of 10 by 5 is 2

CS8461 OPERATING SYSTEMS LABORATORY Page 8


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No .2.c PROGRAMS USING CONDITIONAL STATEMENTS

Ex. No .2.c. Odd (or) Even

Aim
To check whether the given number is odd or even.
Algorithm
1. Get a number from the user.
2. Divide the number by 2.
3. Check the remainder of the division.
4. If it is zero then the given number is even.
5. Otherwise the given number is odd.

Program
echo –n “enter the number”
read num
x = `expr $num % 2`
if [ $x –eq 0 ]
then
echo “$x is an even number”
else
echo “$x is a odd number”
fi

Sample Input & Output


$ enter the number: 11
11 is a odd number
$ enter the number: 50
50 is an even number

CS8461 OPERATING SYSTEMS LABORATORY Page 9


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No. 2d Positive or Negative

Aim
To check whether the given number is +ve or –ve or zero.
Algorithm
1. Get a number from the user.
2. Check the number is greater than or less than zero or equal to zero.
3. If it is greater than zero then the given number is +ve.
4. If it is less than zero then the given number is –ve. Otherwise the given number is zero.

Program
echo –n “enter the number”
read num
if [ $num –gt 0 ] then
echo “$num is a positive number”
elif [ $num –lt 0 ]
then
echo “$num is a negative number” else
echo “$num is zero” fi

Sample Input & Output

$ enter the number: 11


11 is a positive number
$ enter the number: -5
-5 is a negative number
$ enter the number: 0
0 is zero

CS8461 OPERATING SYSTEMS LABORATORY Page 10


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No: 2e Biggest among three numbers

Aim
To find a biggest number among three numbers.
Algorithm
1.Get three numbers from the user.
2.If the 1st number is greater than other two numbers then the 1st number is a biggest numbers.
3.Otherwise if the 2nd number is greater than 3rd number then the 2nd number is a biggest
number.
4.Otherwise 3rd number is a biggest number.

Program
echo -n "Enter the numbers"
read num1 num2 num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ] then
echo "$num1 is a biggest number" elif [ $num2 -gt $num3 ]
then
echo "$num2 is a biggest number" else
echo "$num3 is a biggest number"
fi

Sample Input & Output

$ Enter the numbers 12 45 34


45 is a biggest number

CS8461 OPERATING SYSTEMS LABORATORY Page 11


Sri Vidya College of Engineering & Technology Lab Manual

Ex No.2f Arithmetic Operations - switch case

Aim
To perform all the arithmetic operations using case statements.
Algorithm
1.Get two numbers from the user.
2.Select the options .
3.Execute arithmetic operations using case statements..
4.Print the output

Program
echo “Enter two numbers”
read a b
echo “menu” echo “1.add” echo “2.sub” echo “3.mul” echo “ 4. div”
echo “Enter your option” read option
case $option in
1)c=`expr $a + $b`;;
2)c=`expr $a - $b`;;
c=`expr $a \* $b`;;
c=`expr $a / $b`;;
esac
echo $c

Sample input & output


Enter two numbers 45
menu
1.add
2.sub
3.mul
4.div
Enter your option
1
9

CS8461 OPERATING SYSTEMS LABORATORY Page 12


Sri Vidya College of Engineering & Technology Lab Manual

Ex No.2g Finding the area of circle, square, rectangle and triangle


Aim
To find the area of circle, square, rectangle and triangle using case statements.
Algorithm
1.Get two numbers from the user.
2.Select the option.
3.Execute the statement to find the area using case statements..
4.Print the output

Program

echo 1.circle
echo 2. rectangle
echo 3.square
echo 4.triangle
echo enter your choice read opt
case $opt in
1) echo enter the radius
read r
area=`expr $r \* $r \* 22 / 7` echo $area;;
2) echo enter the length and breadth
read l b
area=`expr $l \* $b` echo $area;;
3) echo enter the side
read s
area=`expr $s \* $s`
echo $area;;
4)echo enter the height and breadth
read b h
area=`expr $b \* $h \* 1 / 2`
echo $area;;
*) echo wrong choice;; esac

Sample input & output


1.circle
2. rectangle
3.square
4.triangle
enter your choice 3
enter the side 5
25

CS8461 OPERATING SYSTEMS LABORATORY Page 13


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No. TESTING AND LOOPS

Ex. No.2h Fibonacci Series

Aim
To generate a fibonacci series of first n numbers
Algorithm
1.Get the value n from the user.
2.Initial numbers of fibonacci series is 0,1.
3.Using the sum of last adjacent two numbers generate a next number in the series.
4.Repeat the step 3 until n numbers are generated.

Program
echo -n "enter the limit"
read n
echo "the fibonacci series is : "
b=0
c=1
i=0
if [ $n -ge 2 ] then
echo -n " $b $c" n=`expr $n - 2`
while [ $i -lt $n ] do
a=`expr $b + $c` b=$c
c=$a
echo -n " $c" i=`expr $i + 1`
done
fi

Sample Input & Output $ Enter the limit 5

The fibonacci series is


01123

CS8461 OPERATING SYSTEMS LABORATORY Page 14


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No.2i Armstrong Number

Aim
To Check Whether the given number is Armstrong or not.
Algorithm

1.Get the number from the user.


2.Extract each and every digit of the given number.
3.Find the cube of every digit and calculate the sum of all cubes.
4.Check whether the sum is equal to the given number, if it is equal, the given number is
Armstrong number otherwise not.

Program
echo -n "enter the number"
read num
x=$num
sum=0
while [ $num -gt 0 ] do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done

if [ $x -eq $sum ] then


echo $x is an Armstrong number else
echo $x is not an Armstrong number
fi

Sample Input & Output

$ enter the number: 371


371 is an armstrong number
$ enter the number: 500
500 is not an armstrong number

CS8461 OPERATING SYSTEMS LABORATORY Page 15


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No. 2j Sum of n even numbers

Aim
To write a shell program to find the sum of even numbers
Algorithm

1.Start the program


2.Read the number
3.Initialize i=2
4.Calculate sum=sum+1
5.Print the sum
6.Stop the program execution

Program

echo “enter the number of upper limit”


read n
I=2
C=0
while [ $I –lt $n ] do
echo “number is $I”
I=`expr $I + 2‟ C=`expr $I + $C‟
done
echo “ the sum is $c”

Sample Input & Output

Enter the number of upper limit 6 2 4


the sum is 6

CS8461 OPERATING SYSTEMS LABORATORY Page 16


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No: 2k Combinations of 1 2 3

Aim
To generate all combinations of 1 2 3 using for loop
Algorithm
1.Assign values 1 2 3 to first for loop
2.Form another for loop within the first for loop and assign values 1 2 3
3.Within the second for loop execute another for loop with values 1 2 3
4.Close all for loops
5.Display the outputs

Program

for I in 1 2 3 do
for j in 1 2 3 do
for k in 1 2 3 do
Echo $I $j $k
done
done
done

Sample output

111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 311 312 313 321 322 323

331 332 333

CS8461 OPERATING SYSTEMS LABORATORY Page 17


Sri Vidya College of Engineering & Technology Lab Manual

Ex. No: 2l nth power of a given number

Aim
To compute the nth power of a given number
Algorithm
1.Get the value for the power & number to be computed.
2.Initialize the value for I as 2.
3.Set the variable ans as a.
4.Repeat the steps 5,6 until the value of I ie less than or equal to the power value.
5.Multiply the value of ans and a.
6.Increment the I value.
7.Display the value ans as result

Program
echo enter the number and its power
read a pow
i=2 ans=$a
while [ $i -le $pow ] do
ans=`expr $ans \* $a`
i=`expr $i + 1`
done
echo $ans

Sample output

$ enter the number and its power 24 16

RESULT:

Thus the shell program was written and successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 18


Sri Vidya College of Engineering & Technology Lab Manual

EX. NO: 3 Implementation of process management using the following system


calls of UNIX operating system: fork, exec, getpid, exit, wait, close.

AIM:
To write a program for implementing process management using the following
system calls of UNIX operating system: fork, exec, getpid, exit, wait, close.

ALGORITHM:

1. Start the program.


2. Read the input from the command line.
3. Use fork() system call to create process, getppid() system call used to get the
parent process ID and getpid() system call used to get the current process ID
4. execvp() system call used to execute that command given on that
command line argument
5. execlp() system call used to execute specified command.
6. Open the directory at specified in command line input.
7. Display the directory contents.
8. Stop the program.

PROGRAM:

#include<stdio.h>
main(int arc,char*ar[])

{
int pid;
char s[100];
pid=fork();
if(pid<0)

printf("error");

else if(pid>0)

{
wait(NULL);

CS8461 OPERATING SYSTEMS LABORATORY Page 19


Sri Vidya College of Engineering & Technology Lab Manual

printf("\n Parent Process:\n"); printf("\n\tParent


Process id:%d\t\n",getpid());
execlp("cat","cat",ar[1],(char*)0); error("can’t
execute cat %s,",ar[1]);

}
else

{
printf("\nChild process:");
printf("\n\tChildprocess parent id:\t %d",getppid());
sprintf(s,"\n\tChild process id :\t%d",getpid());
write(1,s,strlen(s));
printf(" ");
printf(" ");
printf(" ");
execvp(ar[2],&ar[2]);
error("can’t execute %s",ar[2]);

}
}

OUTPUT:

[root@localhost ~]# ./a.out tst date


Child process:
Child process id :
3137 Sat Apr 10 02:45:32 IST 2010
Parent Process:
Parent Process id:3136
sd
dsaASD[root@localhost ~]# cat tst
sd
dsaASD

RESULT:

Thus the program for process management was written and successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 20


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:3.1 Implementation of directory management using the following system


calls of UNIX operating system: opendir, readdir.

AIM:

To write a program for implementing Directory management using the following


system calls of UNIX operating system: opendir, readdir.

ALGORITHM:

1.Start the program.


2. Open the directory at specified in command line input.
3. Display the directory contents.
4. Stop the program.

PROGRAM:

#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
main(int c,char* arg[])
{

DIR *d;

struct dirent *r;


int i=0;
d=opendir(arg[1]);
printf("\n\t NAME OF ITEM \n");
while((r=readdir(d)) != NULL)
{

printf("\t %s \n",r->d_name);
i=i+1;
}
printf("\n TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS
%d \n",i);
}

CS8461 OPERATING SYSTEMS LABORATORY Page 21


Sri Vidya College of Engineering & Technology Lab Manual

OUTPUT:

[root@localhost ~]# cc dr.c


[root@localhost ~]# ./a.out lab_print

NAME OF ITEM
pri_output.doc
sjf_output.doc
fcfs_output.doc
rr_output.doc

ipc_pipe_output.doc

pro_con_prob_output.doc

TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS 8

RESULT:

Thus the program for directory management was written and successfully
executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 22


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:3.2 Write a program using the I/O system calls of UNIX operating
system (open,read, write, etc)

AIM:

To write a program using the I/O system calls of UNIX operating system (open,
read, write, etc)

ALGORITHM:

1. Start the program.


2. Read the input from user specified file.
3. Write the content of the file to newly created file.
4. Show the file properties (access time, modified time, & etc,.)
5. Stop the program.

PROGRAM:

#include<stdio.h>
#include<sys/stat.h>
#include<time.h>
main(int ag,char*arg[])

{
char buf[100];
struct stat s;
int fd1,fd2,n;
fd1=open(arg[1],0);
fd2=creat(arg[2],0777);
stat(arg[2],&s);
if(fd2==-1)

printf("ERROR IN CREATION");
while((n=read(fd1,buf,sizeof(buf)))>0)
{

if(write(fd2,buf,n)!=n)

CS8461 OPERATING SYSTEMS LABORATORY Page 23


Sri Vidya College of Engineering & Technology Lab Manual

{
close(fd1);
close(fd2);

}
}
printf("\t\n UID FOR FILE.......>%d \n FILE ACCESS TIME.....>%s \n FILE
MODIFIED TIME........>%s \n FILE I-NODE NUMBER......>%d \n
PERMISSION FOR FILE.....>%o\n\n",s.st_uid,ctime
(&s.st_atime),ctime(&s.st_mtime),s.st_mode);

close(fd1);
close(fd2);
}

OUTPUT:

[root@localhost ~]# cc iosys.c


[root@localhost ~]# ./a.out
UID FOR FILE.......>0
FILE ACCESS TIME.....>Thu Apr 8 01:23:54 2011
FILE MODIFIED TIME........ >Thu Apr 8 01:23:54 2011
FILE I-NODE NUMBER...... >33261
PERMISSION FOR FILE..... >1001101014

RESULT:
Thus the program using the I/O system calls was written and
successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 24


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:3.3 Write a programs to simulate UNIX commands like ls, grep, etc.

AIM:

To write a program to simulate UNIX commands like ls, grep, etc.

ALGORITHM:

1. Start the program.


2. Read the input through command line.
3. Open the specified file.
4. Options (c & i) are performed.
5. Stop the program.

PROGRAM:

#include<stdio.h>
#include<string.h>
main(int ag,char* arg[])

{
char buf[200],line[200];
int i,j,n,fd1,count=0,opt;
if(ag==4)
{

fd1=open(arg[3],0);
if(strcmp(arg[1],"-c")==0)
opt=2;
if(strcmp(arg[1],"-i")==0)
opt=3;
}
else if(ag==3)
{
fd1=open(arg[2],0);
opt=1;
}
if(fd1==-1)
printf("error in opening");

CS8461 OPERATING SYSTEMS LABORATORY Page 25


Sri Vidya College of Engineering & Technology Lab Manual

j=0;
switch(opt)
{
case 1:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)

{
if(buf[i]!='\n') line[j]=buf[i];
else
{

line[j]='\n';
if(strstr(line,arg[1])!=0)
write(1,line,j+1);
}
}
}
break;
case 2:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strstr(line,arg[2])!=0)
count=count+1;
j=-1;
}
}
}
printf("%d \n",count);
break;

CS8461 OPERATING SYSTEMS LABORATORY Page 26


Sri Vidya College of Engineering & Technology Lab Manual

case 3:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strcasestr(line,arg[2])!=0)
write(1,line,j+1);
j=-1;
}
}
}
break;
}
close(fd1);
}

OUTPUT:

[root@localhost ~]# cat tst sd


dsaASD[root@localhost ~]# ./a.out -i a tst
aA[root@localhost ~]# ./a.out -c a tst 1[root@localhost ~]# ./a.out -c A tst
1[root@localhost ~]# ./a.out -c sd tst 1[root@localhost ~]# ./a.out -c s tst 2

RESULT:
Thus the program to stimulate the UNIX commands was written and
successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 27


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:4 (a ) Write a program for implementing the FCFS Scheduling algorithm

AIM:

To write a program for implementing FCFS scheduling algorithm.

ALGORITHM:

1. Start the process.


2. Declare the array size.
3. Get the number of elements to be inserted.
4. Select the process that first arrived in the ready queue
5. Make the average waiting the length of next process.
6. Start with the first process from it’s selection as above and let other process to
be in queue.
7. Calculate the total number of burst time.
8. Display the values.
9. Stop the process.

PROGRAM:

#include<stdio.h>
main()
{

float avgwt,avgtt;

char pname[10][10],c[10][10];
int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
printf("\n\n Enter the number of processes: ");
scanf("%d",&n);
printf("\n\n Enter the NAME , BURST TIME and ARRIVAL TIME of the
process");
for(i=0;i<n;i++)
{

CS8461 OPERATING SYSTEMS LABORATORY Page 28


Sri Vidya College of Engineering & Technology Lab Manual

printf("\n\n NAME : ");


scanf("%s",&pname[i]);
printf("\n\n BURST TIME : ");
scanf("%d",&bt[i]);
printf("\n\n ARRIVAL TIME : ");
scanf("%d",&at[i]);

}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])

{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);

Strcpy(pname[i],pname[j]);

strcpy(pname[j],c[i]);
}

}
wt[0]=0;
for(i=0;i<n;i++)

{
wt[i+1]=wt[i]+bt[i];
sum=sum+(wt[i]-at[i]);
sbt=sbt+(wt[i+1]-at[i]);
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];

CS8461 OPERATING SYSTEMS LABORATORY Page 29


Sri Vidya College of Engineering & Technology Lab Manual

avgwt=(float) sum/n;

avgtt=(float)sbt/n;
printf("\n\n Average waiting time = %f",avgwt);
printf("\n\n Average turn-around time = %f",avgtt);
printf("\n\n GANTT CHART\n");
for(i=0;i<n;i++)
printf("|\t%s\t",pname[i]);
printf("\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d\n",ss);
printf("\n");
}

OUTPUT:

[root@localhost ~]# ./a.out


Enter the number of processes: 4
Enter the NAME , BURST TIME and ARRIVAL TIME of the process
NAME : p1
BURST TIME : 4
ARRIVAL TIME : 0

NAME : p2
BURST TIME : 9
ARRIVAL TIME : 2

NAME : p3
BURST TIME : 8
ARRIVAL TIME : 4

NAME : p4
BURST TIME : 3
ARRIVAL TIME : 3

CS8461 OPERATING SYSTEMS LABORATORY Page 30


Sri Vidya College of Engineering & Technology Lab Manual

Average waiting time = 6.000000


Average turn-around time = 12.000000
GANTT CHART

| p1 | p2 | p4 | p3

0 4 13 16 24

RESULT:
Thus the program for implementing FCFs scheduling algorithm was written
and successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 31


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:4 (b) Write a program for simulation of SJF Scheduling algorithm

AIM:
To write a program for implementing SJF scheduling algorithm

ALGORITHM:

1.Start the process.


2. Declare the array size.
3. Get the number of elements to be inserted.
4. Select the process which have shortest burst will execute first.
5. If two process have same burst length then FCFS scheduling algorithm used.
6. Make the average waiting the length of next process.
7. Start with the first process from it’s selection as above and let other process to
be in queue.
8. Calculate the total number of burst time.
9. Display the values.
10. Stop the process.

PROGRAM:

#include<stdio.h>
main()
{

float avgwt,avgtt;

char pname[10][10],c[10][10];
int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
printf("\n\n Enter the number of processes: ");
scanf("%d",&n);
printf("\n\n Enter the NAME, BURSTTIME, and ARRIVALTIME of the
processes ");
for(i=0;i<n;i++)
{

printf("\n\n NAME : ");


scanf("%s",&pname[i]);
printf("\n\n BURST TIME : ");

CS8461 OPERATING SYSTEMS LABORATORY Page 32


Sri Vidya College of Engineering & Technology Lab Manual

scanf("%d",&bt[i]);
printf("\n\n ARRIVAL TIME : ");
scanf("%d",&at[i]);

}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(at[i]==at[j])
if(bt[i]>bt[j])

{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);

strcpy(pname[j],c[i]);
}
if(at[i]!=at[j])
if(bt[i]>bt[j])

{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);

CS8461 OPERATING SYSTEMS LABORATORY Page 33


Sri Vidya College of Engineering & Technology Lab Manual

}
wt[0]=0;
for(i=0;i<n;i++)

{
wt[i+1]=wt[i]+bt[i];
sum=sum+(wt[i]-at[i]);
sbt=sbt+(wt[i+1]-at[i]);
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];

}
printf("\n\n GANTT CHART");

printf("\n\n---------------------------------------------------------------------- \n");
for(i=0;i<n;i++)
printf("|\t%s\t",pname[i]);
printf("\n-----------------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d\n",ss);

printf("\n--------------------------------------------------------------------------");
printf("\n\n Total WAITING TIME = %d ",sum);
printf("\n\n Total TURNAROUND TIME = %d ",sbt);
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n\n Average WAITING TIME = %f ",avgwt);
printf("\n\n Average TURNAROUND TIME = %f ",avgtt);

OUTPUT:

Enter the number of processes: 5


Enter the NAME, BURSTTIME, and ARRIVALTIME of the processes

CS8461 OPERATING SYSTEMS LABORATORY Page 34


Sri Vidya College of Engineering & Technology Lab Manual

NAME : p0
BURST TIME : 2
ARRIVAL TIME : 0

NAME : p1
BURST TIME : 4
ARRIVAL TIME : 0

NAME : p2
BURST TIME : 5
ARRIVAL TIME : 0

NAME : p3
BURST TIME : 6
ARRIVAL TIME : 0

NAME : p4
BURST TIME : 8
ARRIVAL TIME : 0

GANTT CHART
----------------------------------------------------------------------
| p0 | p1 | p2 | p3 | p4
-----------------------------------------------------------------------
0 2 6 11 17 25

Total WAITING TIME = 36


Total TURNAROUND TIME = 61
Average WAITING TIME = 7.200000

RESULT:

Thus the program for implementing FCFS scheduling algorithm was written and
successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 35


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:5 (a) Write a program for implementing the round robin Scheduling
algorithm
AIM:

To write a program for implement the round robin scheduling algorithm

ALGORITHM:

1. Start the process.


2. Declare the array size.
3. Get the number of elements to be inserted.
4. Get the value.
5. Set the time sharing system with preemption.
6. Define quantum is defined from 10 to 100ms.
7. Declare the queue as a circular.
8. Make the CPU scheduler goes around the ready queue allocating CPU to each

process for the time interval specified.


9. Make the CPU scheduler picks the first process and sets time to interrupt
after quantum expired dispatches the process.
10. If the process has burst less than the time quantum than the process
releases the CPU.

PROGRAM:

#include<stdio.h>
main()
{

int pt[10][10],a[10][10],at[10],pname[10][10],i,j,n,k=0,q,sum=0;

float avg;
printf("\n\n Enter the number of processes : ");
scanf("%d",&n);
for(i=0;i<10;i++)
{

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

CS8461 OPERATING SYSTEMS LABORATORY Page 36


Sri Vidya College of Engineering & Technology Lab Manual

{
pt[i][j]=0;
a[i][j]=0;
}

}
for(i=0;i<n;i++)

{
j=0;
printf("\n\n Enter the process time for process %d :
",i+1); scanf("%d",&pt[i][j]);

}
printf("\n\n Enter the time slice : ");

scanf("%d",&q);
printf("\n\n");
for(j=0;j<10;j++)
{

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

{
a[2*j][i]=k;
if((pt[i][j]<=q)&&(pt[i][j]!=0))

pt[i][j+1]=0;
printf(" %d P%d %d\n",k,i+1,k+pt[i][j]);
k+=pt[i][j];
a[2*j+1][i]=k;

}
else if(pt[i][j]!=0)

{
pt[i][j+1]=pt[i][j]-q;

CS8461 OPERATING SYSTEMS LABORATORY Page 37


Sri Vidya College of Engineering & Technology Lab Manual

printf(" %d P%d %d\n",k,i+1,(k+q));


k+=q;
a[2*j+1][i]=k;

}
else

{
a[2*j][i]=0;
a[2*j+1][i]=0;

}
}
}
for(i=0;i<n;i++)

sum+=a[0][i];
for(i=0;i<n;i++)
{

for(j=1;j<10;j++)
{
if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))
sum+=((a[j+1][i]-a[j][i]));
}
}
avg=(float)sum/n;

printf("\n\n Average waiting time = %f msec",avg);


sum=avg=0;
for(j=0;j<n;j++)

{
i=1;
while(a[i][j]!=0)
i+=1;
sum+=a[i-1][j];

CS8461 OPERATING SYSTEMS LABORATORY Page 38


Sri Vidya College of Engineering & Technology Lab Manual

avg=(float)sum/n;
printf("\n\n Average turnaround time = %f msec\n\n",avg);
}

OUTPUT:

[root@localhost ~]# ./a.out


Enter the number of processes : 4

Enter the process time for process 1 : 8 Enter the process time for process 2 : 3 Enter the
process time for process 3 : 6 Enter the process time for process 4 : 1

Enter the time slice : 2

0 P1 2
2 P2 4
4 P3 6
6 P4 7
7 P1 9
9 P2 10
10 P3 12
12 P1 14
14 P3 16
16 P1 18

Average waiting time = 8.250000 msec


Average turnaround time = 12.750000 msec

RESULT:
Thus the program for implementing RR scheduling algorithm was written and
successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 39


Sri Vidya College of Engineering & Technology Lab Manual

EX NO:5 (b) Write a program for implementing the Priority scheduling algorithm

AIM:
To write a program for implement the priority scheduling algorithm.

ALGORITHM:

1. Start the process.


2. Declare the array size.
3. Get the number of elements to be inserted.
4. Get the priority for each process and value
5. start with the higher priority process from it’s initial position let other process to
be queue.
6. calculate the total number of burst time.
7. Display the values
8. Stop the process.

PROGRAM:

#include<stdio.h>
main()
{

float avgwt,avgtt;

char pname[10][10],c[10][10];
int wt[10],tt[10],bt[10],pt[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=10;
printf("\n\n Enter the number of processes : "); scanf("%d",&n);

printf("\n\n Enter the NAME and BURSTTIME ");


for(i=0;i<n;i++)
{

printf("\n\n NAME : ");


scanf("%s",&pname[i]);
printf("\n\n BURSTTIME : ");
scanf("%d",&bt[i]);

CS8461 OPERATING SYSTEMS LABORATORY Page 40


Sri Vidya College of Engineering & Technology Lab Manual

printf("\n\n Enter the priorities of the processes


"); for(i=0;i<n;i++)

{
printf("\n\n Priority of process%d : ",i+1);
scanf("%d",&pt[i]);

}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])

{
t=pt[i];
pt[i]=pt[j];
pt[j]=t;
q=bt[i];

bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);

}
}
wt[0]=0;
for(i=0;i<n;i++)

{
wt[i+1]=wt[i]+bt[i];
sum=sum+wt[i];
sbt=sbt+wt[i+1];
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];

}
printf("\n\n GANTT CHART");

CS8461 OPERATING SYSTEMS LABORATORY Page 41


Sri Vidya College of Engineering & Technology Lab Manual

printf("\n-----------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("|\t%s\t",pname[i]);
printf("\n-----------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d\n",ss);
printf("\n-----------------------------------------------------------------\n");
printf("\n\n Total WAITING TIME of the process = %d",sum);
printf("\n\n Total TURNAROUND TIME of the process = %d",sbt);
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n\n Average WAITING TIME of the process = %f",avgwt);
printf("\n\n Average TURNAROUND TIME of the process = %f",avgtt);

OUTPUT:

[root@localhost ~]# ./a.out


Enter the number of processes : 4
Enter the NAME and BURSTTIME

NAME : p1
BURSTTIME : 8
NAME : p2
BURSTTIME : 3
NAME : p3
BURSTTIME : 6
NAME : p4
BURSTTIME : 1

Enter the priorities of the processes


Priority of process1 : 1
Priority of process2 : 5
Priority of process3 : 2
Priority of process4 : 4

CS8461 OPERATING SYSTEMS LABORATORY Page 42


Sri Vidya College of Engineering & Technology Lab Manual

GANTT CHART
------------------------------------------------------
| p1 | p3 | p4 | p2
------------------------------------------------------
0 8 14 15 28

Total WAITING TIME of the process = 37


Total TURNAROUND TIME of the process = 55
Average WAITING TIME of the process = 9.250000
Average TURNAROUND TIME of the process = 13.750000

RESULT:

Thus the program for implementing FCFS scheduling algorithm was written and
successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 43


Sri Vidya College of Engineering & Technology Lab Manual

EX NO: 6 . Developing Application using Inter Process communication (using


shared memory, pipes or message queues)

AIM:
To write a program for developing Application using Inter Process communication
with pipes.

ALGORITHM:

1. Start the program.


2. Read the input from parent process and perform in child process.
3. Write the date in parent process and read it in child process.
4. Fibonacci Series was performed in child process.
5. Stop the program.

PROGRAM:

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/uio.h>
#include<sys/types.h>
main()
{

int pid,pfd[2],n,a,b,c;

if(pipe(pfd)==-1)

{
printf("\nError in pipe connection\n");
exit(1);

}
pid=fork();
if(pid>0)

{
printf("\nParent Process");\
printf("\n\n\tFibonacci Series");

CS8461 OPERATING SYSTEMS LABORATORY Page 44


Sri Vidya College of Engineering & Technology Lab Manual

printf("\nEnter the limit for the series:");


scanf("%d",&n);
close(pfd[0]);
write(pfd[1],&n,sizeof(n));
close(pfd[1]);
exit(0);

}
else

{
close(pfd[1]);
read(pfd[0],&n,sizeof(n));
printf("\nChild Process");
a=0;

b=1;
close(pfd[0]);
printf("\nFibonacci Series is:");
printf("\n\n%d\n%d",a,b);
while(n>2)

{
c=a+b;

printf("\n%d",c);
a=b;
b=c;
n--;

}
}

OUTPUT:

[root@localhost ~]# ./a.out


Parent Process
Fibonacci Series
Enter the limit for the series:5

CS8461 OPERATING SYSTEMS LABORATORY Page 45


Sri Vidya College of Engineering & Technology Lab Manual

Child Process
Fibonacci Series is:
01123

RESULT:

Thus the program for developing Application using Inter Process communication
with pipes is written and successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 46


Sri Vidya College of Engineering & Technology Lab Manual

EX NO: 7 Implement the Producer – Consumer problem using semaphores (using

UNIX system calls).

AIM:

To write a program for Implement the Producer – Consumer problem using semaphores
(using UNIX system calls).

ALGORITHM:

1. Start the process


2. Initialize buffer size
3. Consumer enters, before that producer buffer was not empty.
4. Producer enters, before check consumer consumes the buffer.
5. Stop the process.

PROGRAM:

#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()

{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{

printf("\nENTER YOUR CHOICE\n");

CS8461 OPERATING SYSTEMS LABORATORY Page 47


Sri Vidya College of Engineering & Technology Lab Manual

scanf("%d",&n);
switch(n)
{

case 1:

if((mutex==1)&&(empty!=0))
producer();

else
printf("BUFFER IS FULL");
break;

case 2:
if((mutex==1)&&(full!=0))
consumer();

else
printf("BUFFER IS EMPTY");
break;

case 3:
exit(0);
break;

}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()

CS8461 OPERATING SYSTEMS LABORATORY Page 48


Sri Vidya College of Engineering & Technology Lab Manual

{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);

OUTPUT:

[root@localhost ~]# ./a.out


1.PRODUCER
2.CONSUMER
3.EXIT
ENTER YOUR CHOICE
1producer produces the item1
ENTER YOUR CHOICE
1producer produces the item2
ENTER YOUR CHOICE
2consumer consumes item2
ENTER YOUR CHOICE
2consumer consumes item1
ENTER YOUR CHOICE
2BUFFER IS EMPTY

CS8461 OPERATING SYSTEMS LABORATORY Page 49


Sri Vidya College of Engineering & Technology Lab Manual

ENTER YOUR CHOICE


3

RESULT:
Thus the program for Implement the Producer – Consumer problem using semaphores
(using UNIX system calls) was written and successfully executed.

CS8461 OPERATING SYSTEMS LABORATORY Page 50


EX NO: 8 Implement first fit algorithm for memory management

AIM:
To write a program to implement first fit algorithm for memory management.

ALGORITHM

1. Start the process.


2. Declare the size.
3. Get the number of processes to be inserted.
4. Allocate the first hole that is big enough searching.
5. Start at the beginning of the set of holes.
6. If not start at the hole that is sharing the pervious first fit search end.
7. If large enough then stop searching in the procedure.
8. Display the values.
9. Stop the process.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct allocate
{

int pid;

int st_add;
int end_add;
struct allocate *next;
};

struct free_list
{
int st_add;
int end_add;
struct free_list *next;
};

struct process
{
int pid;
int size;
};

struct process pro[10];


struct free_list *flist=NULL;
struct allocate *alot=NULL;

void display_alot(struct allocate *temp_a)


{
printf("\n\n allocated list:");

printf("\n================");
while(temp_a!=NULL)
{

printf("\n process:%d st_add:%d end_add:%d ",temp_a->pid,

temp_a- >st_add,temp_a->end_add);
temp_a=temp_a->next;

void display_free(struct free_list *temp_f)

{
printf("\n\n free list:");

printf("\n=================");
while(temp_f!=NULL)
{
printf("\n st_add:%d end_add:%d",temp_f->st_add,

temp_f->end_add);

temp_f=temp_f->next;
}
}
void insert(int p)
{
struct free_list *temp_f;

struct allocate *temp_a,*pre_a;


int i,n;
do
{

srand((unsigned int)time((time_t*)NULL));

n=rand()%5;
}

while(n==0);
printf("\n\n no. of process:%d",n);
for(i=0;i<n;i++)
{

pro[i].pid=i+p;
do
{

pro[i].size=rand()%300;

}
while(pro[i].size==0);
}
for(i=0;i<n;i++)
{
printf("\n\n process to be inserted:%d size:%d",pro[i].pid,pro[i].size);
temp_f=flist;
temp_a=alot;
while(temp_f!=NULL && temp_f->end_add-temp_f->st_add

< pro[i].size)
{

temp_f=temp_f->next;

}
if(temp_f!=NULL)
{
pre_a=(struct allocate*)malloc(sizeof(struct allocate));

pre_a->st_add=temp_f->st_add;
pre_a->end_add=temp_f->st_add=temp_f->st_add+pro[i].size;
pre_a->pid=pro[i].pid;
if(temp_a==NULL)
{

alot=pre_a;

pre_a->next=NULL;

else
{

while(temp_a->next!=NULL)
{

temp_a=temp_a->next;
}

temp_a->next=pre_a;

pre_a->next=NULL;
}
}
else

printf("\n there is not enough space");


display_alot(alot);
display_free(flist);
getch();
}

void main()

{
int no,n,i,nod,ndpid;

struct process pro[10];


struct free_list *temp_f,*free_alot,*pre_f;
struct allocate *temp_a,*pre_a;
clrscr();
alot=NULL;
flist=(struct free_list*)malloc(sizeof(struct free_list));
flist->st_add=0;
flist->end_add=1024;
flist->next=NULL;
insert(0);
do
{

srand((unsigned int)time((time_t*)NULL));

nod=rand()%2;
}

while(nod==0);
printf("\n\n no.of process deletion:%d",nod);
for(i=0;i<nod;i++)
{

printf("\n\n\n process to be deleted:");


scanf("%d",&ndpid);
temp_a=alot;
temp_f=flist;
while(temp_a!=NULL && temp_a->pid!=ndpid)
{

pre_a=temp_a;

temp_a=temp_a->next;
}
if(temp_a!=NULL)
{
if(alot==temp_a)

alot=temp_a->next;
else
pre_a->next=temp_a->next;
pre_f=NULL;
while(temp_f!=NULL && temp_f->st_add < temp_a->st_add)
{

pre_f=temp_f;

temp_f=temp_f->next;
}
if(pre_f!=NULL && pre_f->end_add==temp_a->st_add)

pre_f->end_add=temp_a->end_add;
else if(pre_f!=NULL&&temp_f!=NULL&&

temp_f- >st_add==temp_a->end_add)
temp_f->st_add=temp_a->st_add;
else
{

free_alot=(struct free_list*)malloc(sizeof

(struct free_list));
free_alot->st_add=temp_a->st_add;
free_alot->end_add=temp_a->end_add;
if(pre_f!=NULL)
pre_f->next=free_alot;
free_alot->next=temp_f;
if(flist==temp_f)

{
flist=free_alot;
}
}
free(temp_a);
}
else printf("\n process not in memory");

temp_f=flist;
while(temp_f!=NULL)
{

if(temp_f->end_add==temp_f->next->st_add)

{
temp_f->end_add=temp_f->next->end_add;
temp_f->next=temp_f->next->next;

temp_f=temp_f->next;
}

display_alot(alot);
display_free(flist);

getch();
}
insert(10);
}
OUTPUT:

no. of process:3
process to be inserted:0 size:120
allocated list:
================
process:0 st_add:0 end_add:120
free list:

=================
st_add:120 end_add:1024
process to be inserted:1 size:185

allocated list:

================
process:0 st_add:0 end_add:120
process:1 st_add:120 end_add:305

free list:
=================

st_add:305 end_add:1024
process to be inserted:2 size:246
allocated list:

================
process:0 st_add:0 end_add:120
process:1 st_add:120 end_add:305
process:2 st_add:305 end_add:551

free list:
=================
st_add:551 end_add:1024
no.of process deletion:1
process to be deleted:0
allocated list:
================
process:1 st_add:120 end_add:305
process:2 st_add:305 end_add:551

free list:
=================
st_add:0 end_add:120

st_add:551 end_add:1024
no. of process:3
process to be inserted:10 size:195
allocated list:

================
process:1 st_add:120 end_add:305
process:2 st_add:305 end_add:551
process:10 st_add:551 end_add:746

free list:

=================
st_add:0 end_add:120
st_add:746 end_add:1024

process to be inserted:11 size:96


allocated list:

================
process:1 st_add:120 end_add:305
process:2 st_add:305 end_add:551
process:10 st_add:551 end_add:746
process:11 st_add:0 end_add:96

free list:

=================
st_add:96 end_add:120
st_add:746 end_add:1024
process to be inserted:12 size:148
allocated list:

================
process:1 st_add:120 end_add:305
process:2 st_add:305 end_add:551
process:10 st_add:551 end_add:746
process:11 st_add:0 end_add:96
process:12 st_add:746 end_add:894

free list:

=================
st_add:96 end_add:120
st_add:894 end_add:1024

RESULT:

Thus the program for Implement first fit algorithm for memory management is
written and successfully executed.
EX NO: 9 (a) Implement Best fit algorithm for memory management

AIM :
To write a program for Implement best fit memory management

ALGORITHM:

1.Start the program.


2. Declare the size.
3. Get the number of processes to be inserted.
4. Allocate the best hole that is small enough searching.
5. Start at the best of the set of holes.
6. If not start at the hole that is sharing the pervious best fit search end.
7. Compare the hole in the list.
8. If small enough then stop searching in the procedure.
9. Display the values.
10. Stop the program.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct allocate
{

int pid;

int st_add;
int end_add;
struct allocate *next;
};

struct free_list
{
int st_add;
int end_add;
struct free_list *next;
};

struct process
{
int pid;
int size;
};

struct process pro[10];


struct free_list *flist=NULL;
struct allocate *alot=NULL;

void display_alot(struct allocate *temp_a)


{

printf("\n\n allocated list:");


printf("\n================");
while(temp_a!=NULL)
{

printf("\n process:%d st_add:%d end_add:%d ",temp_a->pid,

temp_a->st_add,temp_a->end_add);
temp_a=temp_a->next;
}

void display_free(struct free_list *temp_f)

{
printf("\n\n free list:");

printf("\n=================");
while(temp_f!=NULL)
{
printf("\n st_add:%d end_add:%d",temp_f->st_add,

temp_f->end_add);

temp_f=temp_f->next;
}
}
void insert(int p)
{
struct free_list *temp_f;

struct allocate *temp_a,*pre_a;


int i,n;
do
{

srand((unsigned int)time((time_t*)NULL));

n=rand()%10;
}
while(n==0);
printf("\n\n no. of process:%d",n);
for(i=0;i<n;i++)
{
pro[i].pid=i+p;
do
{
pro[i].size=rand()%550;
}
while(pro[i].size==0);
}
for(i=0;i<n;i++)
{
printf("\n\n process to be inserted:%d size:%d",pro[i].pid,pro[i].size);

temp_f=flist;
temp_a=alot;
while(temp_f!=NULL && temp_f->end_add-temp_f->
st_add < pro[i].size)
{

temp_f=temp_f->next;

}
if(temp_f!=NULL)
{
pre_a=(struct allocate*)malloc(sizeof(struct allocate));

pre_a->st_add=temp_f->st_add;
pre_a->end_add=temp_f->st_add=temp_f->st_add+pro[i].size;
pre_a->pid=pro[i].pid;
if(temp_a==NULL)
{

alot=pre_a;

pre_a->next=NULL;

else
{

while(temp_a->next!=NULL)
{

temp_a=temp_a->next;

}
temp_a->next=pre_a;
pre_a->next=NULL;
}
}
else

printf("\n there is not enough space");


display_alot(alot);
display_free(flist);
getch();
}

void bestfit(int p)

{
struct free_list *temp_f,*enough_hole;

struct allocate *temp_a,*pre_a;


int i,n,rem_space;
do
{

srand((unsigned int)time((time_t*)NULL));

n=rand()%10;
}

while(n==0);
printf("\n no of processes:%d",n);
for(i=0;i<n;i++)
{

pro[i].pid=i+p;

do

pro[i].size=rand()%200;
}

while(pro[i].size==0);
}

for(i=0;i<n;i++)
{
printf("\n process to be inserted:%d size:%d",pro[i].pid,pro[i].size);

temp_f=flist;
temp_a=alot;
enough_hole=NULL;
rem_space=1024;
while(temp_f!=NULL)
{

if(temp_f->end_add - temp_f->st_add >= pro[i].size)

if(temp_f->end_add - temp_f->

st_add - pro[i].size<rem_space)
{

rem_space=temp_f->end_add - temp_f->

st_add - pro[i].size;
enough_hole=temp_f;

}
}
temp_f=temp_f->next;
}
if(enough_hole!=NULL)
{
pre_a=(struct allocate*)malloc(sizeof(struct allocate));

pre_a->st_add=enough_hole->st_add;
pre_a->end_add=enough_hole->st_add=

enough_hole->st_add + pro[i].size;
pre_a->pid=pro[i].pid;
if(temp_a==NULL)
{
alot=pre_a;

pre_a->next=NULL;
}
else
{
while(temp_1->next!=NULL)

temp_a=temp_a->next;
}

temp_a->next=pre_a;
pre_a->next=NULL;

}
}
else

printf("\n there is not enough space");


display_alot(alot);
display_free(flist);
getch();
}

void main()

{
int no,n,i,nod,ndpid;

struct process pro[10];


struct free_list *temp_f,*free_alot,*pre_f;
struct allocate *temp_a,*pre_a;
clrscr();
alot=NULL;
flist=(struct free_list*)malloc(sizeof(struct free_list));
flist->st_add=0;
flist->end_add=1024;
flist->next=NULL;
insert(0);
do
{

srand((unsigned int)time((time_t*)NULL));

nod=rand()%5;
}
while(nod==0);

printf("\n\n no.of process deletion:%d",nod);


for(i=0;i<nod;i++)
{

printf("\n\n\n process to be deleted:");

scanf("%d",&ndpid);
temp_a=alot;
temp_f=flist;
while(temp_a!=NULL && temp_a->pid!=ndpid)
{

pre_a=temp_a;

temp_a=temp_a->next;
}
if(temp_a!=NULL)
{
if(alot==temp_a)

alot=temp_a->next;
else
pre_a->next=temp_a->next;
pre_f=NULL;
while(temp_f!=NULL && temp_f->st_add < temp_a->st_add)
{
pre_f=temp_f;

temp_f=temp_f->next;
}
if(pre_f!=NULL && pre_f->end_add==temp_a->st_add)

pre_f->end_add=temp_a->end_add;
else if(pre_f!=NULL&&temp_f!=NULL&&

temp_f->st_add==temp_a->end_add)
temp_f->st_add=temp_a->st_add;
else
{

free_alot=(struct free_list*)malloc(sizeof

(struct free_list));

free_alot->st_add=temp_a->st_add;
free_alot->end_add=temp_a->end_add;

if(pre_f!=NULL)
pre_f->next=free_alot;
free_alot->next=temp_f;
if(flist==temp_f)
{

flist=free_alot;

}
}
free(temp_a);
}
else printf("\n process not in memory");

temp_f=flist;
while(temp_f!=NULL)
{
if(temp_f->end_add==temp_f->next->st_add)

{
temp_f->end_add=temp_f->next->end_add;
temp_f->next=temp_f->next->next;

temp_f=temp_f->next;
}

display_alot(alot);
display_free(flist);

getch();
}
bestfit(10);
}

OUTPUT:

no. of process:7
process to be inserted:0 size:351
allocated list:

================
process:0 st_add:0 end_add:351
free list:
=================

st_add:351 end_add:1024
process to be inserted:1 size:466
allocated list:

================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817

free list:
=================
st_add:817 end_add:1024
process to be inserted:2 size:337
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817

free list:
=================
st_add:817 end_add:1024
process to be inserted:3 size:410
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817

free list:
=================
st_add:817 end_add:1024
process to be inserted:4 size:542
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817

free list:
=================
st_add:817 end_add:1024
process to be inserted:5 size:547
there is not enough space
allocated list:
================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817

free list:
=================

st_add:817 end_add:1024
process to be inserted:6 size:89
allocated list:

================
process:0 st_add:0 end_add:351
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906

free list:
=================

st_add:906 end_add:1024
no.of process deletion:4
process to be deleted:0
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906

free list:
=================
st_add:0 end_add:351
st_add:906 end_add:1024
process to be deleted:2
process not in memory
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
free list:

=================
st_add:0 end_add:351
st_add:906 end_add:1024

process to be deleted:3
process not in memory
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906

free list:

=================
st_add:0 end_add:351
st_add:906 end_add:1024

process to be deleted:4
process not in memory
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906

free list:

=================
st_add:0 end_add:351
st_add:906 end_add:1024

no of processes:9
process to be inserted:10 size:155
allocated list:
================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
free list:

=================
st_add:155 end_add:351
st_add:906 end_add:1024

process to be inserted:11 size:43


allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155

process:11 st_add:906 end_add:949


free list:

=================
st_add:155 end_add:351
st_add:949 end_add:1024

process to be inserted:12 size:188


allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343

free list:

=================
st_add:343 end_add:351
st_add:949 end_add:1024
process to be inserted:13 size:7
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350

free list:

=================
st_add:350 end_add:351
st_add:949 end_add:1024

process to be inserted:14 size:160


there is not enough space
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350

free list:

=================
st_add:350 end_add:351
st_add:949 end_add:1024
process to be inserted:15 size:100
there is not enough space

allocated list:
================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350

free list:

=================
st_add:350 end_add:351
st_add:949 end_add:1024

process to be inserted:16 size:198


there is not enough space
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906

process:10 st_add:0 end_add:155


process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350

free list:

=================
st_add:350 end_add:351
st_add:949 end_add:1024

process to be inserted:17 size:51


allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350
process:17 st_add:949 end_add:1000

free list:

=================
st_add:350 end_add:351
st_add:1000 end_add:1024

process to be inserted:18 size:42


there is not enough space
allocated list:

================
process:1 st_add:351 end_add:817
process:6 st_add:817 end_add:906
process:10 st_add:0 end_add:155
process:11 st_add:906 end_add:949
process:12 st_add:155 end_add:343
process:13 st_add:343 end_add:350
process:17 st_add:949 end_add:1000

free list:

=================
st_add:350 end_add:351
st_add:1000 end_add:1024

RESULT:

Thus the program for Implement best fit algorithm for memory management is
written and successfully executed.
EX NO: 9 (b) Implement worst fit algorithm for memory management

AIM :
To write a program for Implement worst fit memory management.

ALGORITHM:

1. Start the program.


2. Declare the size.
3. Get the number of processes to be inserted.
4. Allocate the first hole that is small enough searching.
5. If small enough then stop searching in the procedure.
6. Display the values.
7. Stop the program.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct allocate
{

int pid; int st_add; int end_add; struct allocate *next;


};
struct free_list
{
int st_add; int end_add; struct free_list *next;
};
struct process
{
int pid; int size;
};
struct process pro[10];
struct free_list *flist=NULL;
struct allocate *alot=NULL;

void display_alot(struct allocate *temp_a)


{
printf("\n\n allocated list:");

printf("\n================");
while(temp_a!=NULL)
{

printf("\n process:%d st_add:%d end_add:%d ",temp_a->pid,

temp_a->st_add,temp_a->end_add);
temp_a=temp_a->next;
}

void display_free(struct free_list *temp_f)

printf("\n\n free list:");


printf("\n=================");
while(temp_f!=NULL)
{

printf("\n st_add:%d end_add:%d",temp_f->st_add,temp_f->end_add);

temp_f=temp_f->next;
}
}
void insert(int p)
{
struct free_list *temp_f;
struct allocate *temp_a,*pre_a;
int i,n;
do
{

srand((unsigned int)time((time_t*)NULL));

n=rand()%10;
}
while(n==0);

printf("\n\n no. of process:%d",n);


for(i=0;i<n;i++)
{

pro[i].pid=i+p;

do
{
pro[i].size=rand()%550;
}
while(pro[i].size==0);

for(i=0;i<n;i++)
{

printf("\n\n process to be inserted:%d size:%d",pro[i].pid,pro[i].size);


temp_f=flist;

temp_a=alot;
while(temp_f!=NULL && temp_f->end_add-temp_f->st_add < pro[i].size)
{

temp_f=temp_f->next;

}
if(temp_f!=NULL)
{
pre_a=(struct allocate*)malloc(sizeof(struct allocate));

pre_a->st_add=temp_f->st_add;
pre_a->end_add=temp_f->st_add=temp_f->st_add+pro[i].size;
pre_a->pid=pro[i].pid;
if(temp_a==NULL)
{

alot=pre_a;

pre_a->next=NULL;

else
{

while(temp_a->next!=NULL)
{

temp_a=temp_a->next;
}

temp_a->next=pre_a;
pre_a->next=NULL;

}
}
else

printf("\n there is not enough space");


display_alot(alot);
display_free(flist);
getch();
}

void worstfit(int p)
{
struct free_list *temp_f,*big_hole;

struct allocate *temp_a,*pre_a;


int i,n;
do
{

srand((unsigned int)time((time_t*)NULL));

n=rand()%10;
}

while(n==0);
printf("\n no.of process:%d",n);
for(i=0;i<n;i++)
{

pro[i].pid=i+p;

do

pro[i].size=rand()%250;
}

while(pro[i].size==0);
}

for(i=0;i<n;i++)
{

printf("\n process to be inserted: %d size :


%d",pro[i].pid,pro[i].size); temp_f=flist;

temp_a=alot;
big_hole=NULL;
while(temp_f!=NULL)
{
if(temp_f->end_add-temp_f->st_add>=pro[i].size)

{
if(big_hole==NULL)
big_hole=temp_f;
else if(temp_f->end_add - temp_f->st_add > big_hole
-> end_add - big_hole-> st_add) big_hole=temp_f;

temp_f=temp_f->next;
}

if(big_hole!= NULL)
{

pre_a=(struct allocate*) malloc (sizeof(struct allocate));

pre_a->st_add=big_hole->st_add;
pre_a->end_add=big_hole->st_add=big_hole->st_add + pro[i].size;
pre_a->pid=pro[i].pid;
if(temp_a==NULL)
{

alot=pre_a;

pre_a->next=NULL;
}
else
{
while(temp_a->next!=NULL)

temp_a=temp_a->next;
temp_a->next= pre_a;
pre_a->next=NULL;
}

}
else

printf("\n there is not enough space");


display_alot(alot);
display_free(flist);
getch();
}

void main()

int no,n,i,nod,ndpid;
struct process pro[10];
struct free_list *temp_f,*free_alot,*pre_f;
struct allocate *temp_a,*pre_a;
clrscr();
alot=NULL;
flist=(struct free_list*)malloc(sizeof(struct free_list));
flist->st_add=0;
flist->end_add=1024;
flist->next=NULL;
insert(0);
do
{

srand((unsigned int)time((time_t*)NULL));

nod=rand()%5;
}

while(nod==0);
printf("\n\n no.of process deletion:%d",nod);
for(i=0;i<nod;i++)
{

printf("\n\n\n process to be deleted:");


scanf("%d",&ndpid);
temp_a=alot;
temp_f=flist;
while(temp_a!=NULL && temp_a->pid!=ndpid)
{

pre_a=temp_a;

temp_a=temp_a->next;

if(temp_a!=NULL)
{

if(alot==temp_a)
alot=temp_a->next;

else

pre_a->next=temp_a->next;
pre_f=NULL;
while(temp_f!=NULL && temp_f->st_add < temp_a->st_add)
{

pre_f=temp_f;

temp_f=temp_f->next;
}
if(pre_f!=NULL && pre_f->end_add==temp_a->st_add)

pre_f->end_add=temp_a->end_add;
else if(pre_f!=NULL&&temp_f!=NULL&&

temp_f->st_add==temp_a->end_add)
temp_f->st_add=temp_a->st_add;
else
{

free_alot=(struct free_list*)malloc(sizeof(struct free_list));


free_alot->st_add=temp_a->st_add;
free_alot->end_add=temp_a->end_add;
if(pre_f!=NULL)
pre_f->next=free_alot;
free_alot->next=temp_f;
if(flist==temp_f)
{

flist=free_alot;

}
}
free(temp_a);
}
else printf("\n process not in memory");

temp_f=flist;
while(temp_f!=NULL)
{

if(temp_f->end_add==temp_f->next->st_add)

{
temp_f->end_add=temp_f->next->end_add;
temp_f->next=temp_f->next->next;

temp_f=temp_f->next;
}

display_alot(alot);
display_free(flist);

getch();
}
worstfit(10);
}
OUTPUT:

no. of process:9
process to be inserted:0 size:21
allocated list:

================
process:0 st_add:0 end_add:21
free list:
=================

st_add:21 end_add:1024
process to be inserted:1 size:469
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490

free list:
=================

st_add:490 end_add:1024
process to be inserted:2 size:30
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520

free list:
=================

st_add:520 end_add:1024
process to be inserted:3 size:74
allocated list:
================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594

free list:
=================

st_add:594 end_add:1024
process to be inserted:4 size:182
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776

free list:
=================

st_add:776 end_add:1024
process to be inserted:5 size:100
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:
=================
st_add:876 end_add:1024
process to be inserted:6 size:183
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:
=================
st_add:876 end_add:1024
process to be inserted:7 size:411
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:
=================
st_add:876 end_add:1024
process to be inserted:8 size:292
there is not enough space
allocated list:

================
process:0 st_add:0 end_add:21
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:
=================

st_add:876 end_add:1024
no.of process deletion:2
process to be deleted:0
allocated list:

================
process:1 st_add:21 end_add:490
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:

=================
st_add:0 end_add:21
st_add:876 end_add:1024

process to be deleted:1
allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876

free list:

=================
st_add:0 end_add:490
st_add:876 end_add:1024
no.of process:6
process to be inserted: 10 size :105
allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105

free list:

=================
st_add:105 end_add:490
st_add:876 end_add:1024

process to be inserted: 11 size :93


allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105
process:11 st_add:105 end_add:198

free list:

=================
st_add:198 end_add:490
st_add:876 end_add:1024

process to be inserted: 12 size :60


allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105
process:11 st_add:105 end_add:198
process:12 st_add:198 end_add:258

free list:

=================
st_add:258 end_add:490
st_add:876 end_add:1024

process to be inserted: 13 size :103


allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105
process:11 st_add:105 end_add:198
process:12 st_add:198 end_add:258
process:13 st_add:258 end_add:361

free list:

=================
st_add:361 end_add:490
st_add:876 end_add:1024

process to be inserted: 14 size :72


allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105
process:11 st_add:105 end_add:198

process:12 st_add:198 end_add:258 process:13 st_add:258 end_add:361 process:14


st_add:876 end_add:948

free list:

=================
st_add:361 end_add:490
st_add:948 end_add:1024

process to be inserted: 15 size :17


allocated list:

================
process:2 st_add:490 end_add:520
process:3 st_add:520 end_add:594
process:4 st_add:594 end_add:776
process:5 st_add:776 end_add:876
process:10 st_add:0 end_add:105
process:11 st_add:105 end_add:198
process:12 st_add:198 end_add:258
process:13 st_add:258 end_add:361
process:14 st_add:876 end_add:948
process:15 st_add:361 end_add:378

free list:

=================
st_add:378 end_add:490
st_add:948 end_add:1024

RESULT:

Thus the program for Implement best fit algorithm for memory management is
written and successfully executed.
EX NO: 10 Implement Contiguous file allocation technique.

AIM :

To write a program for implement the contiguous file allocation technique.

ALGORITHM:

1. Start the program


2. Declare the size
3. Get the number of files to be inserted.
4. Get the capacity of each file.
5. Get the starting address.
6. The file is allocated in memory
7. The file is not allocated if the contiguous memory is not available.
8. Display the result
9. Stop the program.

PROGRAM:

#include<stdio.h>
main()
{
int nf, fc[20], mb[100], i, j, k, fb[100], fs[20],
mc=0; clrscr();
printf("\nEnter the number of files: ");
scanf("%d",&nf);
for(i=0;i<nf;i++)
{
printf("\nEnter the capacity of file %d:
",i+1); scanf("%d",&fc[i]);
printf("\nEnter the starting address of file %d:
",i+1); scanf("%d",&fs[i]);
}
printf("\n---CONTIGUOUS FILE ALLOCATION---\n");
for(i=0;i<100;i++)
fb[i]=1;
for(i=0;i<nf;i++)
{
j=fs[i];
{
if(fb[j]==1)
{
for(k=j;k<(j+fc[i]);k++)
{
if(fb[k]==1)
mc++;
}
if(mc==fc[i])
{
for(k=fs[i];k<(fs[i]+fc[i]);k++)
{
fb[k]=0;
}
printf("\nFile %d allocated in memory %d
to %d...",i+1,fs[i],fs[i]+fc[i]-1); }

}
else
printf("\nFile %d not allocated since %d contiguous memory not
available from %d...",i+1,fc[i],fs[i]); }

mc=0;
}
}

OUTPUT:

Enter the number of files: 3


Enter the capacity of file 1: 21
Enter the starting address of file 1: 21
Enter the capacity of file 2: 24
Enter the starting address of file 2: 36
Enter the capacity of file 3: 2
Enter the starting address of file 3: 54
---CONTIGUOUS FILE ALLOCATION---
File 1 allocated in memory 21 to 41...
File 2 not allocated since 24 contiguous memory not available from 36...
File 3 allocated in memory 54 to 55...
RESULT:

Thus the program for contiguous file allocation technique was written and
successfully executed.

You might also like