0% found this document useful (0 votes)
16 views66 pages

Os Lab-1

The document is a laboratory record notebook for the Operating Systems Laboratory at Veerammal Engineering College, detailing student information and a list of experiments to be conducted. It includes various UNIX commands, their syntax, explanations, and examples, covering topics such as file management, process management, and system commands. The document serves as a practical guide for students to learn and implement operating system concepts.

Uploaded by

no1542845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views66 pages

Os Lab-1

The document is a laboratory record notebook for the Operating Systems Laboratory at Veerammal Engineering College, detailing student information and a list of experiments to be conducted. It includes various UNIX commands, their syntax, explanations, and examples, covering topics such as file management, process management, and system commands. The document serves as a practical guide for students to learn and implement operating system concepts.

Uploaded by

no1542845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

VEERAMMAL ENGINEERING COLLEGE

PVP NAGAR, K.SINGARAKOTTAI (Post),

DINDIGUL-624708.

CS3461- OPERATING SYSTEMS LABORATORY

RECORD NOTE BOOK

NAME : ……………………………………………………

REGISTER NO : ……………………………………………………

BRANCH : ……………………………………………………

DEGREE/YEAR/SEM : ……………………………………………………
VEERAMMAL ENGINEERING COLLEGE

PVP NAGAR, K.SINGARAKOTTAI (Post),

DINDIGUL-624708.

BONAFIDE CERTIFICATE

NAME :…………………………………………..

REGISTER NO :…………………………………………..

BRANCH/YEAR/SEM :…………………………………………..

SUBJECT :………………………………………….

Certified that this is the bonafide record of work done by the above
student in ...................................................................... Laboratory during the year
20 - 20

Lab-In Charge Head of the Department

Submitted for the Practical Examination held on …………………..

Internal Examiner External Examiner


LIST OF EXPERIMENTS

S.No Date List of Experiments Page No Signature

1 28/2/2025 Installation of windows operating system

2 28/2/2025 Illustrate UNIX commands and Shell


Programming

3 7/3/2025 Process Management using System Calls : Fork,


Exit, Getpid, Wait, Close

4 7/3/2025 Write C programs to implement the various CPU


Scheduling Algorithms
Illustrate the inter process communication
5 14/3/2025 strategy
Implement mutual exclusion by Semaphore
6 14/3/2025

7 21/3/2025 Write C programs to avoid Deadlock using


Banker's Algorithm

21/3/2025 Write a C program to Implement Deadlock


8
Detection Algorithm

9 28/3/2025 Write C program to implement Threading

10 28/3/2025 Implement the paging Technique using C


program
Write C programs to implement the following
11 4/4/2025 Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit
12
Write C programs to implement the various
11/4/2025
Page Replacement Algorithms
13 25/4/2025
Write C programs to Implement the various
File Organization Techniques
14 2/5/2025 Implement the following File Allocation
Strategies using C programs
a. Sequential b. Indexed c. Linked
15 9/5/2025 Write C programs for the implementation of
various disk scheduling algorithms

16 16/5/2025 Install any guest operating system like Linux


using VMware
Ex. No.: 1 BASIC SHELL COMMANDS
Date:

AIM
To Study the basic Shell Commands in UNIX.

GENERAL PURPOSE COMMANDS

1. date command
Syntax: date
Explanation: The date command is used to display the currents date with day of the week, month,
day time (24hr clock) and the year.
Example: ssmiet@ubuntu:~$ date
Sample output: Wed Feb 10 05:05:44 EST 2010
ssmiet@ubuntu:~$

2. echo command
Syntax: echo text
Explanation: The echo command is used to print the message on the screen, whatever is typed on
the line.
Example: ssmiet@ubuntu:~$ echo SSM Institute of Engineering and Technology
Sample output: SSM Institute of Engineering and Technology
ssmiet@ubuntu:~$

3. cal command
Syntax: cal month and year
Explanation: The calendar (cal) command displays the calendar for the specified month of year.
Example 1: ssmiet@ubuntu:~$ cal 2 2005
Sample output: February 2005
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
ssmiet@ubuntu:~$

4. bc command
Syntax: bc
Explanation: The bc (calculator) command offers an online calculator.
Example: ssmiet@ubuntu:~$ bc
Sample output: bc 1.06.94
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation,
Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
7+5*2
17
(50-10)*2
80 (Press CTRL + d to exit)
ssmiet@ubuntu:~$

5. who command
Syntax: who
Explanation: The ‘who’ command is used to display data about all the users who are currently
logged into the system.
Command line options
-a displays everything about all users
-b displays the date and time the systems was last
rebooted
-d displays all the dead process
-H displays verbose column headings
-m displays only your own statistics
-q gets only the number of users and their login names

Example: ssmiet@ubuntu:~$ who


Sample output: ssmiet tty7 2010-02-10 15:34 (:0)
ssmiet pts/0 2010-02-10 05:05 (:0.0)
ssmiet@ubuntu:~$
Example: ssmiet@ubuntu:~$ who –b
Sample output: system boot 2010-02-10 15:34
ssmiet@ubuntu:~$
Example: ssmiet@ubuntu:~$ who –q
Sample output: ssmiet ssmiet
# users=2
ssmiet@ubuntu:~$

6. who am i command
Syntax: who am i
Explanation: The “who am i” command displays a single line of output pertaining to the login details
of the user.
Example: ssmiet@ubuntu:~$ who am i
Sample output: ssmiet pts/0 2010-02-10 05:05 (:0.0)
ssmiet@ubuntu:~$

FILE COMMANDS

7. cat command

Syntax: cat >filename


Explanation: The cat command is used to create a new file.
Example: ssmiet@ubuntu:~$ cat > check1
Hello
(Press CTRL +d)
ssmiet@ubuntu:~$
Sample output: ssmiet@ubuntu:~$ cat check1
Hello
ssmiet@ubuntu:~$

8. cat command
Syntax: cat filename
Explanation: The cat command is also used to display the contents of a specified file.
Example: ssmiet@ubuntu:~$ cat check2 (check2 is an existing file)
Sample output: world
ssmiet@ubuntu:~$

9. cat command
Syntax: cat file1 file2 file3> file_out
Explanation: The cat command can also be used to concatenate multiple files into a single file.
Example: ssmiet@ubuntu:~$ cat check1 check2 > check3
Sample output: ssmiet@ubuntu:~$ cat check3
hello
world
ssmiet@ubuntu:~$
10. cp command
Syntax: cp old_file new_file
Explanation: The cp command is used to copy the contents of one file to another and
copied the file from one place to another.
Example: ssmiet@ubuntu:~$ cp test1.c test2.c
Sample output: ssmiet@ubuntu:~$ cat test1.c
int main()
{
printf(“This is a C file…”);;
}
ssmiet@ubuntu:~$

ssmiet@ubuntu:~$ cat test2.c


int main()
{
printf(“This is a C file…”);;
}
ssmiet@ubuntu:~$

11. mv command
Syntax: mv old_filename new_filename
Explanation: The mv command is used to move a file from one place to another. It removes a
specified file from its original location and places it in specified location.
Example: ssmiet@ubuntu:~$ mv test1.c test2.c
Sample output: Moves the contents of the file test1.c to test2.c

12. rm command
Syntax: rm filename
Explanation: The rm command is used to remove or erase an existing file.
Example: ssmiet@ubuntu:~$ rm test1.c
Sample output: ssmiet@ubuntu:~$ ls
a a.out check2 dma2.c examples.desktop factorial file1
hello reverse sumnat system.c unixcommand.c addition biggest
check3 dma3.c exec.c factorial.abw filechar.c Music sort sumodd

13. ls command
Syntax: ls
Explanation: The ls command is used to view the contents of a directory.
Example: ssmiet@ubuntu:~$ ls
Sample output: lists files and directories
Other examples: ssmiet@ubuntu:~$ ls -x
ssmiet@ubuntu:~$ ls -t
ssmiet@ubuntu:~$ ls –r
ssmiet@ubuntu:~$ ls -A
Options: -x (Displays multi-columnar output)
-t (Lists the files and subdirectories in time order)
-r (lists the files and subdirectories in reverse order)
-A (Lists almost all files)
-a (Lists all the files including hidden files)
14. wc command
Syntax: wc filename
Explanation: The wc command is used to count the number of words lines and characters in
a file.
Example: ssmiet@ubuntu:~$ wc check1
Sample output: 1 1 6 check1
ssmiet@ubuntu:~$
Options: -l (prints only the number of lines)
-w (prints only the number of words)
-c (prints only the number of characters)
DIRECTORY COMMANDS

15. mkdir command


Syntax: mkdir dirname
Explanation: The mkdir command is used to create an empty directory in a disk.
Example: ssmiet@ubuntu:~$ mkdir SSM
Sample output: ssmiet@ubuntu:~$ ls
a SSM1.txt biggest check2 Desktop Documents exec.c factorial.abw file1 fork.c Pictures
sort SSM2.txt SSM check check3 dma2.c

16. pwd command


Syntax: pwd
Explanation: The pwd command is provided to know the current working directory. pwd is the
abbreviation for “print working directory”.
Example : ssmiet@ubuntu:~$ pwd
Sample output: /home/ssmiet
ssmiet@ubuntu:~$
ssmiet@ubuntu:~$ cd check
ssmiet@ubuntu:~/check$

17. rmdir command


Syntax: rmdir dirname
Explanation: The rmdir command is used to remove a directory from the disk. Thecommand
deletes only empty directory.
Example: ssmiet@ubuntu:~$ rmdir SSM
Sample output: rmdir: failed to remove `SSM': Directory not empty
ssmiet@ubuntu:~$
ssmiet@ubuntu:~$ cd SSM
ssmiet@ubuntu:~/SSM$ rm f1
ssmiet@ubuntu:~/SSM$ cd
ssmiet@ubuntu:~$ rmdir SSM
ssmiet@ubuntu:~$
Sample output: ssmiet@ubuntu:~$ ls
a 1.txt biggest check2 Desktop Documents exec.c factorial.abw file1 fork.c Pictures
sort SSM2.txt SSM check check3 dma2.c

18. cd command
Syntax: cd dirname
Explanation: The cd command is used to move from one directory to another.
Example: ssmiet@ubuntu:~$ cd SSM

Sample output: ssmiet@ubuntu:~/SSM$ cd ..


ssmiet@ubuntu:~$

19. PATH command


Syntax: echo $PATH
Explanation: The PATH command is used to specify the current path of the OS. This
means that the sequence of directories the shell searches to look for a
command.
Example: ssmiet@ubuntu:~/check$ echo $PATH
Sample output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

FILE PERMISSIONS
20. chmod command

Syntax: chmod category operation permission file(s).


Where category is either user or group, others or all.
Operation is “assign or remove”
Permission is the type of permission

CATEGO OPERATION PERMISI


RY ON
U – + ASSIGN R – READ
USERS
G – -REMOVE W -
GROUP WRITE
O – = ASSIGN ABSOLUTELY X -
OTHERS EXECUTE
A - ALL

Explanation: The chmod command is used to set file permissions for various types of users.
Example 1: ssmiet@ubuntu:~$ chmod u-wx fibo.c

Sample output: ssmiet@ubuntu:~$ ls -l


total 456
-r--r--r-- 1 ssmiet ssmiet 203 2010-02-12 01:32 fibo.c

Example 2: ssmiet@ubuntu:~$ chmod u+rw,g+rw fibo.c

Sample Output: ssmiet@ubuntu:~$ ls -l


total 456
-rw-rw-r-- 1 ssmiet ssmiet 203 2010-02-12 01:32 fibo.c

FILTERS
21. head command
Syntax: head filename
Explanation: The head command displays the first ten lines of file.
Example: ssmiet@ubuntu:~$ head -5 test.c
Sample output: #include "stdio.h"
int main (void)
{
int age;
char name[32];
ssmiet@ubuntu:~$

22. tail command


Syntax: tail filename
Explanation: The head command displays the last ten lines of file.
Example 2: ssmiet@ubuntu:~$ tail -3 test2.c
Sample output: printf("\nYour name is %s and your age is %d\n\n",name,age);
return 0;
}
ssmiet@ubuntu:~$

23. sort command


Syntax: sort filename
Explanation: The sort command is used to sort the contents of a file
Example: ssmiet@ubuntu:~$ sort animals
Sample output: elephant
horse
lion
tiger
zebra
ssmiet@ubuntu:~$
PATTERN SEARCHING
24. grep command
Syntax: grep [options] pattern files
Explanation: The grep command is used to search and print specified patterns from a file.
Example: ssmiet@ubuntu:~$ cat > student
101 arun cse
102 bala ece
103 chandru cse
104 darshan cse
105 gopal ece
106 harsha eee
(Press CTRL + d)
ssmiet@ubuntu:~$

ssmiet@ubuntu:~$ grep cse student

Sample output: 101 arun cse


103 chandru cse
104 darshan cse
ssmiet@ubuntu:~$

TRANSFERING DATA BETWEEN DEVICES/USERS

25. mesg command


Syntax: mesg y
mesg n
Explanation: The mesg command is used to give permissions to other users to send
message to your terminal.
Example: mesg y
Sample output: $ mesg y – This command allows others to send message to you.
$ mesg n – This command denies others to send message to you.
$“who –t” is used to find the terminals being used by all the users.
$“who –t” also displays the permissions of all the users.

26. write command


Syntax: write user_name
Explanation: The write command is used to communicate with the users that are logged
in at the same time.
Example: $write user2
Hai, I have finished my work. Have you?
CTRL-D

Sample output: $write user2


Hai, I have finished my work. Have you?
CTRL-D

27. wall command


Syntax: wall message
Explanation: The wall command is used to send message to all the users who are currently
logged on and have permissions to receive messages.
Example: $wall
After 5PM the system will not be available
(CTRL-D)
$
Sample output: The above command displays if any error is created. If there is no error
generated the shell prompt returns back.

RESULT
Thus the basic shell commands in UNIX is studied and verified successfully.
Ex. No: 2a IMPLEMENTATION OF SYSTEM CALLS USING UNIX OS
Date:

AIM:

To write a program to invoke fork() and getpid() system calls in UNIX.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare pid as integer.
STEP 3: Create the process using fork() command.
STEP 4: Check
i. If pid is less than 0, then print error
ii. Else if pid is equal to 0 then print child process ID and display Parent process ID using getpid() and
getppid() commands.
iii. Else, print parent process and display Parent process ID using getpid() and getppid() commands.
STEP 5: Stop the program.

PROGRAM:
File name: vi pc.c
void main()
{
int pid;
pid=fork();
if(pid<0)
{
printf(“Error in Process Creation”);
exit(-1);
}
if(pid==0)
{
printf(“Child process”);
printf(“Child Process ID = “,getpid());
printif(“Parent Process ID = “,getppid());
exit(0);
}
else
{
printf(“Parent Process”);
printf(“Parent Process ID = “,getpid());
exit(0);
}
}

OUTPUT:
$cc pc.c
$.\a.out

RESULT:
Thus the program to invoke the fork(), getpid() and getppid() system calls is written, executed and
verified successfully.
Ex. No: 2b Implementing readdir(), opendir(), exit() system calls
Date:

AIM:
To write a program to invoke readdir(), opendir(), exit() system calls in UNIX.

ALGORITHM:
1. Start the program
2. Declare the variable to the structure dirent (defines the file system-independent directory) and also
for DIR
3. Specify the directory path to be displayed using the opendir system call
4. Check for the existence of the directory and read the contents of the directory using readdir system
call (returns a pointer to the next active directory entry)
5. Repeat the above step until all the files in the directory are listed
6. Stop the program

Program
#include<stdio.h>
#include<dirent.h>
main()
{
DIR *p;
struct dirent *dp;
p=opendir("."); //p=opendir("./shantha");
if(p==NULL)
{
perror("opendir");
exit(0);
}
dp=readdir(p);
while(p!=NULL)
{
printf("%d%s\n",dp->d_ino,dp->d_name);
dp=readdir(p);
}
}
Output:
"di.c" [New] 21L, 239C written
[test1@localhost test1]$ cc di.c
[test1@localhost test1]$ ./a.out
1049278.
442373..
1049279.kde
1049364.aa.c.swp
1049285.balan.sh.swp
1049387ar.sh
1049404firstfit.c
1049403wai.c
1049406sta1.c
1049405di.c

RESULT:
Thus the program to invoke readdir(), opendir(), exit() system calls in UNIX is written, executed and
verified successfully.
EX.NO: 2c UNIX I/O SYSTEM CALLS - STAT, OPEN, CLOSE, EXIT
Date:

AIM
To implement UNIX I/O system calls open, read , write etc.

ALGORITHM
1. Create a new file using creat command (Not using FILE pointer).
2. Open the source file and copy its content to new file using read and write command.
3. Find size of the new file before and after closing the file using stat command.

PROGRAM
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
main()
{
int fd1,fd2,n;
char source[30],ch[5];
struct stat s,t,w;
fd1=creat("test.txt",0644);
printf("Enter the file to be Copied\n");
scanf("%s",source);
fd2=open(source,0);
if(fd2==-1)
{
perror("file doesnot exist");
exit(0);
}
while((n=read(fd2,ch,1))>0)
write(fd1,ch,n);

close(fd2);
stat(source,&s);
printf("Source file size=%d\n",s.st_size);

fstat(fd1,&t);
printf("Destination File size before closing=%d\n",t.st_size);
close(fd1);
fstat(fd1,&w);
printf("Destination File Size after closing=%d\n",w.st_size);
}

Sample Output:
[test1@localhost test1]$ cc sta1.c
[test1@localhost test1]$ ./a.out
Enter the file to be Copied
sta1.c
Source file size=670
Destination File size before closing=670
Destination File Size after closing=3
[test1@localhost test1]$

RESULT:
Thus the program to implement UNIX I/O system calls open, read , write system calls in UNIX is written,
executed and verified successfully.
EX: NO: 3.a IMPLEMENTATION OF UNIX COMMANDS USING C
Date:

AIM
To simulate the UNIX command ls.

ALGORITHM
1. Import dir.h header file
2. Create directory structure variable
3. Using findfirst and findnext methods display the files available in the current directory.

PROGRAM
#include <stdio.h>
#include <dir.h>
#include<conio.h>
char dir[MAXDIR];
void main(void)
{
struct ffblk ffblk;
int done;
clrscr();
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}
getch();
}

RESULT
Thus the program to implement UNIX I/O system calls open, read , write system calls is written, executed
and verified successfully.
Exp. No. :3b IMPLEMENTATION OF UNIXCOMMAND: GREP
Dat:

AIM: To simulate the UNIX command grep.

ALGORITHM:
1. Create a file with some content.
2. Get any one pattern / string as a input
3. Read file content as strings and compare it with the input pattern
4. If pattern match with file string or part of the string then print that string / line.
5. Close the file.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fptr;
char ch;
int i;
char p[10], a[50];
clrscr();
fptr=fopen("input.txt","w");
printf("Enter the data to be stored in the file\n");
scanf("%c",&ch);
while(ch!='$')
{
fprintf(fptr,"%c",ch);
scanf("%c",&ch);
}
fclose(fptr);
printf("Enter the pattern to be searched");
scanf("%s",p); Sample Output:
fptr=fopen("input.txt","r"); Enter the data to be stored in
i=0; the file
while(!feof(fptr)) India
{ Country
ch=getc(fptr); welcome
if( ch!='\n') sit
a[i] = ch; well
else $
{ Enter the pattern to be
a[i]='\0'; searched
if(strnicmp(a,p,strlen(p))==0) wel
printf("%s\n",a); welcome
i=-1; well
}
i++;
}
fclose(fptr);
getch();
}

RESULT:
Thus the program to implement UNIX GREP omand is written, executed and verified successfully.
SHELL PROGRAMMING

EX NO: 4.a FINDING THE BIGGEST OF THREE NUMBERS


DATE:

AIM:
To write a shell script to find the biggest of three numbers

ALGORITHM:
Step 1: Start the program.
Step 2: Get three numbers, a, b and c.
Step 3: Check if a greater than b and c. If yes, print a is the biggest number and go to step 6. Otherwise, go
to next step.
Step 4: Check if b greater than a and c. If yes, print b is the biggest number and go to step 6. Otherwise, go
to next step.
Step 5: Check if c greater than a and b. If yes, print c is the biggest number and go to step 6. Otherwise, go
to next step.
Step 6: Stop the program.

PROGRAM:
echo Enter the Three Numbers
read a
read b
read c
if test $a -gt $b
then if test $a -gt $c
then
echo "A is the biggest"
fi
elif test $c -gt $b
then
echo "C is biggest"
elif test $b -gt $c
then
echo " B is the Biggest"
else echo"C is the biggest"
fi

OUTPUT:
acet@ubuntu:~$ sh biggest

Enter the Three Numbers


10
20
30
C is biggest

RESULT:
Thus the shell script is written and executed to find the biggest of three numbers.
EX NO: 4.b CHECKING THE NUMBER FOR ODD OR EVEN
DATE:

AIM:
To write a shell script to check whether the given number is odd or even

ALGORITHM:
Step 1: Start the program.
Step 2: Input the number, n.
Step 3: Get the remainder by dividing the number by 2, using modulo division operation.
Step 4: Check if the remainder is 0, then the given number is even. Otherwise the given number is odd.
Step 5: Print the result.
Step 6: Stop the program.

PROGRAM:
echo "Enter number : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

OUTPUT:
acet@ubuntu:~$ sh oddeven.sh

Enter number :
7
7 is odd number

RESULT:
Thus the shell script is written and executed for checking the given number is even or odd.
EX NO:4.c SORTING THE CONTENTS OF A FILE
DATE:

AIM:
To write a shell script to sort the contents of a file

ALGORITHM:
Step 1: Start the program.
Step 2: Enter the filename to be sorted.
Step 3: Display the contents of the file before sorting.
Step 4: Sort the contents using sort command.
Step 5: Display the contents of the file after sorting.
Step 6: Stop the program.

PROGRAM:
echo Sorting the contents of a file
echo enter the file name
read fn
echo
echo The contents of the file before sorting
cat $fn
echo
echo The result of sorting
sort $fn
echo

OUTPUT:
acet@ubuntu:~$ sh sort.sh

Sorting the contents of a file


enter the file name
sample

The contents of the file before sorting


tiger
lion
cat

The result of sorting


cat
lion
tiger

RESULT: Thus the shell script is written and executed to sort the contents of a file.
EX NO:4.d PERFORMING ARITHMETIC OPERATIONS USING CASE STATEMENT
DATE:
AIM:
To write a shell script to perform arithmetic operations using case statement.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the option and the operators, a and b.
Step 3: Evaluate the expression based on the user option.
a) If the operator is ‘+’, then perform a + b.
b) If the operator is ‘-’, then perform a - b.
c) If the operator is ‘*’, then perform a * b.
d) If the operator is ‘/’, then perform a / b.
e) Else, by default option, display that the option is invalid.
Step 4: Print the results.
Step 5: Stop the program.
PROGRAM:
echo Arithmetic operations using case in Shell
echo MENU
echo "1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit"
echo Choose the option
read op
while [ $op -lt 5 ]
do
echo Enter the two numbers
read a
read b
case $op in
1) c=`expr $a + $b`;;
2) c=`expr $a - $b`;;
3) c=`expr $a \* $b`;;
4) c=`expr $a / $b`;;
5) exit
esac
echo Value of c is $c
echo Choose the option again:
read op
done
OUTPUT:
acet@ubuntu:~$ sh arithmetic
Arithmetic operations using case esac in Shell
MENU
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Choose the option 1
Enter the two numbers 4
7
Value of c is 11

RESULT:
Thus the shell script is written and executed to perform arithmetic operations using case statement.
EX NO: 4.e CALCULATING THE SUM OF ‘n’ NUMBERS
DATE:

AIM:
To write a shell script to find the sum of numbers up to ‘n’

ALGORITHM:
Step 1: Start the program.
Step 2: Input limit, n.
Step 3: Assign i = 1 and sum = 0.
Step 4: Check if i is lesser than n, then calculate sum = sum + i and increment the i value by 1.
Step 5: Repeat step 4.
Step 6: Print the sum value.
Step 7: Stop the program.

PROGRAM:
echo Finding the Sum of first 'n' Odd Numbers
echo Enter the Value of n
read n
sum=0
incr=1
i=1
while test $i -le $n
do
sum=`expr $sum + $incr`
i=`expr $i + 1`
incr=`expr $incr + 1`
done
echo Sum of the first $n Odd Numbers is $sum

OUTPUT:
acet@ubuntu:~$ sh sumodd

Finding the Sum of first n Odd Numbers


Enter the Value of n
5
Sum of the first 5 Odd Numbers is 25

RESULT:
Thus the shell script is written and executed for finding the sum of numbers up to ‘n’.
EX NO: 4.f GENERATING FIBONACCI SERIES
DATE:

AIM:
To write a shell script to generate the Fibonacci series up to the given limit

ALGORITHM:
Step 1: Start the program.
Step 2: Input number, n.
Step 3: Assign a = 0, b = 1 and print a and b values.
Step 4: Assign i = 2.
Step 5: Check if i less than n, then go to step 6, otherwise go to step 9.
Step 6: Calculate c = a + b, increment the i value by 1, and assign a = b, b = c.
Step 7: Print the value of c.
Step 8: Repeat steps 5 to 7.
Step 9: Stop the program.

PROGRAM:
echo Fibonacci Series generation
echo Enter the range/stopping limit:
read n
a=0
b=1
echo FIBONACCI SERIES
echo $a
echo $b
i=2
while [ $i -lt $n ]
do
c=`expr $a + $b`
echo $c
i=`expr $i + 1`
a=$b
b=$c
done

OUTPUT:
acet@ubuntu:~$ sh fibonacci
Fibonacci Series generation
Enter the range/stopping limit:
13
FIBONACCI SERIES
0
1
1
2
3
5
8
13
21
34
55
89
144

RESULT:
Thus the shell script is written and executed to generate the Fibonacci series up to the given limit.
CPU SCHEDULING ALGORITHMS

Ex. No.: 5(a) IMPLEMENTATION OF FCFS SCHEDULING


Date:

AIM
To write a program for first come first serve (FCFS) scheduling algorithm.
ALGORITHM
1. Declare and Initialize the variables.
2. Get the number of process, its burst time and arrival time.
3. Calculate the average turnaround time and waiting time of each process.
1. Twt=Twt+(Wt[i]-A[i]);
2. Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
3. Att=(float)Ttt/n;
4. Awt=(float)Twt/n;
4. Display the results

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float bt[20],wt[20],tat[20],avwt=0,avtat=0;
int n,i,j;
clrscr();
printf("\n\n*** FIRST COME FIRST SERVED SCHEDULING ALGORITHM ***\n\n");
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%f",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
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");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%.f\t\t%.f\t\t%.f",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%f",avwt);
printf("\nAverage Turnaround Time:%f",avtat);
getch();
}

OUTPUT

RESULT
Thus the program to implement the FCFS (First Come First Serve) scheduling Algorithm was
written, executed and the output was verified successfully.
Ex. No.: 5b IMPLEMENTATION OF SJF SCHEDULING
Date:

AIM
To write a program for shortest job first (SJF) scheduling algorithm.

ALGORITHM
1. Declare and Initialize the variables.
2. Get the number of process and its burst time.
3. Re-arrange the burst times using “BUBBLE SORT” in ascending order.
4. Calculate the average turnaround time and waiting time of each process.
 Twt=Twt+(Wt[i]-A[i]);
 Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
 Att=(float)Ttt/n;
 Awt=(float)Twt/n
5. Display the results.

PROGRAM
SJF SCHEDULING
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
clrscr();
printf("\n\n*** SHORTEST JOB FIRST SCHEDULING ALGORITHM ***\n\n");
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("Process[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
getch();
}
OUTPUT

RESULT
Thus the program to implement the SJF (Shortest Job First) scheduling Algorithm was written,
executed and the output was verified successfully.
Ex. No.: 5c IMPLEMENTATION OF PRIORITY SCHEDULING
Date:

AIM
To write the program to perform priority scheduling.
ALGORITHM
1. Get the number of processes, their burst time and priority.
2. Initialize the waiting time for process 1 is 0.
3. Based upon the priority processes are arranged.
4. The waiting time and turnaround time for other processes are calculated as
 twait=twait+wait[i];
 totl=totl+tta;
5. The waiting time and turnaround time for all the processes are summed and then the average
waiting time and turnaround time are calculated.
 wavg=twait/n;
 tavg=totl/n;
6. The average waiting time and turnaround time are displayed.

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
int tot_wait,tot_tunr;
clrscr();
printf("\n\n*** PRIORITY SCHEDULING ALGORITHM ***\n\n");
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("P[%d]",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
tot_wait=total;
total=0;
printf("\nProcess\t Priority\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t %d\t\t\t%d",p[i],pr[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
tot_tunr=total;
printf("\nTotal Waiting Time=%d",tot_wait);
printf("\nAverage Waiting Time=%.2f",avg_wt);
printf("\nTotal Turnaround Time=%d",tot_tunr);
printf("\nAverage Turnaround Time=%.2f",avg_tat);
getch();
}
OUTPUT

Result
Thus the program to implement Priority scheduling Algorithm was written, executed and the output
was verified successfully.
Ex. No.: 5d IMPLEMENTATION OF ROUND ROBIN SCHEDULING
Date:

AIM
To write a program to implement the Round Robin (RR) CPU scheduling.
ALGORITHM
1. Get the number of Processes
2. Get the value for burst time for individual processes
3. Get the value for time quantum
4. Make the CPU scheduler go around the ready queue allocating CPU to each process for the time
interval specified
5. Make the CPU scheduler pick the first process and set time to interrupt after quantum. And after
it's expiry dispatch the process
6. If the process has burst time less than the time quantum then the process is released by the CPU
7. If the process has burst time greater than time quantum then it is interrupted by the OS and the
process is put to the tail of ready queue and the schedule selects next process from head of the
queue
8. Calculate the total and average waiting time and turnaround time and display the results

PROGRAM
#include<stdio.h>
#include<conio.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];
clrscr();
printf("\n\n*** ROUND ROBIN SCHEDULING ***\n\n");
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Process[%d]: Arrival & Burst Time : ",count+1);
scanf("%d%d",&at[count],&bt[count]);
rt[count]=bt[count];
}
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;
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);
getch();
return 0;
}

OUTPUT

RESULT
Thus the program to implement Priority scheduling Algorithm was written, executed and the output
was verified successfully
Ex. No.: 6 IMPLEMENTATION OF SEMAPHORE(PRODUCER - CONSUMER PROBLEM)
Date:

AIM
To implement the producer – consumer problem using semaphores
ALGORITHM
1. Declare the variables in the type of pthread_t as tid_producetid_consume.
2. Declare a structure for semaphore variables.
3. During run time read the number of items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer function.

PROGRAM
#include<stdio.h>
#include<conio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
clrscr();
printf("\n*** PRODUCER CONSUMER PROBLEM USING SEMAPHORES ***\n");
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
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;
}
}
getch();
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
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("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}

OUTPUT

Result:
Thus, the program to implement producer - consumer problem using semaphores was executed.
Ex. No.: 7 IMPLEMENTATION OF SHARED MEMORY AND IPC
Date:

AIM
To write a program for inter-process communication using shared memory.

ALGORITHM
1. Create the child process using fork()
2. Create the shared memory for parent process using shmget() system call
3. Now allow the parent process to write inn shared memory using shmpet pointer which is return type of
shmat()
4. Now across and attach the same shared memory to the child process
5. The data in the shared memory is read by the child process using the shnot pointer
6. Now, detach and rebase the shared memory

PROGRAM
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child,shmid,i;
char * shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,IPC_CREAT|0666);
shmptr=shmat(shmid,0,0);
printf(“\n Parent writing\n”);
for(i=0;i<10;i++)
{
shmptr[i]=’a’+i;
putchar(shmptr[i]);
}
printf(“\n\n %s”, shmptr);
wait(NULL);
}
else OUTPUT:
{
shmid=shmget(2041,32,0666); [cse2@localhost ~]$ cc share.c
shmptr=shmat(shmid,0,0); [cse2@localhost ~]$ ./a.out
printf(“\n Child is reading\n”);
for(i=0;i<10;i++) Parent writing
putchar(shmptr[i]); abcdefghij
shmdt(NULL); Child is reading
shmctl(shmid,IPC_RMID,NULL); abcdefghij
}
return 0;
}

Result
Thus the inter-process communication using shared memory was successfully executed.
Ex. No.: 8 BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE
Date:

AIM:
To implement deadlock avoidance by using Banker’s Algorithm.
ALGORITHM:
1. Get the values of resources and processes.
2. Get the avail value.
3. After allocation find the need value.
4. Check whether its possible to allocate.
5. If it is possible then the system is in safe state.
6. Else system is not in safety state.
7. If the new request comes then check that the system is in safety or not if we allow the request.

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("\n*** BANKER'S ALGORITHM FOR DEADLOCK AVOIDANCE ***\n");
printf("Enter number of processes : ");
scanf("%d",&n);
printf("Enter number of resources : ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t : \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t : \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t : \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details : ");
printf("\nEnter pid \t : \t");
scanf("%d",&id);
printf("Enter Request for Resources \t : \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED : DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is : (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}

OUTPUT

Result:
Thus the program to implement bankers algorithm for dead lock avoidance was executed
successfully.
Ex. No.: 9 IMPLEMENTATION OF DEADLOCK DETECTION ALGORITHM
Date:

AIM
To write a program to implement the scenario of deadlock detection algorithm.

ALGORITHM
1. Declare the necessary variables.
2. Get the number of processes and resources from the user.
3. Prompt user to enter claim(Max. Need) matrix.
4. Get the allocation matrix and the resource vector.
5. Ask user to enter the availability vector.
6. Finally, Display the deadlock causing processes.
7. End the program by getting an input from the user using getch() function.

PROGRAM

#include <stdio.h>
#include <conio.h>
void main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5];
int i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("\n*** DEADLOCK DETECTION ALGORITHM***\n");
printf("Enter total no of processes : ");
scanf("%d",&tp);
printf("Enter total no of resources : ");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
scanf("%d",&r[i]);
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
sum+=p[i][j];
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("Deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
OUTPUT

Result
Thus the program for the implementation of deadlock detection has been written, executed and verified
successfully.
Ex. No.: 10 IMPLEMENTATION OF THREADING AND SYNCHRONIZATION APPLICATION
Date:

AIM
To write a program for Dinning Philosopher problem using c program.

ALGORITHM
1. Get the number of philosophers from the user.
2. Initialize the philosophers name as integers starting from 1 to total number of them.
3. Set the status flag as 1 for all the philosophers.
4. Get how many are hungry at the time.
5. If the given value is equal to the number of philosophers, display a deadlock has occurred and exit from
the program.
6. If the given value is less than the total numbers, then ask the option and give solution accordingly.
7. End the program by choosing the exit option.
PROGRAM
#include<stdio.h>
#include<conio.h>
int tph, philname[20], status[20], howhung, hu[20], cho;
void main()
{
int i;
clrscr();
printf("\n*** DINING PHILOSOPHER PROBLEM ***\n");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
exit(0);
default:
printf("\nInvalid option..");
}
}while(1);
}
getch();
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
return;
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
return;
}
OUTPUT

Result
Thus a program using threads and synchronization was written, executed and verified successfully.
EX. NO.: 11.a FIRST FIT ALLOCATION
Date:

Aim
To allocate memory requirements for processes using first fit allocation.

First fit
 Allocate the first hole that is big enough.
 Searching starts from the beginning of set of holes.

Algorithm
1. Declare structures hole and process to hold information about set of holes and processes respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
b. Otherwise check the next from the set of hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.

Program
#include <stdio.h>
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int size;
int actual;
} h[10];
void main()
{
int i, np, nh, j;
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
{
for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1;
p[i].holeid = j;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole"); Output
for(i=0; i<np; i++) Enter the number of Holes : 5
{ Enter size for hole H0 : 100
if(p[i].flag != 1) Enter size for hole H1 : 500
printf("\nP%d\t%d\tNot allocated", i, p[i].size); Enter size for hole H2 : 200
else Enter size for hole H3 : 300
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid); Enter size for hole H4 : 600
} Enter number of process : 4
printf("\n\nHole\tActual\tAvailable"); enter the size of process P0 : 212
for(i=0; i<nh ;i++) enter the size of process P1 : 417
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size); enter the size of process P2 : 112
printf("\n"); enter the size of process P3 : 426
}
First fit
Process PSize Hole
P0 212 H1
P1 417 H4
P2 112 H1
P3 426 Not allocated

Hole Actual Available


H0 100 100
H1 500 176
H2 200 200
H3 300 300
H4 600 183

Result
Thus the C program to implement first fit allocation method is written and executed
.EX. NO.: 11.b. BEST FIT ALLOCATION
Date:

Aim
To write a C program to allocate memory requirements for processes using best fit allocation.

Best fit
 Allocate the smallest hole that is big enough.
 The list of free holes is kept sorted according to size in ascending order.
 This strategy produces smallest leftover holes

Algorithm
1. Declare structures hole and process to hold information about set of holes and processes respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.

Program
#include <stdio.h>
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int hid;
int size;
int actual;
} h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
{
bsort(h, nh);
for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1;
p[i].holeid = h[j].hid;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,h[i].size);
printf("\n");
getch();
}
void bsort(struct hole bh[], int n)
{
struct hole temp;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(bh[i].size > bh[j].size)
{
temp = bh[i];
bh[i] = bh[j];
bh[j] = temp;
}
}
}
}

Output
Enter the number of Holes : 5
Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for hole H3 : 300
Enter size for hole H4 : 600
Enter number of process : 4
enter the size of process P0 : 212
enter the size of process P1 : 417
enter the size of process P2 : 112
enter the size of process P3 : 426

Best fit
Process PSize Hole
P0 212 H3
P1 417 H1
P2 112 H2
P3 426 H4

Hole Actual Available


H1 500 83
H3 300 88
H2 200 88
H0 100 100
H4 600 174

Result
Thus the C program to allocate memory using best fit method is written and implemented successfully.
.EX. NO.: 11c WORST FIT ALLOCATION
Date:

Aim
To write a C program to allocate memory requirements for processes using worstt fit allocation.

Algorithm
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the maximum block size that can be assigned to current process
i.e., find max(bockSize[1], blockSize[2],.....blockSize[n]) > processSize[current],
4. if found then assign it to the current process.
5- If not then leave that process and keep checking the further processes

#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First 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]);
}
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();
}

OUTPUT
Enter the number of blocks: 3
Enter the number of files: 2

Enter the size of the blocks:-


Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

File No File Size Block No Block Size Fragment


1 1 1 5 4
2 4 3 7 3

Result
Thus the C program to allocate memory using wodst fit method is written and implemented
successfully.
EX.NO.: 12 IMPLEMENTATION OF PAGING TECHNIQUE OF MEMORY MANAGEMENT
DATE.:

Aim:
To write a C program to implement Paging technique of memory management.

Algorithm:
Step1 : Start the program.
Step2 : Read the page size, number of pages.
Step 3: Get the frame number of each page.
Step4 : Create the page table with the number of pages and page address.
Step5: Get the logical address and calculate
i. frameno=logadd / pagesize
ii. offset=logadd % pagesize
iii. phyadd=(p[frameno]*pagesize)+offset;
Step 6: Display the physical address.
Step7 : Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
OUTPUT
int i;
int choice=0;
printf("\nYour mem size is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
getch();
}

Result:
Thus the C program to implement Paging technique of memory management is written and executed.
Ex. No.: 13a IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - FIFO
Date:

AIM:
To write a c program to implement FIFO(First In First Out) page replacement algorithm
ALGORITHM:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
clrscr();
printf("\n*** PAGE REPLACEMENT ALGORITHM - FIFO ***\n");
printf("\n Enter the length of reference string : ");
scanf("%d",&n);
printf("\n Enter the reference string : ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames : ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is : \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();
}
OUTPUT

Result
Thus a c program to implement FIFO(First In First Out) page replacement algorithm has been written,
executed and verified successfully.
Ex. No.: 13b IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - LRU
Date:

AIM
To write a c program to implement LRU (Least Recently Used) page replacement algorithm
ALGORITHM
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25];
int n, f, pf=0, next=1;
clrscr();
printf("\n*** PAGE REPLACEMENT ALGORITHM - LRU ***\n");
printf("Enter the length of reference string : ");
scanf("%d",&n);
printf("Enter the reference string : ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames : ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. : %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}
OUTPUT

Result
Thus a c program to implement LRU(Least Recently Used) page replacement algorithm has been written,
executed and verified successfully.
Ex. No.: 13c IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM - LFU
Date:

AIM
To write a c program to implement LFU page replacement algorithm
ALGORITHM
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least frequently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\n*** PAGE REPLACEMENT ALGORITHM - LFU ***\n");
printf("\nEnter number of page references ");
scanf("%d",&m);
printf("\nEnter the reference string : ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames : ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
printf("\nThe Page Replacement Process is \n");
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf("\tPF No. %d",pf);
}
printf("\n\n Total number of page faults : %d",pf);
getch();
}

OUTPUT

Result
Thus a c program to implement LFU(Least Frequently Used) page replacement algorithm has been
written, executed and verified successfully.
FILE ORGANIZATION TECHNIQUE
Ex. No.: 14a SINGLE LEVEL DIRECTORY
Date:

AIM
To implement Single level directory structure in C.

ALGORITHM
Step 1:Get the name of the directory.
Step 2: Display the different options..
Step 3: Create file option is used to create file in the specified directory.
Step 4: Delete file option is used to delete the specified file from the directory.
Step 5: Display option is used to list the file names in that directory.

PROGRAM
#include<stdio.h>
#include<conio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory : ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1.Create File\t2.Delete File\t3.Search File\n4.Display Files\t5.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of the file :");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file : ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt:;
break;
case 3:
printf("\nEnter the name of the file : ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4:
if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are : ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default:
exit(0);
}
}
getch();
}

OUTPUT

RESULT
Thus the program to implement single level directory was executed successfully.
Ex. No.: 14b TWO LEVEL DIRECTORY - FILE ORGANIZATION TECHNIQUE
Date:

AIM
To implement Two-level directory structure in C.
ALGORITHM
1. Start the program
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>

struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
printf(“\n*** TWO LEVEL FILE ORGANIZATION ***\n”);
dir.fcnt = 0;
printf("\nEnter name of directory : ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1.Create File\t2.Delete File\t3.Search File\n4.Display Files\t5.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of the file :");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file : ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt:;
break;
case 3:
printf("\nEnter the name of the file : ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4:
if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are : ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default:
exit(0);
}
}
getch();
}

OUTPUT

RESULT
Thus the program to implement two level directory was executed successfully.
Ex. No.: 14c HIERARCHIAL DIRECTORY - FILE ORGANIZATION TECHNIQUE
Date:

AIM
To implement hierarchical directory structure in C.
ALGORITHM
1. Start the program
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop the program.

PROGRAM
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i, gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}
}

OUTPUT

RESULT
Thus the program to implement hierarchical directory was executed successfully.
FILE ALLOCATION STRATEGIES
Ex. No.: 15a SEQUENTIAL FILE ALLOCATION
Date:

AIM
Write a C Program to implement Sequential File Allocation method.
ALGORITHM
Step 1: Start the program.
Step 2: Initialize the file flag to 1 for all the blocks available.
Step 3: Get the starting block and length of block from the user.
Step 4: For the given file length, allocate the file flag as 1.
Step 5:When the given input is already available, then display the message that the block is already
allocated.
Step 6: Get the choice from user whether to repeat the process or exit.
Step 7:End the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int f[50],i,st,j,len,c,k;
clrscr();
printf("\n*** SEQUENTIAL FILE ALLOCATION TECHNIQUE ***\n");
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\nEnter the starting block & length of file : ");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
}
if(j==(st+len))
printf("\nThe file is allocated to disk");
printf("\nPress 1 to continue and 0 to exit : ");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}

OUTPUT

Result
Thus the program for Sequential File Allocation method was executed and verified successfully.
Ex. No.: 15b INDEXED FILE ALLOCATION
Date:

AIM
Write a C Program to implement Indexed File Allocation method.
ALGORITHM
Step 1: Start the program.
Step 2: Initialize the file flag to 1 for all the blocks available.
Step 3: Get the index block.
Step 4: Allocate the file flag for index as 1.
Step 5: Get the number of files on index and the block numbers.
Step 6: Assign the file flag for given block as 1 if its value is not equal to 1.
Step 7: If the flag is 1, then display the message that the block is already allocated.
Step 6: Get the choice from user whether to repeat the process or exit.
Step 7:End the program.

PROGRAM
#include<stdio.h>
#include<conio.h>

void main()
{
int f[50],i,k,j,index[50],n,c,p;
clrscr();
printf(“\n**** INDEXED FILE ALLOCATION TECHNIQUE ***\n”);
for(i=0;i<50;i++)
f[i]=0;
x:
printf("\nEnter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("\nEnter no of files on index\t");
scanf("%d",&n);
printf("\nEnter the file block numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&index[i]);
if(f[index[i]]!=1)
f[index[i]]=1;
else
{
printf("Block Id is already allocated",index[i]);
goto x;
}
}
for(j=0;j<n;j++)
printf("\n%d-->%d,%d",p,index[j],f[index[j]]);
goto y;
}
else
{
printf("\nThe block is already allocated");
}
y:
printf("\nPress 1 to continue and 0 to exit");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
}

OUTPUT

RESULT
Thus the program for Indexed File Allocation method was executed and verified successfully.
Ex. No.: 15c LINKED FILE ALLOCATION
Date:

AIM
Write a C Program to implement Linked File Allocation method.
ALGORITHM
Step 1: Start the program
Step 2: Get the number of files.
Step 3: For all files, Get the name.
Step 4: Get the number of blocks that are linked to the file.
Step 5: Get the block numbers.
Step 6: Display the files along with the blocks linked to the files.
Step 7: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
void main()
{
int i,j,n;
clrscr();
printf("\n*** LINKED FILE ALLOCATION STRATEGY ***\n");
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
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("\n\nFile\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=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();

OUTPUT

RESULT
Thus the program for Linked File Allocation method was executed and verified successfully

You might also like