100% found this document useful (1 vote)
75 views130 pages

AL3452 - OS Lab Record

The document outlines the Operating System Laboratory course (AL3452) for the Department of Artificial Intelligence and Data Science at Agni College of Technology for the academic year 2023-2024. It includes a bonafide certificate template for students, an index of practical exercises, and detailed procedures for installing Windows 10 and using basic UNIX commands. The document serves as a practical guide for students to complete their laboratory work and prepare for examinations.

Uploaded by

22aids057
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
100% found this document useful (1 vote)
75 views130 pages

AL3452 - OS Lab Record

The document outlines the Operating System Laboratory course (AL3452) for the Department of Artificial Intelligence and Data Science at Agni College of Technology for the academic year 2023-2024. It includes a bonafide certificate template for students, an index of practical exercises, and detailed procedures for installing Windows 10 and using basic UNIX commands. The document serves as a practical guide for students to complete their laboratory work and prepare for examinations.

Uploaded by

22aids057
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/ 130

AGNI COLLEGE OF TECHNOLOGY

(Approved by AICTE & Affiliated to Anna University)

Off Old Mahabalipuram Road,Thalambur,Chennai - 600130

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

AL3452 – OPERATING SYSTEM LABORATORY

ACADEMIC YEAR
2023-2024
EVEN SEMESTER

NAME OF THE STUDENT :


REGISTER NUMBER :
COURSE :
YEAR :

SEMESTER :
Department of AI&DS AGNI COLLEGE OF TECHNOLOGY

AGNI COLLEGE OF TECHNOLOGY


(Approved by AICTE& Affiliated to Anna University) Old Mahabalipuram Road,

Thalambur, Chennai – 600 130

BONAFIDE CERTIFICATE

Register Number :

Name of the lab : AL3452 – OPERATING SYSTEM LABORATORY

Department : Artificial Intelligence and Data Science

Certified that this is a Bonafide Record of Practical Work done by


Mr./Ms.………………………………………………………………………………………....
of Artificial Intelligence and Data Science Department Fourth semester in the
AL3452 – Operating System Laboratory during the year 2023-2024.

Signature of Lab-in-Charge Signature of Head of the Department

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

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

S.NO DATE NAME OF THE PROGRAM PAGE MARKS SIGN


NO
1. Installation of Windows Operating System

2. Illustrate UNIX commands and Shell Programming

3. Process Management using System Calls : Fork, Exit,


Getpid, Wait, Close

4. Write C programs to implement the various CPU


Scheduling Algorithms

5. Illustrate the inter process communication strategy

6. Implement mutual exclusion by Semaphore

7. Write C programs to avoid DeadlockusingBanker's


Algorithm

8. Write a C program to Implement Deadlock Detection


Algorithm
9. Write C program to implement Threading

10. Implement the paging Technique using C program

11. Write C programs to implement the following


Memory Allocation Methods
a) First Fit b) Worst Fit c) Best Fit
12. Write C programs to implement the various Page
Replacement Algorithms
13. Write C programs to Implement the various File
Organization Techniques
14. Implement the following File Allocation Strategies
using C Programs
a) Sequential b) Indexed c) Linked
15. Write C programs for the implementation of various
disk scheduling algorithms
EX NO: 1 Installation of Windows Operating System

AIM: To install Windows 10 Operating system in our computer

PROCEDURE:

Let's start to install a clean and fresh Windows 10. Follow the steps given below:

o To perform a clean installation of Windows 10, download the free official

Windows 10 Installation media from Microsoft.

o Download it from here:

https://www.microsoft.com/en-us/software-download/windows10startfresh

o Click on the "Download Tool Now" button.

o You can see that the tool is downloaded now. Now, run the downloaded file.

o Accept the license term. Now, you will be asked to "Give your PC a fresh start."
o Choose any one of the options given below. Select what you want to keep.

o Now, select "Create installation media for another PC" option. See the below image:

o Select the language, architecture, and edition according to your need.


o For example, if you have a laptop or PC with a 64-bit CPU, then you have to install the 64-bit
version. If you have a computer or PC with a 32-bit CPU, then you have to install the 32-bit version.
If you want to install Windows 10 on your current using laptop or PC, check the box "Use the
recommended options for this PC," and it will automatically download the best compatible version
for your current using PC; otherwise, uncheck this box. See the image below:

o Now, you can copy the Windows 10 installation files to a USB drive or burn them to a DVD. The
Windows 10 installation files occupy the space greater than 3 GB so, your USB drive must be 4 GB or
larger in size. The USB drive must be empty because all files in the USB drive will be
erased in this process.

o Now, the installation media is created. Please insert it into your laptop or PC and
install Windows 10. You have to boot to the Windows 10 installer. For the booting
process, Click on the Start menu button and restart your laptop.

o Now, press and hold Del or F2 button to enter setup. This key may be different in some
computers. Generally, it is displayed as a message on startup that says, "Press [key] to enter setup."
o Now, go to the "Boot" option on the above menu bar and select a device from
which you have to boot. You will see the following two option there:
1. For a USB flash drive, select the Removable Devices option.
2. For a disc installation, select the CD-ROM Drive option.
o Now, save your setting and press the Enter button to confirm the changes. Now,
wait for your PC to restart.
o Now, the installation is started. Select your language, time, and currency format
on the Windows setup screen and then click "Next" to continue.
o You may have to click on several "next" buttons, and finally, you will see the
installer screen. See the below image:

o Click on the install now button and follow the given instructions to install
Windows 10 on your system successfully.
o Windows 10 is installed successfully. Now, you will see the Activate Windows
screen. You can enter a key or skip it. If installing Windows 10 automatically detects a
key associated with your PC's hardware, you will not see it on your screen.

o If you have a valid product key for Windows 10, you can paste it here. You can
also paste a valid Windows 7, 8, or 8.1 key here. You will get the advantage of the free
Windows 10 upgrade offer on your PC.
o After that, you will see a new Windows setup screen "Which type of installation do you
want?" Click "Custom" if you want to perform a clean installation. It will remove everything on
your PC. If you want to upgrade your existing installation, click the "Upgrade" option.
o Select the hard drive partition where you want to install Windows. It will delete the data you
have in that partition. Make sure that you have backups of any important files before doing this.

o In this installation process, your system may restart several times. At the end of
the process, you will get a new Windows 10 as your operating system.

o Restart your PC after complete installation. It is now ready to work.

RESULT:

These are the procedure to be followed to install Windows Operating System in our computer.
EX NO: 2 Illustrate UNIX commands and Shell Programming

2A) UNIX COMMANDS

AIM:

To study of Basic UNIXCommands are General commands, Directory


commands and File commands.

PROCEDURE:
GENERAL COMMANDS:

Command Function

date Used to display the current system date and time.


date +%D Displays date only
date +%T Displays time only
date +% Y Displays the year part of date
date +% H Displays the hour part of time
cal Calendar of the current month
calyear Displays calendar for all months of the specified year
calmonth year Displays calendar for the specified month of the year
who Login details of all users such as their IP, Terminal No, User
name,
who am i Used to display the login details of the user
tty Used to display the terminal name
uname Displays the Operating System
uname –r Shows version number of the OS (kernel).
uname –n Displays domain name of the server
echo "txt" Displays the given text on the screen
echo $HOME Displays the user's home directory
bc Basic calculator. Press Ctrl+dto quit
lpfile Allows the user to spool a job along with others in a print queue.
man cmdname Manual for the given command. Press q to exit
history To display the commands used by the user since log on.
exit Exit from a process. If shell is the only process then logs out
DIRECTORY COMMANDS:

Command Function
pwd Path of the present working directory
mkdirdir A directory is created in the given name under the current directory.
mkdirdir1 dir2 A number of sub-directories can be created under one stroke
Change Directory. If the subdirstarts with / then path starts from root
cd subdir
(absolute) otherwise from current working directory.
Cd To switch to the home directory.
cd / To switch to the root directory.
cd.. To move back to the parent directory
rmdirsubdir Removes an empty sub-directory.

FILE COMMANDS:

Command Function
To create a file with some contents. To end typing press Ctrl+d. The >symbol
cat >filename
means redirecting output to a file. (<for input)

cat filename Displays the file contents.

cat >>filename Used to append contents to a file

cpsrc des Copy files to given location. If already exists, it will be overwritten

cp –isrc des Warns the user prior to overwriting the destination file

cp –r src des Copies the entire directory, all its sub-directories and files.

mv old new To rename an existing file or directory. –i option can also be used

mv f1 f2 f3 dir To move a group of files to a directory.

mv –v old new Display name of each file as it is moved.

Rmfile Used to delete a file or group of files. –i option can also be used

rm * To delete all the files in the directory.

rm –r * Deletes all files and sub-directories

rm –f * To forcibly remove even write-protected files

Ls Lists all files and subdirectories (blue colored) in sorted manner.


Lsname To check whether a file or directory exists.

lsname* Short-hand notation to list out filenames of a specific pattern.

ls –a Lists all files including hidden files (files beginning with .)

ls –x dirname To have specific listing of a directory.

ls –R Recursive listing of all files in the subdirectories


Long listing showing file access rights (read/write/execute-rwxfor
ls –l
user/group/others-ugo).

cmpfile1 file2 Used to compare two files. Displays nothing if files are identical.

Wcfile It produces a statistics of lines (l), words(w), and characters(c).


Changes permission for the specified file. (r=4, w=2, x=1) chmod 740 file sets
chmodperm file
all rights for user, read only for groups and no rights for others

OUTPUT- GENERAL COMMANDS:

1. To create a file.
Syntax: $ cat>filename
Example: $ cat>ex1

2. To view the content of the file.

Syntax: $ cat filename

Example: $ cat ex1

3. To append some details with the existing details


in the file Syntax: $ cat>>filename
Example: $ cat>>ex1

4. To concatenate multiple files


Syntax: $ cat file1 file2 > file3
Example: $ cat computer compiler>world

5. To know the list of all files in


directory Syntax: $ ls

6. To copy the file to another file


Syntax: $ cp source destination
Example: $ cp ex1 ex2

7. To rename the file


Syntax: $ mv oldfilenewfile
Example: $ mv ex1 ex3

8. To delete a file

Syntax: $ rm filename
Example: $ rm ex1

9. To delete all files

Syntax: $ rm *

10. To create a directory

Syntax: $ mkdirdirname

11. To change the name of the

directory Syntax: $ cd dirname

12. To remove the directory


Syntax: $ rmdirdirname
Example: $ rmdir flower

13. Echo

i. To display the filename starting with


single letter Syntax: $ echo?

ii. To display the filename starting with


two letters Syntax: $ echo??

iii. To display the filename starting with the


letter f Syntax: $ echo f*
iv. To display the filename ending with
letter f. Syntax: $ echo *f

14. Present Working Directory


i. To display the present working directory
Syntax: $ pwd

ii. To clear the screen

Syntax: $ tput clear

iii. To calculate the values

Syntax: $ bc

iv. Uname: To know your machine name -

n: Tells machine name in network

Syntax: $ uname –n
v. To display the version number of the OS

Syntax: $ uname –r
15. Head

i. To display first 10 lines


Syntax: $ head filename

ii. To display first 6 characters

Syntax: $ head -6c filename

iii. To display 5 lines from 2 files

Syntax: $ head -5 file1 file2

16. To display last 10 lines

Syntax: $ tail filename

Example: $ tail ex3

17. Word Count


i. To display the number of words in a file

Syntax: $ wc filename

Example: $ wc ex1

ii. To display the number of characters


in a file Syntax: $ wc –c filename
Example: $ wc –c ex1

iii. To display the number of lines


Syntax: $ wc –l filename
Example: $ wc –l ex3
18. Line Number
i. To display number of lines with numbers

Syntax: $ nl filename
Example: $ nl ex1

ii. To increment the line number by

5 Syntax: $ nl –i5 filename

Example: $ nl –i5 ex3

19. Sort
i. To reverse and sort the content of file

Syntax: $ sort –r filename

Example: $ sort –r ex1


ii. To sort the content of the file

Syntax: $ sort filename

Example: $ sort ex1

iii. To sort and remove the duplicate

Syntax: $ sort –u filename

Example: $ sort –u ex1

20. VI Editor Commands


i. To compile and run shell program
Syntax: $ sh filename
Example: $ sh odd.sh
ii. To compile a C program
Syntax: $ cc –o filename filename.c

iii. To run a C program

Syntax: $ ./filename

21. To paste the contents of file


Syntax: $ paste filename1, filename2
Example: $ paste ex1, ex2

22. To display file contents page by

page Syntax: $ more filename

GENERAL PURPOSE UTILITY COMMANDS

1. Calendar:
i. To display the calendar.
Syntax: $ cal

ii. To display the previous, current and


next month. Syntax: $ cal -3

iii. To display the current month starting from


Sunday. Syntax: $ cal –s

iv. To display the current month starting from Monday.


Syntax: $ cal –m

2. Date:

i. To display system date.


Syntax: $ date
Output: Tue Jan 20 10:54:25 IST 2009

ii. To display month only.

Syntax: $ date+%m

iii. To display month name and


month Syntax: $date
+%h%m Output: Jan01
iv. To display month name

Syntax: $ date+%h
Output: Jan

v. To display the time in hours


Syntax: $ date+%H
Output: 10

vi. To display the time in


minutes Syntax: $
date+%M Output: 53

vii. To display the time in AM or PM

Syntax: $ date+%r
Output: 10: 53:24 AM
viii. To display date of

month Syntax: $

date+%d Output: 20

3. WHO
i. To display the login details

Syntax: $ who
Output: root :0 Jan 20 10:51
cs1010 pts/0 Jan 20 10:51 (172.16.1.72)

ii. To display the login user


details Syntax: $ who
am i Output: cs1010

iii. To display my login id

Syntax: $ logname

Output: cs1010

4. MAN
i. It is used to view more details of all the

commands Syntax: $ man command_name

Example: $ man date


RESULT: Thus the study and execution of Unix commands has been completed successfully.
2B) SHELL PROGRAMMING

AIM:
To write simple shell scripts using shell programming fundamentals.

PROCEDURE:

The original UNIX came with the Bourne shell (sh) and it is universal even today.
Then came a plethora of shells offering new features. Two of them, C shell (csh)
and Korn shell (ksh) has been well accepted by the UNIX fraternity. Linux offers
Bash shell (bash) as a superior alternative to Bourne shell.

Preliminaries

1. Comments in shell script start with #. It can be placed anywhere in a line; the shell
ignores contents to its right. Comments are recommended but not mandatory

2. Shell variables are loosely typed i.e. not declared. Their type depends on the value
assigned. Variables when used in an expression or output must be prefixed by $.
3. The read statement is shell's internal tool for making scripts interactive.

4. Output is displayed using echo statement. Any text should be within quotes.
Escape sequence should be used with –e option.

5. Commands are always enclosed with ` ` (back quotes).

6. Expressions are computed using the expr command. Arithmetic operators are
+ - * / %. Meta characters * ( ) should be escaped with a \.

7. Multiple statements can be written in a single line separated by ;

8. The shell scripts are executed using the sh command (sh filename).
I. Swapping values of two variables

Algorithm

Step 1 : Start

Step 2 : Read the values of a and b

Step 3 : Interchange the values of a and b using another variable t as follows:

t=aa=bb=t

Step 4 : Print a and b

Step 5 : Stop

Program (swap.sh) # Swapping values

echo -n "Enter value for A : " read a


echo -n "Enter value for B : " read b
t=$a
a=$b

b=$t

echo "Values after Swapping"


echo "A Value is $a"
echo "B Value is $b"
Output

[student@vecit~]$sh swap.sh

Enter Value for A:5

Enter Value for B:6

Values after Swapping A value is 6 B values is 5


II. Fahrenheit to Centigrade Conversion

Algorithm

Step 1: Start

Step 2: Read Fahrenheit value

Step 3: Convert Fahrenheit to centigrade using the formulae:

(Fahrenheit – 32) × 5/9

Step 4: Print centigrade

Step 5: Stop

Program

# Degree conversion

echo -n "Enter Fahrenheit : "

read f

c=`expr\( $f - 32 \) \* 5 / 9`

echo "Centigrade is : $c"

Output

[student@vecit~]$sh temp.sh

Enter Fahrenheit:4

Centrigrade is: -15


III. ODD OR EVEN

Algorithm

Step 1 : Start
Step 2 : Read number
Step 3 : If number divisible by 2 then Print "Number is Even"

Step 3.1 : else Print "Number is Odd"


Step 4 : Stop Program

Program: # Odd or even using if-else

echo "Enter the Number"


read n
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo "$n is Even number"

else

echo "$n is Odd number"


fi

Output

[student@vecit~]$sh oddeven.sh
Enter a non-zero number : 12
12 is Even
IV. String comparison

Algorithm

Step 1 : Start
Step 2 : Read strings str1 and str2
Step 3 : If str1 = str2 then Print "Strings are the same"

Step 3.1 : else Print "Strings are distinct"


Step 4 : Stop

Program

echo -n "Enter the first string : "


read s1
echo -n "Enter the second string : "
read s2
if [ $s1 == $s2 ] then
echo "Strings are the same" else
echo "Strings are distinct" fi

Output

[student@vecit~]$sh strcomp.sh
Enter the first string :ece-a
Enter the second string : ECE-A
Strings are distinct

RESULT: Thus the shell programs are executed successfully.


EX NO: 3 Process Management using System Calls :
Fork, Exit, Getpid, Wait, Close
3A) To write a program to implement the system call fork, getpid, exexc
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);
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());

printf(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.outtst 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 tstsd
dsaASD
3B) To write a program to implement the system callopendir, readdir, closedir

AIM: To write the program to implement the system calls opendir( ), readdir ( ),closedir ().

ALGORITHM:

Step 1: Start the program.


Step 2: In the main function pass the arguments.
Step 3: Create structure as stat buff and the variables as integer.
Step 4: Open the directory.
Step 5: Read the contents of the directory (filenames).
Step 6: Display the contents of the directory.

Step 7: Close the directory.


Step 4: Stop the program.
PROGRAM:
/* Recursively descend a directory hierarchy pointing a file */
#include<stdio.h>
#include<dirent.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
struct dirent *direntp; DIR *dirp; if(argc!=2)
{

printf("ussage %s directory name \n",argv[0]);


return 1;
}
if((dirp=opendir(argv[1]))==NULL)
{
perror("Failed to open directory \n");
return 1;

}
while((direntp=readdir(dirp))!=NULL)

printf("%s\n",direntp->d_name);
while((closedir(dirp)==-1)&&(errno==EINTR));

return 0;
}

OUTPUT:
$ vi direc.c
$ cc direc.c
$ ./a.out ./

f1.txt

fflag.c b2

comm.c

dir1

demo2 a1

m1 db

a.out

direc.c

db1

file1

b1
xyz

abc

aaa

file2
aaa.txt

m2

RESULT: Thus the program was executed and verified successfully.


EX NO: 4 CPU Scheduling Algorithms
4 A) FCFS CPU SCHEDULING ALGORITHM
AIM:

To write a c program to simulate the CPU scheduling algorithm First


Come First Serve (FCFS).

DESCRIPTION:

Jobs are executed on FCFS (First Come, First Serve) basis.

It is a non-preemptive, preemptive scheduling algorithm.

Easy to understand and implement.

Its implementation is based on FIFO (First In First Out) queue.

Poor in performance as average wait time is high.


Wait time of each process is as follows –

Process Wait Time : Service Time - Arrival Time

P0 -0=0

P1 -1=4

P2 -2=6

P3 -3=13

Average Wait Time: (0+4+6+13) / 4 = 5.75


ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time

Step 4: Set the waiting of the first process as ‗0‘and its burst time as its turnaround time

Step 5: for each process in the Ready Q calculate


a). Waiting time (n) = waiting time (n-1) + Burst time (n-1) b).

Turnaround time (n)= waiting time(n)+Burst time(n)


Step 6: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of

process Step 7: Stop the process

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{

int bt[20], wt[20], tat[20], i, n;


float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");

scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)

{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg =wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n"); for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i],
tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f",


tatavg/n); getch(); }

INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time--17.000000
Average Turnaround Time -- 27.000000
4 B) SJF CPU SCHEDULING ALGORITHM
AIM:

To write a program to stimulate the CPU scheduling algorithm Shortest


job first (Non-Preemption).

DESCRIPTION:

This is also known as shortest job first, or SJF

This is a non-preemptive, pre-emptive scheduling algorithm.

Best approach to minimize waiting time.

Easy to implement in Batch systems where required CPU time is known in advance.

Impossible to implement in interactive systems where required CPU time is not known.

The processer should know in advance how much time process will take.

Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

P0 5

P1 3

P2 8

P3 6
Waiting time of each process is as follows –

Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14-2=12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and
accept the CPU burst time

Step 4: Start the Ready Q according the shortest Burst time by


sorting according to lowest to highest burst time.

Step 5: Set the waiting time of the first process as ‗0‘ and its
turnaround time as its burst time.

Step 6: Sort the processes names based on their Burt time


Step 7: For each process in the ready

queue, calculate

a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)


b) Turnaround time (n)= waiting time(n)+Burst time(n)

Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process

Step 9: Stop the process

PROGRAM :

#include<stdio.h>
#include<conio.h>
main()

int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float


wtavg, tatavg; clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)

{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];

p[i]=p[k];
p[k]=temp;
}

wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}

printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t


TURNAROUND TIME\n");

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

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i],


tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f", tatavg/n);


getch();
}
INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6

Enter Burst Time for Process 1 -- 8


Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST WAITING TURNARO
TIME TIME UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
4 C) ROUND ROBIN CPUSCHEDULING ALGORITHM
AIM:
To write a C program to implement Round Robin CPU scheduling algorithm.

DESCRIPTION

Round Robin is the preemptive process scheduling algorithm.

Each process is provided a fix time to execute, it is called a quantum.

Once a process is executed for a given time period, it is


preempted and other process executes for a given time period.

Context switching is used to save states of preempted processes.

Wait time of each process is as follows –

Process Wait Time : Service Time - Arrival Time

P0 (0-0) + (12 - 3) = 9

P1 (3-1) = 2

P2 (6-2) + (14 - 9) +(20-17)=12


P3 (9-3) + (17 -12)=11

Average Wait Time: (9+2+12+11) / 4 = 8.5


ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue and time
quantum (or) time slice

Step 3: For each process in the ready Q, assign the process id and
accept the CPU burst time

Step 4: Calculate the no. of time slices for each process where
No. of time slice for process (n) = burst time process (n)/time slice

Step 5: If the burst time is less than the time slice then the no. of time slices =1.

Step 6: Consider the ready queue is a circular Q, calculate

a) Waiting time for process (n) = waiting time of


process(n-1)+ burst time of process(n-1 ) + the time
difference in getting the CPU from process(n-1)

b) Turnaround time for process(n) = waiting time of process(n) + burst


time of process(n)+ the time difference in getting CPU from process(n).

Step 7: Calculate

c) Average waiting time = Total waiting Time / Number of process

d) Average Turnaround time = Total Turnaround Time / Number of

process Step 8: Stop the process


SOURCE CODE

#include<stdio.h>

main()
{

inti,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");

scanf("%d",&n);
for(i=0;i<n;i++)

{
printf("\nEnter Burst Time for process %d -- ", i+1);

scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)

for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;

}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}

for(i=0;i<n;i++)
{
wa[i]=tat[i]- ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- f",att/n);

printf("\nThe Average Waiting time is -- %f ",awt/n);

printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND

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

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

getch();

}
INPUT:

Enter the no of processes – 3


Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3

Enter Burst Time for process 3 – 3


Enter the size of time slice – 3

OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2 3 4 7
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ------------ 5.666667
4 D) PRIORITY CPU SCHEDULING ALGORITHM
AIM:
To write a C program to implement Priority CPU scheduling algorithm.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the input process and their burst time.

Step 3: Sort the processes based on priority.

Step 4: Compute the waiting time and turnaround time for each process.

Step 5: Calculate the average waiting time and average turnaround time.

Step 6: Print the details about all the processes.

Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<string.h>
void main()

int bt[30],pr[30],np; intwt[30],tat[30],wat[30],at[30],ft[30]; int i,j,x,z,t;

float sum1=0,sum=0,awt,att;
char p[5][9],y[9];
printf("\nenter the number of process");
scanf("%d",&np);
printf("\nEnter the process,burst-time and

priority:"); for(i=0;i<np;i++)
scanf("%s%d%d",p[i],&bt[i],&pr[i]);
printf("\nEnter the arrival-time:");
for(i=0;i<np;i++)scanf("%d",&at[i]);

for(i=0;i<np;i++)
for(j=i+1;j<np;j++)

if(pr[i]>pr[j])
{
x=pr[j];
pr[j]=pr[i];
pr[i]=x;
strcpy(y,p[j]);

strcpy(p[j],p[i]);
strcpy(p[i],y);
z=bt[j];
bt[j]=bt[i];
bt[i]=z;
}
}
wt[0]=0;
for(i=1;i<=np;i++)

wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0];
for(i=1;i<np;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\tGANTT CHART\n");
printf("\n \n");

for(i=0;i<np;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");

printf("\n");
for(i=0;i<=np;i++)
printf("%d\t\t",wt[i]);
printf("\n \n");
printf("\n");
for(i=0;i<np;i++)
wat[i]=wt[i]-at[i];
for(i=0;i<np;i++)
tat[i]=wat[i]-at[i];

printf("\nPRIORITY SCHEDULING:\n");
printf("\nProcess Priority Burst-time Arrival-time Waiting-time Turnaround-time");

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

printf("\n\n%d%s\t%d\t\t%d\t\t%d\t%d\t\t%d",i+1,p[i],pr[i],bt[i],at[i],wt[i],tat[i]);

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

sum=sum+wat[i];
awt=sum/np;
for(i=0;i<np;i++)
sum1=sum1+tat[i];
att=sum1/np;
printf("\n\nAverage waiting time:%f",awt); printf("\n\nAverageturn around time is:%f",att);
}

OUTPUT:

Enter the number of process 3

Enter the process, burst-time and priority:


p1 3 3

p2 4 2
p3 5 1
Enter the arrival-time: 0 1 2
GANTT CHART

| p3 | p2 | p1 |

0 5 9 12

PRIORITY SCHEDULING:

Process Priority Burst-time Arrival-time Waiting-time Turnaround-time

p3 1 5 0 0 0
p2 2 4 1 5 3
p1 3 3 2 9 5
Average waiting time: 3.666667

Average turnaround time is: 2.666667

RESULT
The CPU scheduling algorithms are implemented and executed successfully.
EX NO 5: INTERPROCESS COMMUNICATION

AIM:

To Write C programs to simulate Intra & Inter – Process Communication. IPC


techniques: Pipes, Messages Queues, and Shared Memory.

DESCRIPTION:

Inter Process Communication (IPC) is a mechanism that involves communication of

one process with another process. This usually occurs only in one system.

Communication can be of two types:

Between related processes initiating from only one process, such as parent and child processes.

Between unrelated processes, or two or more different processes.

Following are some important terms that we need to know before proceeding further on this

topic. Pipes − Communication between two related processes. The mechanism is half duplex

meaning the first process communicates with the second process. To achieve a full duplex i.e.,

for the second process to communicate with the first process another pipe is required.

FIFO − Communication between two unrelated processes. FIFO is a full duplex, meaning the

first process can communicate with the second process and vice versa at the same time.

Message Queues − Communication between two or more processes with full duplex capacity. The

processes will communicate with each other by posting a message and retrieving it out of the

queue. Once retrieved, the message is no longer available in the queue.

Shared Memory − Communication between two or more processes is achieved


through a shared piece of memory among all processes. The shared memory needs
to be protected from each other by synchronizing access to all the processes.
Semaphores − Semaphores are meant for synchronizing access to multiple processes. When
one process wants to access the memory (for reading or writing), it needs to be locked (or
protected) and released when the access is removed. This needs to be repeated by all the
processes to secure data. Signals − Signal is a mechanism to communication between
multiple processes by way of signaling. This means a source process will send a signal
(recognized by number) and the destination process will handle it accordingly.

PROGRAM 4.A ) ECHOSERVER USING PIPES SOURCE CODE:

/* Implementation of echoserver using Pipes */


#include<stdio.h>
#include<string.h>
#define msgsize 29
int main()
{

int ser[2],cli[2],pid;
char inbuff[msgsize];
char *msg="Thank you";
system("clear");
pipe(ser);
pipe(cli);

printf("\n server read id =%d,write id=%d",ser[0],ser[1]);


printf("\n client read id =%d,write id=%d",cli[0],cli[1]);
pid=fork();
if(pid==0)

{
printf("\n i am in child process !");
close(cli[0]);
close(ser[1]);
write(cli[1],msg,msgsize);

printf("\n message written to pipe..!");

sleep(2);
read(ser[0],inbuff,msgsize);
printf("\n echo message received from server");

printf("\n %s",inbuff);
}
else

{
close(cli[1]);
close(ser[0]);
printf("\n parent process");
read(cli[0],inbuff,msgsize);
write(ser[1],inbuff,msgsize);
printf("\n parent ended!");
}
}
OUTPUT:
$ vi echopipe.c
$ cc echopipe.c
$ ./a.out
server read id =3,write

id=4 client read id =5,write

id=6 i am in child process

! client read id =5,write

id=6 parent process

parent ended!
$ message written to pipe..! echo

message received from server

PROGRAM 4.B) ECHO SERVER USING MESSAGES SOURCE CODE:

/* Implementation of echo server using messages */


#include<sys/ipc.h>
#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#include<stdlib.h>
struct
{
long mtype;
char mtext[20];
}send,recv;
main()
{
int qid,pid,len;
qid=msgget((key_t)0X2000,IPC_CREAT|0666);
if(qid==-1)
{

perror("\n message failed");


exit(1);
}
send.mtype=1;

strcpy(send.mtext,"\n hello i am parent");


len=strlen(send.mtext);
pid=fork();
if(pid>0)
{
if(msgsnd(qid,&send,len,0)==-1)

{
perror("\n message sending failed");
exit(1);
}
printf("\n message has been posted");
sleep(2);
if(msgrcv(qid,&recv,100,2,0)==-1)
{

perror("\n msgrcv error:");


exit(1);
}
printf("\n message received from child - %s\n",recv.mtext);
}
else
{

send.mtype=2;
strcpy(send.mtext,"\n hi i am child");

len=strlen(send.mtext);

if(msgrcv(qid,&recv,100,1,0)==-1)
{
perror("\n child message received failed");
exit(1);
}
if(msgsnd(qid,&send,len,0)==-1)

{
perror("\n child message send failed");
}

printf("\n received from parent - %s",recv.mtext);

}
}
OUTPUT:

$ vi echomsg.c
$ cc echomsg.c
$ ./a.out

received from parent -


hello i am parent message has been

posted message received from child -

hi i am child

RESULT:

Thus the program was executed and verified successfully.


EX NO 6: MUTUAL EXCLUSIVE BY SEMAPHORE

AIM:
To write a C program to implement the Producer & consumer Problem (Semaphore)

ALGORITHM:
Step 1: The Semaphore mutex, full & empty are initialized.
Step 2: In the case of producer process
i) Produce an item in to temporary variable.

ii) If there is empty space in the buffer check the mutex value for enter into the critical section.

iii) If the mutex value is 0, allow the producer to add value in the temporary variable to the

buffer.

It should wait if the buffer is empty

If there is any item in the buffer check for mutex value, if the mutex==0,
remove item from buffer

Signal the mutex value and reduce the empty

value by 1. Consume the item.

Step 4: Print the result


PROGRAM:

#include<stdio.>
void main()
{
int buffer[10], bufsize, in, out, produce, consume,choice=0; in = 0;out = 0;

bufsize = 10;
while(choice !=3)

{
printf(“\n1. Produce \t 2. Consume \t3. Exit”); printf(“\nEnter your choice: ”);

scanf(“%d”,&choice);

switch(choice) {

case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);

buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2:
if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];

printf(“\nThe consumed value is %d”,


consume); out = (out+1)%bufsize;
}
break;
}
}
}

OUTPUT

1. Produce 2. Consume 3. Exit


Enter your choice: 2
Buffer is Empty

1. Produce 2. Consume 3. Exit


Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit

Enter your choice: 3

RESULT: Thus the program was executed and verified successfully.


EX NO: 7 DEADLOCK AVOIDANCE
AIM:
To write a C program to implement bankers algorithm for dead lock avoidance.

ALGORITHM:
Step 1: Start the Program.
Step 2: Obtain the required data through char and int datatypes.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks.
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution.
PROGRAM: /* Bankers algorithm for Deadlock Avoidance */
#include<stdio.h>
struct process
{
int allocation[3];
int max[3];

int need[3];
int finish;
}p[10];
int main()
{

int n,i,I,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf("\nEnter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth process allocated resources:",i);
scanf("%d%d%d",&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);

printf("\nEnter the %dth process maximum resources:",i);

scanf("%d%d%d",&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[1]=p[i].max[1]-p[i].allocation[1];
p[i].need[2]=p[i].max[2]-p[i].allocation[2];
}
printf("\nEnter the available vector:");

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

work[i]=avail[i];
while(count!=n)
{
count=0;

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

flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j];
p[i].finish=1;
sequence[k++]=i;
flag=0;
}
if(flag==1)
count++;
}

}
count=0;
for(i=0;i<n;i++)
if(p[i].finish==1)
count++;
printf("\n The safe sequence is:\t");

if(count++==n)

for(i=0;i<k;i++)
printf("%d\n",sequence[i]);
else
printf("SYSTEM IS NOT IN A SAFE STATE \n\n");

return 0;

}
OUTPUT
$ vi bankersavoidance.c

$ cc bankersavoidance.c
$ ./a.out
Enter the number of process: 3
Enter the 0th process allocated resources: 1 2 3
Enter the 0th process maximum resources: 4 5 6
Enter the 1th process allocated resources: 3 4 5

Enter the 1th process maximum resources: 6 7 8


Enter the 2th process allocated resources: 1 2 3
Enter the 2th process maximum resources: 3 4 5
Enter the available vector: 10 12 11
The safe sequence is: 0 1 2

RESULT:
Thus the program was executed and verified successfully.
EX NO: 8 DEADLOCK DETECTION

AIM:
To write a C program to implement Deadlock Detection algorithm.

ALGORITHM:
Step 1: Start the Program.
Step 2: Obtain the required data through char and in data types.

Step 3: Enter the filename, index block.


Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks.
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution.
PROGRAM:

/* Bankers algorithm for Deadlock detection */


#include<stdio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
printf("enter total no of processes: \n");

scanf("%d",&tp);
printf("enter clain matrix: \n");
for(i=0;i<4;i++)
{

for(j=0;j<5;j++)
{
scanf("%d",&c[i][j]);
}
}
printf("enter allocation matrix: \n");

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

{
for(j=0;j<5;j++)
{
scanf("%d",&p[i][j]);
}
}
printf("enter resource vector: \n");

for(i=0;i<5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability vector: \n");

for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=0;i<4;i++)

{
sum=0;
for(j=0;j<5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;

}
}
for(i=0;i<4;i++)
{
for(l=1;l<k;l++)

{
if(i!=m[l])
{
flag=1;
for(j=0;j<5;j++)
{
if(c[i][j]>temp[j])

{
flag=0;
break;
}
}

}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=0;j<5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are: \n");

for(j=0;j<tp;j++)
{

found=0;
for(i=1;i<k;i++)

{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}

OUTPUT:
$ vi bankersdetection.c
$ cc bankersdetection.c

$ ./a.out
enter total no of processes:

4 enter clain matrix:

0 1 0 01

0 0 1 01
0 0 0 01
1 0 1 0 1

enter allocation matrix:

1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0

enter resource vector:

2 1 1 2 1 enter

availability vector:

0 0 0 0 1

deadlock causing

processes are: 0 1

RESULT:
Thus the program was executed and verified successfully.
EX NO: 9 THREADING
AIM:
To Write C programs to demonstrate threading.
ALGORITHM:
Step 1: Start the process
Step 2: Declare process thread, thread-id.

Step 3: Read the process thread and thread state.

Step 4: Check the process thread equals to thread-id by using if condition.

Step 5: Check the error state of the thread.


Step 6: Display the completed thread process.
Step 7: Stop the process.

PROGRAM :

#include<pthread.h>
#include<stdio.h>
#define NUM_THREADS 3

int je,jo,evensum=0,sumn=0,oddsum=0,evenarr[50],oddarr[50];

void *Even(void *threadid)


{

int i,n;
je=0;
n=(int)threadid;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
evenarr[je]=i;
evensum=evensum+i;

je++;

}
}
}
void *Odd(void *threadid)

{
int i,n;
jo=0;

n=(int)threadid;
for(i=0;i<=n;i++)
{
if(i%2!=0)
{

oddarr[jo]=i;
oddsum=oddsum+i;
jo++;
}
}
}
void *SumN(void *threadid)

{
int i,n;
n=(int)threadid;
for(i=1;i<=n;i++)
{
sumn=sumn+i;
}
}
int main()

pthread_tthreads[NUM_THREADS];
int i,t;
printf("Enter a number\n");
scanf("%d",&t);

pthread_create(&threads[0],NULL,Even,(void *)t);
pthread_create(&threads[1],NULL,Odd,(void *)t);
pthread_create(&threads[2],NULL,SumN,(void *)t);
for(i=0;i<NUM_THREADS;i++)
{
pthread_join(threads[i],NULL);

printf("The sum of first N natural nos is %d\n",sumn);


printf("The sum of first N even natural nos is %d\n",evensum);
printf("The sum of first N odd natural nos is %d\n",oddsum);
printf("The first N even natural nos is --- \n");

for(i=0;i<je;i++)
printf("%d\n",evenarr[i]);
printf("The first N odd natural nos is --- \n");

for(i=0;i<jo;i++)
printf("%d\n",oddarr[i]);
pthread_exit(NULL);
}
OUTPUT:

$ vi threadf.c

$ cc threadf.c -pthread
$ ./a.out
Enter a number 12

The sum of first N natural nos is 78


The sum of first N even natural nos is 42
The sum of first N odd natural nos is 36
The first N even natural nos is----
2

4
6
8
10

12
The first N odd natural nos is----
1
3
5
7

9
11

RESULT:
Thus the program was executed and verified successfully.
EX NO: 10 PAGING TECHNIQUES
AIM:
To write a C program to implement paging concept for memory management.

ALGORIHTM:

Step 1: Start the program.


Step 2: Enter the logical memory address.

Step 3: Enter the page table which has offset and page frame.
Step 4: The corresponding physical address can be calculate by, PA = [
pageframe* No. of page size ] + Page offset.
Step 5: Print the physical address for the corresponding logical address.
Step 6: Terminate the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()

{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
clrscr();
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- ");

scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
printf("\nEnter number of processes -- ");
scanf("%d",&np);

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

printf("\nEnter no. of pages required for p[%d]-- ",i);

scanf("%d",&s[i]);
if(s[i] >rempages)
{

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

rempages = rempages - s[i];


printf("\nEnterpagetable for p[%d] --- ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);

printf("\nEnter Logical Address to find Physical

Address "); printf("\nEnter process no. and

pagenumber and offset -- "); scanf("%d %d

%d",&x,&y, &offset); if(x>np || y>=s[i] || offset>=ps)

printf("\nInvalid Process or Page Number or offset");

else
{
pa=fno[x][y]*ps+offset;

printf("\nThe Physical Address is -- %d",pa);


}
getch();
}

OUTPUT
Enter the memory size – 1000
Enter the page size -- 100
The no. of pages available in memory are 10

Enter number of processes -- 3


Enter no. of pages required for p[1]-- 4
Enter pagetable for p[1] --- 8 6 9 5
Enter no. of pages required for p[2]-- 5
Enter pagetable for p[2] --- 1 4 5 7 3
Enter no. of pages required for p[3]-- 5

Memory is Full

Enter Logical Address to find Physical Address Enter process no. and pagenumber

and offset –
2

60

The Physical Address is -- 760


RESULT:

Thus C program for implementing paging concept for memory


management has been executed successfully.
EX NO: 11 MEMORY ALLOCATION TECHNIQUES
AIM:

To write a C program to implement Memory Management concept using the


technique best fit, worst fit and first fit algorithms.

ALGORITHM:

1. Get the number of process.

2. Get the number of blocks and size of process.

3. Get the choices from the user and call the corresponding switch cases.

4. First fit -allocate the process to the available free block match with the size of the process

5. Worst fit –allocate the process to the largest block size available in the list

6. Best fit-allocate the process to the optimum size block available in the list

7. Display the result with allocations

PROGRAM:

WORST-FIT

#include<stdio.h>
#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();

INPUT

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

OUTPUT

File No File Size Block No Block Size Fragment

1 1 1 5 4

2 4 3 7 3
5.3.2 BEST-FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;

static int bf[max],ff[max];


clrscr();
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)

if(lowest>temp)

ff[i]=j;

Lowest=temp;}

frag[i]=lowest;

bf[ff[i]]=1;

lowest=10000;

}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");

for(i=1;i<=nf&& ff[i]!=0;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

getch();

INPUT

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
OUTPUT

File No File Size Block No Block Size Fragment

1 1 2 2 1

2 4 1 5 1

5.3.3 FIRST-FIT

#include<stdio.h>

#include<conio.h>

#define max 25

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;

static int bf[max],ff[max];

clrscr();

printf("\n\tMemory Management Scheme - Worst Fit");

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

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

scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");

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

printf("Block %d:",i);

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

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)//if bf[j] is not allocated

temp=b[j]-f[i];

if(temp>=0)

if(highest<temp)

ff[i]=j;

highest=temp;

frag[i]=highest;

bf[ff[i]]=1;

highest=0;

}
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();

INPUT

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

OUTPUT

File No File Size Block No Block Size Fragment

1 1 3 7 6

2 4 1 5 1

RESULT:

Thus a UNIX C program to implement memory management scheme using


Best fit worst fit and first fit were executed successfully.
EX NO: 12 PAGE REPLACEMENT ALGORITHMS
AIM:

To write a UNIX C program to implement FIFO 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. Format 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>
main()
{

int i, j, k, f, pf=0, count=0, rs[25], m[10], n;


clrscr();

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

Enter the length of reference string – 20

Enter the reference string -- 70120304230321201701

Enter no. of frames -- 3

7 -1 -1 PF No. 1

7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
2 0 1
2 3 1 PF No. 5
2 3 0 PF No. 6

4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
0 2 3
0 2 3

0 1 3 PF No. 11
0 1 2 PF No. 12
0 1 2
0 1 2

7 1 2 PF No. 13

7 0 2 PF No. 14

7 0 1 PF No. 15

The number of Page Faults using FIFO are 15

RESULT:
Thus the program was executed and verified successfully
LRU page replacement algorithm
AIM:

To write UNIX C program a program to implement LRU 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>
main()
{

int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0,


next=1; clrscr();

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

Enter the length of reference string -- 20

Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

Enter the number of frames -- 3

The Page Replacement process is --

7 -1 -1 PF No. -- 1

7 0 -1 PF No. -- 2

7 0 1 PF No. -- 3

2 0 1 PF No. -- 4

2 0 1

2 03 PF No. -- 5
2 0 3

4 03 PF No. -- 6
4 02 PF No. -- 7
4 32 PF No. -- 8
0 32 PF No. -- 9

0 3 2

0 3 2

1 32 PF No. -- 10
1 3 2

1 02 PF No. -- 11

1 0 2

1 0 7 PF No. -- 12
1 0 7

1 0 7

The number of page faults using LRU are 12

RESULT:
Thus the program was executed and verified successfully
LFU page replacement algorithm
AIM:

To write a program in C to implement LFU page replacement algorithm.

ALGORITHM

Step1: Start the program

Step2: Declare the required variables and initialize it.

Step3; Get the frame size and reference string from the user

Step4: Keep track of entered data elements

Step5: Accommodate a new element look for the element that is not to be
used in frequently replace.

Step6: Count the number of page fault and display the value

Step7: Terminate the program

PROGRAM

#include<stdio.h>
#include<conio.h>
main()

{int rs[50], i, j, k, m, f, cntr[20], a[20], min,


pf=0; clrscr();
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
Enter number of page references -- 10
Enter the reference string -- 123452525143
Enter the available no. of frames 3
The Page Replacement Process is –

1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3

5 2 3
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
Total number of page faults -- 8

RESULT:

Thus the C programs to implement LFU page replacement algorithm


was executed successfully.
EX NO: 13 FILE ORGANIZATION TECHNIQUES
AIM:

Write a C program to simulate the following file organization techniques a) Single


level directoryb) Two level directoryc) Hierarchical

ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.

Step-3: Read the number of files


Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level

Step-7: Display the files


Step-8: Stop the program

PROGRAM:

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;

}dir;
void main()
{
int i,ch;
charf[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 \n

4. Display Files\t5. Exit\nEnter


your choice -- ");
5. 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:

Enter name of directory -- CSE


1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- A


1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- B


1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- C


1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 4

The Files are -- A B C


1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 3

Enter the name of the file – ABC File


ABC not found
1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 2

Enter the name of the file – B


File B is deleted
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit Enter your
choice – 5

RESULT:
Thus the program was executed and verified successfully
Two level directory.

AIM:
To write C program to organize the file using two level directory.

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the count, file name, graphical interface.

Step-3: Read the number of files


Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level

Step-7: Display the files


Step-8: Stop the program

PROGRAM:
#include<stdio.h>

struct

char dname[10],fname[10][10];

int fcnt;

}dir[10];

void main()

int i,ch,dcnt,k;
char f[30], d[30];

clrscr();

dcnt=0;

while(1)

printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");

printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice --");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter name of directory -- ");

scanf("%s", dir[dcnt].dname);

dir[dcnt].fcnt=0;

dcnt++;

printf("Directory created");

break;

case 2:printf("\nEnter name of the directory -- ");

scanf("%s",d);

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

if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file -- ");

scanf("%s",dir[i].fname[dir[i].fcnt]);

dir[i].fcnt++;

printf("File created");

break;

if(i==dcnt)
printf("Directory %s not found",d);

break;
case 3:printf("\nEnter name of the directory -- ");

scanf("%s",d);

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

if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file -- ");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is deleted ",f);

dir[i].fcnt--;

strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);

goto jmp;

printf("File %s not found",f);

goto jmp;

printf("Directory %s not found",d);

jmp : break;
case 4:printf("\nEnter name of the directory -- ");

scanf("%s",d);

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

if(strcmp(d,dir[i].dname)==0)

printf("Enter the name of the file -- ");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is found ",f);

goto jmp1;

printf("File %s not found",f);

goto jmp1;

printf("Directory %s not found",d);

jmp1: break;

case 5: if(dcnt==0)

printf("\nNo Directory's ");

else

printf("\nDirectory\tFiles");

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

printf("\n%s\t\t",dir[i].dname);

for(k=0;k<dir[i].fcnt;k++)

printf("\t%s",dir[i].fname[k]);

}
break;

default:exit(0);

getch();

OUTPUT:

4. Create Directory 2. Create File 3. Delete 4.File Search File 5. Display 6. Exit

Enter your choice -- 1


Enter name of directory -- DIR1 Directory created
1. Create Directory 2. Create File 3. Delete File 4.Search File 5. Display 6. Exit

Enter your choice -- 1


Enter name of directory -- DIR2 Directory
created

1. Create Directory 2. Create File 3. Delete File 4.Search File 5. Display 6. Exit

4. Enter your choice -- 2 Enter name


of the directory – DIR1

Enter name of the


file -- A1 File
created

2. Create Directory 2. Create File 3. Delete File 4.Search File 5.

Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A2
File created
3. Create Directory 2. Create File 3. Delete File 4.Search File 5.

Display 6. Exit Enter your choice – 6

RESULT:
Thus the program was executed and verified successfully
Sequential file for processing the student information

AIM:
To write a C program for sequential file for processing the student information.

ALGORITHM:

Step-1: Start the program.

Step-2: Get the number of records user want to store in the system.

Step-3: Using Standard Library function open the file to write the data into the file.

Step-4: Store the entered information in the system.

Step-5: Using do..While statement and switch case to create the


options such as 1-DISPLAY, 2.SEARCH, 3.EXIT.

Step-6: Close the file using fclose() function.


Step-7: Process it and display the result.
Step-8: 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_elementnode;


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,intlev,char *dname,intlx,intrx,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]);

}}

INPUT

Enter Name of dir/file(under root): ROOT

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for ROOT): 2

Enter Name of dir/file(under ROOT): USER1

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for USER1): 1

Enter Name of dir/file(under USER1): SUBDIR1

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for SUBDIR1): 2

Enter Name of dir/file(under USER1): JAVA

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for JAVA): 0

Enter Name of dir/file(under SUBDIR1): VB

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for VB): 0
Enter Name of dir/file(under ROOT): USER2

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for USER2): 2

Enter Name of dir/file(under ROOT): A

Enter 1 for Dir/2 for File: 2

Enter Name of dir/file(under USER2): SUBDIR2

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for SUBDIR2): 2

Enter Name of dir/file(under SUBDIR2): PPL

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for PPL): 2

Enter Name of dir/file(under PPL): B

Enter 1 for Dir/2 for File: 2

Enter Name of dir/file(under PPL): C

Enter 1 for Dir/2 for File: 2

Enter Name of dir/file(under SUBDIR): AI

Enter 1 for Dir/2 for File: 1

No of subdirectories/files(for AI): 2

Enter Name of dir/file(under AI): D

Enter 1 for Dir/2 for File: 2

Enter Name of dir/file(under AI): E

Enter 1 for Dir/2 for File: 2


Output:

RESULT:
Thus the program was executed and verified successfully.
EX NO: 14 FILE ALLOCATION STRATEGIES
EX.NO:14 A SEQUENTIAL FILE ALLOCATION

AIM: To implement sequential file allocation technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the number of files.

Step 3: Get the memory requirement of each file.

Step 4: Allocate the required locations to each in sequential order.

a). Randomly select a location from available location s1= random(100);

b). Check whether the required locations are free from the selected

location. c). Allocate and set flag=1 to the allocated locations.

Step 5: Print the results fileno, length , Blocks allocated. Step 6: Stop the progr

PROGRAM

#include<stdio.h>
main()
{

int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("\n Enter 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("\n the file is allocated to disk");

printf("\n if u want to enter more files?(y-1/n-0)");

scanf("%d",&c);
if(c==1)
gotoX;

else

exit();
getch();

}
OUTPUT

Output: Enter the starting block & length of file 4 10

4->1

5->1

6->1

7->1

8->1

9->1

10->1

11->1

12->1

13->1

The file is allocated to disk

If you want to enter more files? (Y-1/N-0)

RESULT:
Thus the program was executed and verified successfully
EX.NO:14 B INDEXED FILE ALLOCATION

AIM: To write a C program to implement file Allocation concept using


the technique indexed allocation Technique

ALGORITHM:
Step 1: Start the Program

Step 2: Get the number of files.


Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly.

Step 5:Print the results file no,length, blocks allocated.

Step 6: Stop the execution.

PROGRAM
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;

main()
{

clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)

{
f[p]=1;

printf("enter no of files on index\t");


s canf("%d",&n);
}
else
{

printf("Block already allocated\n");


goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{

printf("Block already allocated");


goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)

printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);

printf(" Enter 1 to enter more files and 0 to exit\t");

scanf("%d",&c);

if(c==1)
goto x;
else
exit();
getch();
}

OUTPUT:

Enter index block 9

Enter no of files on index 3 1 2 3

Allocated

File indexed 9-> 1:1

9-> 2:1

9->3:1

Enter 1 to enter more files and 0 to exit.

RESULT:
Thus the program was executed and verified successfully
EX.NO:14 C LINKED FILE ALLOCATION

AIM: To write a C program to implement File Allocation concept using


the technique Linked List Technique.

ALGORITHM:

Step 1: Start the Program


Step 2: Get the number of files.
Step 3: Allocate the required locations by selecting a location randomly.

Step 4: Check whether the selected location is free.


Step 5: If the location is free allocate and set flag =1 to the allocated locations.

Step 6: Print the results file no, length, blocks allocated.

Step 7: Stop the execution

PROGRAM:

#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;

printf("Enter how many blocks that are already


allocated"); scanf("%d",&p);

printf("\nEnter the blocks no.s that are already allocated");


for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}

printf("Enter the starting index block &


length"); scanf("%d%d",&st,&len); k=len;

for(j=st;j<(k+st);j++)

{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else

{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");

scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );

}
Output:
Enter how many blocks are already allocated 3

Enter the blocks no’s that are already allocated 4 7 9

Enter the starting index block & length 3 7

3-> 1

4-> File is already allocated 5->1

6->1

7-> File is already allocated 8->1

9-> File is already allocated 10->1

11->1

12->1

If u want to enter one more file? (yes-1/no-0)

RESULT:
Thus the program to implement the linked file allocation was executed successfully
EX NO: 15 DISK SCHEDULING ALGORITHMS

AIM:
Write a C program to simulate disk scheduling algorithms

a)FCFS b) SCAN c) C-SCAN

PROGRAM

FCFS DISK SCHEDULING ALGORITHM

#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0;
float avhm;
clrscr();

printf(“enter the no.of tracks”);


scanf(“%d”,&n);
printf(“enter the tracks to be traversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{

tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)

tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”); for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverage header movements:%f",avhm);
getch();
}

OUTPUT

Enter no.of tracks:9


Enter track position:55 58 60 70 18 90 150 160 184
Tracks traversed Difference between tracks
55 45
58 3
60 2

70 10
18 52
90 72
150 60
160 10
184 24

Average header movements:30.888889


SCAN DISK SCHEDULING ALGORITHM

#include<stdio.h>
main()

int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p,


sum=0; clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);

printf("enter the position of head");


scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{

temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;

p=0;

while(t[j]!=0)
{
atr[p]=t[j];
j--;
p++;
}

atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];

else

d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);

getch();

}
OUTPUT
Enter no.of tracks:9

Enter track position:55 58 60 70 18 90 150 160 184 Tracks traversed


Difference between tracks
150 50
160 10

1841 24
90 94
70 20

60 10

58 2

55 3

18 37

Average header movements: 27.77


C-SCAN DISK SCHEDULING ALGORITHM

#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;

clrscr();

printf("enter the no of tracks to be traveresed");


scanf("%d'",&n);

printf("enter the position of head");


scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;

printf("enter the tracks");


for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])

{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
for(i=0;i<=n+2;i++)
if(t[i]==h)
j=i;
break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{

atr[p]=t[i];
i++;
p++;
}
for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])

d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}

OUTPUT
Enter the track position: 55 58 60 70 18 90 150 160 184
Enter starting position : 100
Tracks traversed Difference Between tracks
150 50
160 10
184 24
18 240
55 37
58 3

60 2
70 10
90 29

Average seek time : 35.7777779

RESULT:

Thus the program to implement disk Scheduling algorithm has been executed and verified.

You might also like