Professional Documents
Culture Documents
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 ………….
……………………………… …………...…………………………
Date :
…………………………. ………………………….
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:
BASIC COMMANDS:
1. File Manipulation Commands
2. General Purpose Commands
3. Command Grouping & Filter Commands
4. Directory Commands And Process Management 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
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.
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
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
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
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
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
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.
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.
wel
come
NGP
it
[student@localhost student]$ wc -c b3
34 3
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
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
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
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>
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.
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
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]
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 $$
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.
AIM:
To develop C programs using
a. fork, getpid, execlp, exit & wait system calls
b. stat, opendir, readdir & closedir system calls
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
Algorithm:
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);
}
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.
ALGORITHM:
PROGRAM:
#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);
}
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.
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:
#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);
}
OUTPUT:
RESULT:
Thus the program to simulate the UNIX commands was written and successfully
executed.
SHELL PROGRAMMING
Ex.No:4.1 TO FIND GREATEST AMONG THREE NUMBERS
AIM:
To find the greatest of three numbers using shell programming.
ALGORITHM:
PROGRAM:
OUTPUT :
RESULT:
Thus the program to find greatest of three numbers using shell programming was
executed and verified
ALGORITHM:
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:
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.
ALGORITHM:
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.
ALGORITHM:
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);
}
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;
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.
ALGORITHM:
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++)
{
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;
}
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.
ALGORITHM:
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");
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];
}
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.
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])
{
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;
}
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
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;
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);
}
}
void *Consumer(void *arg)
{
int consumed;
int data;
printf("Consumer created\n");
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.
ALGORITHM:
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;
}
OUTPUT:
RESULT:
The C program to develop an application using Inter process Communication (IPC)
using pipes is implemented.
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;
}
OUTPUT:
RESULT:
The C program to develop an application using Inter process Communication (IPC)
using shared memory is implemented
ALGORITHM:
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…”);
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++)
{
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...
RESULT:
Thus the program to implement the bankers Algorithm for Dead Lock avoidance has
been executed successfully.
AIM:
To write a C program to implement the bankers Algorithm for Dead Lock Detection
ALGORITHM:
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);
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)
{
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.
ALGORITHM:
PROGRAM:
#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
{
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:
RESULT:
Thus the program has been executed and verified successfully.
ALGORITHM:
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;
}
}
}
}
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])
{
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::");
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");
OUTPUT:
ENTER NO OF PROCESSES::5
**MAIN MENU**
MEMORY MANAGEMENT
1. FIRST FIT
2. BEST FIT
3. WORST FIT
4. QUIT
FIRST FIT ::
PROCESSES::
10 20 15 30 45
MEMORY HOLES::
5 15 10 35 25 20 25
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.
ALGORITHM:
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)
{
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();
}
OUTPUT:
RESULT:
Thus C program to implement Paging Technique of Memory Management was
written, executed and output is verified successfully.
ALGORITHM:
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;
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();
}
OUTPUT:
__________________________
F F H F F F F H F H F F
RESULT:
Thus C program to implement FIFO Page Replacement Algorithm was written,
executed and output is verified successfully.
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;
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);
getch();
}
OUTPUT:
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
RESULT:
Thus C program to implement LRU Page Replacement Algorithm was written,
executed and output is verified successfully.
ALGORITHM:
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';
(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]);
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:
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
RESULT:
Thus C program to implement LFU Page Replacement Algorithm was written,
executed and output is verified successfully.
ALGORITHM:
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);
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.
ALGORITHM:
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);
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)
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.
ALGORITHM:
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);
OUTPUT:
RESULT:
Thus C program to implement Hierarchial File Organization Technique was written,
executed and output is verified successfully.
ALGORITHM:
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();
}
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);
}
}
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);
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.
AIM:
To write a C program to implement Sequential File Allocation Strategy.
ALGORITHM:
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)
{
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)
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;
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:
RESULT:
Thus C program to implement Sequential File Allocation Strategy was written,
executed and output is verified successfully.
ALGORITHM:
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;
}
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.
ALGORITHM:
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++)
{
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:
RESULT:
Thus C program to implement Indexed File Allocation Strategy was written, executed
and output is verified successfully.