0% found this document useful (0 votes)
42 views141 pages

OS Record - Satheesh

Uploaded by

Aravind Krishnan
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)
42 views141 pages

OS Record - Satheesh

Uploaded by

Aravind Krishnan
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/ 141

APOLLO ENGINEERING

COLLEGE

Chennai

LABORATORY RECORD

Name: _____________________________________________________
Class : II B.E (CSE) Semester: VI
Subject Code & Name: CS3461–Operating Systems Laboratory
Academic Year: 2021 – 2025
APOLLO ENGINEERING
COLLEGE
Chennai

Certificate

Name:
Class: II B.E (CSE) Reg. No:

Certificate that this is a bonafide record of work done by the above

student in the CS3461-Operating Systems Laboratory during the year 2021 – 2022.

Staff-In-Charge Head of Department

Submitted for the practical examination held on ………………………….

Internal Examiner External Examiner


TABEL OF CONTENTS

SL. NO DATE NAME OF THE EXPERIMENT PAGE SIGN


NO

1. Installation of windows operating system 1

2. UNIX commands and Shell Programming 9

3a. System Calls: Fork 24

3b. System Calls: Exit 25

3c. System Calls: Getpid 27

3d. System Calls: Wait 28

3e. System Calls: Close 30

4a. CPU Scheduling: FCFS Scheduling 31

4b. CPU Scheduling: Shortest job Scheduling 34

4c. CPU Scheduling: Priority Scheduling 38

4d. 42
CPU Scheduling: Round Robin

5. 47
Inter Process Communication Strategy

6. 51
Implement mutual exclusion by Semaphone

7. 56
To avoid Deadlock using Banker’s Algorithm

8. 63
To implement Deadlock Detection Algorithm

9. 68
Implement Threading

10. 71
Implement Paging Technique

11a. 74
Memory Allocation Methods: First Fit
11b. 77
Memory Allocation Methods: Worst Fit

11c. 81
Memory Allocation Methods: Best Fit

12a. 85
Page Replacement Algorithm: FIFO

12b. 88
Page Replacement Algorithm: LRU

12c. 93
Page Replacement Algorithm: LFU

13a. File Organization Technique: Singel level 100


Directory
13b. File Organization Technique: Two level 105
Directory
14a. 112
File Allocation Strategies: Sequential

14b. 115
File Allocation Strategies: Indexed

14c. 119
File Allocation Strategies: Linked

15a. 122
Disk Scheduling Algorithm: FCFS

15b. 125
Disk Scheduling Algorithm: SSTF

16. Installation of Linux operating system using 128


VMare
Ex.No:1

Date: Installation of windows operating system

Instructions to Install Ubuntu Linux 12.04 (LTS) along with Windows Back Up Your Existing
Data!

This is highly recommended that you should take backup of your entire data before start with the
installation process.

Obtaining System Installation Media

Download latest Desktop version of Ubuntu from this link: http://www.ubuntu.com/download/desktop

Booting the Installation System

There are several ways to boot the installation system. Some of the very popular ways are , Booting from
a CD ROM, Booting from a USB memory stick, and Booting from TFTP.

Here we will learn how to boot installation system using a CD ROM.

Before booting the installation system, one need to change the boot order and set CD-ROM as first boot
device.

Changing the Boot Order of a Computers

As your computer starts, press the DEL, ESC, F1, F2, F8 or F10 during the initial startup screen.
Depending on the BIOS manufacturer, a menu may appear. However, consult the hardware
documentation for the exact key strokes. In my machine, its DEL key as shown in following screen- shot.

1
2. Find the Boot option in the setup utility. Its location depends on your BIOS. Select the Boot
option from the menu, you can now see the options Hard Drive, CD-ROM Drive, Removable
Devices Disk etc.

3. Change the boot sequence setting so that the CD-ROM is first. See the list of “Item Specific
Help” in right side of the window and find keys which is used to toggle to change the boot
sequence.

4. Insert the Ubuntu Disk in CD/DVD drive.


5. Save your changes. Instructions on the screen tell you how to save the changes
on yourcomputer. The computer will restart with the changed settings.
Machine should boot from CD ROM, Wait for the CD to load...

In a few minutes installation wizard will be started. Select your language and click the "Install
Ubuntu" button to continue...

2
Optionally, you can choose to download updates while installing and/or install third party software,
such as MP3 support. Be aware, though, that if you select those options, the entire installation process
will be longer!

Since we are going to create partitions manually, select Something else, then click Continue. Keep
in mind that even if you do not want to create partitions manually, it is better to select the same option
as indicated here. This would insure that the installer will not overwrite your Windows , which will
destroy your data. The assumption here is that sdb will be used just for Ubuntu 12.04, and that there
are no valuable data on it.

3
Where are you? Select your location and Click the "Continue" button.

Keyboard layout

Select your keyboard layout and UK (English) and Click on “Continue” button.

4
Who are you?

Fill in the fields with your real name, the name of the computer (automatically generated, but can be
overwritten), username, and the password.

Also at this step, there's an option called "Log in automatically." If you check it, you will
automatically be logged in to the Ubuntu desktop without giving the password.

Option "Encrypt my home folder," will encrypt your home folder. Click on the "Continue" button to
continue...

Now Ubuntu 12.04 LTS (Precise Pangolin) operating system will be installed.

5
It will take approximately 10-12 minutes (depending on computer's speed), a pop-up window will
appear, notifying you that the installation is complete, and you'll need to restart the computer in order
to use the newly installed Ubuntu operating system. Click the "Restart Now" button.

Please remove the CD and press the "Enter" key to reboot. The computer will be restarted. In a few
seconds, you should see Windows 7′s boot menu with two entires listed – Windows 7 and Ubuntu
12.04 (LTS).

Then you may choose to boot into Windows 7 or Ubuntu 12.04 using the UP/Down arrow key.

6
Please select Ubuntu 12.04 (LTS) and press Enter to boot the machine in Ubuntu 12.04

Linux.

Here you can see the users on the machine, Click on the user name and enter the password and press
Enter key to login.

7
We have successfully install and login to Ubuntu 12.04 LTS.

RESULT:

Thus the installation of windows operating system is completed.

8
Ex.No:2

Date: Illustrate UNIX commands and Shell Programming

AIM:

To Illustrate UNIX commands and Shell Programming.

1. Calendar Command:

 cal is a standard program on Unix and Unix-like operating systems that prints an ASCII calendar
of the given month or year.

 If the user does not specify any command-line options, cal will print a calendar of the current
month.

dell@dell-Inspiron-5423:~$ cal

November 2018

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 29 30

2. Date command:

Print or set the system date and time. The following options are used with this command.

%a locale's abbreviated weekday name (Sun..Sat)

%A locale's full weekday name, variable length (Sunday..Saturday)

%b locale's abbreviated month name (Jan..Dec)

%B locale's full month name, variable length (January..December)

%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)

%C century (year divided by 100 and truncated to an integer) [00-99]

9
%d day of month (01..31)

%D date (mm/dd/yy)

%e day of month, blank padded ( 1..31)

%F same as %Y-%m-%d

%g the 2-digIt year corresponding to the %V week number

%G the 4-digIt year corresponding to the %V week number

%h same as %b

%H hour (00..23)

%I hour (01..12)

%j day of year (001..366)

%k hour ( 0..23)

%l hour ( 1..12)

%m month (01..12)

%M minute (00..59)

%n a newline

%N nanoseconds (000000000..999999999)

%p locale's upper case AM or PM indicator (blank in many locales)

%P locale's lower case am or pm indicator (blank in many locales)

%r time, 12-hour (hh:mm:ss [AP]M)

%R time, 24-hour (hh:mm)

%s seconds since `00:00:00 1970-01-01 UTC' (a GNU extension)

%S second (00..60); the 60 is necessary to accommodate a leap second

%t a horizontal tab

%T time, 24-hour (hh:mm:ss)

%u day of week (1..7); 1 represents Monday

%U week number of year wIth Sunday as first day of week (00..53)

10
%V week number of year wIth Monday as first day of week (01..53)

%w day of week (0..6); 0 represents Sunday

%W week number of year wIth Monday as first day of week (00..53)

%x locale's date representation (mm/dd/yy)

%X locale's time representation (%H:%M:%S)

%y last two digIts of year (00..99)

%Y year (1970...)

dell@dell-Inspiron-5423:~$ date

Thu Nov 8 15:53:29 IST 2018

dell@dell-Inspiron-5423:~$ date +%m

11

dell@dell-Inspiron-5423:~$ date +%a

Thu

dell@dell-Inspiron-5423:~$ date +%B

November

3. Who command: Lists who is logged on your machine

dell@dell-Inspiron-5423:~$ who

dell :0 2018-11-08 13:39 (:0)

dell pts/1 2018-11-08 15:51 (:0)

4. Who am I command: Displays the name of the user who runs the command.

dell@dell-Inspiron-5423:~$ who am i

dell pts/1 2018-11-08 15:51 (:0)

5. Finger Command:

1. In Unix, finger is a program you can use to find information about computer users.

2. It usually lists the login name, the full name, and possibly other details about the user you
are fingering.

11
$ finger

Login Name Tty Idle Login Time Office Office Phone

Mani pts/2 29 Oct 29 10:53 (197.168.1.4)

Balajik tty5 31 Oct 29 10:52

Balajik pts/1 Oct 29 10:55 (197.168.1.7)

6.Help command:

To display the help information about a particular command.

$ man clear

clear(1) clear(1)

NAME

clear - clear the terminal screen

SYNOPSIS

clear

DESCRIPTION

clear clears your screen if this is possible. IT looks in the environment for the terminal type and
then in the terminfo database to figure

out how to clear the screen.

clear ignores any command-line parameters that may be present.

SEE ALSO

tput(1), terminfo(5)

This describes ncurses version 5.5 (patch 20060715).

clear(1)

(END)

7.Clear Command: To clear the screen.

$ clear

8. ID command: To display the user id and group id information about a user. i.e. user identITy.

12
$ id

uid=832(Balajik) gid=832(Balajik) groups=832(Balajik) context=user_u:system_r:unconfined_t

9. write command: Send message to another user.

10. echo command:

Display a line of text.

$ echo good

good

11. tty command:

print the file name of the terminal connected to standard input

$ tty

/dev/pts/2

12. Changing Password:

update a user’s authentication tokens(s)

II . FILE MANIPULATION COMMANDS

1.Create a file:

$ vi file1.c

(Type the contents of the file)

To save press shift+:+wq

(OR)

$ cat > filename

(Type the contents of the file)

press ctrl+Z to quit

$ cat filename ( To view the content in file )

13
2.List information about files and directories

$ ls

chemical file1.c file2.cpp

3.List all directories (including .(dot) entries in the long listing.)

$ ls -la

total 104

drwx------ 4 Balajik Balajik 4096 Oct 29 11:31 .

drwxr-xr-x 17 root root 4096 Oct 29 10:52 ..

-rw------- 1 Balajik Balajik 3 Oct 29 10:55 .bash_history

-rw-r--r-- 1 Balajik Balajik 24 Oct 29 10:51 .bash_logout

-rw-r--r-- 1 Balajik Balajik 176 Oct 29 10:51 .bash_profile

-rw-r--r-- 1 Balajik Balajik 124 Oct 29 10:51 .bashrc

drwxrwxr-x 2 Balajik Balajik 4096 Oct 29 11:31 chemical

-rw-r--r-- 1 Balajik Balajik 515 Oct 29 10:51 .emacs

-rw-rw-r-- 1 Balajik Balajik 47 Oct 29 11:30 file1.c

-rw-rw-r-- 1 Balajik Balajik 48 Oct 29 11:31 file2.cpp

drwxr-xr-x 3 Balajik Balajik 4096 Oct 29 10:51 .kde

-rw------- 1 Balajik Balajik 763 Oct 29 11:31 .viminfo

-rw-r--r-- 1 Balajik Balajik 658 Oct 29 10:51 .zshrc

4.Copy a file:

$ cp file1.c file3.c

$ ls

chemical file1.c file2.cpp file3.c

5.Move or rename a file:

$ mv file1.c file4.c

14
$ ls

chemical file2.cpp file3.c file4.c

6.Remove a file:

$ rm file3.c

$ ls

chemical file2.cpp file4.c

7.Change the access permission of a file:

$ chmod 777 file2.cpp

-rwxrwxrwx 1 Balajik Balajik 48 Oct 29 11:31 file2.cpp

8.Display the contents of a file onto screen:

$ cat file4.c

#include <stdio.h>

void main()

return 0;

9.Word count command:

IT is used to count the number of lines, words and characters in a file.

$ wc f1.txt

12 43 221 f1.txt

10.Find Command:

IT is used to locate files in a directory and ITs subdirectories.

$ find

15
../pointer1.c

./sort.c

./f1.txt

./file_copy.c

./pointer2.c

./pointer3.c

./a.out

11. Comparing files:

compare two files

$ cmp file1 file2

file1 file2 differ: byte 1, line 1

12. Differentiating Files:

find differences between two files

$ diff file1 file2

1c1

< Hello

---

> world

13. Touch Command:

change file timestamps

$ ls -l file2

-rw-rw-r-- 1 Balajik Balajik 6 Nov 16 08:50 file2

$ touch file2

$ ls -l file2

-rw-rw-r-- 1 Balajik Balajik 6 Nov 16 09:23 file2

14. Link Command:

16
make link between files

$ ls

file1 file2 file3 dma

$ ln -t dma file1

$ cd dma

$ ls

file1

15. Head Command:

Displays the inITial part of the text file. By default, IT displays the first 10 lines of a file.

$ head -3 f1.txt

Good day

CSE is raining

IT may be leave tomorrow.

16.Tail command:

Displays the last part of he text file. By default, IT displays the last 10 lines of a file.

$ tail -5 f1.txt

The college is good

Boys are playing

the ball is red in color.

girls are dancing

III. DIRECTORY COMMANDS

1. Create a directory:

$ mkdir dir1

2. Change from one directory to another:

17
$ cd dir1

3. Display the full path of the current working directory:

$ pwd

/home/Balajik/dir1

4. Remove a directory:

$ rmdir dir

$ ls

file2.cpp file4.c

5.Change the owner of a specified file:

Only the super-user can change this.

[root@CSE ~]$chown Mani file2.cpp

6.Change the group of a file:

Only the super-user can change this.

[root@CSE ~]$chgrp Mani file2.cpp

It changes the group-id of the file o a group called ‘Mani’ .

IV . PIPES AND FILTERS

Pipes: A mechanism in which the output of one command is redirected as input to another command.

Example 1: Count the number of lines in who command

$ who | wc -l

Example2: Display the first 3 lines of a file

$ cat sort.c | head -3

18
#include <stdio.h>

#define SIZE 50

void sortnum(int *n,int d);

Filter: Sort and grep commands are used in customizing the various commands.

Sort Filter:

It arranges the input taken from standard input in alphabetical order.

Options with sort are: -r,-f,-n,-b,-t

$ sort f1.txt

Boys are playing

girls are dancing

Good day

How to go to school

IT is raining

IT may be leave tomorrow.

Kids are playing outside.

the ball is red in color.

The boy is playing with the ball.

The college is good

Welcome

Grep filter:

It is used to search for a particular pattern from a file or from std. input. IT displays those lines on the
standard output. “grep” stands for “ Global Search for Regular Expression”

$ grep "playing" f1.txt

Kids are playing outside.

The boy is playing wITh the ball.

Boys are playing

egrep command:

19
IT is an extension for grep command Multiple Patterns can be searched in a file by issuing a single
command. These multiple patterns should be separated by a pipe ( | ) symbol.

$ egrep "good|girls" f1.txt

The college is good

girls are dancing

fgrep command:

IT stands for “fixed grep”. The command is used to extract only fixed strings without the use of any
regular expression.

$ fgrep "boy" f1.txt

The boy is playing with the ball.

Other Filters:

1.Uniq filter: It compares the adjacent lines in the sorted output file.

2.More filter:

IT displays the output of a command on the screen page by page. The next screen is viewd by pressing the
space bar.

$ ls | more

a.out

f1.txt

file_copy.c

pointer1.c

pointer2.c

pointer3.c

sort.c

3.Cut command:

Cuts the specified character from the file

$ cut -c 1 file1

20
B

4.Paste Command:

IT merges the contents of 2 files in to a single file. IT reads a line from each file in the list
specified and combines them into a single file.

$ cat f2.txt

Anand

Akhila

Arun

$ cat f3.txt

Badri

Rajaraman

Anand

$ cat f4.txt

Balajik

Subha

Akila

$ paste f3.txt f2.txt f4.txt

Badri Anand Balajik

Rajaraman Akhila Subha

Anand Arun Akila

5.Usage of pipes and filters together:

An important use of grep using the “^”pattern is to list the directories.

$ ls -l | grep "^d"

drwxrwxr-x 2 Balajik Balajik 4096 Oct 30 11:28 clab

21
6. Tee Command:

(read from standard input and write to standard output and files)

$ tee jes

CSE

CSE

[3]+ Stopped tee jes

$ cat jes

CSE

uniq Command

$ cat kb

cubs

kItten

kItten

kiwi

parrot

peacock

puppy

puppy

(report or omIt repeated lines)

$ uniq -d kb

kITten

puppy

$ uniq -u kb

cubs

kiwi

22
parrot

peacock

$ uniq -c kb

1 cubs

2 kItten

1 kiwi

1 parrot

1 peacock

2 puppy

RESULT:

Thus the study and execution of Unix commands has been complied successfully.

23
Ex.No:3a

Date: System Call: Fork

AIM:

To create a process using Fork System call.

ALGORITHM:

1. Declare the necessary variables.

2. Parent process is the process of the program which is running.

3. Create the child1 process using fork() When parent is active.

4. Create the child2 process using fork() when child1 is active.

5. Create the child3 process using fork() when child2 is active.

PROGRAM:

#include<stdio.h>

int main(void)

int fork(void),value;

value=fork();

printf("main:value =%d\n",value);

return 0;

OUTPUT:

main:value =0

main:value =3172

RESULT:

Thus the program for fork system call has been executed successfully.

24
Ex.No:3b

Date: System Call: Exit

AIM:
To Execute a Unix Command in a ‘C’ program using exit() system call.

ALGORITHM:
1. Start the program
2. Create a C program example.c to print the current id of the process.
3. Create an array of character pointers. The last element of this array should be NULL as the terminating
point.
4. Use the function execv() which takes the file name and the character pointer array as its argument.
5. Use ./ with the name of file, to specify the path of the file.
6. Stop the program.

PROGRAM:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("PID of example.c = %d\n", getpid());
char *args[] = {"Hello", "C", "Programming", NULL};
execv("./hello", args);
printf("Back to example.c");
return 0;
}
hello.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])

25
{
printf("We are in Hello.c\n");
printf("PID of hello.c = %d\n", getpid());
return 0;
}

OUTPUT:
dell@dell-Inspiron-5423:~$ gcc -o example example.c
dell@dell-Inspiron-5423:~$ gcc -o hello hello.c
dell@dell-Inspiron-5423:~$ ./example
PID of example.c = 3752
We are in Hello.c
PID of hello.c = 3752

RESULT:
Thus the program for exit system call has been executed successfully.

26
Ex.No:3c

Date: System Call: Getpid

AIM:
To Execute a Unix Command in a ‘C’ program using getpid() system call.

ALGORITHM:
1. Start the program
2. Declare the necessary variables
3. The getpid() system call returns the process ID of the parent of the calling process.
5. Stop the program.

PROGRAM:
#include<stdio.h>
int main()
{
int pid;
pid=getpid();
printf("process ID is %d\n",pid);
pid=getppid();
printf("parent process ID id %d\n",pid);
}

OUTPUT:
process ID is 3187
parent process ID id 3139

RESULT:
Thus the program for getpid system call has been executed successfully.

27
Ex.No:3d

Date: System Call: Wait

AIM :
To Execute a Unix Command in a ‘C’ program using wait() system call.

ALGORITHM :
1. Start the program
2. Initialize the necessary variables
3. Use wait() to return the parent id of the child else return -1 for an error
4. Stop the program.

PROGRAM:
#include<stdio.h>
#include<unistd.h>
int main(void)
{
int pid,status,exitch;
if((pid=fork())==-1)
{
perror("error");
exit (0);
}
if(pid==0)
{
sleep(1);
printf("child process");
exit (0);
}
else
{
printf("parent process\n");
if((exitch=wait(&status))==-1)

28
{
perror("during wait()");
exit (0);
}
printf("parent existing\n");
exit (0);
}
}

OUTPUT :
parent process
child process parent existing

RESULT:
Thus the program for wait system call has been executed successfully.

29
Ex.No:3e

Date: System Call: Close

AIM:
To Execute a Unix Command in a ‘C’ program using close() system call.

ALGORITHM:
1. Start the program
2. Declare the necessary variables
3. The close() system call returns the process ID of the parent of the calling process.
5. Stop the program

PROGRAM:
#include<stdio.h>
#include<fcntl.h>
{
int fd1 = open(“foo.txt”,O_RDONLY,0);
close(fd1);
int fd2 = open(“baz.txt”,O_RDONLY,0);
printf(“fd2 = %d\n”,fd2);
exit(0);
}

OUTPUT:
fd2 = 3

RESULT:
Thus the program for wait system call has been executed successfully.

30
Ex.No:4a

Date: CPU Scheduling: FCFS Scheduling

AIM:

To write a C program to implement First Come First Serve scheduling algorithm. (FCFS)

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: Set the waiting of the first process as ‘0’ and its burst time as its turn around time

Step 5: for each process in the Ready Q calculate Waiting time for process(n)= waiting time of
process (n-1) + Burst time of process (n-1) Turn around time for Process(n)= waiting time of
Process(n)+ Burst time for process(n)

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

Step 7: Stop the process

PROGRAM:

#include<stdio.h>

void main()

int i,n,sum,wt,tat,twt,ttat;

int t[10];

float awt,atat;

printf("Enter number of processors:\n");

scanf("%d",&n);

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

31
printf("\n Enter the Burst Time of the process %d",i+1);

scanf("\n %d",&t[i]);

printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");

printf("\n Process ID \t Waiting Time \t Turn Around Time \n");

printf("1 \t\t 0 \t\t %d \n",t[0]);

sum=0;

twt=0;

ttat=t[0];

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

sum+=t[i-1];

wt=sum;

tat=sum+t[i];

twt=twt+wt;

ttat=ttat+tat;

printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);

printf("\n\n");

awt=(float)twt/n;

atat=(float)ttat/n;

printf("\n Average Waiting Time %4.2f",awt);

printf("\n Average Turnaround Time %4.2f",atat);

32
OUTPUT:

dell@dell-Inspiron-5423:~$ cc fcfs.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter number of processors: 3

Enter the Burst Time of the process 124

Enter the Burst Time of the process 23

Enter the Burst Time of the process 33

FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Time Turn Around Time

1 0 24

2 24 27

3 27 30

Average Waiting Time 17.00

Average Turnaround Time 27.00

RESULT:

Thus the C program to implement First Come First Serve scheduling algorithm (FCFS) is
executed successfully.

33
Ex.No:4b

Date: CPU Scheduling: Shortest Job First Scheduling

AIM:

To write a C program to implement Shortest Job First scheduling algorithm. (SJF)

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: For each process in the ready queue, calculate Waiting time for process(n)= waiting time
of process (n-1) + Burst time of process (n-1) Turn around time for Process(n)= waiting time of
Process(n)+ Burst time for process(n)

Step 7: Calculate Average waiting time = Total waiting Time / Number of process Average
Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop the process

PROGRAM:

#include<stdio.h>

void main()

int i,j,k,n,sum,wt[10],tt[10],twt,ttat;

int t[10],p[10];

float awt,atat;

printf("Enter number of process\n");

scanf("%d",&n);

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

printf("\n Enter the Burst Time of Process %d: ",i);

scanf("\n %d",&t[i]);

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

p[i]=i;

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

for(k=i+1;k<n;k++)

if(t[i]>t[k])

int temp;

temp=t[i];

t[i]=t[k];

t[k]=temp;

temp=p[i];

p[i]=p[k];

p[k]=temp;

printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");

printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n\n");

wt[0]=0;

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

35
{

sum=0;

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

wt[i]=sum+t[k];

sum=wt[i];

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

tt[i]=t[i]+wt[i];

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

printf("%5d \t\t%5d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);

twt=0;

ttat=t[0];

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

twt=twt+wt[i];

ttat=ttat+tt[i];

awt=(float)twt/n;

atat=(float)ttat/n;

36
printf("\n AVERAGE WAITING TIME %4.2f",awt);

printf("\n AVERAGE TURN AROUND TIME %4.2f\n:",atat);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc sjf.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter number of process 3

Enter the Burst Time of Process 0: 3

Enter the Burst Time of Process 1: 1

Enter the Burst Time of Process 2: 2

SHORTEST JOB FIRST SCHEDULING ALGORITHM

PROCESS ID BURST TIME WAITING TIME TURNAROUND TIME

1 1 0 1

0 3 1 4

2 2 4 6

AVERAGE WAITING TIME 1.67

AVERAGE TURN AROUND TIME 3.67

RESULT:

Thus the C program to implement Shortest Job First scheduling algorithm (SJF) is executed
successfully.

37
Ex.No:4c

Date: CPU Scheduling: Priority Scheduling

AIM:

To write a C program to implement Priority scheduling algorithm.

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: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turn around time

Step 6: For each process in the Ready Q calculate

1. Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)

2. Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)

Step 7: Calculate

3. Average waiting time = Total waiting Time / Number of process

4. Average Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop the process

PROGRAM:

#include <stdio.h>

void main()

int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;

float awt,atat;

printf("\n-----------PRIORITY SCHEDULING--------------\n");

printf("Enter the No of Process: ");

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

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

pid[i] = i;

printf("Enter the Burst time of Pid %d : ",i);

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

printf("Enter the Priority of Pid %d : ",i);

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

// Sorting start

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

for(j=i+1;j<n;j++)

if (pr[i] > pr[j] )

t = pr[i];

pr[i] = pr[j];

pr[j] = t;

t = bt[i];

bt[i] = bt[j];

bt[j] = t;

t = pid[i];

pid[i] = pid[j];

pid[j] = t;

39
}

// Sorting finished

tat[0] = bt[0];

wt[0] = 0;

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

wt[i] = wt[i-1] + bt[i-1];

tat[i] = wt[i] + bt[i];

printf("\n---------------------------------------------------------------\n");

printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");

printf("\n--------------------------------------------------------------\n");

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

printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);

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

ttat = ttat+tat[i];

twt = twt + wt[i];

awt = (float)twt / n;

atat = (float)ttat / n;

printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);

40
OUTPUT:

dell@dell-Inspiron-5423:~$ cc priority.c

dell@dell-Inspiron-5423:~$ ./a.out

-----------PRIORITY SCHEDULING--------------

Enter the No of Process: 3

Enter the Burst time of Pid 0 : 1

Enter the Priority of Pid 0 : 3

Enter the Burst time of Pid 1 : 4

Enter the Priority of Pid 1 : 1

Enter the Burst time of Pid 2 : 2

Enter the Priority of Pid 2 : 2

---------------------------------------------------------------

Pid Priority Burst time WaitingTime TurnArroundTime

--------------------------------------------------------------

1 1 4 0 4

2 2 2 4 6

0 3 1 6 7

Avg.Waiting Time: 3.333333

Avg.Turn Around Time: 5.666667

RESULT:

Thus the C program to implement Priority scheduling algorithm is executed successfully.

41
Ex.No:4d

Date: CPU Scheduling: Round Robin Scheduling

AIM:

To Write a C program to implement Round Robin Scheduling algorithm.

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 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) Turn around 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 Average waiting time = Total waiting Time / Number of process Average
Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop the process

PROGRAM:

#include<stdio.h>

int main()

int bt[20],gc[20],wt[20],tat[20],bt1[20],st[20],ts,n,i,j,k,count=0,count1,sum_bt=0,tq;

int swt=0,stat=0,temp,sq=0,c,p=0;

float awt=0.0,atat=0.0;

printf("Enter the no. of process");

scanf("%d",&n);

42
printf("Enter the burst time");

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

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

bt1[i]=bt[i];

st[i]=bt[i];}

printf("Enter the time slice");

scanf("%d",&ts);

tq=ts;

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

sum_bt=sum_bt+bt[i];

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

do

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

if(bt[i]>=ts)

for(j=count; j<(count+ts); j++)

gc[j]=i+1;

count+=ts;

bt[i]=bt[i]-ts;

else

for(j=count; j<=(count+bt[i]); j++)

43
gc[j]=i+1;

count+=bt[i];

bt[i]=0;

}while(bt[k]!=0);

while(1)

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

temp=tq;

if(st[i]==0)

count++;

continue;

if(st[i]>tq)

st[i]=st[i]-tq;

else

if(st[i]>=0)

temp=st[i];

st[i]=0;

sq=sq+temp;

44
tat[i]=sq;

if(n==count)

break;

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

wt[i]=tat[i]-bt1[i];

swt=swt+wt[i];

stat=stat+tat[i];

awt=(float)swt/n;

atat=(float)stat/n;

printf("Process_no Burst time Wait time Turn around time\n");

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

printf("%d %d %d %d\n",i+1,bt1[i],wt[i],tat[i]);

printf("Avg wait time is %f \n Avg turn around time is %f\n",awt,atat);

45
OUTPUT:

dell@dell-Inspiron-5423:~$ cc rr1.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter the no. of processs3

Enter the burst time24 3 3

Enter the time slice2

Process_no Burst time Wait time Turn around time

1 24 6 30

2 3 6 9

3 3 7 10

Avg wait time is 6.333333

Avg turn around time is 16.333334

dell@dell-Inspiron-5423:~$

RESULT:

Thus the C program to implement the Round Robin scheduling mechanism is executed
sucessfully.

46
Ex.No:5

Date: Illustrate inter process communication strategy

AIM:

To Write a C program to implement Inter Process Communication using pipe.

ALGORITHM:

Step 1: Start the program

Step 2: There is a main process P1 and 2 child processes C1 and C2 .

Step 3: The main process reads a string and passes it to one of the child process ,the child
process C1 receive string from P1.

Step 4: Find its length and sends the value to P1.

Step 5: C1 also reads an integer array of length equal to the length of the string and sends it to
C2.

Step 6: C2 receive integer array from C1 and find the sum of the elements and sends the sum
to the parent process P1.

Step 7: The sleep() function is used in the program to suspend the processes for a desired
interval of time.

Step 8: Stop the program

PROGRAM:

#include<stdio.h>

#include<unistd.h>

#include<string.h>

void main()

int pid[2],pid1[2],pid2[2],pid3[2],pid4[2];

int a[20],i,l,s=0;

char str[20];

47
pipe(pid);

pipe(pid1);

pipe(pid2);

pipe(pid3);

pipe(pid4);

if(fork()==0)

sleep(5);

close(pid[1]);

read(pid[0],str,sizeof(str));

for(i=0,l=0;str[i]!='\0';i++)

l=l+1;

close(pid3[0]);

write(pid3[1],&l,sizeof(l));

sleep(6);

printf("Enter %d array elements:",l);

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

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

close(pid1[0]);

write(pid1[1],a,sizeof(a));

close(pid4[0]);

write(pid4[1],&l,sizeof(l));

else if(fork()==0)

sleep(2);

48
close(pid1[1]);

close(pid4[1]);

read(pid4[0],&l,sizeof(l));

read(pid1[0],a,sizeof(a));

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

s=s+a[i];

close(pid2[0]);

write(pid2[1],&s,sizeof(s));

else

printf("\nEnter string:");

scanf("%s",str);

close(pid[0]);

write(pid[1],str,sizeof(str));

sleep(7);

close(pid3[1]);

read(pid3[0],&l,sizeof(l));

printf("\nThe string length=%d",l);

sleep(8);

close(pid2[1]);

read(pid2[0],&s,sizeof(s));

printf("\nSum=%d\n",s);

49
OUTPUT:

dell@dell-Inspiron-5423:~$ cc IPC.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter string:OPERATING

Enter 9 array elements:

11

23

45

11

23

The string length=9

Sum=129

dell@dell-Inspiron-5423:~$

RESULT:

Thus the C program to implement IPC using pipe is executed sucessfully.

50
Ex.No:6

Date: Implement mutual exclusion by Semaphone

AIM:

To Write a C program to implement Producer Consumer problem using Semaphore.

ALGORITHM:

Step 1: START the program

Step 2: If the request is for Producer

a. Get the count of number of items to be produced

b. Add the count with current Buffer size

c. If the new Buffer size is full Don’t produce anything; display an error message
Producer has to wait till any consumer consume the existing item

d. Else Produce the item Increment the Buffer by the number of item produced

Step 3: If the request is for Consumer

a. Get the count of number of items to be consumed

b. Subtract the count with current Buffer size

c. If the new Buffer size is lesser than 0

Don’t allow the consumer to consume anything; display an error message

Consumer has to wait till the producer produce new items

d. Else Consume the item Decrement the Buffer by the number of item consumed

Step 4: STOP the program

PROGRAM:

#include<stdio.h>

int n=0,buffersize=0,currentsize=0;

void producer()

51
printf("\nEnter number of elements to be produced: ");

scanf("%d",&n);

if(0<=(buffersize-(currentsize+n)))

currentsize+=n;

printf("%d Elements produced by producer where buffersize is %d\n", currentsize,buffersize);

else

printf("\nBuffer is not sufficient\n");

void consumer()

int x;

printf("\nEnter no. of elements to be consumed: ");

scanf("%d",&x);

if(currentsize>=x)

currentsize-=x;

printf("\nNumber of elements consumed: %d, Number of Elements left: %d", x, currentsize);

else

printf("\nNumber of Elements consumed should not be greater than Number of Elements produced\n");

void main()

52
{

int c;

printf("\nEnter maximum size of buffer:");

scanf("%d",&buffersize);

do

printf("\n1.Producer 2.Consumer 3.Exit");

printf("\nEnter Choice:");

scanf("%d",&c);

switch(c)

case 1:

if(currentsize >= buffersize)

printf("\nBuffer is full. Cannot produce");

else

producer();

break;

case 2:

if(currentsize <= 0)

printf("\nBuffer is Empty. Cannot consume");

else

consumer();

break;

default:

break;

53
}

while(c!=3);

OUTPUT:

Enter maximum size of buffer: 20

1. Producer 2. Consumer 3. Exit

Enter Choice: 2

Buffer is Empty, cannot consume

1. Producer 2. Consumer 3. Exit

Enter Choice: 1

Enter number of elements to be produced: 23

Buffer is not sufficient

Producer 2. Consumer 3. Exit

Enter Choice: 1

Enter number of elements to be produced: 5

Elements produced by producer where buffer size is 20

1.Producer 2. Consumer 3. Exit

Enter Choice: 2

Enter no.of elements to be consumed: 4

Number of elements consumed : 4, Number of Elements left: 1

1.Producer 2. Consumer 3. Exit

Enter Choice: 2

Enter no.of elements to be consumed: 4

Number of elements consumed should not be greater than Number of Elements Produced

1.Producer 2. Consumer 3. Exit

Enter Choice: 1

54
Enter number of elements to be produced: 5

Elements produced by producer where buffer size is 20

1.Producer 2. Consumer 3. Exit

Enter Choice: 1

Enter number of elements to be produced: 15

Buffer is not sufficient

RESULT:

Thus the C program to implement Producer Consumer problem using semaphore is executed
sucessfully.

55
Ex.No:7

Date: Deadlock using Banker’s Algorthim

AIM:

To Write a C program to implement Bankers algorithm for deadlock avoidance.

ALGORITHM:

Step 1: Start the program .Initialize the necessary variables.

Step 2: Enter the number of process.

Step 3: Enter the no of resources instances

Step 4: Enter the Max Matrix and calculate with Need = Max - Allocation

Step 5: Enter the Allocation Matrix and calculate for each process.

Step 6: Calculate the available Resources.

Step 7: Check if all the process are in the safe sequence

Step 8: If safe sequence exists, processes are in safe state.

Step 9: Else Processes are deadlocked.System is in unsafe state.

Step 10: Print the result and end the program

PROGRAM:

#include<stdio.h>

static int visited[20];

int i,j,np,nr;

int safe(int n[10][10],int ar[10],int a[10][10],int m[10][10]);

int canbeprocessed(int x[10],int y[10],int z[10],int ar[10]);

void additionalrequest(int a1[10][10],int m1[10][10],int ar1[10]);

void main()

int a[10][10],m[10][10],n[10][10],ar[10],t[10];

56
int a1[10][10],m1[10][10],ar1[10];

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

scanf("%d",&np);

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

scanf("%d",&nr);

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

printf("\nTotal Amount of the Resource R%d: ",i+1);

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

printf("\nEnter the allocation matrix\n\n\t");

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

printf("R%d\t",i+1);

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

printf("\nP%d\t",i+1);

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

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

a1[i][j]=a[i][j];

printf("\n\n");

printf("\nEnter the maximum matrix\n\n\t");

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

printf("R%d\t",i+1);

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

printf("\nP%d\t",i+1);

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

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

m1[i][j]=m[i][j];

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

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

n[i][j]=m[i][j]-a[i][j];

/*Available Resource calculation*/

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

ar[j]=t[j];

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

ar[j]-=a[i][j];

ar1[j]=ar[j];

/*Available Resource Calculation ends*/

if(safe(n,ar,a,m))

printf("\n\nConclusion: System is in safe state");

else

58
printf("\n\nConclusion: The system cannot acheive safe state");

additionalrequest(a1,m1,ar1);

/*Function To Check The Safe State*/

int safe(int n[][10],int ar[10],int a[][10],int m[][10])

int count;

count=0;

printf("\nSafe State Sequence:");

while(count<np)

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

if((visited[i]==0) && canbeprocessed(n[i],a[i],m[i],ar))

count++;

visited[i]=1;

printf("P%d\t",i+1);

break;

if(i==np)

return 0;

return 1;

59
/*function for checking the processing of the process*/

int canbeprocessed(int x[],int y[],int z[],int ar[])

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

if(x[j]>ar[j])

return 0;

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

ar[j]+=y[j];

y[j]=z[j]=0;

return 1;

/*Additional Request*/

void additionalrequest(int a1[10][10],int m1[10][10],int ar1[10])

int pno,vector[10];

int n1[10][10];

printf("\nEnter the process no which is making additional request");

scanf("%d",&pno);

printf("\nEnter the request vector: ");

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

scanf("%d",&vector[j]);

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

a1[pno][j]+=vector[j];

60
ar1[j]-=vector[j];

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

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

n1[i][j]=m1[i][j]-a1[i][j];

if(safe(n1,ar1,a1,m1))

printf("\nAllocation made \n");

else

printf("\nAllocation cannot be made \n");

a1[pno][j]-=vector[j];

ar1[j]+=vector[j];

OUTPUT:

Enter the no of process:3

Enter the no of resources: 3

Total Amount of the Resource R1: 9

Total Amount of the Resource R2: 4

Total Amount of the Resource R3: 5

Enter the allocation matrix

R1 R2 R3

P1 2 1 1

P2 4 2 1

P3 2 0 1

Enter the maximum matrix

61
R1 R2 R3

P1 4 2 2

P2 3 2 2

P3 3 2 2

Safe State Sequence: P2 P1 P3

Conclusion: System is in safe state

Enter the process no which is making additional request 1

Enter the request vector: 2 1 1

Safe State Sequence:

Allocation cannot be made

RESULT:

Thus the C program to implement banker’s algorithm for deadlock avoidance was executed
successfully.

62
Ex.No:8

Date: Deadlock using Detection Algorithm

AIM:

To Write a C program to implement deadlock detection algorithm.

ALGORITHM:

Step 1: Start the program .Initialize the necessary variables.

Step 2: Enter the number of process.

Step 3: Enter the no of resources instances in available vector.

Step 4: Read the allocated matrix.

Step 5: Read the maximum allocation matrix.

Step 6: Find a process such that the process can complete with the available resources.

Step 7: After completion release and add the resources to available vector.

Step 8: Repeat step 6 and 7 until all process completed its execution.

Step 9: If any process is unable to complete, then the system is in deadlock.

Step 10: Print the result and end the program

PROGRAM:

#include<stdio.h>

static int mark[20];

int i,j,np,nr;

int main()

int alloc[10][10],request[10][10],avail[10],r[10],w[10];

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

scanf("%d",&np);

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

63
scanf("%d",&nr);

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

printf("\nTotal Amount of the Resource R%d: ",i+1);

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

printf("\nEnter the request matrix:");

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

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

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

printf("\nEnter the allocation matrix:");

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

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

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

/*Available Resource calculation*/

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

avail[j]=r[j];

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

avail[j]-=alloc[i][j];

//marking processes with zero allocation

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

64
int count=0;

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

if(alloc[i][j]==0)

count++;

else

break;

if(count==nr)

mark[i]=1;

// initialize W with avail

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

w[j]=avail[j];

//mark processes with request less than or equal to W

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

int canbeprocessed=0;

if(mark[i]!=1)

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

if(request[i][j]<=w[j])

canbeprocessed=1;

else

65
canbeprocessed=0;

break;

if(canbeprocessed)

mark[i]=1;

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

w[j]+=alloc[i][j];

//checking for unmarked processes

int deadlock=0;

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

if(mark[i]!=1)

deadlock=1;

if(deadlock)

printf("\n Deadlock detected");

else

printf("\n No Deadlock possible");

66
OUTPUT:

dell@dell-Inspiron-5423:~$ cc detect.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter the no of process: 3

Enter the no of resources: 3

Total Amount of the Resource R1: 10

Total Amount of the Resource R2: 9

Total Amount of the Resource R3: 8

Enter the request matrix:

223

211

111

Enter the allocation matrix:

211

111

111

No Deadlock possible

RESULT:

Thus the C program to implement deadlock detection algorithm was executed successfully.

67
Ex.No:9

Date: Implementation of Threading

AIM:

To write a C program to implement Threading.

ALGORITHM:

Step 1: Create two threads

Step 2: Let the threads share a common resource, say counter

Step 3: Even if thread2 is scheduled to start while thread was not done, access to shared

Resource is not done as it is locked by mutex.

Step 4: Once thread1 completes, thread2 starts execution

Step 5: Stop

PROGRAM:

#include <stdio.h>

#include <string.h>

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

pthread_t tid[2];

int counter;

pthread_mutext_t lock;

void* trythis(void *arg)

unsigned long i = 0;

counter += 1;

printf(“\n Job %d has started \n”, counter);

68
for (i=0; i<0(0xFFFFFFFF); i++);

printf(“\n Job %d has finished\n”, counter);

pthread_mutex_unlock( &lock);

return NULL;

main()

int i = 0;

if (pthread_mutex_init( &lock, NULL) != 0)

printf (“\n mutex init has failed\n”);

return 1;

while ( i < 2 )

err = pthread_create( &(tid[i]), NULL, &trythis, NULL);

if (error != 0 )

printf(“\n Thread can’t be created :[%s]”, strerror(error));

i++;

pthread_join(tid[0], NULL);

pthread_join(tid[1], NULL);

pthread_mutex_destroy( &lock);

return 0;

69
OUTPUT:

$ gcc filename.c –lpthread

$ ./a.out

Job 1 started

Job 1 finished

Job 2 started

Job 2 finished

RESULT:

Thus the concurrent threads are executed and the output is verified successfully.

70
Ex.No:10

Date: Paging Technique

AIM:

To Write a C program to implement paging technique of memory management.

ALGORITHM:

Step 1: Start the program.

Step 2: Read the memory size, page size and number of process.

Step 3: Read the pages required for each process.

Step 4: If available allocate the page, otherwise display error memory is full.

Step 5: Read the logical address with page number and offset value.

Step 6: Calculate and print physical address.

Step 7: End the program

PROGRAM:

#include<stdio.h>

Void main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

printf("\nEnter the memory size --");

scanf("%d",&ms);

printf("\nEnter the page size --");

scanf("%d",&ps);

nop = ms/ps;

printf("\nThe no. of pages available in memory are --%d ",nop);

printf("\nEnter number of processes --");

71
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("\nEnter pagetable 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);

72
}

OUTPUT:

dell@dell-Inspiron-5423:~$ ./a.out

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 3 60

The Physical Address is --760dell@dell-Inspiron-5423:~$

RESULT:

Thus the C program to implement paging technique of memory management was executed
successfully.

73
Ex.No:11a

Date: Memory Allocation Methods:First Fit

AIM:

To Write a C program to implement first fit memory allocation method.

ALGORITHM:

Step 1: Start the program .

Step 2: Read the number of blocks and block size of each blocks.

Stpe 3: Read the number of process and size of each process.

Step 4: Initialize all memory blocks as free.

Step 5: Start by picking each process and check if it can be assigned to current block.

Step 6: If size-of-process <= size-of-block if yes then assign and check for next process.

Step 7: If not then keep checking the further blocks.

a. Print the allocation and end the program

PROGRAM:

#include<stdio.h>

void main()

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

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

flags[i] = 0;

allocation[i] = -1;

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

scanf("%d", &bno);

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

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

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

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

scanf("%d", &pno);

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

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

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

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

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

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

allocation[j] = i;

flags[j] = 1;

break;

//display allocation detailss

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

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

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

if(flags[i] == 1)

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

else

printf("Not allocated");

75
}

OUTPUT:

dell@dell-Inspiron-5423:~$ ./a.out

Enter no. of blocks: 5

Enter size of each block: 100 150 200 350 450

Enter no. of processes: 5

Enter size of each process: 125 75 100 200 350

Block no. size process no. size

1 100 2 75

2 150 1 125

3 200 3 100

4 350 4 200

5 450 5 350

RESULT:

Thus the C program to implement first fit memory allocation method was executed successfully.

76
Ex.No:11b

Date: Memory Allocation Methods: Worst Fit

AIM:

To Write a C program to implement worst fit memory allocation method.

ALGORITHM:

Step 1: Start the program.

Step 2: Read the number of blocks and block size of each blocks.

Step 3: Read the number of process and size of each process.

Step 4: Initialize all memory blocks as free.

Step 5: 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],
if found then assign it to the current process.

Step 6: If not then leave that process and keep checking the further processes.

Step 7: Print the allocation and end the program

PROGRAM:

#include<stdio.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];

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

77
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;

78
}

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]);

79
OUTPUT:

dell@dell-Inspiron-5423:~$ cc worstfit.c

dell@dell-Inspiron-5423:~$ ./a.out

Memory Management Scheme - Worst Fit

Enter the number of blocks:3

Enter the number of files:3

Enter the size of the blocks:-

Block 1:10

Block 2:20

Block 3:30

Enter the size of the files :-

File 1:15

File 2:23

File 3:30

File_no: File_size : Block_no: Block_size: Fragement

1 15 3 30 15

2 23 0 -1080227116 0

3 30 0 -1080227116 0

RESULT:

Thus the C program to implement worst fit memory allocation method was executed successfully.

80
Ex.No:11c

Date: Memory Allocation Methods: Best Fit

AIM:

To Write a C program to implement best fit memory allocation method.

ALGORITHM:

Step 1: Start the program.

Step 2: Read the number of blocks and block size of each blocks.

Step 3: Read the number of process and size of each process.

Step 4: Initialize all memory blocks as free.

Step 5: 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],
if found then assign it to the current process.

Step 6: If not then leave that process and keep checking the further processes.

Step 7: Print the allocation and end the program

PROGRAM:

#include<stdio.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];

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");

81
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;

82
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]);

OUTPUT:

dell@dell-Inspiron-5423:~$ ./a.out

Enter the number of blocks:4

Enter the number of files:4

Enter the size of the blocks:-

Block 1:10

Block 2:15

Block 3:25

Block 4:35

Enter the size of the files :-

File 1:15

File 2:23

File 3:45

File 4:29

File No File Size Block No Block Size Fragment

1 15 2 15 0

2 23 3 25 2

83
RESULT:

Thus the C program to implement best fit memory allocation method was executed successfully.

84
Ex.No:12a

Date: Page Replacement Algorithm: FIFO

AIM:

To Write a C program to implement Page replacement algorithm- First In First Out (FIFO).

ALGORITHM:

Step 1: Start the process

Step 2: Declare the size with respect to page length

Step 3: Check the need of replacement from the page to memory

Step 4: Check the need of replacement from old page to new page in memory

Step 5: Forma queue to hold all pages

Step 6: Insert the page require memory into the queue

Step 7: Check for bad replacement and page fault

Step 8: Get the number of processes to be inserted

Step 9: Display the values

Step 10: Stop the process

PROGRAM:

#include <stdio.h>

int main()

int i, j, n, a[50], frame[10], no, k, avail, count = 0;

printf(“\n Enter THE NUMBER OF PAGES:\n”);

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

scanf(“%d”,&a[i]);

printf(“\n ENTER THE NUMBER OF FRAMES:”);

scanf(“%d”, &no);

85
for(i=0; i<no; i++)

frame[i] = -1;

j = 0;

printf(“\t ref string\t page frames\n”);

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

printf(“%d\t\t”, a[i]);

avail = 0;

for(k=0; k<no; k++)

if(frame[k] == a[i] )

avail = 1;

if (avail == 0)

frame[j] = a[i];

j = (j+1) % no;

count++;

for(k=0;k<no;k++)

printf(“%d\t”,frame[k]);

printf(“\n”)

Printf(“Page Fault Is %d\n”, count):

86
return 0;

OUTPUT:

dell@dell-Inspiron-5423:~$ ./a.out

ENTER THE NUMBER OF PAGES:

10

ENTER THE PAGE NUMBER:

7012030423

ENTER THE NUMBER OF FRAMES :3

ref string page frames

7 7 -1 -1

0 7 0 -1

1 7 0 1

2 2 0 1

3 2 3 1

0 2 3 0

4 4 3 0

2 4 2 0

3 4 2 3

Page Fault Is 9

RESULT:

Thus the C program to implement Page replacement algorithm- First In First Out (FIFO) was
executed successfully.

87
Ex.No:12b

Date: Page Replacement Algorithm: LRU

AIM:

To Write a C program to implement Page replacement algorithm- Least Recently Used (LRU).

ALGORITHM:

Step 1: Start the process

Step 2: Declare the size

Step 3: Get the number of pages to be inserted

Step 4: Get the value

Step 5: Declare counter and stack

Step 6: Select the least recently used page by counter value

Step 7: Stack them according the selection.

Step 8: Display the values

Step 9: Stop the process

PROGRAM:

#include<stdio.h>

main()

int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");

scanf("%d",&n);

printf("Enter the reference string:");

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

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

printf("Enter no of frames:");

88
scanf("%d",&f);

q[k]=p[k];

printf("\n\t%d\n",q[k]);

c++;

k++;

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

c1=0;

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

if(p[i]!=q[j])

c1++;

if(c1==f)

c++;

if(k<f)

q[k]=p[i];

k++;

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

printf("\t%d",q[j]);

printf("\n");

else

89
for(r=0;r<f;r++)

c2[r]=0;

for(j=i-1;j<n;j--)

if(q[r]!=p[j])

c2[r]++;

else

break;

for(r=0;r<f;r++)

b[r]=c2[r];

for(r=0;r<f;r++)

for(j=r;j<f;j++)

if(b[r]<b[j])

t=b[r];

b[r]=b[j];

b[j]=t;

for(r=0;r<f;r++)

90
{

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]);

printf("\n");

printf("\nThe no of page faults is %d",c);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc lru.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter no of pages:10

Enter the reference string:7 5 9 5 4 7 3 7 5 2

Enter no of frames:3

7 5

7 5 9

4 5 9

4 5 7

4 3 7

5 3 7

5 2 7

The no of page faults is 8

91
RESULT:

Thus the C program to implement Page replacement algorithm- Least Recently Used (LRU) was
executed successfully.

92
Ex.No:12c

Date: Page Replacement Algorithm: LFU

AIM:

To Write a C program to implement Page replacement algorithm- Least Frequently Used (LFU).

ALGORITHM:

Step 1: Start the process

Step 2: Declare the size

Step 3: Get the number of pages to be inserted

Step 4: Get the value

Step 5: In LFU (Least Frequently Used) algorithm the page which has with the smallest count is
going to be replaced.

Step 6: Display the values

Step 7: Stop the process

PROGRAM:

#include<stdio.h>

int n,nf;

int in[100];

int p[50];

int hit=0;

int i,j,k;

int pgfaultcnt=0;

void getData()

printf("\nEnter length of page reference sequence:");

scanf("%d",&n);

93
printf("\nEnter the page reference sequence:");

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

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

printf("\nEnter no of frames:");

scanf("%d",&nf);

void initialize()

pgfaultcnt=0;

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

p[i]=9999;

int isHit(int data)

hit=0;

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

if(p[j]==data)

hit=1;

break;

return hit;

int getHitIndex(int data)

94
{

int hitind;

for(k=0; k<nf; k++)

if(p[k]==data)

hitind=k;

break;

return hitind;

void dispPages()

for (k=0; k<nf; k++)

if(p[k]!=9999)

printf(" %d",p[k]);

void dispPgFaultCnt()

printf("\nTotal no of page faults:%d",pgfaultcnt);

void lfu()

95
{

int usedcnt[100];

int least,repin,sofarcnt=0,bn;

initialize();

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

usedcnt[i]=0;

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

printf("\n For %d :",in[i]);

if(isHit(in[i]))

int hitind=getHitIndex(in[i]);

usedcnt[hitind]++;

printf("No page fault!");

else

pgfaultcnt++;

if(bn<nf)

p[bn]=in[i];

usedcnt[bn]=usedcnt[bn]+1;

bn++;

else

96
least=9999;

for(k=0; k<nf; k++)

if(usedcnt[k]<least)

least=usedcnt[k];

repin=k;

p[repin]=in[i];

sofarcnt=0;

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

if(in[i]==in[k])

sofarcnt=sofarcnt+1;

usedcnt[repin]=sofarcnt;

dispPages();

dispPgFaultCnt();

int main()

getData();

lfu();

97
OUTPUT:

dell@dell-Inspiron-5423:~$ cc lfu.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter length of page reference sequence:20

Enter the page reference sequence: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

Enter no of frames: 3

For 7 : 7

For 0 : 7 0

For 1 : 7 0 1

For 2 : 2 0 1

For 0 :No page fault!

For 3 : 3 0 1

For 0 :No page fault!

For 4 : 4 0 1

For 2 : 2 0 1

For 3 : 2 0 3

For 0 :No page fault!

For 3 :No page fault!

For 2 :No page fault!

For 1 : 1 0 3

For 2 : 2 0 3

For 0 :No page fault!

For 1 : 2 0 1

For 7 : 2 0 7

For 0 :No page fault!

For 1 : 2 0 1

98
Total no of page faults:13

RESULT:

Thus the C program to implement Page replacement algorithm- Least Frequently Used (LRU)
was executed successfully.

99
Ex.No:13a

Date: File Organization Technique: Single level directory

AIM:

To Write a C program to implement file organization technique- single level directory structure.

ALGORITHM:

Step1: Start

Step 2: Create a single directory and read the name of the directory.

Step 3: Create files in the directory.

Step 4: Display the files in the directory.

Step 5: Search the files in the directory and display the result of search operation.

Step 6: Delete the files in the directory and display the files.

Step 7: Stop

PROGRAM:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct

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

int fcnt;

}dir;

void main()

int i,ch;

char f[30];

100
dir.fcnt = 0;

printf("\nEnter name of directory -- ");

scanf("%s", dir.dname);

while(1)

printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice --
");

scanf("%d",&ch);

switch(ch)

case 1: printf("\n Enter the name of the file -- ");

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

dir.fcnt++;

break;

case 2: printf("\n Enter 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);

101
else

dir.fcnt--;

break;

case 3: printf("\n Enter 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("\n Directory Empty");

else

printf("\n The Files are -- ");

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

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

break;

default: exit(0);

102
}

OUTPUT:

dell@dell-Inspiron-5423:~$ cc single.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter name of directory – dir1

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 -- 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 -- 4

The Files are -- a c

103
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit

Enter your choice -- 3

Enter the name of the file -- b

File b not found

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

Enter your choice -- 5

RESULT:

Thus the C program to implement file organization technique-single level directory structure was
executed successfully.

104
Ex.No:13b

Date: File Organization Technique: Two level directory

AIM:

To Write a C program to implement file organization technique- two level directory structure.

ALGORITHM:

Step1: Start

Step 2: Create directories to store files and read the name of the directories.

Step 3: Create files in the directory by entering the name of the directory first.

Step 4: Display the files in the directory.

Step 5: Search the files in the directory and display the result of search operation.

Step 6: Delete the files in the directory and display the files.

Step 7: Stop

PROGRAM:

#include<stdio.h>
#include<string.h>

#include<stdlib.h>

struct

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

int fcnt;

}dir[10];

void main()

int i,ch,dcnt,k;

char f[30], d[30];

105
dcnt=0;

while(1)

printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");

printf("\n 4. Search File \t \t 5. Display \t 6. Exit \n Enter your choice -- ");

scanf("%d",&ch);

switch(ch)

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

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

dir[dcnt].fcnt=0;

dcnt++;

printf("Directory created");

break;

case 2: printf("\n Enter 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)

106
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;

107
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

108
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);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc two.c

dell@dell-Inspiron-5423:~$ ./a.out

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 1

Enter name of directory -- aa

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 -- bb

Directory created

109
1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 2

Enter name of the directory -- aa

Enter name of the file -- one

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 2

Enter name of the directory -- aa

Enter name of the file -- two

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 2

Enter name of the directory -- bb

Enter name of the file -- one

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 5

Directory Files

aa one two

bb one

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

110
Enter your choice -- 4

Enter name of the directory -- aa

Enter the name of the file -- on

File on not found

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit

Enter your choice -- 6

RESULT:

Thus the C program to implement file organization technique-two level directory structure was
executed successfully.

111
Ex.No:14a

Date: File Allocation Strategies: Sequential

AIM:

To Write a C program to implement sequential file allocation.

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize the necessary variables.

Step 3: Enter the starting block and the length of file.

Step 4: Check the files are allocated in a sequential manner

Step 5: Allocate the files to the disk.

Step 6: If the block is alredy occupied,error occurs.

Step 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

void main()

int f[50],i,st,j,len,c,k;

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

f[i]=0;

X:

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)

112
{

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)

goto X;

else

exit(0);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc seq.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter the starting block & length of file10 7

10->1

11->1

12->1

13->1

113
14->1

15->1

16->1

the file is allocated to disk

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

Enter the starting block & length of file12 6

Block already allocated

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

RESULT:

Thus the C program to implement sequential file allocation was executed successfully.

114
Ex.No:14b

Date: File Allocation Strategies: Indexed

AIM:

To Write a C program to implement indexed file allocation.

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize the necessary variables.

Step 3: Enter the index block & number of files in block.

Step 4: Check the files are allocated based on the indexes that are created for each fragment of the
file.
Step 5: Check that each and every similar indexed file is maintained by the primary index
thereby providing flow to the file fragments.
Step: 6 Allocate the files to the disk.

Step 7: If the block is alredy occupied, error occurs.

Step 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

int f[50],i,k,j,inde[50],n,c,count=0,p;

void main()

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

f[i]=0;

x:

printf("\nenter index block\t");

scanf("%d",&p);

115
if(f[p]==0)

f[p]=1;

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

scanf("%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(" \nEnter 1 to enter more files and 0 to exit\t");

116
scanf("%d",&c);

if(c==1)

goto x;

else

exit(0);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc index.c

dell@dell-Inspiron-5423:~$ ./a.out

enter index block 5

enter no of files on index 2

14

allocated

file indexed

5->1:1

5->4:1

Enter 1 to enter more files and 0 to exit 1

enter index block 5

Block already allocated

enter index block 3

enter no of files on index 3

367

Block already allocated

enter index block 4

Block already allocated

enter index block 9

117
enter no of files on index 2

11 12

allocated

file indexed

9->11:1

9->12:1

Enter 1 to enter more files and 0 to exit

RESULT:

Thus the C program to implement indexed file allocation was executed successfully.

118
Ex.No:14c

Date: File Allocation Strategies: Linked

AIM:

To Write a C program to implement linked file allocation.

ALGORITHM:

Step 1: Start the program.

Step 2: Initialize the necessary variables.

Step 3: Enter how many blocks that are already allocated

Step 4: Enter the blocks no.s that are already allocated

Step 5: Enter the starting index block & length

Step 6: Check the files are allocated in a linked list format

Step: 7 Check that each and every fragment is linked to the other file through either addresses or
pointers. Check that the starting location of the file serves for the purpose of extraction of the
entire file because every fragment is linked to each other.
Step: 8 Allocate the files to the disk.

Step: 9 If the block is alredy occupied, error occurs.

Step: 10 Stop the program.

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

void main()

int f[50],p,i,j,k,a,st,len,n,c;

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

f[i]=0;

printf("Enter how many blocks that are already allocated");

119
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;

X:

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

120
if(c==1)

goto X;

else

exit(0);

OUTPUT:

dell@dell-Inspiron-5423:~$ cc linked.c

dell@dell-Inspiron-5423:~$ ./a.out

Enter how many blocks that are already allocated 5

Enter the blocks no.s that are already allocated

14892

Enter the starting index block & length2 5

2->file is already allocated

3->1

4->file is already allocated

5->1

6->1

7->1

8->file is already allocated

9->file is already allocated

10->1

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

dell@dell-Inspiron-5423:~$

RESULT:

Thus the C program to implement linked file allocation was executed successfully.

121
Ex.No:15a

Date: Disk Scheduling Algorithm: FCFS Scheduling

AIM:

To write C program to implement FCFS Scheduling

ALGORITHM:

Step 1: Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.

Step 2: Let us one by one take the tracks in default order and calculate the absolute distance of the
track from the head.

Step 3: Increment the total seek count with this distance.

Step 4: Currently serviced track position now becomes the new head position.

Step 5: Go to step 2 until all tracks in request array have not been serviced.

PROGRAM:

#include<stdio.h>

int main()

int queue[20],n,head,i,j,k,seek=0,max,diff;

float avg;

printf("Enter the max range of disk\n");

scanf("%d",&max);

printf("Enter the size of queue request\n");

scanf("%d",&n);

printf("Enter the queue of disk positions to be read\n");

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

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

printf("Enter the initial head position\n");

122
scanf("%d",&head);

queue[0]=head;

for(j=0;j<=n-1;j++)

diff=abs(queue[j+1]-queue[j]);

seek+=diff;

printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);

printf("Total seek time is %d\n",seek);

avg=seek/(float)n;

printf("Average seek time is %f\n",avg);

return 0;

123
OUTPUT:

Enter the max range of disk

200

Enter the size of queue request

Enter the queue of disk positions to be read

90 120 35 122 38 128 65 68

Enter the initial head position

50

Disk head moves from 50 to 90 with seek 40

Disk head moves from 90 to 120 with seek 30

Disk head moves from 120 to 35 with seek 85

Disk head moves from 35 to 122 with seek 87

Disk head moves from 122 to 38 with seek 84

Disk head moves from 38 to 128 with seek 90

Disk head moves from 128 to 65 with seek 63

Disk head moves from 65 to 68 with seek 3

Total seek time is 482

Average seek time is 60.250000

RESULT:

Thus the C program for to implement FCFS disk scheduling algorithms was executed and the
output is verified successfully.

124
Ex.No:15b

Date: Disk Scheduling Algorithm: SSTF Scheduling

AIM:

To write a C program to implement SSTF disk scheduling algorithm.

ALGORITHM:

Step 1: Let Request array represents an array storing indexes of tracks that have been requested.
‘head’ is the position of disk head.

Step 2: Find the positive distance of all tracks in the request array from head.

Step 3: Find a track from requested array which has not been accessed/serviced yet and has
minimum distance from head.

Step 4: Increment the total seek count with this distance.

Step 5: Currently serviced track position now becomes the new head position.

Step 6: Go to step 2 until all tracks in request array have not been serviced.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int queue[100],t[100],head,seek=0,n,i,j,temp;

float avg;

clrscr();

printf("*** SSTF Disk Scheduling Algorithm ***\n");

printf("Enter the size of Queue\t");

scanf("%d",&n);

printf("Enter the Queue\t");

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

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

printf("Enter the initial head position\t");

scanf("%d",&head);

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

t[i]=abs(head-queue[i]);

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

for(j=i+1;j<n;j++)

if(t[i]>t[j])

temp=t[i];

t[i]=t[j];

t[j]=temp;

temp=queue[i];

queue[i]=queue[j];

queue[j]=temp;

for(i=1;i<n-1;i++)

seek=seek+abs(head-queue[i]);

126
head=queue[i];

printf("\nTotal Seek Time is%d\t",seek);

avg=seek/(float)n;

printf("\nAverage Seek Time is %f\t",avg);

return 0;

OUTPUT:

*** SSTF Disk Scheduling Algorithm ***

Enter the size of Queue 5 Enter the Queue 10 17 2 15 4

Enter the initial head position 3

Total Seek Time is14

Average Seek Time is 2.800000

RESULT:

Thus the C program to implement SSTF disk scheduling algorithm was executed and the output is
verified successfully.

127
Ex.No:16

Date: Operating System like Linux VMare

Install New OS in VMware Workstation

Step 1: Go to File > New Virtual Machine.

Step 2: Select Typical (recommended), and then press Next.

128
Step 3: Pick how to install the operating system.

Select Installer disc if the operating system is in the disc drive. Otherwise, choose Installer disc image
file (iso) if you have a file that contains the OS, like a Windows 10 ISO or an ISO for macOS.

Step 4: Choose precisely where the OS files are located.

129
If you selected to install the operating system from a disc, choose the correct disc drive from the drop-
down menu. For an ISO install, select Browse and locate the ISO image.

Step 5: Press Next to proceed to the screen where you name the new virtual machine and pick where its
files should be stored. Fill out that information and then select Next again.

130
Be sure to pick a location that can handle the possibly ultra-large files created by the operating system as
you use it. You might end up needing hundreds of gigabytes if you use this VM heavily. Storing it on an
external hard drive is a good idea if your local hard drive doesn’t have the space.

Note: For some operating system setups, you’ll be asked to enter the product key used to activate it. You
should be able to skip through that step if you want to enter the key later.

Step 6: Define how storage should take place with this virtual machine, and then press Next.

131
You can change the maximum size of the virtual hard drive from the small box. From the bottom of this
window are two options: Store virtual disk as a single file and Store virtual disk into multiple files.

Pick the second option if you think you might one day move the VMware hard drive to a different
computer, but take note of the text on that screen that mentions a possible performance reduction of using
multiple files with a large hard drive.

In other words, if you plan to store lots of data in this VM, select the “single file” option, otherwise go
with “multiple files.”

Step 7: Select Customize Hardware and make any necessary changes. You can change details about the
memory, processors, disc drive, network adapter, USB controller, sound card, printer, and display.

132
For example, the Memory section is how you specify how much physical RAM the virtual machine is
allotted. If you later decide that you’ve given too little memory to the VM, it might perform slowly.
Similarly, if you give too much, your host computer will perform sluggishly and will have a difficult time
even using the VM.

VMware Workstation recommends a specific amount based on how much you have installed, but you can
adjust the memory to anything you like (though it’s best to stick to the recommended level). Different
operating systems require different amounts of memory (e.g., Windows 10 needs more than Windows
2000).

Network Adapter is an important component here that you might end up having to adjust later if your
virtual machine doesn’t have internet access. There are multiple options, and some may or may not work
properly depending on how your host computer is set up and how the guest OS (the VM) works. You can
skip this for now to have the default option chosen for you.

133
Step 8: Choose Close to exit the Hardware screen, and then press Finish.

VMware Workstation will create the virtual disk you specified in Step 6 and then turn on the virtual
machine automatically. This process might take a while, but you can watch the progress bar for an
estimation of when it will finish.

Step 9: Follow any on-screen prompts to begin the OS installation. For example, if you see Press any key
to boot from CD or DVD, do that to start the OS setup.

Step 10: Follow the directions for your specific operating system install to add the OS to VMware
Workstation.

134
At any time when the VMware virtual machine is on, you can shut it down or restart it from
the VM > Power menu. This is also how you suspend the VM, which means to pause it so that you can
resume at that exact same point the next time you access it. This is different from powering it on from an
off state where you have to log back in again, open programs and files, etc.

135
RESULT:

Thus the installation of Linux operating system using VMare is completed.

136
137

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy