0% found this document useful (0 votes)
4 views

OS CSE TECH

The document is a record notebook for the Operating Systems Lab at Thangavelu Engineering College, detailing various UNIX commands and their uses. It includes commands for date, calendar, echo, mathematical calculations, user details, directory management, file operations, and more. The document serves as a practical guide for students to execute and understand basic UNIX commands during their lab sessions.

Uploaded by

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

OS CSE TECH

The document is a record notebook for the Operating Systems Lab at Thangavelu Engineering College, detailing various UNIX commands and their uses. It includes commands for date, calendar, echo, mathematical calculations, user details, directory management, file operations, and more. The document serves as a practical guide for students to execute and understand basic UNIX commands during their lab sessions.

Uploaded by

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

THANGAVELU ENGINEERING COLLEGE

(APPROVED BY AICTE, NEW DELHI AND AFFILIATED TO ANNA


UNIVERSITY, CHENNAI)
RAJIV GANDHI SALAI, OMR, KARAPAKKAM, CHENNAI-97

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

RECORD NOTEBOOK
CS3461- OPERATING SYSTEMS LAB

FOURTH SEMESTER

STUDENT NAME :

REGISTER NUMBER :
THANGAVELU ENGINEERING COLLEGE
IT Highway, Rajiv Gandhi Salai- 600 097
BONAFIDE CERTIFICATE

REGISTER No.:
Certified that this is the Bonafide Record of Work done by

Mr./Ms.

in the Degree course in the

Laboratory during the academic year

Place : Chennai - 600 097

Date

STAFF-IN-CHARGE HEAD OF THE DEPARTMENT

Submitted for the University practical Examination held on……………………………………………. at


Thangavelu Engineering College Chennai -600 097

INTERAL EXAMINER EXTERNAL EXAMINER


LIST OF EXPERIMENTS:

EX.NO DATE NAME OF THE EXPERIMENT PAGE NO SIGNATURE

3
EX.NO DATE NAME OF THE EXPERIMENT PAGE NO SIGNATURE

4
EX.NO DATE NAME OF THE EXPERIMENT PAGE NO SIGNATURE

5
DATE :
EXPT.NO.1

BASICS OF UNIX COMMANDS


AIM:

To execute the following UNIX commands.

1. DATE COMMAND:

SYNTAX : date
USES : Used to display system date and

time. OUTPUT : Thu May 4 15:52:02 IST 2006

OPTIONS :

SYNTAX : date +%m


USES : Used to print the month in number.

OUTPUT : 05

SYNTAX : date +%h


USES : Used to print the month in name.

OUTPUT : May

SYNTAX : date +%y


USES : Used to print the last 2 digits of year.

OUTPUT : 06

SYNTAX : date +%M


USES : Used to displays the time in minute.

OUTPUT : 53

SYNTAX : date +%H


USES : Used to displays the time in hour.

OUTPUT : 15

6
SYNTAX : date +%a
USES : Used to displays the aggregated form of the day.

OUTPUT : Thu

SYNTAX : date +%r


USES : Used to displays the time in AM or PM.

OUTPUT : 03:54:43 PM

SYNTAX : date +%T


USES : Used to displays the full time in the format of HH:MM:SS.

OUTPUT : 15:55:58

2. CALENDER COMMAND:

SYNTAX : cal
USES : It displays the current month calendar.

OUTPUT :

May 2006
Su Mo Tu We Th S
Fr a
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 31

SYNTAX : cal 1998


USES : It displays the mentioned year calendar.

OUTPUT : It will displays the given year calendar.

3. ECHO COMMAND:

SYNTAX : echo “ UNIX ”


USES : Used to displaying the given text.

OUTPUT : UNIX
7
TYPING A CONTENTS MORE THAN ONE LINE

SYNTAX : echo “ This command


> line exceeds
> a single line"

OUTPUT : This
command line exceeds
a single line

USES : Used to display the multiple line as we entered while


executing the command.

SYNTAX : echo This command \


> line exceeds \
> a single line.

OUTPUT : This command line exceeds a single line.

USES : The Backslash character at the end of each line (followed by


pressing the ENTER key) informs the shell that the user wants to continue the command
on the next line. It is used to display the multiple lines into single line.

4. BC COMMAND:

SYNTAX : bc
USES : Used to perform simple mathematical calculations. By default this
command accepts decimal numbers and also perform mathematical calculations on
octal and hexadecimal numbers.

OUTPUT :

1) 5+3
8
2) 1.1+2.2
3.3
3) 3.3-1.2
2.1
4) 5.6*4.1
22.9
5) 8.4/3.2
2

8
6) 3-1
2
7) 4*5
20

5. WHO COMMAND:

SYNTAX : who
USES : Used to display the login details for all users using the UNIX systems.

OUTPUT :

student pts/1 May 4 15:50 (192.168.2.3)


student pts/11 May 4 10:47 (192.168.2.19)
root :0 May 4 14:52 (console)

WHO AM I

SYNTAX : who am i
USES : Used to displays the login details of the current users of the system
, who invokes the command.

OUTPUT : student pts/1 May 4 15:50 (192.168.2.3)

6. TTY COMMAND:

SYNTAX : tty(tele type)


USES : It gives the file name of the terminal that you are using , tell
you the device name and the name of the terminal that you currently working in.

OUTPUT : /dev/pts/1

7. MAN COMMAND:

SYNTAX : man any commmand


USES : It offers the online help facilities and it gives all details for the
particular commands that which user can type file and the directory commands.

9
DIRECTORY
COMMANDS
1. MAKE DIRECTORY

SYNTAX : mkdir directoryname


USES : Used to create a new directory.

OUTPUT : mkdir jeeva

2. CHANGE DIRECTORY

SYNTAX : cd directory name


USES : Used to change from one working directory to another directory
specified by its argument

OUTPUT : cd jeeva
3. REMOVE DIRECTORY

SYNTAX : rmdir
USES : Used to remove the directory.

OUTPUT : rmdir jeeva

4. PWD

SYNTAX : pwd
USES : Used to display the path that we are working.

OUTPUT : /home/student/vs

FILE & REDIRECTION COMMANDS

5. CAT

SYNTAX : cat > filename


USES : Used to create a new file & insert a content into that file.Press
CTRL + D to exit from that file.

OUTPUT :

cat
>sample
flower
jasmine
rose
Press ctrl + d

10
SYNTAX : cat filename
USES : Used to display the content of that file.

OUTPUT :
cat
sample
flower
jasmine
rose

SYNTAX : cat < filename1 > filename2


USES : It will copy the content of filename1 to filename2 .the
content of filename to is erased.

OUTPUT :
cat sample1
a

c
d
cat <sample >sample1
flower
jasmin
e rose

cat
sample1
flower
jasmine
rose

SYNTAX : cat filename1 filename2 > filename3


USES : The output of filename1 and filename2 are concatenated and stored
in the filename3 , and it is not displayed in the terminal. Here filename3 is already
exits.To view the contents of the file use cat command.

OUTPUT :

cat sample1 cat sample2


flower ab
jasmine bc
rose d
ef
11
cat
sample3
flower
jasmine
rose

a
b
bc
d
ef

SYNTAX : any command > filename


USES : The output of any command is stored in the file(filename) and it
is not displayed in the command prompt. To view the output use cat command.

SYNTAX : who > sample


USES : The result of who am i is stored in sample. to view the
contents use cat sample.

OUTPUT :

who >

sample4 cat

sample4
student pts/1 May 4 15:50 (192.168.2.3)
student pts/11 May 4 10:47 (192.168.2.19)
root :0 May 4 14:52 (console)

APPEND THE FILE CONTENTS

SYNTAX : cat filename1 >> filename2


USES : Used to append the contents of filename1 to filename2.

OUTPUT :

cat sample1 cat sample2


flower ab
jasmine bc-
rose d
ef

cat sample1 >>

sample2 cat sample3


12
ab

13
b
c
d
ef

flower
jasmin
e rose

SYNTAX : cat filename1 filename2 >> filename3


USES : The output of filename1 and filename2 are appended in the
filename3, and it is not displayed in the terminal. Here filename3 is not already exits.
It is a new file .so contents are not overwritten. To view the contents of the file use cat
command.

RENAMING THE FILE

SYNTAX : mv source filename destination filename


USES : Used to rename the file.

LIST OUT COMMANDS

SYNTAX : ls
USES : Used to list out all files in a directory.

SYNTAX : ls -a
USES : Used to list out all files including hidden files ( Files that begin with ( . , .. )

SYNTAX : ls -i
USES : Used to list out all files with its i-node number in the first column.

SYNTAX : ls -r
USES : Used to list out all files in reverse alphabetical order.

SYNTAX : ls -t
USES : Used to list out all files in the order of their last modification time.

SYNTAX : ls -u
USES : Used to list out all files in the order of their last access time.

SYNTAX : ls -l
USES : Used to list out all files in long format ( one entry per line) ,giving
its mode, number of links, owner, group , size in bytes, the time that each file was last
modified.

14
SYNTAX : ls -lt
USES : Used to list out all files in long format with their last modification time.

FILTER COMMANDS

SYNTAX : head -n filename


USES : Used to display the Top ‘n’ lines of the file.

SYNTAX : tail -n filename


USES : Used to display the Bottom ‘n’ lines of the file.

SYNTAX : more filename


USES : To see the content of the filename on the screen one page at a time.

WORD COUNT COMMAND

SYNTAX : wc filename
USES : It will display the no of lines , no of words and used to
display no of characters.

SYNTAX : wc -l filename
USES : Used to display no of lines in a filename.

SYNTAX : wc -w filename
USES : Used to display no of words in a filename.

SYNTAX : wc -c filename
USES : Used to display no of Characters in a filename.

COMMON COMMAND

SYNTAX : comm. filename1 filenmae2


USES : This command each line of first file with its corresponding line
in the second file. Output contains 3 columns. First column contains lines unique to
filename1.Second column contains lines unique to filename2.Third column
contains lines common to both.

EX:
$cat n1 $cat nithi
sridhar sridhar
ramamoorthy vikky
mahalashmi vimal
Jegan prabu

15
OUTPUT :
$comm
n1 nithi
sridhar
ramamoorth
y
mahalashmi
jegan
prhb
u
prhb
u
vikky
vimal

OPTIONS

SYNTAX : comm -1 filename


USES : It doesn’t include the first column in output.

SYNTAX : comm -2 filename


USES : It doesn’t include the second column in output.

SYNTAX : comm -3 filename


USES : It doesn’t include the third column in output.

COMPARE COMMAND

SYNTAX : cmp filename1 filename2


USES : Two files are compared byte by byte and the location of
the first mismatch is echoed in the screen.

OUTPUT :
cmp sample1 sample5
sample1 sample5 differ: byte 7, line 1

SYNTAX : cmp –l file1 file2


USES : This option gives the detailed list of the byte number and the
differing bytes in octal for each character that differ in both files.

OUTPUT :
cat sample1 cat sample2
flower flower
jasmine carrot
rose papaya
banana

cmp -l sample1 sample5


16
7 12 40
8 152 12
9 141 143
10 163 141
11 155 162
12 151 162
13 156 157
14 145 164
16 162 160
17 157 141
18 163 160
19 145 141
20 12 171
cmp: EOF on sample1

CUT COMMAND

SYNTAX : cut -d “any character “ -f2 filename.


USES : Used to cut the character from first to the specified character from
the file.

OUTPUT :

cat
sample1
flower
jasmine
rose

cut -d "w" -f2 sample1


er
jasmin
e rose

SYNTAX : cut –c 1-3 filename


USES : It will command the character which are defined in numbers and
it will displays the character upto that specified number.

OUTPUT :

cat
sample5
flower
carrot
papaya
banana

17
cut -c 4 sample5
w
r
a
a

COPY COMMAND

SYNTAX : cp source filename destination filename


USES : Used to copy the source file contents to destination file contents.

PASTE COMMAND

SYNTAX : paste file1 file2


USES : This command merges the contents of 2 files in into a single file. It
reads a line from file in the file list specified and combines them into a single file.
OUTPUT :

paste sample1 sample2

flower ab
jasmine bc
rose d
ef
flower
jasmine
rose

UNIQUE COMMAND

SYNTAX : uniq filename


USES : Used to display the unique lines present in the file.

EX :
OUTPU
T:

$ cat n1
sridhar
ramamoorth
y

18
mahalash
mi jegan
prhb
u
prhb
u

$ uniq n1
sridhar
ramamoorth
y
mahalashmi
jegan
prhbu

OPTIONS

SYNTAX : uniq - u filename


USES : Used to display only unique lines present in the file.

OUTPUT :

$ uniq –u
n1 sridhar
ramamoorth
y
mahalashmi
jegan

SYNTAX : uniq - d filename


USES : Used to display the duplicate lines in the file.

OUTPUT :

$ uniq –d n1
prhbu

SYNTAX : uniq - c filename


USES : Used to count the duplicate lines in the file.

OUTPUT :

$ uniq –c
n1 sridhar
ramamoorth
y
mahalashmi
19
GREP COMMAND:

SYNTAX : $grep ` ` filename


USES : This command is used to search for a particular pattern from a file or
from the standard input and display those lines on the standard output.

OPTIONS:

a) SYNTAX : $grep –v ` ` filename


USES : Displays only those lines that do not match the pattern specified.

OUTPUT:
name: Bahirathi

b) SYNTAX : $grep –c ` ` filename


USES : Displays only the count of those lines, which match the pattern, specified.

OUTPUT:
$grep –c `
`alamu 3

c) SYNTAX : $grep –n ` ` filename


USES : Displays those lines, which match the pattern, specified along
with line number at the beginning of the line

OUTPUT:
$grep –n ` `
alamu 1: rathi
3:rgi
5:vrs

d) SYNTAX : $grep i ` ` filename


USES : displays those lines, which match the pattern specified ignoring
the case distinction.

OUTPUT:
$grep –i ` ` alamu
Rathi
Raj
i
vrs

PIPE COMMANDS

PIPE SYMBOL :

20
A pipe is a mechanism, which takes the output of the command as its input for the next
command .

SYNTAX : Command | command

OUTPUT :
$ who |wc -l

TEE COMMAND:

SYNTAX : command |tee


USES : used to join pipes and make copies of input .

OUTPUT : $who |tee rs | wc -l

Wild
Cards
1. *

SYNTAX : $ls *
USES : used in representing any number of characters when used in the
prefix or suffix.

2. ?

SYNTAX : $ls ?
USES : this character is use to represent one character wither in prefix
or in suffix of the filename .

SORTING COMMANDS :

Sort :

SYNTAX : $sort filename


USE : Sort filter arranger input from standard input in alphabetical order .

OUTPUT :
$Sort alamu

OPTIONS :

a) SYNTAX : $ sort –r filename


USES : sort command when used with this option will display input taken
from keyboard in reverse alphabetical order .

21
OUTPUT : sort –r alamu
343
56
34
12

b) SYNTAX : $ sort –n filename


USES : This option will arrange the input according to numerical and display it .

OUTPUT :
sort –n
alamu 12
24
56
343

c) SYNTAX: $ sort –f filename


USES : Digits alphabets and other characters taken as input are converted
to ASCII value . Sort arranges them according to their ASCII value .

OUTPUT :
sort –f alamu
*
#
1
2
24
56
343

d) SYNTAX : $ sort –u filename


USES : This option will remove duplicate lines from input and displays as output

OUTPUT :
sort –u
alamu
Malathi
Raji
Rath
i
Vassant
hi Vrs

CHMOD COMMAND

SYNTAX : $ chmod -w dir name

22
USES : This command is used to set the three permissions for all the three
categories of users of the file . Only the owner of the file can use it .

OUTPUT:
$ chmod –w cs 2 3
Ls –l cs 2 3
Total 8
Drwx rwx r-x 2 linux 4096 jun 24 14.46 cs 23

CP COMMAND

SYNTAX : $ cp –I filename1 filename 2


USES : This command helps us to create duplicate copies of ordinary file .

OPTIONS: $ cp –i filename1 filename2

OUTPUT:
$cp rathi :rs
Cat rs
Name :
bahirathi Roll
no: 01 cs 23
3.7.1982

OPTIONS:
SYNTAX : $cp –I filename1 filename2
USES : The –I ( interactive option , originally warns the users before
overwriting the destination file .

OUTPUT

$cp –I rs rathi
cp : overwrite ‘rathi’? n

SYNTAX : $cp –r filename1 filename2


USES : It is now possible to copy an entire directory structures with –r (
recursive ) option.

OUTPUT
$ cp –r rs rathi

RESULT :

Thus the above UNIX commands have been executed and the output is verified.

23
DATE:
EXPT. NO.: 2(a)

Program Using System Calls – fork()


AIM:

To write c program to implement the Process system calls.


ALGORITHM:

1. Start the program.


2. Declare the pid and get the pid by using the getpid() method.
3. Create a process by calling the fork() system call
4. Check if(pid==0) then print the process id
5. Stop the program

PROGRAM: (PROCESS SYSTEM CALLS)

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void main(int argc,char *arg[])


{
int pid;
pid=fork();
if(pid<0)
{
printf("fork failed");
exit(1);
}
else if(pid==0)
{
execlp("whoami","ls",NULL);
exit(0);
}
else
{
printf("\n Process id is %d\n",getpid());
wait(NULL);
exit(0);
}
} 18
OUTPUT

[roshon@localhost ~]$ cc prog4a.c


[roshon@localhost ~]$ ./a.out
roshon

Process id is 3394

Result:
The Program for Process System Call was implemented and the output was
verified.

19
DATE:
EXPT. NO.: 2(b)

Program Using System Calls – getpid() and getppid()


AIM:

To write a program for implementing program using the


System Calls getpid() and getppid()

ALGORITHM:

1. Start the program.


2. Read the input from the command line.
3. Use fork() system call to create process, getppid() system
call used to get the parent process ID and getpid() system
call used to get the current process ID
4. Stop the program.

PROGRAM CODING:

#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork();
if(pid== -
1)
{
perror(“Fork Failed”);
exit(0);
}
If(pid==0)
{
printf("\n Child Process under Execution……");
printf(“\n Process id of the Child Process is %d”,getpid());
printf(“\n Process id of the Parent Process is %d”, getppid());
}
20
else
{
printf("\n Parent Process under Execution……");
printf(“\n Process id of the Parent Process is %d”,getpid());
printf(“\n Process id of the Child Process in Parent is %d”, getppid());
printf(“\n Process id of the Parent of Parent is %d”, getppid());
}
return(0);
}

OUTPUT:

[roshon@localhost ~]$cc progb.c


[roshon@localhost ~]$ ./a.out

Child Process under Execution……


Process id of the Child Process is 3451
Process id of the Parent Process is 3450

Parent Process under Execution……


Process id of the Parent Process is 3450
Process id of the Child Process in Parent is 3450
Process id of the Parent of Parent is 3288

RESULT:

Thus the program for process management was implemented andthe


output was successfully verified.

21
DATE
EXPT.NO.:2(c)
Write a program using the system calls – execlp

AIM:
To write a program using the system calls of UNIX operating
system (execlp)

ALGORITHM:

1. Start the program.


2. Use the execlp() function to display the date
3. Print the time and date
4. Stop the program.
PROGRAM:

#include<stdio.h>
#include<unistd.h>
Main()
{
printf(“\n Execlp System Calls”);
printf(“Displaying the Date:”);
execlp(“/bin/date”,”date”,0);
}

OUTPUT:

[roshon@localhost ~]$cc prog4d.c


[roshon@localhost ~]$ ./a.out
exec System Call

Thu Jan 1 07:20:09 IST 2004

RESULT:

Thus the program for the system call – execlp() was implemented and
the Output was successfully Verified.

2
DATE
EXPT.NO.:2(d)
Write a program using the system calls – Wait() and Exit()
AIM:
To write a program using the system calls of UNIX operating system
(wait() and exit())

ALGORITHM:

1. Start the program.


2. Read the input from the command line.
3. Use fork() system call to create process
4. If fork() ==0 child process executes else it goes to
waiting state and exit.
4. Stop the program.

PROGRAM:

#include<stdio.h>
#include<unistd.h>
main()
{
int i, pid;
pid=fork();
if(pid==-1)
{
perror(“Fork Failed”);
exit(0);
}
else if(pid ==0)
{
printf(“\n Child Process Starts…….”);
for (i=0;i<5;i++)
{
printf(“\n Child Process is called”,i);
}
printf(“\n Child Process ends…”);
}
else
{
wait(0);
printf(“\n Parent Process Ends…..”);
}exit(0);} 2
OUTPUT:

[roshon@localhost ~]$cc prog4e.c


[roshon@localhost ~]$ ./a.out

Child Process Starts……


Child Process 0 is called
Child Process 1 is called
Child Process 2 is called
Child Process 3 is called
Child Process 4 is called
Child Process Ends……
Parent Process
ends……

Result:
Thus the program for the system call – wait() and exit() was implemented and
the Output was successfully Verified

2
DATE
EXPT.NO.:2(e)
Write a program using the system calls –Open() read() and
write()

AIM:
To write the program to implement the system calls open( ),read( ) and
write( ).
ALGORITHM :

Step 1 : Declare the structure elements.


Step 2 : Create a temporary file named temp1.
Step 3 : Open the file named “test” in a write mode.
Step 4 : Enter the strings for the file.
Step 5 : Write those strings in the file named “test”.
Step 6 : Create a temporary file named temp2.
Step 7 : Open the file named “test” in a read mode.
Step 8 : Read those strings present in the file “test” and save it in temp2.
Step 9 : Print the strings which are read.

PROGRAM CODING:

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
{
int fd[2];
char buf1[25]= ”just a test\n”; char
buf2[50];
fd[0]=open(“file1”,
O_RDWR);
fd[1]=open(“file2”,
O_RDWR); write(fd[0],
buf1, strlen(buf1));
printf(“\n Enter the text
now….”);
scanf(“\n%s”,buf1);
printf(“Cat File1 is \n Hai”); 25
write(fd[0], buf1,
strlen(buf1)); lseek(fd[0],
SEEK_SET, 0); read(fd[0],
buf2, sizeof(buf1));
write(fd[1], buf2,
sizeof(buf2));
close(fd[0]);
close(fd[1]
);
printf(“\n”)
; return0;
}

OUTPUT:

Enter the text now….progress


Cat file1 Hai

Cat file2 Just a test progress

RESULT:
Thus the program for the system call –open(), read() and write() was
implemented and the Output was successfully Verified

26
DATE
EXPT.NO.:2(f)
Write a program using the system calls –opendir() readdir() and
closedir()

AIM :

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

ALGORITHM :

Step 1 : Start.
Step 2 : In the main function pass the arguments.
Step 3 : Create structure as stat buff and the variables as integer.
Step 4 : Using the for loop,initialization

PROGRAM CODING:

#include<stdio.h>
#include<sys/types.h>
#include<sys/dir.h>
void main(int age,char *argv[])
{
DIR *dir;
struct dirent *rddir;
printf("\n Listing the directory
content\n"); dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t\n",rddir->d_name);
}
closedir(dir);
}

OUTPUT
RP
roshi.c
first.c
pk6.c f2
abc FILE1

RESULT:
Thus the program for the system call –opendir(), readdir() and closedir()
was implemented and the Output wassuccessfully Verified

27
DATE:
EXPT.NO.:3(a)
Write a programs to simulate ls UNIX
commands
AIM:
To write a program to simulate UNIX commands like ls

ALGORITHM:
1. Start the program.
2. Read the input through command line.
3. Open the specified file.
4. Options (c & i) are performed.
5. Stop the program.

PROGRAM:
#include<stdio.h>
#include<dirent.h>
int main(int ag,char* arg[])
{
Struct dirent **namelist;
int n,i;
char
pathname[100];
n=scandir(“pathname,&namelist,0,alphasort);
if(n<0)
printf(“\n Error”);
else
for(i=0;i<n;i++)
printf(“%s\n”,namelist[i]->d_name);
}

28
OUTPUT:

[roshon@localhost ~]$cc ls1.c


[roshon@localhost ~]$ ./a.out

aaa
.
..
.bash_history
.bash_logout
.bash_profile
.bashre
emacs
lab1.c.swp
.mozilla
Progf.c.swo
Progf.c.swp
.viminfo
a.out
big.sh
big1.sh
even.sh
fact.sh
lab1.c
lab2.c
q1.c
q1.sh
q2.sh
q4.sh

RESULT:

Thus the program to simulate the UNIX commands like ls was


executed and the output was verified successfully.

29
DATE:
EXPT.NO.:3(b)
Write a programs to simulate grep UNIX
commands
AIM:
To write a program to simulate UNIX commands like grep

ALGORITHM:

1. Start the program.


2. Read the input through command line.
3. Open the specified text file.
4. Enter the string values.
5. Execute the c file
6. Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE *fp;
char c[100], pat[10];
int i,j=0,l,count=0,len,k,flag = 0;
printf(“\n Enter the Pattern :”);
scanf(“%s”,pat);
len=strlen(pat);
fp=fopen(“nn.txt”,”r”);
while(!feof(fp))
{
fscanf(fp,”%s”,c);
l=strlen©;
count++;
for(i=0;i<count;i++)
{
30
{
flag = 0;
for(k=1;k<i;k++)
{
if(c[i+k]!=pat[k])
flag=1;
}
if (flag==0)
printf(“\n The Pattern %s is present in the word %d”,pat, count);
} }
}
}
nn.text
Apple
Orange
Guava
Pomogranat
e
Banana
Mangoes

OUTPUT:

[roshon@localhost ~]$cc gre.c


[roshon@localhost ~]$ ./a.out

Enter the Pattern: Orange

The Pattern Orange is present in the word 2

RESULT:

Thus the program to simulate the UNIX commands like grep was
executed and the output was verified successfully.

31
DATE:
EXPT.NO.:4(a)

CALCULATE SIMPLE INTEREST


AIM
To write a shell program to calculate simple interest.

ALGORITHM:

STEP 1: Read the Three Numbers.


STEP 2: Compute the value using simple interest
formula. STEP 3: Print value of sim.
STEP 4: Stop The Program.

PROGRAM:

echo “Enter the Principle, Rate, Time


Period” read p n r
sim =$(expr $p \* $n \* $r / 100)
echo $sim

OUTPUT:

bash q1.sh

Enter the Principle Rate Time


Period 5000 5 2
500

RESULT:
Thus the Shell program for calculating the value from quadratic
equations was implemented and output verified successfully.

32
DATE:
EXPT.NO.:4(
b) SWAPPING OF TWO NUMBERS

AIM:
To write a shell program to swap the two numbers.

ALGORITHM:

STEP 1: Read the Numbers.


STEP 2: Print the values before
swapping. STEP 3: Assign a to t, b to
a, t to b.
STEP 4: Print the values after swapping.
STEP 5:Stop the program execution.

PROGRAM:

echo "enter first number"


read a
echo "enter second number"
read b
echo "Before swapping the value of a is $a and b is $b"
#swapping
a=$((a+b))
b=$((a - b))
a=$((a-b))
echo "After swapping the value of a is $a and b is $b"

OUTPUT:
bash q2.sh

enter the first number


45
enter the second number
32
Before swapping the value of a is 45 and b is 32
After swapping the value of a is 32 and b is 45
RESULT:

Thus the Shell program for swapping the two numbers was

33
implemented and output verified successfully.

34
DATE:
EXPT.NO.:4(
c) BIGGEST OF THREE NUMBERS

AIM:
To write a Shell Program to Find Biggest In Three Numbers.

ALGORITHM:

STEP 1: Read The Three Numbers.


STEP 2: If A Is Greater Than B And A Is Greater Than C Then
Print A Is Big. STEP 3: Else If B is greater Than C Then C Is Big.
STEP 4: Else Print C Is
Big. STEP 5: Stop The
Program.

PROGRAM:

echo “Enter the Three Numbers”


read a b c
if [ $a –gt $b –a $a –gt $c ]
then
echo “$a is greater”
elif [ $b –gt $c –a $b –gt
$a] then
echo “$b is greater”
else
echo “$c is
greater” fi
OUT PUT:

bash big1.sh

Enter the Three Numbers


92 55 37

92 is greater

RESUL
T:
Thus the Shell program for finding biggest of three numbers was
implemented and output verified using various samples.
35
DATE:
EXPT.NO.:4(
d) GIVEN NUMBER IS ODD OR EVEN

AIM:
To write a program to find whether a number is even or odd.

ALGORITHM:

STEP 1: Read the input number.


STEP 2: Perform modular division on input number by 2.
STEP 3: If remainder is 0 print the number is even
STEP 4: Else print number is odd.
STEP 5: Stop the program.

PROGRAM:

echo -n "Enter the Number : "


read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is Even Number"
else
echo "$n is Odd Number"
fi

OUTPUT:
bash even.sh

Enter the Number : 80

80 is Even Number

RESULT:

Thus the Shell program for finding the given number is ODD or EVEN
was implemented and output verified using various samples.
36
DATE:
EXPT.NO.:4(
e)
ARITHEMETIC OPERATIONS

AIM:

To write a program to find whether a number is even or odd.


ALGORITHM:

STEP 1: Read the input number.


STEP 2: Perform Addition, Subtraction, Multiplication, Division and Modulus
Operation. STEP 3: Print the result
STEP 4: Stop the program.
PROGRAM:

a=1
0
b=2
0
val=$((a + b))
echo "a + b :
$val" val=$((a -
b))
echo "a - b :
$val" val=$((a *
b)) echo "a * b :
$val" val=$((b %
a))
OUTPUT:
bash
q4.sh a +
b : 30 a -
b : -10
a * b : 200
b/a:2

RESULT:
Thus the Shell program for finding the Arithmetic Operations was
37
implemented and output verified using various samples.

38
DATE:
EXPT.NO.:4(
f)
FACTORIAL VALUE

AIM:

To write a program to find the factorial of a given number.


ALGORITHM:

STEP 1: Read the input number.


STEP 2: Perform the Factorial Value of the given
Number. STEP 3: Print the result
STEP 4: Stop the program.
PROGRAM:

echo "enter a number"


read num
fact=1
while [ $num -ge 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num – 1
done
echo $fact

OUTPUT:
bash fact.sh

Enter the Number :

5 Factorial of 5 is
120

RESULT:

Thus the Shell program for finding Factorial of a given Number was
39
implemented and output verified using various samples.

40
DATE:
EXPT.NO.:5(
a) FCFS SCHEDULING
AIM:
To write a C program to implement the array representation of the
CPU scheduling algorithm first come first serve using arrival time.
ALGORITHM:

Step 1: Create the number of process.


Step 2: Get the ID and Service time for each process.
Step 3: Initially, Waiting time of first process is zero and Total time for the
First process is the starting time of that process.
Step 4: Calculate the Total time and Processing time for the remaining
processes.
Step 5: Waiting time of one process is the Total time of the previous process.
Step 6: Total time of process is calculated by adding Waiting time and Service
time.
Step 7: Total waiting time is calculated by adding the waiting time for lack
process.
Step 8: Total turn around time is calculated by adding all total time of each
process.
Step 9: Calculate Average waiting time by dividing the total waiting time by
total number of process.
Step 10: Calculate Average turn around time by dividing the total time by the
number of process.
Step 11: Display the result.

41
PROGRAM CODING:
#include<stdio.h
>
#include<conio.
h> void main()
{
int n, b[10], w[10],i,j,h;
int tat[10];
float avg=0, avgtt=0;
clrscr();
printf("\n FCFS Job
Scheduling"); printf("\n Enter the
No. of Jobs:"); scanf("%d",&n);
printf("\n Enter the Burst Time:");
for(i=0;i<n;i++)
{
printf("\n Process %d:", i+1);
scanf("%d",&b[i]);
}
w[0] = 0; tat[0]=b[0];
printf("\n Process 1 waiting time is 0");
for(i=1;i<n;i++)
{
w[i]=b[i-1] + w[i-1];
printf("\n Process %d Waiting Time %d", i+1, w[i]);
avg=avg+w[i];
}
printf("\n Total Waiting Time : %f", avg);
printf("\n The avg. Waiting Time:
%f",avg/n); for(i=0;i<n;i++)
{
tat[i]=b[i] + w[i];
printf("\n Process %d Turnaround Time %d", i,
tat[i]); avgtt += tat[i];
}
printf("\n Total Turnaround Time : %f",avgtt);
printf("\n Avg. Turn around time %f",
avgtt/n); getch();
}

42
OUTPUT:
FCFS Job Scheduling
Enter the No. of Jobs :
3

Enter the Burst Time:


Process 1: 3

Process 2: 6

Process 3: 7

Process 1 Waiting time is 0


Process 2 Waiting time is 3
Process 3 Waiting time is 9
Total Waiting Time:
12.000000
The Avg. Waiting Time :
4.000000 Process 0
Turnaround Time 3
Process 1 Turnaround Time 9
Process 2 Turnaround Time 16
Total Turnaround Time :
28.000000 Avg. Turn Around
time 9.333333

RESULT:
Thus the program for generating CPU scheduling algorithm first come first
serve using arrival time was implemented and output verified using various
samples.

43
DATE
EXPT.NO.:5(
b) SJF SCHEDULING

AIM:
A program to simulate the SJF CPU scheduling algorithm .

ALGORITHM:
Step 1: Get the number of process.
Step 2: Get the id and service time for each process.
Step 3: Initially the waiting time of first short process as 0 and total time
of first short is process the service time of that process.
Step 4: Calculate the total time and waiting time of remaining
process.
Step 5: Waiting time of one process is the total time of the
previous process.
Step 6: Total time of process is calculated by adding the waiting
time and service time of each process.
Step 7: Total waiting time calculated by adding the waiting time of
each process.
Step 8: Total turnaround time calculated by adding all total time of
each process.
Step 9: Calculate average waiting time by dividing the total waiting time
by total number of process.
Step 10: Calculate average turnaround time by dividing the total waiting time
by total number of process.
Step11: Display the result.

PROGRAM CODING:

#include<stdio.h
>
#include<conio.
h> void main()
{
int n, b[10], w[10],i,j,h,t,tt,turn[10];
int a[10];
float avg=0,
avgtt=0; clrscr();
printf("\n Shortest Job Scheduling");
printf("\n Enter the No. of
44
Processes:"); scanf("%d",&n);

45
for(i=0;i<n;i++)
{
printf("\n Process %d:", i+1);
scanf("%d",&b[i]);
a[i]=i;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i]<b[j])
{
t = b[i];
tt = a[i];
b[i]=b[j];
a[i]=a[j];
b[j]=t;
a[j]=tt;
}
}
}
w[0] = 0; turn[0]=b[0];
for(i=1;i<n;i++)
{
w[i]=b[i-1] + w[i-1];
avg=avg+w[i];
}
for(i=0;i<n;i++)
{
turn[i]=b[i] + w[i];
avgtt += turn[i];
}
printf("\n Process Id\t Burst Time \t Waiting Time \t Turn Around Time
\n"); for(i=0;i<n;i++)
{
printf("%d\t\t %d\t\t %d\t\t %d\n", a[i],b[i], w[i],turn[i]);
}
printf("\n Total Waiting Time : %f", avg);
printf("\n Total Turn Around Time
:%f",avgtt); avg=avg/n;
printf("\n The avg. Waiting Time:
%f",avg); avgtt=avgtt/n;
printf("\n Avg. Turn around time %f",
avgtt); getch();
}

46
OUTPUT:
Shortest Job Scheduling
Enter the No. of
Processes:3

Enter the Burst Time:


Process 1 : 7

Process 2 : 4

Process 3 : 6

Process Id Burst Time Waiting Time Turnaround


Time
1 4 0 4
2 6 4 10
0 7 10 17

Total Waiting Time: 14.000000


Total Turnaround Time :
31.000000 Average Waiting
Time: 4.666667
Average Turn Around Time: 10.333333

RESULT:
Thus the program for generating CPU scheduling algorithm for SJF
using arrival time was implemented and output verified using various samples.

47
DATE:
EXPT.NO.:5(c)

PRIORITY SCHEDULING
AIM
:

A program to simulate the CPU scheduling algorithm for Priority.

ALGORITHM:
Step 1: Get the number of process
Step 2: Get the id and service time for each process.
Step 3: Initially the waiting time of first short process as 0 and total time
of first short is process the service time of that process.
Step 4: Calculate the total time and waiting time of remaining process.
Step 5: Waiting time of one process is the total time of the
previous process.
Step 6: Total time of process is calculated by adding the waiting
time and service time of each process.
Step 7: Total waiting time calculated by adding the waiting time of each
process.
Step 8:Total turnaround time calculated by adding all total time of
each process.
Step 9: Calculate average waiting time by dividing the total waiting time by
total number of process.
Step 10: Calculate average turnaround time by dividing the total
waiting time by total number of process.
Step 11: Display the result.

PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];

printf("Enter the number of process:");44


clrscr();

printf("Enter the number of process:");44


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i]; p[i]=p[j];
p[j]=temp; temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
45
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

OUTPUT:
Input:

Enter the number of processes: 3


Enter the Process Name, Arrival Time, execution time & priority:
1 2 3 1 Enter the Process Name, Arrival Time, execution time &
priority: 2 4 5 2 Enter the Process Name, Arrival Time, execution
time & priority: 3 5 6 3

Output:

Pname arrivaltime executiontime priority waitingtime tatime


1 2 3 1 0 3
2 4 5 2 1 6
3 5 6 3 5 11

Average Waiting Time: 2.0000


Average Turn Around Time:
6.6667

RESULT:
Thus the program for generating CPU scheduling algorithm for priority using
arrival time was implemented and output verified using various samples.

46
DATE
EXPT.NO.:5(d
)

ROUND ROBIN SCHEDULING

AIM:
A program to simulate the Round Robin CPU scheduling algorithm.

ALGORITHM:

Step 1: Initialize all the structure elements


Step 2: Receive inputs from the user to fill process id,burst time and
arrival time.
Step 3: Calculate the waiting time for all the process id.
i) The waiting time for first instance of a process is
calculated as:
a[i].waittime=count + a[i].arrivt
ii) The waiting time for the rest of the instances of the
process is calculated as:
a) If the time quantum is greater than the remaining
burst time then waiting time is calculated as:
a[i].waittime=count + tq
b) Else if the time quantum is greater than the
remaining burst time then waiting time is calculated as:
a[i].waittime=count - remaining burst time
Step 4: Calculate the average waiting time and average turnaround
time
Step 5: Print the results of the step 4.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0; char pn[10][10];
clrscr();
printf("Enter the no of processes:");
47
scanf("%d",&n);

48
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:");
for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]); et[i]=0;}
}
}
printf("\n Total Estimated Time:%d",x);
getch();
}

49
OUTPUT:

Input:

Enter the no of
processes: 2 Enter the
time quantum: 3

Enter the process name & estimated time:


p1 12 Enter the process name & estimated
time: p2 15

Output:
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3

Total Estimated Time: 27

RESULT:
Thus the program for generating CPU scheduling algorithm for round robin
using arrival time was implemented and output verified using various samples.

50
DATE:
EXPT.NO.:6( FILE ALLOCATION STRATERGIES
a) SEQUENTIAL ALLOCATION

AIM:

To write a C program to implement File Allocation concept using


the sequential allocation Technique.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order.
a). Randomly select a location from available location s1= random(100);
b). Check whether the required locations are free from the selected
location.
c). Allocate and set flag=1 to the allocated locations.
Step 5: Print the results fileno, length , Blocks allocated.
Step 6: Stop the program.

51
PROGRAM:

#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1) goto X;
else exit();
getch();
}

51
Output:

Enter the starting block & length of file 4 10

4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk
If you want to enter more files? (Y-1/N-0)

RESULT:

Thus the C program for File Allocation concept using the technique
sequential Allocation was implemented and output was verified using
various samples.

52
DATE: INDEXED ALLOCATION
EXPT.NO.:6(
b)

AIM:
To write a C program to implement File Allocation concept using the
indexed allocation Technique.

ALGORITHM:

Step 1: Start the Program


Step 2: Get the number of files.
Step 3: Allocate the required locations by selecting a location randomly
Step 4: Check whether the selected location is free.
Step 5: If the location is free allocate and set flag =1 to the
allocated locations. Step 6: Print the results file no, length,
blocks allocated.
Step 7: Stop the execution

PROGRAM:

#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{ scanf("%d",&a);
f[a]=1;
} X:
printf("Enter the starting index block & length");

for(j=st;j<(k+st);j++) 53
scanf("%d%d",&st,&len);

for(j=st;j<(k+st);j++) 53
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}

54
OUTPUT:
Enter how many blocks are already allocated 3
Enter the blocks no’s that are already allocated 479
Enter the starting index block & length 3
7
3-> 1
4- > File is already allocated
5->1
6->1
7-> File is already allocated
8->1
9-> File is already allocated
10->1
11->1
12->1
If u want to enter one more file? (yes-1/no-0)

RESULT:

Thus the C program for File Allocation concept using the technique indexed
Allocation was implemented and output was verified using various samples.

55
DATE:
EXPT.NO.:6(
c) LINKED ALLOCATION

AIM:
To write a C program to implement File Allocation concept using the
linked list allocation Technique.

ALGORITHM:

Step 1: Start the Program


Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly.
Step 5: Print the results file no,length, blocks allocated.
Step 7: Stop the execution.

PROGRAM:
#include<stdio.h>
main( )
{
int f[50],i,k,j,index[50],n,c,count=0;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("Enter index block");
scanf("%d",&i);
if(f[i]!=1)
{
f[i]=1;
printf("Enter no of files on index");
scanf("%d",&n);
}
Y:
for(i=0;i<n;i++)
scanf("%d",&index[i]);
if(f[index[i]]==0) count++;
if(count==n)
{
for(j=0;j<nj++)
f[index[j]]=1;
printf("\nAllocated");
printf("\n File indexed");

56
for(k=0;k<n;k++)
printf("\n%d->%d:%d",i,index[k],f[index[k]]);
}
else
{
printf("\nFile in the index already allocation");
printf("\nEnter another file indexed");
goto Y;
}
printf("\nIndex is already allocated");
count=0;
printf("\n if u enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto X;
}

OUTPUT:
Enter how many blocks already
allocated 5
Enter block numbers : 3 7 9 10 14
Enter index starting block & length : 4 10

4->1
5- >1
6- >1
7- >file is already
allocated 8->1
9->file is already
allocated 10->file is
already allocated 11->1
12->1
13->1
14->file is already
allocated 15->1
16->1
17->1

RESULT:

Thus the C program for File Allocation concept using the technique
linked list Allocation was implemented and output was verified using
various samples.

57
DATE:
EXPT.NO.:7
SEMAPHORE USING PRODUCER CONSUMER PROBLEM

AIM:

To write a C program to implement the Producer & consumer Problem


(Semaphore).

ALGORITHM:

Step 1: The Semaphore mutex, full & empty are initialized.


Step 2: In the case of producer process
i) Produce an item in to temporary variable.

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

iii) If the mutex value is 0, allow the producer to add value in the
temporary variable to the buffer.
Step 3: In the case of consumer process
i) It should wait if the buffer is empty

ii) If there is any item in the buffer check for mutex value, if the
mutex==0, remove item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the item.
Step 4: Print the result

58
PROGRAM:
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0; out = 0; bufsize = 10;
while(choice !=3)
{
printf("\n1. Produce \t 2. Consume \t3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");

else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed valueis %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}

59
OUTPUT:
1. Producer 2. Consumer 3.Exit

Enter your choice: 1

Enter the Value: 23

1. Producer 2. Consumer 3.Exit

Enter your choice: 2

The Consumed Value is : 23

1. Producer 2. Consumer 3.Exit

Enter your choice: 3

RESULT:
Thus the C program for the Producer and Consumer using the
technique Semaphore was implemented and output was verified using
various samples.

60
DATE:
EXPT.NO.:8. a
SHARED MEMORY
AIM:

To write a C program to implement shared memory.

ALGORITHM:

Step 1: Start the Program


Step 2: Obtain the required data through char and int data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM CODING:
#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 WAITING");
for(i=0;i<10;i++)
{
shmptr[i]='a'+i;
putchar(shmptr[i]);
}
printf("\n%s",shmptr);
wait(NULL);
}
61
else

62
{
shmid=shmget(2041,32,0666);
shmptr=shmat(shmid,0,0);
printf("\nCHILD IS READING");
for(i=0;i<10;i++)
putchar(shmptr[i]);
shmdt(NULL);
shmct(shmid,IPC_RMID,NULL);
}
return 0;
}

OUTPUT:

"share.c" 34L, 500C written


[roshon@localhost ~]$ cc shared.c
[roshon@localhost ~]$./a.out

Process Id :3839 Count is : 8267


Process Id :3839 Count is : 8268
Process Id :3839 Count is : 8269
Process Id :3839 Count is : 8270
Process Id :3839 Count is : 8271

RESULT:

Thus the program for shared memory was implemented and the output
executed successfully.

63
DATE:
EXPT.NO.:8. b
INTER PROCESS COMMUNICATION
AIM:

To write a C program to implement Inter Process Communication.

ALGORITHM:

Step 1: Start the Program


Step 2:Obtain the required data through char and int datatypes.
Step 3:Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5:Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution

PROGRAM CODING:

#include <stdio.h>
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,5);
write(0);
}
64
else
{
close(fd[1]);
read(fd[0],a,5);
printf("string is retrived from the pipe is%s\n",a);
}
}

OUTPUT:

"ipc.c" 34L, 500C written


[roshon@localhost ~]$ cc shared.c
[roshon@localhost ~]$./a.out
Enter the string to enter into the pipe : Client
Client

RESULT:

Thus the program for Inter Process Communication was implemented and
the output executed successfully.

65
DATE:
EXPT.NO.:9
IMPLEMENTATION OF DEADLOCK DETECTION
ALGORITHM
AIM:

To write a C program to implement Deadlock Detection algorithm

ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and in data types.
Step 3: Obtain the number of process, resources
instances, max and allocation matrix.
Step 4: Enter the available resources.
Step 5: Display the process, available, max , allocation. Step 6: Calculate the
deadlock process and print the deadlock systems if need is greater.
Step 7: Else print the deadlock not occurred process.
Step 8: Stop the program execution.

PROGRAM CODING:

#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++)
66
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l]) 67
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}

68
OUTPUT:

Enter the no. Of processes: 3


Enter the no of resources instances: 3

Enter the max matrix


368
433
344

Enter the allocation matrix


333
203
124

Enter the available resources


120

Process allocation max available


P1 333 368 120
P2 203 433
P3 124 344

System is in deadlock and deadlock process are

P0 p1 p2

RESULT:

Thus the program for the Deadlock Detection concept was implemented and
output was verified using various samples.

69
DATE:
EXPT.NO.:10
IMPLEMENTATION OF DEADLOCK AVOIDANCE
ALGORITHM
AIM:

To write a C program to implement Deadlock Avoidance algorithm

ALGORITHM:

Step 1: Start the Program

Step 2: Get the values of resources and processes.

Step 3: Get the avail value.

Step 4: After allocation find the need value.

Step 5: Check whether its possible to allocate. If possible it is safe state

Step 6: If the new request comes then check that the system is in safety

or not if we allow the request.

Step 7: Stop the execution

PROGRAM CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no ofresource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
scanf("%d",&totext[k]); 70
printf("Enter the allocated resources:");
for(i=1; i<=n; i++)
for(k=1; k<=r; k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1; k<=r; k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1; i<=n; i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1; k<=r; k++)
{
j=0;
for(i=1; i<=n; i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1; i<=n; i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1; k<=r; k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1; k<=r; k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1; 71
break;
}
}
if(u==0)
{
for(k=1; k<=r; k++)
simalloc[k]=totalloc[k];
for(s=1; s<=n; s++)
for(i=1; i<=n; i++)
{
if(active[i]==1)
{
j=0;
for(k=1; k<=r; k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;
break;
}}}
if(j==0)
{
active[i]=0;
for(k=1; k<=r; k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1; k<=r; k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1; k<=r; k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}

72
OUTPUT:

Enter the no of resources: 4


Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running:
Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Deadlock will occur

RESULT:

Thus the program for the Deadlock Avoidance concept was implemented and
output was verified using various samples.

73
DATE:
EXPT.NO.:11(a)

THREADING
APPLICATIONS

AIM:

To write a C program to implement Threading application.

ALGORITHM:

Step 1: Start the Program


Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution

PROGRAM CODING:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void * thread1()
{
while(1)
{
printf("Hello!!\n");
}
}
void * thread2()
{
while(1)
{
printf("How are you?\n");

74
}
}
int main()
{
int status;
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,thre
ad1,NULL);
pthread_create(&tid2,NULL,thre
ad2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}

OUTPUT:

"thread.c" 29L, 480C written


[2cse110@cse ~]$ gcc
thread.c -lpthread
[2cse110@cse ~]$ ./a.out
How are you?
How are you?
How are you?
Hello!!
Hello!!
Hello!!

RESULT:

Thus the program for threading was implemented and executed


and the output was verified successfully.

75
DATE:
EXPT.NO.:11(b)
SYNCHRONIZATION APPLICATIONS

AIM:

To write a C program to implement Synchronisation application.

ALGORITHM:

Step 1: Start the Program


Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution

PROGRAM CODING:

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0 char n[1024];
pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;
int string_read=FALSE;
pthread_cond_t cond;
void * read1()
{
while(1)
{
while(string_read);
pthread_mutex_lock(&lock);
printf("Enter a string: ");
scanf("%s",n);
string_read=TRUE;
75
pthread_mutex_unlock(&lock);
pthread_cond_signal(&cond);
}
}
void * write1()
{
while(1)
{
pthread_mutex_lock(&lock);
while(!string_read)
pthread_cond_wait(&cond,&lock);
printf("The string entered is %s\n",n);
string_read=FALSE;
pthread_mutex_unlock(&lock);
}
}
int main()
{
int status; pthread_t tr, tw;
pthread_create(&tr,NULL,read1,NULL);
pthread_create(&tw,NULL,write1,NULL);
pthread_join(tr,NULL); pthread_join(tw,NULL);
return 0;
}

OUTPUT:

"syn.c" 52L, 1129C written


[2cse110@cse ~]$ gcc syn.c –lpthread
[2cse110@cse ~]$ ./a.out
Enter a string: hai
The string entered is hai
Enter a string: hello
The string entered is hello
Enter a string: how r u
The string entered is how r u

RESULT:

Thus the program for Synchronization was implemented and executed and
the output was verified successfully.
76
DATE:
EXPT.NO.:12(a)
MEMORY ALLOCATION METHODS FOR FIXED
PARTITION – FIRST FIT
AIM:

To write a C program to implement Memory Allocation Methods for Fixed


Partition (First Fit) algorithm.

ALGORITHM:

Step 1: Start the Program

Step 2: Read number of pages n

Step 3: Read number of pages no

Step 4: Read page numbers into an arraya[i]

Step 5: Initialize aval[i]=o, to check page hit

Step 6: Print the results.

Step 7: Stop the Process.

PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)

{ 7
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

7
OUTPUT:
Memory Management Scheme - First Fit
Enter the number of blocks:5
Enter the number of files:4
Enter the size of the blocks:
Block 1:140
Block 2:160
Block 3:270
Block 4:80
Block 5:56
Enter the size of the files :
File 1:200
File 2:130
File 3:180
File 4:200

File_no: File_size : Block_no: Block_size: Fragement


1 200 3 270 70
2 130 1 140 10
3 180 0 0 0
4 200 0 0 0

RESULT:

Thus the program for Memory Allocation Methods for Fixed Partition
using the First Fit was executed successfully.

79
DATE:
EXPT.NO.:12(b)
MEMORY ALLOCATION METHODS FOR FIXED PARTITION –
WORST FIT
AIM:

To write a C program to implement Memory Allocation Methods for Fixed


Partition (Worst Fit) algorithm.

ALGORITHM:

Step 1: Start the Program

Step 2: Read number of pages n

Step 3: Read number of pages no

Step 4: Read page numbers into an arraya[i]

Step 5: Initialize aval[i]=o, to check page hit

Step 6: Print the results.

Step 7: Stop the Process.

PROGRAM CODING:
#include<stdio.h
>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
80
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{

for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

81
OUTPUT:
Memory Management Scheme - Worst Fit
Enter the number of blocks:5
Enter the number of files:4
Enter the size of the blocks:
Block 1:140
Block 2:160
Block 3:270
Block 4:80
Block 5:56
Enter the size of the files :
File 1:200
File 2:130
File 3:180
File 4:200

File_no: File_size : Block_no: Block_size: Fragement


1 200 3 270 70
2 130 2 160 30
3 180 0 0 0
4 200 0 0 0

RESULT:

Thus the program for Memory Allocation Methods for Fixed Partition
using the Worst Fit was executed successfully.

82
DATE:
EXPT.NO.:12(c)
MEMORY ALLOCATION METHODS FOR FIXED PARTITION –
BEST FIT
AIM:

To write a C program to implement Memory Allocation Methods for Fixed


Partition (Bestt Fit) algorithm.

ALGORITHM:

Step 1: Start the Program

Step 2: Read number of pages n

Step 3: Read number of pages no

Step 4: Read page numbers into an arraya[i]

Step 5: Initialize aval[i]=o, to check page hit

Step 6: Print the results.

Step 7: Stop the Process.

PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)

84
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

85
OUTPUT:
Memory Management Scheme - Best Fit
Enter the number of blocks:5
Enter the number of files:4
Enter the size of the blocks:
Block 1:140
Block 2:160
Block 3:270
Block 4:80
Block 5:56
Enter the size of the files :
File 1:200
File 2:130
File 3:180
File 4:200

File_no: File_size : Block_no: Block_size: Fragement


1 200 3 270 70
2 130 1 140 10

RESULT:

Thus the program for Memory Allocation Methods for Fixed Partition
using the Worst Fit was executed successfully.

86
DATE:
EXPT.NO.:
13 PAGING TECHNIQUE OF MEMORY MANAGEMENT

AIM:

To write a C program to implement paging technique of memory


management algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Enter the no of pages and sizes.
Step 3: Print the page address.
Step 4: Stop the program execution.
PROGRAM CODING:
#include<stdio.h>
main()
{
int np,ps,i;
int *sa;
printf("enter how many pages\n");
scanf("%d",&np);
printf("enter the page
size \n");
scanf("%d",&ps);
sa=(int*)malloc(2*np);
for(i=0;i<np;i++)
{
sa[i]=(int)malloc(ps);
printf("page%d\t address %u\n",i+1,sa[i]);
}
}

87
OUTPUT:
“page.c" 34L, 1129C written
[2cse110@cse ~]$cc page.c
[2cse110@cse ~]$ ./a.out
Enter how many pages
3
Enter the
page size
2
page1 address 147984408
page2 address 147984424
page3 address 147984440

RESULT:

Thus the program for Paging Technique of Memory Management was


executed successfully.

88
DATE:
EXPT.NO.:14(a)
PAGE REPLACEMENT ALGORITHMS - FIFO
AIM:

To write a C program to implement page replacement FIFO (First In First


Out) algorithm.

ALGORITHM:

Step 1: Start the Program

Step 2: Read number of pages n

Step 3: Read number of pages no

Step 4: Read page numbers into an arraya[i]

Step 5: Initialize aval[i]=o, to check page hit

Step 6: Print the results.

Step 7: Stop the Process.

PROGRAM CODING:

#include<stdio.h
>
#include<conio.h
> void main()
{
int
a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page
Numbers:",n); for(i=0;i<n;i++)
scanf("%d",&b[i]);
89
{if(p==0)
{
if(q>=3
) q=0;
a[q]=b[i]
; q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
getch();
}

90
OUTPUT:

Enter the Number of Pages: 12


Enter 12 Page Numbers:
232152453252

2 2-> F
3 23-> F
2 23
1 231-> F
5531-> F
2521-> F
4524-> F
5524
3 324-> F
2 324
5 354-> F
2 352-> F

No of faults: 9

RESULT:

Thus the program for Page Replacement Algorithm using the First in
First out was executed successfully.

91
DATE:
EXPT.NO.:14(b)
PAGE REPLACEMENT ALGORITHMS - LRU
AIM:

To write a C program to implement page replacement LRU (Least Recently


Used) algorithm.

ALGORITHM:

Step 1: Start the Program


Step 2: Declare the size.
Step 3: Get the number of pages to be issued.
Step 4: Declare counter and stack
Step 5: Select the LRU page by counter value
Step 6: Stack them according the selection
Step 7: Stop the execution

PROGRAM CODING:
#include<stdio.h>
#include<conio.h> void main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n; char f='F';
clrscr();
printf("Enter the number of pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0; a[q]=b[i];
q++;
if(q1<3)
{
q1=q; 91
//g=1;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0; g=0;
if(q1==3)
{
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k
]) p=1;
}
for(j=0;j<q1;j++)
{
u=0;
k=i;
while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j]
) u++;k--;
}
if(u==0)
q=j;
}
}
else
{
for(k=0;k<q;k++)
{

if(b[i+1]==a[k
])
p=1;
}
}
}
printf("\nNo of faults:%d",m);
getch();} 92
OUTPUT:

Enter the Number of Pages: 12


Enter 12 Page Numbers:
232152453252

2 2-> F
3 2 3-> F
2 23
1 2 3 1-> F
5 2 5 1-> F
2 251
4 2 5 4-> F
5 254

3 3 5 4-> F
2 3 5 2-> F
5 3 5 2
2 3 5 2

No of faults: 7

RESULT:

Thus the program for Page Replacement Algorithm using the Least
Recently Used was executed successfully.

93
DATE:
EXPT.NO.:14(c)
PAGE REPLACEMENT ALGORITHMS - LFU
AIM:

To write a C program to implement page replacement LFU (Least


Frequently Used) algorithm.

ALGORITHM:

Step 1: Start the Program


Step 2: Declare the size.
Step 3: Get the number of pages to be issued.
Step 4: Declare counter and stack
Step 5: Select the LRU page by counter value
Step 6: Stack them according the selection
Step 7: Stop the execution

PROGRAM CODING:
#include<stdio.h>
void main()
{
Int
q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
clrscr();
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of
frames:"); scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{ 94
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++
;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j++)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r]; 95
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

OUTPUT:

Enter the Number of Pages: 12


Enter 12 Page Numbers:
232152453252

2 2-> F
3 2 3-> F
2 23
1 2 3 1-> F
5 2 5 1-> F
2 251
4 2 5 4-> F
5 254

3 3 5 4-> F
2 3 5 2-> F
5 3 5 2
2 3 5 2

No of faults: 7

96
RESULT:

Thus the program for Page Replacement Algorithm using the Least
Frequently Used was executed successfully.

97
DATE:
EXPT.NO.:15(a)

FILE ORGANIZATION TECHNIQUES - SINGLE LEVEL


DIRECTORY
AIM:

To write a C program to implement File Organization concept using the technique


Single level directory.

ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and int data types.
Step 3: Enter the filename,
index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked
allocation. Step 7: Stop the execution

PROGRAM CODING:
#include<stdio.h
>
#include<conio.h
> main()
{
int master,s[20];
char f[20][20][20];
char
d[20][20]; int
i,j;
clrscr();
printf("Enter the Number of Directories:");
scanf("%d",&master);
printf("Enter the Names of the
Directories:"); for(i=0;i<master;i++)
printf("Enter the Size of the
Directories:"); for(i=0;i<master;i++)
scanf("%d",&s[i]);
printf("Enter the File Names
:"); for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfile Names\n");
printf("***************************
**********************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");
getch();
}

OUTPUT

Enter number of directories:


2 Enter names of
directories: a rose
Enter size of directories: 1
1
Enter the file names:
file1 file2

Directory size filename


s
sa 1 file1
rose 1 file2

99
RESULT:

Thus the program for File Organization Technique using the Single level
Directory was executed successfully.

10
DATE:
EXPT.NO.:15(b)

FILE ORGANIZATION TECHNIQUES - TWO LEVEL


DIRECTORY
AIM:

To write a C program to implement File Organization concept using the


technique Two level directory.

ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and int data
types. Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked
allocation. Step 7: Stop the execution

PROGRAM CODING:

#include<stdio.h
>
#include<conio.h
> struct st
{
char dname[10];
char sdname[10][10];
char
fname[10][10][10]; int
ds,sds[10];
}dir[10];
void
main()
{
int i,j,k,n;
scanf("%d",&n 101
for(i=0;i<n;i++)
{
printf("Enter Directory %d Names:",i+1);
scanf("%s",&dir[i].dname);
printf("Enter the Size of the
Directories:"); scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("Enter the Subdirectory Name and
Size:"); scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("Enter File Name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n"); for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}
getch();
}

102
OUTPUT

Enter number of
Directories: 1 Enter Directory
1 Names: Files Enter the Size of
the Directories: 2
Enter the Subdirectory Name and Size: Program 1
Enter File Name: Basics

Enter the Subdirectory Name and Size:

Coding 1 Enter File Name: Shell

Dname Size Subdirname Size Files


***************************************
files 2 program 1 basic
s
coding 1 shell

RESULT:

Thus the program for File Organization Technique using the Two level
Directory was executed successfully.

103
DATE:
EXPT.NO.:15(
b)

FILE ORGANIZATION TECHNIQUES – HIERARCHICAL LEVEL


DIRECTORY
AIM:

To write a C program to implement File Organization concept using the


technique Hierarchical level directory.

ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and int data types.
Step 3: Enter the filename,
index block.
Step 4: Print the file name index loop.
Step 5: Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked
allocation. Step 7: Stop the execution

PROGRAM CODING:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element
*link[5];
};
typedef struct
tree_element node; void
main()
{

clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s)
:",dname); fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 forfile
:"); scanf("%d",&(*root)-
>ftype); (*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)-
>lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i+
+)
(*root)-
>link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)-
>name); scanf("%d",&(*root)->nc);
if((*root)-
>nc==0) gap=rx-
lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+1g0a5p/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
Enter Name of dir/file (under root):
ROOT Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under
ROOT):USER 1 Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER
1):SUBDIR Enter 1 for Dir /2 for file:1 10
No of subdirectories /files (for
SUBDIR):2 Enter Name of dir/file
(under USER 1): JAVA Enter 1 for Dir
/2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under
SUBDIR):VB

Enter 1 for Dir /2 for file:1


No of subdirectories /files (for VB): 0

Enter Name of dir/file (under


ROOT):USER2 Enter 1 for Dir /2 for
file:1
No of subdirectories /files (for
USER2):2 Enter Name of dir/file
(under ROOT):A Enter 1 for Dir /2
for file:2
Enter Name of dir/file (under
USER2):SUBDIR 2 Enter 1 for Dir /2 for
file:1
No of subdirectories /files (for SUBDIR 2):2

Enter Name of dir/file (under


SUBDIR2):PPL Enter 1 for Dir /2 for file:1
No of subdirectories /files (for
PPL):2 Enter Name of dir/file
(under PPL):B Enter 1 for Dir /2
for file:2
Enter Name of dir/file (under
PPL):C Enter 1 for Dir /2 for

107
file:2

Enter Name of dir/file (under

AI):E Enter 1 for Dir /2 for file:2

RESULT:

Thus the program for File Organization Technique using the Hierarchical
level Directory was executed successfully.

108

You might also like