You are on page 1of 96

Dr. N.G.P.

INSTITUTE OF TECHNOLOGY
(Approved by AICTE, New Delhi and Affiliated to Anna University, Chennai)
Recognized by UGC & Accredited by NAAC
Accredited by NBA - (CSE, ECE, EEE, MECH, BME)
Coimbatore - 641 048. Tamil Nadu.

BONAFIDE CERTIFICATE
Department of

…………………………………………………………………………………………………..
Certified that this is a bonafide record of work done by Mr./Ms. ……………………………...
...……………………………………………of the ……………………….year B.E. / B.Tech./
M.E / M.B.A in the ……………………………………………………………………………..
Laboratory conducted in this institution, as prescribed by Anna University - Chennai, for
the ……………………….. Semester, during the academic year 20 …………. / 20 ………….

……………………………… …………...…………………………

Faculty In-Charge Head of the Department

Date :

Anna University Register No. : …………………………..


Submitted for the Anna University Examination held on
…………………………………………………………………………………………………..

…………………………. ………………………….
Internal Examiner External Examiner
CONTENTS
Marks
Exp.No Date Name of the Experiment Page No awarded out Faculty Remarks
of ( ) Signature if any
Marks
Exp.No Date Name of the Experiment Page No awarded out Faculty Remarks
of ( ) Signature if any

Average

Faculty In-charge
Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No : 1 BASICS OF UNIX COMMANDS


AIM:
To learn about the execution of basic UNIX commands.

BASIC COMMANDS:
1. File Manipulation Commands
2. General Purpose Commands
3. Command Grouping & Filter Commands
4. Directory Commands And Process Management Commands

File Manipulation Commands :

 It deals with how to create, copy, rename, utilize and delete the file.
 It contains the following commands:
a. cat b. cp c. mv
d. rm e. head f. tail
g. touch h. file i. less
j. grep k. wc
a. cat:
 This command is used to create a new file in the directory.
Syntax1: cat > filename // create a new file
Ex.
[student@localhost student]$ ls
a it1 it2
[student@localhost student]$ cat >a
-bash: a: Is a directory
[student@localhost student]$ cat >b
Welcome to IT Department,
NGP,
[7]+ Stopped cat >b
[student@localhost student]$
Syntax 2: cat filename // show the contents of the specified file
Ex.
[student@localhost student]$ cat b
Welcome to IT Department,
NGP,
[student@localhost student]$
Syntax 3: cat >>filename // appends the contents to the specified file
Ex.
[student@localhost student]$ cat >>b
COIMBATORE.
[8]+ Stopped cat >>b
[student@localhost student]$ cat b
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost student]$
Syntax 4: cat file1 file2
// concatenates the contents of the files file1 and file2

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 1


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.
[student@localhost student]$ cat >b1
Pincode 628 503.
[9]+ Stopped cat >b1
[student@localhost student]$ cat b b1
Welcome to IT Department,
NGP,
COIMBATORE.
Pincode 628 503.
[student@localhost student]$
Ex.
[student@localhost student]$ cat b b1 b1
Welcome to IT Department,
NGP,
COIMBATORE.
Pincode 628 503.
Pincode 628 503.
[student@localhost student]$
b. cp
 This command copies the contents from one file to another file. After
copying, the contents of the sourcefilename remains the same.
 If the destfilename is an existing file then the contents of the
destfilename replaced by the contents of the sourcefilename.

Syntax: cp <sourcefilename><destfilename>
Ex.
[student@localhost student]$ cat b
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost student]$ cat b1
Pincode 628 503.
[student@localhost student]$ cp b b2
[student@localhost student]$ cat b2
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost student]$ cp b1 b2
[student@localhost student]$ cat b2
Pincode 628 503.
[student@localhost student]$ cat b
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost student]$ cat b1
Pincode 628 503.
[student@localhost student]$
c. mv
 To move a file from one place to another, use the mv command. It can also be used to
rename a file, by moving the file to the same directory, but giving it a different name.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 2


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

 After execution, the sourcefilename is replaced with the destfilename [Source file will
be deleted automatically].
Syntax: mv <sourcefilename><destfilename>
Ex.
[student@localhost student]$ ls
b b1 b2
[student@localhost student]$ mv b b3
[student@localhost student]$ ls
b1 b2 b3
[student@localhost student]$ cat b3
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost student]$

Ex.
[student@localhost student]$ mkdir it1
[student@localhost student]$ mv b3 it1/b4
[student@localhost student]$ ls
b1 b2 it1
[student@localhost student]$ cd it1
[student@localhost it1]$ ls
b4
[student@localhost it1]$ cat b4
Welcome to IT Department,
NGP,
COIMBATORE.
[student@localhost it1]$

Syntax: mv <sourcedirectory1><destdirectory2>
 All the files in the sourcedirectory1 is moved to the destdirectory2.
 If the destdirectory2 is a new directory then all the files are moved
from source to destination.
 If the destdirectory2 is an existing directory then the sourcedirectory1 is
a subdirectory of destdirectory2.
 After moving, sourcedirectory1 is removed.
Ex.
[student@localhost student]$ ls
b1 b2 it1
[student@localhost student]$ mkdir it2
[student@localhost student]$ ls
b1 b2 it1 it2
[student@localhost student]$ mv it1 it3
[student@localhost student]$ ls
b1 b2 it2 it3
[student@localhost student]$ mv it3 it2
[student@localhost student]$ ls
b1 b2 it2
[student@localhost student]$ cd it2
[student@localhost it2]$ ls

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 3


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

it3
[student@localhost it2]$ cd it3
[student@localhost it3]$ ls
b4
[student@localhost it3]$

d. rm
 This command is used to remove the file.
Syntax: rm <filename>
Ex.
[student@localhost student]$ ls
b1 b2 it2
[student@localhost student]$ rm b2
[student@localhost student]$ ls
b1 it2
[student@localhost student]$

Ex.
[student@localhost student]$ rm it2/it3/b4
[student@localhost student]$ cd it2/it3
[student@localhost it3]$ ls
[student@localhost it3]$

e. head
 it is a text filter.
 This command displays the first ten lines of a given file.
Syntax: head <filename>

Ex.
[student@localhost student]$ cat >b
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 4


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

u
v
w
x
y
z
[3]+ Stopped cat >b
[student@localhost student]$ head b
a
b
c
d
e
f
g
h
i
j

Syntax2: head -no <filename>


Here no is the number of lines to be displayed from top to bottom of a
given specified file.
Ex.
[student@localhost student]$ head -5 b
a
b
c
d
e

f. tail
 it is a text filter.
 This command displays the last ten lines of a given file.
Syntax: tail <filename>
Ex.
[student@localhost student]$ tail b
q
r
s
t
u
v
w
x
y
z

Syntax2: tail -no <filename>

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 5


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Here no is the number of lines to be displayed from bottom to top of a


given specified file.
Ex.
[student@localhost student]$ tail -5 b
v
w
x
y
z

g. touch
 This command is used to create an empty file.
Syntax: touch <filename>
Ex.
[student@localhost student]$ ls
b b1 it2
[student@localhost student]$ touch b3
[student@localhost student]$ ls
b b1 b3 it2
[student@localhost student]$ cat >b3
hello
[4]+ Stopped cat >b3
[student@localhost student]$ cat b3
hello
h. file
 This command is used to display filename with its type.
Syntax: file <filename>
file <directoryname>
Ex.
[student@localhost student]$ ls
b b1 b3 it2
[student@localhost student]$ file b3
b3: ASCII text
[student@localhost student]$ file it2
it2: directory
[student@localhost student]$

i. less
 This command is to display the page at a time.
Syntax: less <filename>
Ex.
[student@localhost student]$ less b
a
b
c
d
e
f
g
h

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 6


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
[3]+ Stopped less b
[student@localhost student]$

j. grep
 This command is used to find a particular word in a file.
Syntax: grep word <filename>
Ex.
[student@localhost student]$ cat b3
hello
hai
welcome
wel
come
NGP
it
[student@localhost student]$ grep wel b3
welcome
wel
[student@localhost student]$ grep come b3
welcome
come
[student@localhost student]$
Syntax: grep word <filename> -v // display the line that do not match
Ex.
[student@localhost student]$ grep come b3 -v
hello
hai
wel
NGP
it

Syntax: grep word <filename> -n //display the word with each line number.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 7


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.
[student@localhost student]$ grep come b3 -n
3:welcome
5:come

Syntax: grep word <filename> -c // display only total count of match line.

Ex.
[student@localhost student]$ grep come b3 -n
3:welcome
5:come
[student@localhost student]$ grep come b3 -c
2

k. wc
 This command is used to count number of words or lines or characters
in a file.

Syntax: wc -w <filename> // display number of words in a specified file.


Ex.
[student@localhost student]$ cat b3
hello
hai
welcome
wel
come
NGP
it
[student@localhost student]$ wc -w b3
7 b3

Syntax: wc -l <filename> // display number of lines in a specified file.


Ex.
[student@localhost student]$ cat b3
hello
hai
welcome
wel
come
NGP
it
[student@localhost student]$ wc -l b3
7 b3
Syntax: wc -c <filename> // display number of characters in a specified file.
Ex.
[student@localhost student]$ cat b3
hello
hai
welcome

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 8


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

wel
come
NGP
it
[student@localhost student]$ wc -c b3
34 3

2.General Purpose Commands:


1. man 2. who 3. who am i 4. Echo
5. date 6. cal 7. bc 8. Passwd
9. spell 10. sleep 11.logname 12. finger
13. clear

1. man
 This command is used to display the help manual page.
Syntax: man anycommand
Ex.
[student@localhost student]$ man clear
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.

SEE ALSO
tput(1), terminfo(5)
clear(1)
(END)
2. who
 This command is used to display all the current users of current
directory.
 It prints the login name, terminal type, login time and remote hostname of
each user currently logged on.
Syntax: who
Ex.
[student@localhost student]$ who
student pts/0 Jun 23 09:34 (180.100.103.46)

3. whoami
 This command is used to show the current user identity.
 To display the details of the user.
Syntax: whoami
Ex.
[student@localhost student]$ whoami
student
4. echo
 This command is used to display text on the screen.
Syntax: echo text to be displayed on the screen

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 9


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.
[student@localhost student]$ echo welcome to NGP
welcome to NGP
5. date
 This command is used to display the date and time of the system.
Syntax: date
Ex.
[student@localhost student]$ date
Mon Jun 23 09:54:50 IST 2008
6. cal
 This command is used to display the current year calendar.
Syntax: cal

Ex. // Display the current month


[student@localhost student]$ cal
June 2008
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
// Display the year of the calendar
[student@localhost student]$ cal year
7. bc
 This command is used to perform simple arithmetic operation using calculator.
Syntax: bc
Ex.
[student@localhost student]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+3
5
12*12
144
12/12
1
12-12
0
12-13
-1
8. passwd
 This command is used to create a new password or to change the existing
password.
Syntax: passwd
9. spell

 This command is used to check the spelling of the file.


Syntax: spell <filename>
Ex.
[student@localhost student]$ cat >a
hello, welcome to NGP.
[1]+ Stopped cat >a

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 10


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

[student@localhost student]$ spell a


NGP
[student@localhost student]$ cat >>a
it department,
COIMBATORE.
[2]+ Stopped cat >>a
[student@localhost student]$ spell a
COIMBATORE
NGP
[student@localhost student]$
10. sleep
 This command is used to give the delay for specified amount of time.
Syntax: sleep number // number must be positive value. Number represents the delay in
seconds.
Ex.
[student@localhost student]$ sleep 3 // 3 seconds delay
[student@localhost student]$
11. logname
 This command is used to display the user name.
Syntax: logname
Ex.
[student@localhost student]$ logname
student
12. finger
 This command is used to display the information about system user.
Syntax: finger
Ex.
[student@localhost student]$ finger
Login Name Tty Idle Login Time Office Office Phone
student pts/0 1:06 Jun 23 09:34 (180.100.103.46)
student pts/2 Jun 23 10:59 (180.100.103.40)
[student@localhost student]$
13. clear
 This command is used to clear the screen.
Syntax: clear

3.Command Grouping & Filter Commands

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

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

3) Command : Pr

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 11


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

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

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

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

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

7) Command : Uniq
Purpose : It compares adjacent lines in the file and displays the output byeliminating
duplicate adjacent lines in it.
Syntax : uniq [options] <file name >
Example : $ uniq -c devi

8) Command : Sort
Purpose : It sorts one or more files based on ASCII sequence and also to merge thefile.
Syntax : sort [options] <file name >
Example : $ sort -r devi

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

10) Command : Tr
Purpose : It is used to translate or delete a character or a string from the standardinput to
produce the required output.
Syntax : tr [options] <string1><string2>
Example : $ tr –t ‘a’ ‘b’ < devi>

11) Command : Tee


Purpose : It is used to read the contents from standard input or from output ofanother
command and reproduces the output to both in standard outputand direct into output to one or
more files.
Syntax : tee [options] <file name >

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 12


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Example : $ tee date dat.txt

12) Command : grep


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

4. Directory Commands And Process Management Commands

Directory Commands:
a. mkdir b. cd c. rmdir
d. rm –r e. ls d. pwd
a. mkdir: [ Make Directory]
 This command is used to create a new directory.
Syntax: mkdir <directoryname>
Ex:
[student@localhost student]$ mkdir it1
[student@localhost student]$ ls
it1
[student@localhost student]$ mkdir it2
[student@localhost student]$ ls
it1 it2
[student@localhost student]$
b.cd: [Change Directory]
This command is used to change a directory.
Syntax : cd <directoryname>
cd <directoryname/subdirectoryname> //moves to the
subdirectory
cd .. // moves one directory up the directory tree
cd ~ // moves to your home directory from wherever you
are.
Ex.1
[student@localhost student]$ cd it1
[student@localhost it1]$ mkdir subit1
[student@localhost it1]$ ls
subit1
Ex. 2
[student@localhost student]$ cd it1/subit1
[student@localhost subit1]$
Ex.3
[student@localhost subit1]$ cd ..
[student@localhost it1]$
Ex.4
[student@localhost student]$ cd it1/subit1
[student@localhost subit1]$ cd ~
c. rmdir : [Remove Directory]
 This command removes a given empty directory.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 13


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Syntax: rmdir <directoryname>


Ex.
[student@localhost student]$ ls
it1 it2
[student@localhost student]$ rmdir it1
rmdir: `it1': Directory not empty
[student@localhost student]$ rmdir it2
[student@localhost student]$ ls
it1
[student@localhost student]$
d. rm –r :
 This commands removes the directory along with its associated files
stored in the it.
Syntax: rm -r <directoryname>
Ex.
[student@localhost student]$ ls
it1
[student@localhost student]$ rmdir it1
rmdir: `it1': Directory not empty
[student@localhost student]$ rm -r it1
[student@localhost student]$ ls
[student@localhost student]$
e. ls :
 This command lists the contents of a directory.
Syntax 1: ls // It displays all the non-hidden files in alphabetical order.

Ex.1
[student@localhost student]$ ls
[student@localhost student]$ mkdir it1
[student@localhost student]$ mkdir it2
[student@localhost student]$ ls
it1 it2
[student@localhost student]$ mkdir a
[student@localhost student]$ ls
a it1 it2
[student@localhost student]$

Syntax 2: ls -l // show all the non hidden files with full details.
Ex.1
[student@localhost student]$ ls
a it1 it2
[student@localhost student]$ ls -l
total 12
drwxrwxr-x 2 student student 4096 Jun 18 12:05 a
drwxrwxr-x 2 student student 4096 Jun 18 12:05 it1
drwxrwxr-x 2 student student 4096 Jun 18 12:05 it2
[student@localhost student]$
Syntax 3: ls –a // show all the files both hidden and non-hidden.
Ex.
[student@localhost student]$ ls -a

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 14


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

. a .bash_logout .bashrc .emacs it1 .kde .zshrc


.. .bash_history .bash_profile .canna .gtkrc it2 .xemacs
[student@localhost student]$

Syntax 4: ls –la
* Make the output as a long listing
* Show all the files both hidden and non-hidden.
Ex.
[student@localhost student]$ ls -la
total 68
drwx------ 7 student student 4096 Jun 18 12:05 .
drwxr-xr-x 409 root root 8192 Jun 17 09:35 ..
drwxrwxr-x 2 student student 4096 Jun 18 12:05 a
-rw------- 1 student student 2114 Jun 17 15:04
.bash_history
-rw-r--r-- 1 student student 24 Jun 17 09:35 .bash_logout
-rw-r--r-- 1 student student 191 Jun 17 09:35
.bash_profile
-rw-r--r-- 1 student student 124 Jun 17 09:35 .bashrc
-rw-r--r-- 1 student student 5542 Jun 17 09:35
.canna
-rw-r--r-- 1 student student 237 Jun 17 09:35 .emacs
-rw-r--r-- 1 student student 120 Jun 17 09:35 .gtkrc
drwxrwxr-x 2 student student 4096 Jun 18 12:05 it1
drwxrwxr-x 2 student student 4096 Jun 18 12:05 it2
drwxr-xr-x 3 student student 4096 Jun 17 09:35 .kde
drwxr-xr-x 2 student student 4096 Jun 17 09:35
.xemacs
-rw-r--r-- 1 student student 220 Jun 17 09:35 .zshrc
[student@localhost student]$
f. pwd : [ Present Working Directory]

 This command display the path of the present working directory.

Syntax : pwd
Ex.
[student@localhost student]$ ls
a it1 it2
[student@localhost student]$ pwd
/home/student
[student@localhost student]$ cd it1
[student@localhost it1]$ pwd
/home/student/it1
[student@localhost it1]$
PROCESS COMMANDS
1) Command : echo $$
Purpose : It is used to display the process number of the current shell.
Syntax : echo $$
Example : $ echo $$

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 15


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

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

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

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

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

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

RESULT:
Thus the various basic commands of UNIX have been executed successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 16


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No:2 IMPLEMENTATION OF SYSTEM CALLS

AIM:
To develop C programs using
a. fork, getpid, execlp, exit & wait system calls
b. stat, opendir, readdir & closedir system calls

a. fork, getpid, execlp, wait, exit:


Algorithm
 Create a C file using vi editor.
 Declare a variable of integer type named pid.
 Create a child process by calling fork () system call and assign the return value
of fork () to pid.
 Check whether the child process is created by checking the value of pid. If pid
value is less than zero it will display “Fork failed”. If pid value is equal to zero child process
executes its job. If pid value is greater than zero parent process executes its code.
 Get the calling process id by calling getpid () system call.
 Delay the process for given time using wait () system call.
 Quit from the process using exit () system call.

Program:

#include<stdio.h>
int main(int argc,char *argv[])
{
int pid,p;
pid=fork();
if(pid<0)
{
fprintf(stderr,"fork failed");
exit(0);
}
if(pid==0)
{
execlp("whoami",NULL);
}
wait(NULL);
p=getpid();
printf(“%d\n”,p);
printf("finish\n");
exit(0);
}
Output:
[student@localhost student]$ cc fork.c
[student@localhost student]$ ./a.out
student
4294
finish

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 17


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

b. opendir, closedir, lstat:

Algorithm:

 Create a C file using vi editor.


 Read a directory name
 Check whether the given directory is available
 Open the directory
 Display all the files in that directory and also display whether it is a regular file or directory
with time
 Close the directory

Program:

#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/time.h>
int main(int argc,char *argv[])
{
DIR *dp;
struct dirent *dirp;
struct stat sbuf;
char *ptr;
dp=opendir(argv[1]);
if(chdir(argv[1]))
{
printf("change directory is not done");
exit(0);
}
while((dirp=readdir(dp))!=NULL)
{
printf("%d",dirp->d_ino);
printf("\n%s",dirp->d_name);
lstat(dirp->d_name,&sbuf);
if(S_ISREG(sbuf.st_mode))
printf("regular");
else if(S_ISDIR(sbuf.st_mode))
printf("directory");
else
printf("neither");
ptr=ctime(&sbuf.st_mtime);
printf("%s",ptr);
}
closedir(dp);
exit(0);
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 18


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

./a.out anydirectoryname
[student@localhost student]$ ./a.out a
3064712
. regular Mon Dec 23 15:30:00 2013
1803105
.. regular Sat Apr 5 13:08:23 2014
3064835
f1 neither Mon Dec 23 15:30:00 2013
[student@localhost student]$

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

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 19


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX.NO:3.1 WRITE A PROGRAM TO SIMULATE UNIX


COMMAND ls
AIM:
To write a program to simulate UNIX commands like Is,grep,etc.

ALGORITHM:

 Create a C file using vi editor


 Declare two variables n,i
 Declare the variable pathname of character type
 Store path of current working directory using getcwd system call.
 Scan directory of the stored path using scandir system call and sort the resultant array
of structure
And assign them to n variable
 If n value is less than 0,display as error
 Else Display dname member for all entries if it is not a hidden file.
 Stop.

PROGRAM:

/* ls command simulation - list.c */

#include <stdio.h>
#include <dirent.h>
main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
getcwd(pathname);
n = scandir(pathname, &namelist, 0, alphasort);
if(n < 0)
printf("Error\n");
else
for(i=0; i<n; i++)
if(namelist[i]->d_name[0] != '.')
printf("%-20s", namelist[i]->d_name);
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 20


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

$ cc list.c
$ ./a.out
cmdpipe.c consumer.c dirlist.c exec.c
ex6a.c ex6b.c ex6c.c ex6d.c
fappend.c fcfs.c fcreate.c fork.c
fread.c hello list list.c
pri.c producer.c rr.c simls.c
sjf.c stat.c wait.c

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

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 21


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX NO:3.2 WRITE A PROGRAM TO SIMULATE UNIX


COMMANDS grep

AIM:
To write a program to simulate UNIX command grep

ALGORITHM:
 Create a C file using vi editor
 Initialize filename, pattern to be searched and a temp variable
 Enter the file name to be searched
 Enter the pattern to be searched
 Open the file using system call and read the contents of the file
 Read the contents of file till the end and display them as output
 Repeat the process till the end of the file

PROGRAM:

/* grep command simulation – grepb.c */

#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 22


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

[student@localhost ~]$ vi grepb.c


[student@localhost ~]$ cc grepb.c
[student@localhost ~]$ cat > content
os
is
was
has
[student@localhost ~]$ cat content
os
is
was
has
[student@localhost ~]$ ./a.out
Enter file name
content
Enter pattern to be searched
is
is

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

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 23


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

SHELL PROGRAMMING
Ex.No:4.1 TO FIND GREATEST AMONG THREE NUMBERS

AIM:
To find the greatest of three numbers using shell programming.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the three numbers a,b,c.
Step 3: If a is greater than b and c.
Step 4: Print “A is greatest”.
Step 5: If b is greater than a and c.
Step 6: Print “B is greater”.
Step 7: Else print “C is greater”.
Step 8: Stop the program.

PROGRAM:

echo "Enter three no"


read a
read b
read c
if test $a -gt $b
then
if test $a -gt $c
then
echo "a is greatest"
fi
fi
if test $b -gt $c
then
echo "b is greatest"
else
echo "c is greatest"
fi

OUTPUT :

[cse@unixSERVER ~]$ sh great.sh


Enter three no
10
12
13
C is greatest

RESULT:
Thus the program to find greatest of three numbers using shell programming was
executed and verified

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 24


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX NO: 4.2 TO PERFORM ARITHMETIC OPERATION


AIM:
To perform arithmetic operations using shell programming.

ALGORITHM:

Step 1: Start the program.


Step 2: Read the two numbers a and b.
Step 3: Select the arithmetic operation to be performed.
Step 4: Perform “Addition” if case 1 is selected.
Step 5: Perform “Subtraction” if case 2 is selected.
Step 6: Perform “Multiplication” if case 3 is selected.
Step 7: Perform “Division” if case 4 is selected.
Step 8: Print the required result.
Step 9: Stop the program.

PROGRAM:

echo "1.add"
echo "2.subtract"
echo "3.multiply"
echo "4.divide"
echo "Enter your choice"
read i
echo "Enter two numbers"
read a
read b
case $i in
1) x=`expr $a + $b`
echo "The sum is $x";;
2) x=`expr $a - $b`
echo "The result is $x";;
3) x=`expr $a \* $b`
echo "The product is $x";;
4) x=`expr $a / $b`
echo "The result is $x";;
esac

OUTPUT:

[cse@unixSERVER ~]$ sh arit.sh


1.add
2.subtract
3.multiply
4.divide
Enter your choice
1
Enter two numbers

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 25


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

2
3
The sum is 5
[cse@unixSERVER ~]$ sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
2
Enter two numbers
5
1
The result is 4
[cse@unixSERVER ~]$ sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
3
Enter two numbers
6
5
The product is 30
[cse@unixSERVER ~]$ sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
4
Enter two numbers
20
5 The result is 4

RESULT:
Thus the program to perform arithmetic operations using shell programming was
executed and verified.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 26


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX NO:4.3 FACTORIAL OF A GIVEN NUMBER


AIM:
To find the factorial of a given number using shell programming.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare a variable a.
Step 3: Assume n is equal to a.
Step 4: If a is o, then print the factorial value as 1.
Step 5: Else, subtract the value of a by 1.
Step 6: Multiply with the previous value of a.
Step 7: Print the required result.
Step 8: Stop the program.

PROGRAM:
echo "Factorial of the given number"
echo "Enter the number"
read a
fact=1
n=$a
if [ $a -eq 0 ]
then
echo "Factorial of 0 is 1"
else
while [ $a -gt 0 ]
do
fact=`expr $fact \* $a`
a=`expr $a - 1`
done
echo "Factorial of $n is $fact"
fi

OUTPUT:
[cse@unixSERVER ~]$ sh fact.sh
Factorial of the given number
Enter the number
3
Factorial of 3 is 6

RESULT:
Thus the program to find the factorial of a given number using shell programming was
executed and verified.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 27


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 5.1 IMPLEMENTATION OF FCFS CPU SCHEDULING


ALGORITHM
AIM:
To write a program for implementing first come first serve using C programming.

ALGORITHM:

1. Initialize the variables of integer type I,j,b,n,p[5],wt[5],bt[5] ],at[5],tat[5];


2. Initialize the variables of integer type temp,te,t;
3. Initialize the variables of float type twt,ttat,awt,atat;
4. Get the total count of processes
5. Using for loop get the process number for each process
6. Get the details of each processes arrival time and burst time using for loop
7. To calculate arrival time for each process check if at[i]<at[j],then perform
swapping using temp variable similarly perform the same steps for bt and p
8. Assign wt[-1]=0 and bt[-1]=0
9. Using for loop check if at [i]>0,assign b=0 and j=0
10. Using while loop check if j< I, Calculate the burst time b=b+bt[j], increment the
value of j.
11. Calculate waiting time wt[i]=b-at[i] else calculate wt[i]=wt[i-1]+bt[i-1]-at[i]
12. For each process ,calculate the turnaround time using for loop and Assign twt and
ttat to zero.
13. For each process calculate total waiting time twt=twt+wt[i],Calculate total turn
around time ttat=ttat+tat[i]
14. Calculate average turn around time atat=ttat/n, Calculate average waiting time
Awt=twt/n;
15. Display the result of process turn around time ,waiting time and average waiting
time and average turn around time
16. Stop the program

PROGRAM:
#include<stdio.h>
int main()
{
int i,j=0,b,n,p[5],wt[5],bt[5],at[5],tat[5];
int temp,te,t;
float twt,ttat,awt,atat;
printf("Enter the no of processes:\t");
scanf("%d",&n);
printf("Enter the process numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
printf("Enter the arrival time of %d process\t",i);
scanf("%d",&at[i]);
printf("Enter the burst time of %d process\t",i);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 28


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

}
scanf("%d",&bt[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(at[i]<at[j])
{
temp=at[i];
at[i]=at[j]
at[j]=temp;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
te=p[i];
p[i]=p[j];
p[j]=te;
}
}
}
wt[-1]=0;
bt[-1]=0;
for(i=0;i<n;i++)
{
if(at[i]>0)
{
b=0;
j=0;
while(j<i)

b=b+bt[j];
j++;
}
wt[i]=b-at[i];
}
else
wt[i]=wt[i-1]+bt[i-1]-at[i];
}
for(i=0;i<n;i++)
tat[i]=wt[i]+bt[i];
twt=0;
ttat=0;
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
atat=ttat/n;
awt=twt/n;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 29


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

printf("\nProcess\twt\t tat\n");
for(i=0;i<n;i++)
{
printf("\nP%d\t%d\t%d\n",p[i],wt[i],tat[i]);
}
printf("The average wt is %f\n",awt);
printf("The average tat is %f\n",atat);
return 1;
}{

OUTPUT:
[student@localhost student]$ cc fifo.c
[student@localhost student]$ ./a.out
Enter the no of processes: 3
Enter the process numbers:
1
2
3
Enter the arrival time of 0 process 0
Enter the burst time of 0 process 12
Enter the arrival time of 1 process 0
Enter the burst time of 1 process 8
Enter the arrival time of 2 process 0
Enter the burst time of 2 process 13
Process wt tat
P1 0 12
P2 12 20
P3 20 33
The average wt is 10.666667
The average tat is 21.666666

RESULT:
Thus the program for implementing first come first serve has been executed and
verified.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 30


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 5.2 IMPLEMNTATION OF SJF CPU SCHEDULING


ALGORITHM
AIM:
To write a program for implementing shortest job first using c programming.

ALGORITHM:

1. Initialize the variables of integer type I,j,b,n,p[5],wt[5],bt[5],at[5],tat[5].


2. Initialize the variables of integer type temp,te,t.
3. Initialize the variables of float type twt,ttat,awt,atat.
4. Get the number of processes and process number.
5. Using for loop get the process number for each processes.
6. Using for loop get the details of each processes arrival time and burst time.
7. To calculate arrival time for each process check bt[i]<bt[j] perform the swapping
operation using temp sililarly perform the same steps for bt and p.
8. Assign wt[-1]=0 and bt[-1]=0.
9. Using for loop concatenate wt[i-1] and bt[i-1] and assign it to wt[i].
10. For each process calculate the turn around time using for loop and assign twt and ttat
to zero.
11. For each process calculate the waiting time twt=twt+wt[i], calculate ctotal turn
around time ttat=ttat+tat[i]
12. Calculate average turn around time atat=ttat/n ,calculate average waiting time and
awt=twt/n,
13. Display the result of process turn around time ,waiting time,average waiting timeand
average turn around time .
14. Stop the program

PROGRAM:
#include<stdio.h>
int main()
{
int i,j=0,b,n,p[5],wt[5],bt[5],tat[5];
int te,t;
float twt,ttat,awt,atat;
printf("Enter the no of processes:\t");
scanf("%d",&n);
printf("Enter the process numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
printf("Enter the burst time of %d process\t",i);
scanf("%d",&bt[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<=i;j++)

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 31


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

{
if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
te=p[i];
p[i]=p[j];
p[j]=te;
}
}
}
wt[-1]=0;
bt[-1]=0;
for(i=0;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
}
for(i=0;i<n;i++)
tat[i]=wt[i]+bt[i];
twt=0;
ttat=0;
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
atat=ttat/n;
awt=twt/n;
printf("\nProcess\twt\t tat\n");

for(i=0;i<n;i++)
{
printf("\nP%d\t%d\t%d\n",p[i],wt[i],tat[i]);
}
printf("The average wt is %f\n",awt);
printf("The average tat is %f\n",atat);
return 1;
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 32


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:
[student@localhost student]$ cc sjf.c
[student@localhost student]$ ./a.out
Enter the no of processes: 3
Enter the process numbers:
1
2
3
Enter the burst time of 0 process 12
Enter the burst time of 1 process 6
Enter the burst time of 2 process 9

Process wt tat
P2 0 6
P3 6 15
P1 15 27
The average wt is 7.000000
The average tat is 16.000000

RESULT:
Thus the program for implementing shortest job first has been executed and verified.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 33


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 5.3 IMPLEMENTATION OF ROUND-ROBIN CPU


SCHEDULING ALGORITHM
AIM:
To perform the round robin scheduling using a c program.

ALGORITHM:

1. Initialize the variables of integer type ts,i,j=0,n,p[5],wt[5],bt[5],bt1[5],tat[5]


2. Initialize the variables of integer type t,tbt,et[15]
3. Initialize the variables of float type twt=0,ttat=0,awt,atat
4. Get the number of processes and process number from the user.
5. Get the burst time for each process and time slicing for the process.
6. Assign tbt=0,For each process calculate the total burst time for each process using
formula tbt=tbt+bt[i]
7.Assign bt1[i] as bt[i]
8.Display the total burst time value
9.while tbt is greater than zero, check if bt[i] is greater than zero ,calculate t=bt[i]-ts.
10. If t>=0,calculate bt[i]=bt[i]-ts, tbt=tbt-ts and et[j]=et[j-1]+ts.
11.Check if bt[i] is equal to zero
12. Asssign tat[i]=et[j],increment the value of j.
13. For all n process calculate total turn around time ttat=ttat+tat[i],waiting time=
wt[i]=tat[i]-bt1[i], and total waiting time twt=twt+wt[i].
14. Calculate average turn around time atat= ttat/n ,average waiting time awt=twt/n.
15. Dispaly the Average waiting time and Average turn around time.
16. stop the program

PROGRAM:

#include<stdio.h>
int main()
{
int ts,i,j=0,n,p[5],wt[5],bt[5],bt1[5],tat[5];
int t,tbt,et[15];
float twt=0,ttat=0,awt,atat;
printf("Enter the no of processes:\t");
scanf("%d",&n);
printf("Enter the process numbers:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
{
printf("Enter the burst time of %d process\t",i);
scanf("%d",&bt[i]);
}
printf("Enter the time slice \t");
scanf("%d",&ts);
printf("Process\tBurst time\n");

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 34


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

for(i=1;i<=n;i++)
{
printf("%d\t%d\n",p[i],bt[i]);
}

tbt=0;
for(i=1;i<=n;i++)
{
tbt=tbt+bt[i];
bt1[i]=bt[i];
}
printf("Total burst time\t %d\n",tbt);
et[0]=0;
j=1;
while(tbt>0)
{
for(i=1;i<=n;i++)
{
if(bt[i]>0)
{
t=bt[i]-ts;
if(t>=0)
{
bt[i]=bt[i]-ts;
tbt=tbt-ts;
et[j]=et[j-1]+ts;
}
else
{
bt[i]=bt[i]-ts-t;
tbt=tbt-ts-t;
et[j]=et[j-1]+ts+t;
}

if(bt[i]==0)
{
tat[i]=et[j];
}
j++;
}
}
}
for(i=1;i<=n;i++)
{
ttat=ttat+tat[i];
wt[i]=tat[i]-bt1[i];
twt=twt+wt[i];
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 35


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

atat= ttat/n;
awt=twt/n;
printf("Average waiting time %f",awt);
printf("Average turn around time%f\n",atat);
return 1;
}

OUTPUT:
[student@localhost student]$ cc round.c
[student@localhost student]$ ./a.out
Enter the no of processes: 4
Enter the process numbers:
1
2
3
4
Enter the burst time of 1 process 53
Enter the burst time of 2 process 17
Enter the burst time of 3 process 68
Enter the burst time of 4 process 24
Enter the time slice 15
Process Burst time
1 53
2 17
3 68
4 24
Total burst time 162
Average turn around time119.750000
Average waiting time 79.250000

RESULT:
Thus the program for round robin scheduling has been executed and output is verified
successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 36


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX.NO:5.4 “PRIORITY CPU SCHEDULING ALGORITHM”


AIM:
To write a c program to implement the Priority Scheduling (PR).

ALGORITHM:
1. Iinitialize the variables of integer type i,j=0,b,n,p[5],wt[5],bt[5],pr[5],tat[5],
2. Initialize the variables of integer type temp,te,t.
3. Initialize the variables of float type twt,ttat,awt,atat.
4. Get the number of processes and process numbers from the user using for loop.
5. Get the burst time and priority for each process using for loop.
6. Check if pr[i] < pr[j], swap pr[i] and p[j] using temporary variable pr[i] and pr[j].
7. Swap bt[i],bt[j] using temporary variable t.
8. Swap p[i] and p[j] using temporary variable te.
9. Assign wt[-1]=0 and bt[-1]=0.
10.For each process , calculate wt[i]=wt[i-1]+bt[i-1].
11.For each process , calculate tat[i]=wt[i]+bt[i].
12.Assign twt=0, ttat=0.
13.Calculate twt=twt+wt[i],ttat=ttat+tat[i].
14.Calculate atat=ttat/n,awt=twt/n.
15. Dispaly the Average waiting time and Average turn around time.
16. Stop the program

PROGRAM:

#include<stdio.h>
int main()
{
int i,j=0,b,n,p[5],wt[5],bt[5],pr[5],tat[5];
int temp,te,t;
float twt,ttat,awt,atat;
printf("Enter the no of processes:\t");
scanf("%d",&n);
printf("Enter the process numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
printf("Enter the priority of %d process\t",i);
scanf("%d",&pr[i]);
printf("Enter the burst time of %d process\t",i);
scanf("%d",&bt[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(pr[i]<pr[j])

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 37


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

{
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
te=p[i];
p[i]=p[j];
p[j]=te;
}
}
}
wt[-1]=0;
bt[-1]=0;
for(i=0;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
}
for(i=0;i<n;i++)
tat[i]=wt[i]+bt[i];
twt=0;
ttat=0;
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
atat=ttat/n;
awt=twt/n;
printf("\nProcess\twt\t tat\n");

for(i=0;i<n;i++)
{
printf("\nP%d\t%d\t%d\n",p[i],wt[i],tat[i]);
}
printf("The average wt is %f\n",awt);
printf("The average tat is %f\n",atat);
return 1;
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 38


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:
[student@localhost student]$ cc priority.c
[student@localhost student]$ ./a.out
Enter the no of processes: 4
Enter the process numbers:
1
2
3
4
Enter the priority of 0 process 3
Enter the burst time of 0 process 7
Enter the priority of 1 process 2
Enter the burst time of 1 process 4
Enter the priority of 2 process 4
Enter the burst time of 2 process 1
Enter the priority of 3 process 1
Enter the burst time of 3 process 4
Process wt tat
P4 0 4
P2 4 8
P1 8 15
P3 15 16
The average wt is 6.750000
The average tat is 10.750000

RESULT :
Thus the program to implement the Priority Scheduling was executed successfully
and output was verified

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 39


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX.NO:6 IMPLEMENTATION OF SEMAPHORES


“PRODUCERS – CONSUMER PROBLEM USING SEMAPHORES”
AIM:
To implement the producer consumer relationship using semaphores.

ALGORITHM:
 Start the program
 Include the header files for pthread ,standard library ,semaphore.
 Create an empty buffer size of 10.
 Initialize the buffer[buff-size] of character type
 Initialize the integer variables in ,out and assign values as 0
 Initialize two threads void *Producer(void *); and void *Consumer(void *);
 Initialize the global semaphores sem_t empty, full,mutex;, int numIters.
 Initialize the thread ids pthread_t pid, cid.
 Assign numIters to (argv[1]) convert the string to integer using atoi function
 Initially assign sem variables empty to 1,mutex to 1 and full to 0
 Create a new thread using pthread_create and pass arguments as address of
pid for producer
 Create a new thread using pthread_create and pass arguments as address of
cid for consumer
 To terminate or suspend the thread ,use pthread_join and pass arguments pid
and cid to null.

PRODUCER
 Initialize the integer variable produced
 Check if produced < numiters using for loop , Repeat a loop for 10 times
 Assign in = (in+1) % 10.
 The sem_wait() function locks the semaphore referenced by sem by
performing a semaphore lock operation on that semaphore i.e wait on
semaphore address of empty. And wait on address of mutex
 Put the produced item into the buffer.
 Signal on semaphore mutex using sem_post function , The sem_post()
function shall unlock the semaphore referenced by sem by performing a
semaphore unlock operation on that semaphore.
 Signal on semaphore full using sem_post function

CONSUMER
 Initialize the variables data , consumed of integer type
 Check if consumed less than numiters using for loop
 Using sem_wait function , wait on semaphore address of full and wait on
address of mutex
 Assign out = (out + 1) % 10;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 40


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

 Assign data to buffer[out]


 Use sleep() function in order to wait for a current thread for a 2 seconds.
 Use sem_post() function for address of mutex and empty ,which shall unlock
the semaphore referenced by sem by performing a semaphore unlock
operation on that semaphore

PROGRAM:

#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
#define BUFF_SIZE 10
char buffer[BUFF_SIZE];
int in =0, out =0;
#define SHARED 1
void *Producer(void *); /* the two threads */
void *Consumer(void *);
sem_t empty, full,mutex; /* the global semaphores */
int numIters;
int main(int argc, char *argv[])
{
pthread_t pid, cid; /* thread ids */
numIters = atoi(argv[1]);
sem_init(&empty, SHARED, 1); /* sem empty = 1 */
sem_init(&mutex, SHARED, 1); /* sem mutex = 1 */
sem_init(&full, SHARED, 0); /* sem full = 0 */
printf("main started\n");
pthread_create(&pid, NULL, Producer, NULL);
pthread_create(&cid, NULL, Consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
printf("main done\n");
}
/* deposit 1, ..., numIters into the data buffer */
void *Producer(void *arg)
{
int produced;
printf("Producer created\n");
for (produced = 0; produced < numIters; produced++)
{
printf(" Producer produced : %d\n", produced);
in = (in+1) % 10;
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = produced;
sem_post(&mutex);
sem_post(&full);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 41


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

}
}
void *Consumer(void *arg)
{
int consumed;
int data;
printf("Consumer created\n");

for (consumed = 0; consumed < numIters; consumed++)


{
sem_wait(&full);
sem_wait(&mutex);
out = (out + 1) % 10;
data = buffer[out];
printf(" \tConsumer consumed : %d\n",data);
sleep( 2 );
sem_post(&mutex);
sem_post(&empty);
}
}

OUTPUT:
[student@localhost student]$ cc pcp.c -lpthread
[student@localhost student]$ ./a.out 5
main started
Producer created
Producer produced : 0
Producer produced : 1
Consumer created
Consumer consumed : 0
Producer produced : 2
Consumer consumed : 1
Producer produced : 3
Consumer consumed : 2
Producer produced : 4
Consumer consumed : 3
Consumer consumed : 4
main done

RESULT:
Thus the program to implement the producer-consumer problem was executed
successfully and output was verified.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 42


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX NO:7.1 IMPLEMENTATION OF SHARED MEMORY AND


IPC
AIM:
To write a c program to develop an application using Inter process Communication
(IPC) using pipes.

ALGORITHM:

1. Initialize an array fd[] and child of integer type


2. Initialize array a of character type.
3. Get the string from user to WRITE in to the pipe
4. Get the string from the user using scanf statement.
5. call the function pipe(fd).
6. assign child as fork().
7. Check if not equal to child
8. Then close index fd of 0
9. Write the string in file destination using write () and wait
10. Else close the index 1 of fd
11. Read the index fd(0),b,5
12. Display the string retrieved from the pipe

PROGRAM:

#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf("Enter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,strlen(a));
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,10);
printf("\n The String retrieved from the pipe is: %s\n",a);
}
return 0;
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 43


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

[student@localhost student]$ cc ipcpipe.c


[student@localhost student]$ ./a.out
Enter the string to enter into the pipe:Computer
The String retrieved from the pipe is: Computer

RESULT:
The C program to develop an application using Inter process Communication (IPC)
using pipes is implemented.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 44


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No:7.2 IPC USING SHARED MEMORY


AIM:
To implement interprocess communication using shared memory.

ALGORITHM:
 Create the child process using fork().
 Create the shared memory for parent process using shmget() system call.
 Now allow the parent process to write in shared memory using shmpet pointer
which is return type of shmat().
 Now across and attach the same shared memory to the child process.
 The data in the shared memory is read by the child process using the shmptr
pointer.

PROGRAM:

#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child,shmid,i;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,0666|IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf("\nPARENT WRITING\n");
for(i=0;i<10;i++)
{
shmptr[i]='a'+i;
putchar(shmptr[i]);
}
printf("\n");
wait(NULL);
}
else
{
shmid=shmget(2041,32,0666);
shmptr=shmat(shmid,0,0);
printf("\n CHILD IS READING\n");
for(i=0;i<10;i++)
putchar(shmptr[i]);
printf("\n");
wait(NULL);
}
return 0;
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 45


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

[student@localhost student]$ cc ipc.c


[student@localhost student]$ ./a.out
PARENT WRITING
hello
CHILD IS READING
hello

RESULT:
The C program to develop an application using Inter process Communication (IPC)
using shared memory is implemented

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 46


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

EX NO:8 BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE


AIM:
To implement the bankers Algorithm for Dead Lock Avoidance

ALGORITHM:

1. Start the program


2. Include the functions for alloc,max,cneed,a ,total.
3. With in main function ,initialize the variables r,p,i,j,k,flag,f,count,x,flag1 of integer
type .
4. Assign count and flag1 as 0
5. Get the values of resources from the user
6. Assign f=r.
7. Get the value of total memory for the resources sequentially using for loop
8. Get the number of processes from the user
9. Get the allocated and maxneed memory from the user
10. For each process get alloc value and max need memory for each process
11. Calculate the cneed for each process using the formula cneed[i][j]=max[i][j]-
alloc[i][j]
12. For each process and resources perform the sequence of execution
13. Get the avail value and allocate find and need values
14. Check whether it is possible to allocate
15. It is possible then the system is in safe state
16. Else system is not in safety state
17. If the new requests comes then the check that the system is in safety
18. Stop the program

PROGRAM:

#include<stdio.h>
int alloc[10][10];
int max[10][10];
int cneed[10][10];
int a[10];
int total[10];
void main()
{
int r,p,i,j,k,flag,f,count,x,flag1;
count=0;
flag1=0;
printf(“\nEnter number of resources:”);
scanf(“%d”,&r);
f=r;
printf(“\nEnter total memory of given resources sequentially:”);
for(i=0;i<r;i++)
scanf(“%d”,&total[i]);
printf(“\nEnter number of processes:”);
scanf(“%d”,&p);
printf(“\nEnter Allocated and Maxneed memory…”);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 47


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

getch();
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf(“\nFor Process %d=”,i);
scanf(“%d%d”,&alloc[i][j],&max[i][j]);
//calculating current need
cneed[i][j]=max[i][j]-alloc[i][j];
}
}
printf(“\nSequence of execution is…\n”);
k=0;
for(j=0;j<r;j++)
{
for(i=0;i<p;i++)
{
a[k]+=alloc[i][j];
}
a[k]=total[k]-a[k];
k++;
}
while(count!=p)
{
for(i=0;i<p;i++)
{
flag=0;
if(cneed[i][f]!=1)
{
for(j=0;j<r;j++)
{
total[j]=a[j]-cneed[i][j];
}
for(k=0;k<r;k++)
{
if(total[k]<0)
{
flag=1;
}
}
}
if((flag==0)&&(cneed[i][f]!=1))
break;
}
x=i;
cneed[x][f]=1;
printf(“P%d->”,x);
count++;
for(i=0;i<r;i++)
{

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 48


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

total[i]+=max[x][i];
a[i]=total[i];
}
for(i=0;i<p;i++)
{
if((cneed[i][0]<a[0])&&(cneed[i][f]==0))
flag1=1;
}
if(flag1==0)
break;
}
if(flag1==0)
printf(“\nUnsafe…”);
else
printf(“\nSafe…”);
getchar();
}

OUTPUT:
//For Safe condition
Enter number of resources:3
Enter total memory of given resources sequentially:10 5 7
Enter number of processes:5
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5
For Process 0=0 3
For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
For Process 3=2 2
For Process 3=1 2
For Process 3=1 2
For Process 4=0 4
For Process 4=0 3
For Process 4=2 3
Sequence of execution is...
P1->P3->P0->P2->P4->
Safe...

//For Unsafe condition


Enter number of resources:3
Enter total memory of given resources sequentially:6 5 4
Enter number of processes:3
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 49


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

For Process 0=0 3


For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
Sequence of execution is...
P1->Unsafe...

RESULT:
Thus the program to implement the bankers Algorithm for Dead Lock avoidance has
been executed successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 50


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 9 IMPLEMENTATION OF DEADLOCK DETECTION

AIM:
To write a C program to implement the bankers Algorithm for Dead Lock Detection

ALGORITHM:

1. Start the program


2. Initialize the variables of integer type found , flag,l,p[4][5],tp,tr,c[4][5], i,j,k=1
,m[5], r[5],a[5], temp[5],sum=0.
3. Get the total no of process and total no of resources from user.
4. Enter the claim matrix from the user
5. Using for loop ,assign I as 1 check if i<=tp and increment the value of i, print the
process.
6. Enter the allocation matrix and again using for loop print the process.
7. Using for loop and get the resource vector (total resources) from the user.
8. Using for loop and get the availability vector(available sources) from the user.
9. Assign temp[i]=a[i] using for(i=1;i<=tp;i++) assign sum=0 and using
for(i=1;i<=tp;i++) assign sum+=p[i][j].
10. Using if condition check if sum==0 and assign m[k]=I and increment the k
value.
11. Using for assign i=1 and check if i<=tp and using for loop assign l=1 and check
if l<k check whether if i!=m[l] and assign flag=1.
12. Using for loop assign j=1 and check if j<=tr and assign flag=0.
13. Check if flag==1 then assign m[k]=I and increment the k value.
14. Using for loop assign j=1 and check if j<=tr and assign temp[j]+=p[i][j]. And
then print the dead lock causing processes.
15. Using for loop assign j=1 and check if j<=tp and assign found=0.
16. Using for loop assign i=1 and check if i<k and check if j==m[i] and assign
found=1.
17. Then check if found==0, then print the value of j

PROGRAM:
#include <stdio.h>;
#include <conio.h>;
void main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("Enter total no of processes");
scanf("%d",&tp);

printf("Enter total no of resources");


scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 51


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}

for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
for(j=1;j<=tr;j++)
{
flag=0;
break;
}
}
if(flag==1)
{

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 52


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}

OUTPUT:
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
01001
00101
00001
10101
Enter allocation matrix :
10110
11000
00010
00000
Enter resource vector (Total resources) :
21121
Enter availability vector (available resources) :
00001
deadlock causing processes are : 2 3

RESULT:
Thus the program to implement the bankers Algorithm for Dead Lock Detection has
been executed successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 53


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 10 IMPLEMENTATION OF THREADING AND


SYNCRONIZATION APPLICATION
AIM:
To write a program to implement threading and synchronization applications.

ALGORITHM:

1. Start the program.


2. Include the header files stdio.h,string,h,pthread.h,stdlib.h,unistd.h.
3. Initialize the function pthread_t tid[2].
4. Create the main function void* dosomething(void *arg).
5. Initialize unsigned variable i=0 of long type.
6. Assign pthread_t id = pthread_self().
7. Using if statement check if(pthread_equal(id,tid[0])) then print first thread
processing else print second thread processing.
8. Using for i value 0, check if i less than 0xffffffff and increment the value of i and
return null
9. Create the main function.
11. Initialize i=0 of integer type.
12. Initialize err of integer type.
13. Using while loop check if i<2
14. Assign err = pthread_create(&(tid[i]), null, &dosomething, null).
15. Using if statement check err not equal to 0, then print thread cant created else
print thread created and increment i.
16. Use the function sleep for 5 minutes.
17. Return 0.
18. Terminate the process.

PROGRAM:

$cc thread.c -lpthread

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();

if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 54


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

{
printf("\n Second thread processing\n");
}

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

return NULL;
}
int main(void)
{
int i = 0;
int err;

while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");

i++;
}
sleep(5);
return 0;
}

OUTPUT:

First thread processing 1


Thread created successfully
Second thread processing 2
can't create thread

RESULT:
Thus the program has been executed and verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 55


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 11 IMPLEMENTATION OF MEMORY MANAGEMENT


SCHEME (BEST FIT, FIRST FIT & WORST FIT)
AIM:
To write a C program to implement Memory Management Scheme for Best Fit, First
Fit and Worst Fit.

ALGORITHM:

Step 1: Start the program.


Step 2: Define 3 functions ff(), bf() and wf().
Step 3: Get the choice from the user.
Step 4: Get the number of processes, the processes size and the memory spaces.
Step 5: Based on the choice perform Best Fit, First Fit and Worst Fit.
Step 6: Display the result of the schemes.
Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display(int a[],int n)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
{
printf("\t%d ",a[i]);
}
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 56


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

}
}
}
}
void first_fit(int psize[],int np,int msize[],int nm)
{
int i,j,itot,etot,flag[30]={0};
itot=etot=0;
for(i=0;i<np;i++)
{
for(j=0;j<nm;j++)
{
if(flag[j]==0 && msize[j]>=psize[i])
{
flag[j]=1;
itot=itot+msize[j]-psize[i];
break;
}
}
if(j==nm)
printf("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(i=0;i<nm;i++)
{
if(flag[i]==0)
etot=etot+msize[i];
}
printf("\n\nPROCESSES::");
display(psize,np);
printf("\n\nMEMORY HOLES::");
display(msize,nm);
printf("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",itot);
printf("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",etot);

}
void best_fit(int psize[],int np,int msize[],int nm)
{
int i,j,itot,etot,temp[30],flag[30]={0};
itot=etot=0;

for(i=0;i<nm;i++)
temp[i]=msize[i];

sort(temp,nm);
for(i=0;i<np;i++)
{
for(j=0;j<nm;j++)
{
if(flag[j]==0 && temp[j]>=psize[i])
{

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 57


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

flag[j]=1;
itot=itot+temp[j]-psize[i];
break;
}
}
if(j==nm)
printf("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(i=0;i<nm;i++)
{
if(flag[i]==0)
etot=etot+temp[i];
}
printf("\n\nPROCESSES::");
display(psize,np);
printf("\n\nMEMORY HOLES::");
display(temp,nm);
printf("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",itot);
printf("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",etot);
}
void worst_fit(int psize[],int np,int msize[],int nm)
{
int i,j,itot,etot,temp[30],flag[30]={0};
itot=etot=0;
for(i=0;i<nm;i++)
temp[i]=msize[i];

sort(temp,nm);
for(i=0;i<np;i++)
{
for(j=nm-1;j>=0;j--)
{
if(flag[j]==0 && temp[j]>=psize[i])
{
flag[j]=1;
itot=itot+temp[j]-psize[i];
break;
}
}
if(j==nm)
printf("\n\nTHERE IS NO SPACE FOR PROCESS %d ",i);
}
for(i=0;i<nm;i++)
{
if(flag[i]==0)
etot=etot+temp[i];
}
printf("\n\nPROCESSES::");
display(psize,np);
printf("\n\nMEMORY HOLES::");

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 58


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

display(temp,nm);
printf("\n\nTOTAL SUM OF INTERNAL FRAGMENTATION = %d ",itot);
printf("\n\nTOTAL SUM OF EXTERNAL FRAGMENTATION = %d ",etot);

}
/*************************************************************************/
void main()
{
int ch,np,nm,psize[30],msize[30];
printf("\nENTER NO OF PROCESSES::");
scanf("%d",&np);
printf("\n\nENTER SIZES OF PROCESSES::");
accept(psize,np);
printf("\nENTER NO MEMORY HOLES::");
scanf("%d",&nm);
printf("\n\nENTER SIZES OF MEMORY HOLES::");
accept(msize,nm);

while(1)
{
printf("\n\n\t\t**MAIN MENU**");
printf("\n\n\tMEMORY MANAGEMENT");
printf("\n\n\t1.FIRST FIT");
printf("\n\n\t2.BEST FIT");
printf("\n\n\t3.WORST FIT");
printf("\n\n\t4.QUIT");

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


scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nFIRST FIT::\n");
first_fit(psize,np,msize,nm);
break;
case 2:
printf("\n\n\tBEST FIT::\n");
best_fit(psize,np,msize,nm);
break;
case 3:
printf("\n\n\tWORST FIT::\n");
worst_fit(psize,np,msize,nm);
break;
case 4:
exit(1);
default:
printf("\n\nPLEASE ENTER CORRECT CHOICE!!");
}
}
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 59


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

ENTER NO OF PROCESSES::5

ENTER SIZES OF PROCESSES::10 20 15 30 45


ENTER NO MEMORY HOLES:: 7

ENTER SIZES OF MEMORY HOLES :: 5 15 10 35 25 20 25

**MAIN MENU**
MEMORY MANAGEMENT
1. FIRST FIT
2. BEST FIT
3. WORST FIT
4. QUIT

ENTER YOUR CHOICE:: 1

FIRST FIT ::

THERE IS NO SPACE FOR PROCESS 3


THERE IS NO SPACE FOR PROCESS 4

PROCESSES::
10 20 15 30 45

MEMORY HOLES::
5 15 10 35 25 20 25

TOTAL SUM OF INTERNAL FRAGMENTATION = 30


TOTAL SUM OF EXTERNAL FRAGMENTATION = 60

RESULT:
Thus C program to implement Best Fit, Worst Fit and First Fit of Memeory
Management Schemen was written, executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 60


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 12 IMPLEMENTATION OF PAGING TECHNIQUE OF


MEMEORY MAMANEGMENT
AIM:
To write a C program to implement Paging Technique of Memory Management.

ALGORITHM:

Step 1: Start the program.


Step 2: Using Structure create a page table.
Step 3: Get the page number and frame number from the user.
Step 4: Based on this create a page table.
Step 5: Get the logical address from user and calculate the corresponding physical
address.
Step 6: Display the result.
Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int page;
int frame;
struct list *next;
}*p;
void add(struct list *q,int page,int frame)
{
if(p==NULL)
{
p=malloc(sizeof(struct list));
p->page=page;
p->frame=frame;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=malloc(sizeof(struct list));
q->next->page=page;
q->next->frame=frame;
q->next->next=NULL;
}
}
int search(struct list *q,int page)

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 61


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

{
while(q->page!=page)
{
q=q->next;
}
return q->frame;
}
main()
{
p=NULL;
int pagesize,logicaladdr;
int i,page,frame;
int quotient,offset,physicaladdr;
printf("\nIMPLEMENTATION OF PAGING TECHNIQUE OF MEMORY
MANAGEMENT");
printf("\nEnter page table as structure");
printf("\nEnter 100 as page number for termination");
printf("\nEnter page size:");
scanf("%d",&pagesize);
do
{
printf("Enter the page number:");
scanf("%d",&page);
if(page!=100)
{
printf("\nEnter the frame number:");
scanf("%d",&frame);
add(p,page,frame);
}
}while(page!=100);
printf("\nEnter logical address:");
scanf("%d",&logicaladdr);
quotient=logicaladdr/pagesize;
offset=logicaladdr%pagesize;
physicaladdr=(search(p,quotient)*pagesize)+offset;
printf("\nCORRESPONDING PHYSICAL ADDRESS IS %d\n",physicaladdr);
getch();
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 62


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

IMPLEMENTATION OF PAGING TECHNIQUE OF MEMORY MANAGEMENT


Enter page table as structure
Enter 100 as page number for termination
Enter page size : 8
Enter the page number : 1

Enter the frame number : 5


Enter the page number: 4

Enter the frame number : 4


Enter the page number: 3

Enter the frame number : 2


Enter the page number: 4

Enter the frame number : 3


Enter the page number: 100

Enter logical address: 12

CORRESSPONDING PHYSICAL ADDRESS IS 44

RESULT:
Thus C program to implement Paging Technique of Memory Management was
written, executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 63


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 13.1 IMPLEMENTATION OF FIFO PAGE REPLACEMENT


ALGORITHM
AIM:
To write a C program to implement FIFO Page Replacement Algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the page as string.
Step 3: Get the frame size.
Step 4: Replace the first come page with the new page till all pages are used.
Step 5: Display the chart along with Faults and Hits.
Step 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k=0,flg1,flg2,x=5,y,pfc=0;
clrscr();
printf("\n enter page reference string:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
fp[i]='x';
clrscr();
printf("\n page replacement technique :: FIFO algorithm:");
printf("\n .............................................");
printf("\n F-Page Fault \t H- Page Hit \n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
if(fp[j]==prs[i])
{
ps='H';
flg1=1;
break;
}
if(flg1==0)
{
flg2=0;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 64


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

for(j=0;j<fs;j++)
if(fp[j]=='x')
{
fp[j]=prs[i];
pfc++;
flg2=1;
break;
}
if(flg2==0)
{
pfc++;
fp[k]=prs[i];
k++;
if(k==fs)
k=0;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("--");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c\t",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n \n\n\n\n Total page Faults=%d",pfc);
getch();
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 65


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

Page replacement technique :: FIFO algorithm:


………………………………………………...
F-Page Fault H-Page Hit
2 3 2 1 5 2 4 5 3 2 5 2
__________________________
2 2 2 2 5 5 5 5 3 3 3 3
X 3 3 3 3 2 2 2 2 2 5 5
X X X 1 1 1 4 4 4 4 4 2

__________________________
F F H F F F F H F H F F

Total page Faults = 9

RESULT:
Thus C program to implement FIFO Page Replacement Algorithm was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 66


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 13.2 IMPLEMENTATION OF LRU PAGE REPLACEMENT


ALGORITHM
AIM:
To write a C program to implement LRU Page Replacement Algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the pages as string.
Step 3: Get the frame size.
Step 4: Replace the least recently used page with the new page till all the pages are
processed.
Step 5: Display the page faults and hits along with chart.
Step 6: Stop the program.

PROGRAM:

#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=5,y,pfc=0,ru[10],min;
clrscr();
printf("enter the page reference string:");
gets(prs);
printf("enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='x';
ru[i]=0;
}
clrscr();
printf("PAGE REPLACEMENT TECHNIQUE::LRU algorithm\n");
printf("-----------------------------------\n");
printf("F-Page fault\tH-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=2)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';
ru[j]=i;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 67


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

flg1=1;
break;
}
}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=i;
flg2=1;
break;
}
}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>ru[j])
min=j;
}
fp[min]=prs[i];
ru[min]=i;
}
}
y=5;
gotoxy(x,y);
printf("%c",prs[i]);
y++;
gotoxy(x,y);
printf("- -");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c",fp[j]);
}
y++;
gotoxy(x,y);
printf("--");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n\n total page faults=%d",pfc);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 68


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

getch();
}
OUTPUT:

PAGE REPLACEMENT TECHNIQUE :: LRU algorithm:


………………………………………………...
F-Page Fault H-Page Hit

2 3 2 1 5 2 4 5 3 2 5 2
__________________________
2 3 3 3 5 5 5 5 5 5 5 5
X X 2 2 2 2 2 2 3 3 3 3
X X X 1 1 1 4 4 4 4 4 2

__________________________
F F F F F H F H F F H H

Total page Faults = 8

RESULT:
Thus C program to implement LRU Page Replacement Algorithm was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 69


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 13.3 IMPLEMENTATION OF LFU PAGE REPLACEMENT


ALGORITHM
AIM:
To write a C program to implement LFU Page Repalcement Algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the pages as string.
Step 3: Get the frame size.
Step 4: Replace the pages that are least frequently used with the new page that has
arrived and continue this till all the pages are processed.
Step 5: Display the chart along with page faults and page hits.
Step 6: Stop the program.

PROGRAM:

#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char prs[40],fp[10],ps;
int fs,i,j,k,flg1,flg2,x=10,y,pfc=0,ru[10],min;
clrscr();
printf("\n ENTER THE PAGE REFERENCE STRING:");
gets(prs);
printf("\n enter the frame size:");
scanf("%d",&fs);
for(i=0;i<fs;i++)
{
fp[i]='X';
ru[i]=0;
}
clrscr();
printf("\n PAGE REPLACEMENT TECHNIQUE ::LFU ALGORITHM \n");
printf("\n .......................................... \n");
printf("F-Page Fault \t H-Page Hit\n");
for(i=0;i<strlen(prs);i++,x+=5)
{
flg1=0;
ps='F';
for(j=0;j<fs;j++)
{
if(fp[j]==prs[i])
{
ps='H';

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 70


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

(ru[j])++;
flg1=1;
break;
}
}
if(flg1==0)
{
pfc++;
flg2=0;
for(j=0;j<fs;j++)
{
if(fp[j]=='X')
{
fp[j]=prs[i];
ru[j]=1;
flg2=1;
break;
}
}
if(flg2==0)
{
min=0;
for(j=1;j<fs;j++)
{
if(ru[min]>=ru[j])
{
if(ru[min]>ru[j])
min=j;
else
{
for(k=0;k<i;k++)
{
if(prs[k]==fp[min])
break;
if(prs[k]==fp[j])
{
min=j;
break;
}
}
}
}
}
fp[min]=prs[i];
ru[min]=1;
}
}
y=10;
gotoxy(x,y);
printf("%c",prs[i]);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 71


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

y++;
gotoxy(x,y);
printf("-----");
y++;
for(j=0;j<fs;y++,j++)
{
gotoxy(x,y);
printf("%c(%d)\t",fp[j],ru[j]);
}
y++;
gotoxy(x,y);
printf("-----");
y++;
gotoxy(x,y);
printf("%c",ps);
}
printf("\n\n\n\n\n TOTAL PAGE FAULTS =%d",pfc);
getch();
}

OUTPUT:

PAGE REPLACEMENT TECHNIQUE :: LFU ALGORITHM:


………………………………………………...
F-Page Fault H-Page Hit

2 3 2 1 5 2 4 5 3 2 5 2
_______________________________________
2(1) 2(1) 2(2) 2(2) 2(2) 2(3) 2(3) 2(3) 2(3) 2(4) 2(4) 2(5)
X(0) 3(1) 3(1) 3(1) 5(1) 5(1) 5(1) 5(2) 5(2) 5(2) 5(3) 5(3)
X(0) X(0) X(0) 1(1) 1(1) 1(1) 4(1) 4(1) 3(1) 3(1) 3(1) 3(1)

_______________________________________
F F H F F H F H F H H H

Total page Faults = 6

RESULT:
Thus C program to implement LFU Page Replacement Algorithm was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 72


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 14.1 IMPLEMENTATION OF SINGLE-LEVEL FILE


ORGANIZATION TECHNIQUE
AIM:
To write a C program to implement Single-Level File Organization Technique.

ALGORITHM:

Step 1: Start the program.


Step 2: Using graphics program initialize the graphics window and get the number of
files.
Step 3: Get the file names.
Step 4: Display the file structure for the file name given.
Step 5: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i=0,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
cleardevice();
setbkcolor(BLUE);
printf("enter number of files:");
scanf("%d",&count);
if(i<count)
{
cleardevice();
setbkcolor(6);
printf("enter %d file name:",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"root directory");
setcolor(10);
i++;
for(j=0;j<i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 73


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

outtextxy(cir_x,250,fname[j]);
}
}
getch();
closegraph();
}

OUTPUT ;

RESULT:
Thus C program to implement Single-Level File Organization Technique was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 74


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 14.2 IMPLEMENTATION OF TWO-LEVEL FILE


ORGANIZATION TECHNIQUE
AIM:
To write a C program to implement Two-Level file Organization Technique.

ALGORITHM:

Step 1: Start the program.


Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a 2 level file orgranization and
display it.
Step 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev ,char *dname,int lx,int rx,int x)
{
int i, gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("\nEnter the name of the file name %s:",dname);
fflush(stdin);
gets((*root)->name);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 75


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

if(lev==0 || lev==1)
(*root)-> ftype=1;
else
(*root)->ftype=2;
(*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)
{
if(lev==0 || lev==1)
{
if((*root)->level==0)
printf("\nHow many users:");
else
printf("\nHow many files:");
printf("(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
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)

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 76


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

bar3d(root->x-20, root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:

RESULT:
Thus C program to implement Two-Level File Organization Technique was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 77


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 14.3 IMPLEMENTATION OF HIERARCHIAL FILE


ORGANIZATION TECHNIQUE
AIM:
To write a C program to implement Hierarchial File Organization Technique.

ALGORITHM:

Step 1: Start the program.


Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a hierarchial file orgranization and
display it.
Step 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("\nEnter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 78


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

printf("\nEnter 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("\nNo 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]);
}
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 79


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

OUTPUT:

RESULT:
Thus C program to implement Hierarchial File Organization Technique was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 80


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No:14.4 IMPLEMENTATION OF DAG FILE ORGANIZATION


TECHNIQUE
AIM:
To write a C program to implement DAG File Organization Technique.

ALGORITHM:

Step 1: Start the program.


Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a DAG file orgranization.
Step 6: Get the links from the user and display the structure and the link.
Step 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10];
int nofl;
node *root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 81


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("\nEnter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\nEnter 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("\nNo 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;
}
}
read_links()
{
int i;
printf("\nHow many Links:");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("\nFile/Dir:");
fflush(stdin);
gets(L[i].from);
printf("\nUser Name:");
fflush(stdin);
gets(L[i].to);
}
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 82


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
}

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

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 83


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:

RESULT:
Thus C program to implement DAG File Organization Technique was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 84


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No:15.1 IMPLEMENTATION OF SEQUENTIAL FILE ALLOCATION


STRATEGY

AIM:
To write a C program to implement Sequential File Allocation Strategy.

ALGORITHM:

Step 1: Start the program.


Step 2: Allocate memory space and initialize free space to 500.
Step 3: Display the free space.
Step 4: Get the choice from user.
Step 5: If choice is 1 allocate a memory space for the file given as input.
Step 6: Display the remaining space.
Step 7: If choice is 2 deallocate the memory space.
Step 8: If choice is 3 compact the memory spaces and then proceed for further
process.
Step 9: Stop the program.

PROGRAM:

#include<stdio.h>
void create(int,int);
void del(int);
void compaction();
void display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
int main()
{
int name,size,ch,i;
int *ptr;
ptr=(int *)malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
printf(" Free start address Free Size \n\n");

for(i=0;i<=m;i++)
printf(" %d%d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
{
printf("1.Create.\n");
printf("2.Delete.\n");
printf("3.Compaction.\n");
printf("4.Exit.\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 85


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

case 1:
printf("\nEnter the name of file: ");
scanf("%d",&name);
printf("\nEnter the size of the file: ");
scanf("%d",&size);
create(name,size);
break;
case 2:
printf("\nEnter the file name which u want to delete: ");
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
printf("\nAfter compaction the tables will be:\n");
display();
break;
case 4:
exit(1);
default:
printf("\nYou have entered a wrong choice.\n");
}
}
}
void create(int name,int size)
{
int i,flag=1,j,a;
for(i=0;i<=m;i++)
if( freesize[i] >= size)
a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]=freest[a]+size;
freesize[a]=freesize[a]-size;
printf("\n The memory map will now be: \n\n");
display();
}
else
{
printf("\nNo enough space is available.System compaction......");
flag=1;
compaction();
display();
for(i=0;i<=m;i++)
if( freesize[i] >= size)

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 86


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]+=size;
freesize[a]-=size;
printf("\n The memory map will now be: \n\n");
display();
}
else
printf("\nNo enough space.\n");
}
}
void del(int name)
{
int i,j,k,flag=1;
for(i=0;i<n;i++)
if(fname[i]==name)
break;
if(i==n)
{
flag=0;
printf("\nNo such process exists......\n");
}
else
{
m++;
freest[m]=fstart[i];
freesize[m]=fsize[i];
for(k=i;k<n;k++)
{
fname[k]=fname[k+1];
fsize[k]=fsize[k+1];
fstart[k]=fstart[k+1];
}
n--;
}
if(flag)
{
printf("\n\n After deletion of this process the memory map will be : \n\n");
display();
}
}
void compaction()
{
int i,j,size1=0,f_size=0;

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 87


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

if(fstart[0]!=start)
{
fstart[0]=start;
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
else
{
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
}
void display()
{
int i;
printf("\n *** MEMORY MAP TABLE *** \n");
printf("\n\nNAME SIZE STARTING ADDRESS \n\n");
for(i=0;i<n;i++)
printf(" %d%10d%10d\n",fname[i],fsize[i],fstart[i]);
printf("\n\n");
printf("\n\n*** FREE SPACE TABLE ***\n\n");
printf("FREE START ADDRESS FREE SIZE \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
}

OUTPUT:

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 88


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

RESULT:
Thus C program to implement Sequential File Allocation Strategy was written,
executed and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 89


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 15.2 IMPLEMENTATION OF LINKED FILE ALLOCATION


STRATEGY
AIM:
To write a C program to implement Liked File Allocation Strategy.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the no of programs, memory block and required programs.
Step 3: Based on the requested program display the linked memory addresses.
Step 4: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int bno,flag,next;
}block;
block b[200],b1;
void main()
{
int rnum();
int i,n,s,s1,p[30],r,k[20];
printf("\nEnter no of Programs:");
scanf("%d",&n);
printf("\nEnter the memory block request:");
for(i=1;i<=n;i++)
{
printf("\nEnter Program Requirement:");
scanf("%d",& p[i]);
}
for(i=1;i<=n;i++)
{
s=rnum();
b[s].bno=0;
b[s].flag=1;
k[i]=0;
r=p[i]-1;
while(r!=0)
{
s1=rnum();
b[s].next=s1;
b[s1].flag=1;
b[s1].bno=0;
s=s1;
r=r-1;
}

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 90


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

b[s1].next=NULL;
}
printf("\nStarting Block for Program:");
for(i=1;i<=n;i++)
printf("\n%5d%5d",i,k[i]);
printf("\nAllocated Blocks:");
for(i=1;i<=200;i++)
{
if(b[i].flag==1)
printf("\n%5d%5d",b[i].bno,b[i].next);
}
}
int rnum()
{
int k,i;
for(i=1;i<=200;i++)
{
k=rand()%200;
if(b[i].flag!=1)
break;
}
return k;
}

OUTPUT:

RESULT:
Thus C program to implement Linked File Allocation Strtegy was written, executed
and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 91


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

Ex.No: 15.3 IMPLEMENTATION OF INDEXED FILE ALLOCATION


STRATEGY
AIM:
To write a C program to implement Indexed File Allocation Strategy.

ALGORITHM:

Step 1: Start theprogram.


Step 2: Get the no of files for allocation to be done.
Step 3: Get the file name, size and block size.
Step 4: For each file get the blocks.
Step 5: Display the allocated files.
Step 6: Stop the program.

PROGRAM:

#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n\nEnter blocks for file%d",i+1);
for(j=0;j<b1[i];j++)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&blocks[i][j]);
}
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",&S);
for(i=0;i<n;i++)
{

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 92


Ex.No: Date: Dr.N.G.P.Institute of Technology Reg.No:
: : : No:
No:

if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n");
printf("\n---------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}
}
printf("\n---------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");
scanf("%d",&ch);
}while(ch!=0);
}

OUTPUT:

Enter no.of Files :: 2


Enter file in 1 name :: x.c
Enter file1 size(in kb) :: 1
Enter blocksize of File1 (in bytes) :: 512
Enter file 2 name :: y.c
Enter file2 size (in kb) :: 1
Enter blocksize of File2 (in bytes) :: 512
Enter blocks for file1
Enter the 1block :: 1000
Enter the 2block :: 1001
Enter blocks for file2
Enter the 1block :: 2000
Enter the 2block :: 2001
Enter the Filename :: x.c
Frame Fsize Bsize Nblocks Blocks
_______________________________________
x.c 1 512 2 1000 ->1001->
_______________________________________
Do u want to continue :: (Y:N)1
Enter the Filename :: y.c
Frame Fsize Bsize Nblocks Blocks
_______________________________________
y.c 1 512 2 2000 ->2001->
_______________________________________
Do u want to continue :: (Y:N)1

RESULT:
Thus C program to implement Indexed File Allocation Strategy was written, executed
and output is verified successfully.

Dr.N.G.P.IT/CSE/Lab Manual/IV SEM/OS (R2017) 93

You might also like