Você está na página 1de 64

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING

CS 2257 OPERATING SYSTEMS LABORATORY

DHANALAKSHMI
COLLEGE OF ENGINEERING
1

DR V P R NAGAR, MANIMANGALAM, CHENNAI 601 301

CS 2257

OPERATING SYSTEMS LAB

0 0 3 2

(Implement the following on LINUX or other Unix like platform. Use C for high level
language implementation)
1.

Write programs using the following system calls of UNIX operating system:
fork, exec, getpid, exit, wait, close, stat, opendir, readdir

2.

Write programs using the I/O system calls of UNIX operating system (open, read,
write, etc)

3.

Write C programs to simulate UNIX commands like ls, grep, etc.

4.

Given the list of processes, their CPU burst times and arrival times, display/print
the Gantt chart for FCFS and SJF. For each of the scheduling policies, compute
and print the average waiting time and average turnaround time. (2 sessions)

5.

Given the list of processes, their CPU burst times and arrival times, display/print
the Gantt chart for Priority and Round robin. For each of the scheduling policies,
compute and print the average waiting time and average turnaround time. (2
sessions)

6.

Developing Application using Inter Process communication (using shared


memory, pipes or message queues)

7.

Implement the Producer Consumer problem using semaphores (using UNIX


system calls).

8.

Implement some memory management schemes I

9.

Implement some memory management schemes II

10.

Implement any file allocation technique (Linked, Indexed or Contiguous)

Example for exercises 8 & 9 :


Free space is maintained as a linked list of nodes with each node having the starting byte
address and the ending byte address of a free block. Each memory request consists of the
process-id and the amount of storage space required in bytes. Allocated memory space is again
maintained as a linked list of nodes with each node having the process-id, starting byte address
and the ending byte address of the allocated space. When a process finishes (taken as input) the
appropriate node from the allocated list should be deleted and
this free disk space should be added to the free space list. [Care should be taken to merge
contiguous free blocks into one single block. This results in deleting more than one node from

the free space list and changing the start and end address in the appropriate node]. For
allocation use first fit, worst fit and best fit

INDEX
E.No

Date

Contents

Shell Programming:
a) Unix Commands
b) Vi Commands
c) Unix Shell programming commands
a) Concatenation of two strings
b) Comparison of two strings
c) Maximum of three numbers
d) Fibonacci series
e) Arithmetic operation using case

System Calls
a) Process Creation
b) Executing a command
c) Sleep command
d) Sleep command using getpid
e) Signal handling using kill
k) Wait command
I/O System Calls
a) Reading from a file
b) Writing into a file
c) File Creation
a)Implementation of ls command
b)Implementation of grep command

4
5
6

Given the list of processes, their CPU burst times and arrival times,
display/print the Gantt chart for FCFS and SJF.Print avg.waiting time
and turnaround time.
Given the list of processes, their CPU burst times and arrival times,
display/print the Gantt chart for Priority and Round robin. Print
avg.waiting time and turnaround time.

Developing Application using Inter Process communication


(using shared memory, pipes or message queues)

Producer-Consumer Problem using Semaphore

Memory management Scheme-I


a) Paging Concept
Memory management Scheme-II
a) Segmentation Concept

10

Page No.

11

Implement any file allocation technique (Linked, Indexed or


Contiguous)

Ex.No : 1.a
Date:

UNIX COMMANDS

AIM :
To study and excute the commands in unix.
COMMAND :

1.Date Command :
This command is used to display the current data and time.
Syntax :
$date
$date +%ch
Options : a = Abbrevated weekday.
A = Full weekday.
b = Abbrevated month.
B = Full month.
c = Current day and time.
C = Display the century as a decimal number.
d = Day of the month.
D = Day in mm/dd/yy format
h = Abbrevated month day.
H = Display the hour.
L = Day of the year.
m = Month of the year.
M = Minute.
P = Display AM or PM
S = Seconds
T = HH:MM:SS format
u = Week of the year.
y = Display the year in 2 digit.
Y = Display the full year.
Z = Time zone .
To change the format :
Syntax :
$date +%H-%M-%S
2.Calender Command :
This command is used to display the calendar of the year or the particular month of
calendar year.
Syntax :
a.$cal <year>
b.$cal <month> <year>
Here the first syntax gives the entire calendar for given year & the second Syntax gives
the calendar of reserved month of that year.

3.Echo Command :
This command is used to print the arguments on the screen .
Syntax :
$echo <text>
Multi line echo command :
To have the output in the same line , the following commands can be used.
Syntax :
$echo <text\>text
To have the output in different line, the following command can be used.
Syntax :
$echo text
>line2
>line3
4.Banner Command :
It is used to display the arguments in # symbol .
Syntax :
$banner <arguments>
5.who Command :
It is used to display who are the users connected to our computer currently.
Syntax :
$who options
Options : HDisplay the output with headers.
bDisplay the last booting date or time or when the system was lastely rebooted.
6.who am i Command :
Display the details of the current working directory.
Syntax :
$who am i
7.tty Command :
It will display the terminal name.
Syntax :
$tty
8.Binary Calculator Command :
It will change the $ mode and in the new mode, arithematic operations such as +,-,*,/,
%,n,sqrt(),length(),=, etc can be performed . This command is used to go to the binary calculus
mode.
Syntax :
$bc
operations
^d
$
1 base inputbase
0 base outputbase are used for base conversions.
Base :
Decimal = 1 Binary = 2 Octal
=8
Hexa = 16
9.CLEAR Command :
It is used to clear the screen.
Syntax :
$clear
10.MAN Command :

It help us to know about the particular command and its options & working. It is like
help command in windows .
Syntax :
$man <command name>
11.MANIPULATION Command :
It is used to manipulate the screen.
Syntax :
$tput <argument>
Arguments :
1.Clear to clear the screen.
2.Longname Display the complete name of the terminal.
3.SMSO background become white and foreground become black color.
4.rmso background become black and foreground becomes white color.
5.Cop R C Move to the cursor position to the specified location.
6.Cols Display the number of columns in our terminals.
12.LIST Command :
It is used to list all the contents in the current working directory.
Syntax :
$ ls options <arguments>
If the command does not contain any argument means it is working in the
Current directory.
Options :
a used to list all the files including the hidden files.
c list all the files columnwise.
d- list all the directories.
m- list the files separated by commas.
p- list files include / to all the directories.
r- list the files in reverse alphabetical order.
f- list the files based on the list modification date.
x-list in column wise sorted order.
DIRECTORY RELATED COMMANDS :
1.Present Working Directory Command :
To print the complete path of the current working directory.
Syntax :
$pwd
2.MKDIR Command :
To create or make a new directory in a current directory .
Syntax :
$mkdir <directory name>
3.CD Command :
To change or move the directory to the mentioned directory .
Syntax :
$cd <directory name.
4.RMDIR Command :
To remove a directory in the current directory & not the current directory itself.
Syntax :
$rmdir <directory name>

FILE RELATED COMMANDS :


1.CREATE A FILE :
To create a new file in the current directory we use CAT command.
Syntax :
$cat > <filename.
The > symbol is redirectory we use cat command.
2.DISPLAY A FILE :
To display the content of file mentioned we use CAT command without > operator.
Syntax :
$cat <filename.
Options s = to neglect the warning /error message.
3.COPYING CONTENTS :
To copy the content of one file with another. If file doesnot exist, a new file is created
and if the file exists with some data then it is overwritten.
Syntax :
$ cat <filename source> >> <destination filename>
$ cat <source filename> >> <destination filename>
it
is
avoid
overwriting.
Options : -n content of file with numbers included with blank lines.
Syntax :
$cat n <filename>
4.SORTING A FILE :
To sort the contents in alphabetical order in reverse order.
Syntax :
$sort <filename >
Option :
$ sort r <filename>
5.COPYING CONTENTS FROM ONE FILE TO ANOTHER :
To copy the contents from source to destination file . so that both contents are same.
Syntax :
$cp <source filename> <destination filename>
$cp <source filename path > <destination filename path>
6.MOVE Command :
To completely move the contents from source file to destination file and to remove the
source file.
Syntax :
$ mv <source filename> <destination filename>
7.REMOVE Command :
To permanently remove the file we use this command .
Syntax :
$rm <filename>

8.WORD Command :
To list the content count of no of lines , words, characters .
Syntax :
$wc<filename>
Options :
-c to display no of characters.
-l to display only the lines.
-w to display the no of words.
9.LINE PRINTER :
To print the line through the printer, we use lp command.
Syntax :
$lp <filename>
10.PAGE Command :
This command is used to display the contents of the file page wise & next page can be
viewed by pressing the enter key.
Syntax :
$pg <filename>
11. FILTERS AND PIPES
HEAD : It is used to display the top ten lines of file.
Syntax:
$head<filename>
TAIL : This command is used to display the last ten lines of file.

Syntax:

$tail<filename>

PAGE : This command shows the page by page a screenfull of information is displayed after

which the page command displays a prompt and passes for the user to strike the enter key to
continue scrolling.
Syntax:
$ls a\p
MORE : It also displays the file page by page .To continue scrolling with more command ,

press the space bar key.


Syntax:
$more<filename>
GREP :This command is used to search and print the specified patterns

Syntax:

from the file.

$grep [option] pattern <filename>

SORT : This command is used to sort the datas in some order.

Syntax:

$sort<filename>

PIPE : It is a mechanism by which the output of one command can be channeled into the input

of another command.
Syntax:
$who | wc-l
TR :The tr filter is used to translate one set of characters from the standard inputs to another.
Syntax:
$tr [a-z] [A-Z]
COMMUNICATION THROUGH UNIX COMMANDS
MESG

Description: The message command is used to give permission to other users to send
message to your terminal.
Syntax: $mesg y
2.Command: WRITE
Description: This command is used to communicate with other users, who are logged in at
the same time.
Syntax: $write <user name>
3.Command: WALL
Description: This command sends message to all users those who are logged in using the
unix server.
Syntax: $wall <message>
4.Command: MAIL
Description: It refers to textual information, that can be transferred from one
another
Syntax: $mail <user name>

user to

5.Command: REPLY
Description: It is used to send reply to specified user.
Syntax: $reply<user name>
RESULT:
Thus the unix commands are assigned and executed successfully.

10

SHELL PROGRAMMING
Ex.No :2a
Date:

CONCATENATION OF TWO STRINGS

Aim:
To write a shell program to concatenate two strings.
Algorithm:
Step1: Enter into the vi editor and go to the insert mode for entering the code
Step2: Read the first string.
Step3: Read the second string
Step4: Concatenate the two strings
Step5: Enter into the escape mode for the execution of the result and verify the output
Program:
echo enter the first string
read str1
echo enter the second string
read str2
echo the concatenated string is $str1$str2
Sample I/P:
Enter first string: Hello
Enter first string: World
Sample O/P:
The concatenated string is HelloWorld
Result:
Thus the shell program to concatenate two strings is executed and output is verified
successfully.

11

Ex.No. :2b
Date:

COMPARISON OF TWO STRINGS

Aim:
To write a shell program to compare the two strings.
Algorithm:
Step1: Enter into the vi editor and go to the insert mode for entering the code
Step2: Read the first string.
Step3: Read the second string
Step4: Compare the two strings using the if loop
Step5: If the condition satisfies then print that two strings are equal else print two
strings are not equal.
Step6: Enter into the escape mode for the execution of the result and verify the output
Program:
echo enter the first string
read str1
echo enter the second string
read str2
if [ $str1 = $str2 ]
then
echo strings are equal
else
echo strings are unequal
fi
Sample I/P:1
Enter first string: hai
Enter second string: hai
Sample O/P:1
The two strings are equal
Sample I/P:2
Enter first string: hai
Enter second string: cse
Sample O/P:2
The two strings are not equal
Result:
Thus the shell program to compare the two strings is executed and output is verified
successfully.
12

Ex.No:2c
Date:

MAXIMUM OF THREE NUMBERS

Aim:
To write a shell program to find greatest of three numbers.
Algorithm:
Step1: Declare the three variables.
Step2: Check if A is greater than B and C.
Step3: If so print A is greater.
Step4: Else check if B is greater than C.
Step5: If so print B is greater.
Step6: Else print C is greater.
Program:
echo "enter A"
read a
echo "enter B"
read b
echo "enter C"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is greater"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "B is greater"
else
echo "C is greater"
fi
Sample I/P:
Enter A:23
Enter B:45
Enter C:67
Sample O/P:
C is greater
Result:
Thus the shell program to find the maximum of three numbers is executed and output is
verified successfully.

13

Ex.No:2d
Date:

FIBONACCI SERIES

Aim:
To write a shell program to generate fibonacci series.
Algorithm :
Step 1 : Initialise a to 0 and b to 1.
Step 2 : Print the values of 'a' and 'b'.
Step 3 : Add the values of 'a' and 'b'. Store the added value in variable 'c'.
Step 4 : Print the value of 'c'.
Step 5 : Initialise 'a' to 'b' and 'b' to 'c'.
Step 6 : Repeat the steps 3,4,5 till the value of 'a' is less than 10.
Program :
echo enter the number
read n
a=-1
b=1
i=0
while [ $i le $n ]
do
t=`expr $a + $b`
echo $t
a=$b
b=$t
i=`expr $i + 1
done
Sample I/P :
Enter the no: 5
Sample O/P:
0
1
1
2
3
5
Result :
Thus the shell program to find the fibonacci series is executed and output is
verified successfully.

14

Ex.No:2e
Date:

ARITHMETIC OPERATIONS USING CASE

Aim:
To write a shell program to perform the arithmetic operations using case
Algorithm :
Step 1 : Read the input variables and assign the value
Step 2 : Print the various arithmetic operations which we are going to perform
Step 3 : Using the case operator assign the various functions for the arithmetic
operators.
Step 4 : Check the values for all the corresponding operations.
Step 5 : Print the result and stop the execution.
.
Program :
echo 1.Addition
echo 2.Subraction
echo 3.Multiplication
echo 4.Division
echo enter your choice
read a
echo enter the value of b
read b
echo enter the value of c
read c
echo b is $b c is $c
case $a in
1)d=`expr $b + $c`
echo the sum is $d
;;
2)d=`expr $b - $c`
echo the difference is $d
;;
3)d=`expr $b \* $c`
echo the product is $d
;;
4)d=`expr $b / $c`
echo the quotient is $d
;;
esac
Sample I/P :
1.Addition
2.Subraction
3.Multiplication

15

Division
Enter your choice:1
Enter the value of b:3
Enter the value of c:4
b is 3 c is 4
the sum is 7
Sample O/P:
b is 3 c is 4
the sum is 7
Result :
Thus the shell program to perform arithmetic operations using case is executed and
output is verified successfully.

16

SYSTEM CALLS
Ex.No:3a
Date:

PROCESS CREATION

AIM:
To write a program to create a process in UNIX.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare pid as integer.
STEP 3: Create the process using Fork command.
STEP 4: Check pid is less than 0 then print error else if pid is equal to 0 then execute
command else parent process wait for child process.
STEP 5: Stop the program.
PROGRAM:
void main()
{
int id;
id=fork();
if(id<0)
{
Printf(cannot create the file);
Exit(-1);
}
if(id==0)
{
Printf(child process);
Exit(0);
}
else
{
Printf(parent process);
}
}
SAMPLE OUTPUT:
$cc pc.c
$a.out
parent process$ child process
$ps
PID CLS PRI TTY
TIME COMD
5913
TS 70
pts022 0:00 ksh
6229
TS 59
pts022 0:00
ps
RESULT: Thus the program was executed and verified successfully

17

Ex.No:3b
Date:

EXECUTING A COMMAND

AIM:
To write a program for executing a command.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Execute the command in the shell program using exec ls.
STEP 3: Stop the execution.
PROGRAM:
echo Program for executing UNIX command using shell programming
echo Welcome
ps
exec wc e
SAMPLE OUTPUT:
$ sh exec.sh
program for executing UNIX command using shell programming
Welcome
PID CLS PRI TTY

TIME COMD

958

TS

70

pts001 0:00

ksh

971

TS

70

pts001 0:00

sh

972

TS

59

pts001 0:00

ps

3 41 81 e
RESULT:
Thus the program was executed and verified successfully.

18

Ex.No:3c
Date:

SLEEP COMMAND

AIM:
To create child with sleep command.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create process using fork and assign into a variable.
STEP 3: If the value of variable is < zero print not create and > 0 process create and
else print child create.
STEP 4: Create child with sleep of 2.
STEP 5: Stop the program.
PROGRAM:
void main()
{
int id=fork();
if(id==-1)
{
printf(cannot create the file);
exit(1);
}
else if(id==0)
{
sleep(2);
printf(this is child process);
}
else
{
printf(parent process);
exit(1);
}
}

19

OUTPUT:
$ cc sleep.c
$ a.out
parent process$ this is child process
RESULT:
Thus the program was executed and verified successfully.

20

Ex.No:3d
Date:

SLEEP COMMAND USING GETPID

AIM:
To create child with sleep command using getpid.
ALGORITHM:
STEP 1: Start the execution and create a process using fork() command.
STEP 2: Make the parent process to sleep for 10 seconds.
STEP 3:In the child process print it pid and it corresponding pid.
STEP 4: Make the child process to sleep for 5 seconds.
STEP 5: Again print it pid and it parent pid.
STEP 6: After making the sleep for the parent process for 10 seconds print it pid.
STEP 7: Stop the execution.
PROGRAM:
void main()
{
int pid;
pid=fork();
if (pid==0)
{
printf(\n Child Process\n);
printf(\n Child Process id is %d ,getpid());
printf(\n Its parent process id is %d,getppid());
sleep(5);
printf(Child process after sleep=5\n);
printf(\n Child Process id is %d ,getpid());
printf(\n Its parent process id is %d,getppid());
}
else
{
printf(\nParent process);

21

sleep(10);
printf(\n Child Process id is %d ,getpid());
printf(\n Its parent process id is %d,getppid());
printf(\nParent terminates\n);
}
}
OUTPUT:
$ cc sleepid.c
$ a.out
parent process
child process
child process id is 12691
its parent process id is 12690
child process after sleep=5
child process id is 12691
its parent process id is 12690
child process after sleep=10
child id is 12690
parent id is 11383
parent terminates
$
RESULT:
Thus the program was executed and verified successfully.

22

Ex.No:3e
Date:

SIGNAL HANDLING

AIM:
To write a program for signal handling in UNIX.
ALGORITHM:
STEP 1:start the program
STEP 2:Read the value of pid.
STEP 3:Kill the command surely using kill-9 pid.
STEP 4:Stop the program.
PROGRAM:
echo program for performing KILL operations
ps
echo enter the pid
read pid
kill-9 $pid
echo finished
OUTPUT:
$sh kill.sh
program for performing KILL operations
PID CLS PRI TTY

TIME COMD

858

TS

70

pts001

0:00

ksh

858

TS

70

pts001

0:00

sh

858

TS

59

pts001

0:00

ps

enter the pid


872
killed
$sh kill.sh
program for performing KILL operations
PID CLS PRI TTY
858

TS

70

pts001

TIME COMD
0:00

ksh

23

858

TS

70

pts001

0:00

sh

858

TS

59

pts001

0:00

ps

enter the pid


876
UX:kill(kill.sh):ERROR: no such proccess
$sh kill.sh
program for performing KILL operations
PID CLS PRI TTY

TIME COMD

858

TS

70

pts001

0:00

ksh

858

TS

70

pts001

0:00

sh

858

TS

59

pts001

0:00

ps

enter the pid


858
finished
RESULT:
Thus the program was executed and verified successfully.

24

Ex.No:3f
Date:

WAIT COMMAND

AIM:
To perform wait command using c program.
ALGORITHM:
STEP 1:Start the execution
STEP 2:Create process using fork and assign it to a variable
STEP 3:Check for the condition pid is equal to 0
STEP 4:If it is true print the value of i and teriminate the child process
STEP 5:If it is not a parent process has to wait until the child teriminate
STEP 6:Stop the execution
PROGRAM:
int i=10;
void main()
{
int pid=fork();
if(pid==0)
{
printf(initial value of i %d \n ,i);
i+=10;
printf(value of i %d \n ,i);
printf(child terminated \n);
}
else
{
wait(0);
printf(value of i in parent process %d,i);
}
}
OUTPUT:
$cc wait.c

25

$a.out
initial value of i 10
value of i 20
child teriminated
value of i in parent process 10$
RESULT:
Thus the program was executed and verified successfully.

26

ExNo:4a
Date:

READING FROM A FILE

AIM:
To create the file,read data from the file,update the file.
ALGORITHM:
1.Get the data from the user.
2.Open a file.
3.Read from the file.
4.Close the file.
PROGRAM:
#include<stdio.h>
int main()
{
char str[100];
FILE *fp;
fp=fopen("file1.dat","r");
while(!feof(fp))
{
fscanf(fp,"%s",str);
printf(" %s ",str);
}
fclose(fp);
}
OUTPUT:
$ vi read1.c
$gcc read1.c
$ ./a.out
hai this is a program to read the content of the file.
RESULT:

Thus C program to write data into a file was executed successfully

27

ExNo:4b
Date

WRITING INTO A FILE

AIM:
To write a C program to write the data into a file.
ALGORITHM:
Step1.Get the data from the user.
Step2.Open a file.
Step3.Write the data from the file.
Step4.Get the data and update the file.
PROGRAM:
#include<stdio.h>
int main()
{
char str[100];
FILE *fp;
printf("Enter the string");
gets(str);
fp=fopen("file1.dat","w+");
while(!feof(fp))
{
fscanf(fp,"%s",str);
}
fprintf(fp,"%s",str);
}
OUTPUT:
$ gcc write.c
$ ./a.out
Enter the string: os lab
$vi file1.dat
os lab
RESULT:

Thus C program to write data into a file was executed successfully.

28

ExNo:4c

FILE CREATION

Date:
AIM:
To write a C program to create a file.
ALGORITHM:
Step1:Start the program.
Step2:Create the file using create function and assign a variable to it.
Step3:If the value of the variable is less then print file cannot be created ,otherwise
print file is created.
Step4:Stop the program.
PROGRAM:
void main()
{
int id;
if(id=creat(z.txt,0)==-1)
{
printf(cannot create the file);
exit(1);
}
else
{
printf(file is created);
exit(1);
}
}
OUTPUT:
$ cc fc.c
$ a.out
file is created $
RESULT:
Thus the C program to create a file was executed successfully.

29

Pa
ge
No
.:

Expt. No : 5a
Date
:
Simulation of UNIX Commands ls
Aim:
To write a C program to simulate the operation of ls UNIX command.

Algorithm:
1. Create a variable dir of type DIR.
2. Create a pointer variable dnt of type dirent.h
3. During execution. If there are no command line arguments, show error message and
exit.
4. Else, open a directory as the path specified through command line argument.
5. If that directory has returned NULL value, print Directory not Found
6. Else, read from the directory till it comes to an end and display the list of files in it.

Pa
ge
N
o.
:

Program:
#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
#include<sys/stat.h>
int main(int argc,int *argv[])
{
DIR *dir;
struct dirent *dnt;
printf("\n\n\t\t\tSimulation of ls command\n");
if(argc!=2)
{
printf("\nUsage: ./a.out dirName\n");
exit(1);
}
dir=opendir(argv[1]);
if(dir==NULL)
printf("\nDirectory not found\n");
else
{
while((dnt=readdir(dir))!=NULL)
printf("\n%s",dnt->d_name);
}
close(dir);
}

30

Output:
./a.out
Simulation of ls command
Sun
Moon
Car
bike

Result:
Thus the program for simulating the operation of ls UNIX command was implemented.

31

Ex. No:5b
Date:

IMPLEMENTATION OF GREP COMMAND

AIM:
To write a C program to simulate the operation of grep command in Unix.
ALGORITHM:
1.Obtain the required pattern to be searched and filename from the user.
2.Open the file and read the constant used by word till the end of file.
3.Match the given pattern with the read word and if it matches,display the line of occurrence.
4.Do this till end of file is reached.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main(intargv,char *args[])
{
FILE *f;
charstr[100];
char c;
int I,flag,j,m,k;
charargs[]=HI;
char temp[30];
if(argv<3)
{
}
printf(usage grep<s><val.txt>\n);
return;
f=fopen(args[2],r);
while(!feof(f))
{
i=0;
while(1)
{
fscanf(f,%c,&c);
if(feof(f))
{
str[i++]=\0;
break;

32

}
if(c==\n)
{
str[i++]=\0;
break;
}
str[i++]=c;
}
if(strlen(str)>=strlen(args[1]))
for(k=0;k<=strlen(str)-strlen(args[1]);k++)
{
for(m=0;m<strlen(args[1];m++)
}
}
temp[m]=str[k+m];
temp[m]=\0;
if(strcmp(temp,args[1])==0)
{
printf(%s\n,str);
break;
}
return 0;
}
OUTPUT:
Usage:grep<pattern><filename>
$cc.ff.c
Printf(\n\n\t simulation of ls command);
Printf(\n usage:.a/a.out dirname/n);
Printf(\n directory not found\n);
RESULT:
Thus the program for implementing the grep command was executed and the output
was verified successfully.

33

Page No. :

Expt. No : 6a
Date
:
CPU SCHEDULING-FIFO
Aim:
To write a C program for scheduling the CPU process in the FIFO order.

Algorithm:
1) Declare a structure containing process id,process execution time, process waiting time
and turn around time of process
2) Declare necessary variables
3) Get the number of processes to be scheduled in the CPU from the user
4) Get the execution time of each process from user
5) Initialize total waiting time and total turn around time for all process together
6) Calculate waiting time and turn around time of each process, total waiting time and
TAT for all process as follows:
i) Current process = Waiting time of previous process + Execution time of
previous process
ii) Turn around time of current process = Waiting time of current process +
Execution time of current process
iii) Total waiting time = previous total + Execution time of current process
iv) Total turn around time = previous total + current process Turn Around Time
7) Divide Total Waiting Time by number of process to get the Average Waiting Time
8) Divide Total Turn Around Time by number of process to get the average TAT
Page No. :
9) Display Process id, Execution Time, Waiting time & Turn around time for each process
10) Display Average Waiting Time and Average Turn Around Time for all process

Program:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int extime;
int wtime;
int tatime;
}p[10];
void main()
{
34

int i,,n,totwt,tottat;
float avgwt,avgtat;
clrscr();
printf("\t\tFIFO scheduling\n");
printf("\n\nEnter the number of processes:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\n\nEnter the execution time (burst time) of the process %d:\t",i);
scanf("%d",&p[i].extime);
}
p[1].wtime=totwt=0;
p[1].tatime=tottat=p[1].wtime;
for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].extime;
p[i].tatime=p[i].wtime+p[i].extime;
totwt=totwt+p[i].wtime;
tottat+=p[i].tatime;
}
avgwt=totwt/n;
avgtat=tottat/n;
printf("\n\nProcess Number Execution Time Wait Time Turn Around Time\n");
for(i=1;i<=n;i++)
{
printf("\n");
printf("%d\t\t%d\t\t%d\t\t
%d\t\t",p[i].pid,p[i].extime,p[i].wtime,p[i].tatime);
}
printf("\n\nAverage wait time:\t\t%f",avgwt);
printf("\n\nAverage turn around time:\t%f",avgtat);
getch();
}

Output:
./a.out
FCFS SCHELDULING
Enter the number of process:3
Enter the execution time(burst time) of the process1:24
Enter the execution time(burst time) of the process2:3
Enter the execution time(burst time) of the process3:3
Process no
execution time
wait time
turn around time
1
24
0
24
2
3
24
27
3
3
27
30
35

Avrerage waiting time:17


Average turn around time:27

Result:
Thus the program for scheduling in FIFO order is written and executed
successfully.

36

ExNo:6b

SHORTEST JOB FIRST

Date :
AIM:
To write a C program to implement the CPU scheduling algorithm for Shortest job
first.
ALGORITHM:
Step1:Get the number of process.
Step2:Get the id and service time for each process.
Step3:Initially the waiting time of first short process as 0 and total time of first
short is process the service time of that process.
Step4:Calculate the total time and waiting time of remaining process.
Step5:Waiting time of one process is the total time of the previous process.
Step6:Total time of process is calculated by adding the waiting time and service
time of each process.
Step7:Total waiting time calculated by adding the waiting time of each process.
Step8:Total turn around time calculated by adding all total time of each process.
Step9:calculate average waiting time by dividing the total waiting time by total
number of process.
Step10:Calculate average turn around time by dividing the total waiting time by
total number of process.
Step11:Display the result.
PROGRAM:
#include<stdio.h>
struct ff
{
int pid,ser,wait;
}p[20];
struct ff tmp;
main()
{
int i,n,j,tot=0,avwait,totwait=0,tturn=0,aturn;
printf("enter the number of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process id");
37

scanf("%d",&p[i]);
printf("enter service time");
scanf("%d",&p[i].ser);
p[i].wait=0;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].ser>p[j].ser)
{
tmp=p[i];
p[i]=p[j];
p[j]=tmp;
}
}
}
printf("PID\tSER\tWAIT\tTOT\n");
for(i=0;i<n;i++)
{
tot=tot+p[i].ser;
tturn=tturn+tot;
p[i+1].wait=tot;
totwait=totwait+p[i].wait;
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].ser,p[i].wait,tot);
}
avwait=totwait/n;
aturn=tturn/n;
printf("TOTAL WAITING TIME :%d\n",totwait);
printf("AVERAGE WAITING TIME : %d\n",avwait);
printf("TOTAL TURNAROUND TIME :%d\n",tturn);
printf("AVERAGE TURNAROUND TIME:%d\n",aturn);
}
OUTPUT :
[root@lab1cab5 home]# ./a.out
enter the number of process4
enter process id701
enter service time6
enter process id702
enter service time4
enter process id703
enter service time8
enter process id704
enter service time1
PID SER WAIT TOT

38

704
1
0
1
702
4
1
5
701
6
5
11
703
8
11
19
TOTAL WAITING TIME :17
AVERAGE WAITING TIME : 4
TOTAL TURNAROUND TIME :36
AVERAGE TURNAROUND TIME:9
Result:
Thus the C program to implement the CPU scheduling algorithm for shortest job first
was executed successfully.

39

Page
No.
:

Expt. No : 7a
Date
:
Priority based CPU Scheduling
Aim:
To write a C program for scheduling the CPU processes in the Priority order.

Algorithm:
1. Declare structure with process id, process execution time, process waiting time, turn
around time of process.
2. Declare the necessary variables.
3. Get the number of processes to be scheduled in the CPU from the user.
4. Get execution time of each process from each user.
5. Sort in ascending order based on the execution time.
6. Initialize total waiting time, total turn around time.
7. Calculate waiting time, turn around time, total waiting time, total average time.
i.
Waiting Time of Current process
= Waiting time of previous process +
Execution time of previous process.
ii.
Turn Around time of current process = Waiting time of current process +
Execution time of current process.
iii. Total Waiting Time
= Previous total + Execution time of current
process.
iv. Total Turn Around Time
= Previous total + Current process Turn
around time.
8. Divide total waiting time by number of processes to get average waiting time.
9. Divide turn around time by number of processes to get average Turn around time.
10. Display process id, its Execution time, its Waiting time, and its Turn around time for
each process.
11. Display average waiting time and average turn around time for all process.

40

P
a
g
e

Program:
#include<stdio.h>
#include<conio.h>

N
o
.
:

struct process
{
int pid;
int prty;
int extime;
int wtime;
int tatime;
}p[10];
void main()
{
int i,j,n,temppty,temppno,tempet;
float totwt,tottat,avgwt,avgtat;
clrscr();
printf("\t\t\t\tPriority Scheduling\n\n");
printf("Enter the no of processes:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\n\nEnter the priority for the process #%d :",i);
scanf("%d",&p[i].prty);
printf("\nEnter the execution time of process #%d :",i);
scanf("%d",&p[i].extime);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].prty>p[j].prty)
{
temppty=p[i].prty;
p[i].prty=p[j].prty;
p[j].prty=temppty;
temppno=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temppno;
tempet=p[i].extime;
p[i].extime=p[j].extime;

41

p[j].extime=tempet;
}
}
}
totwt=p[1].wtime=0;
tottat=p[1].tatime=p[i].extime;

Page No. :

for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].extime;
p[i].tatime=p[i].wtime+p[i].extime;
totwt=totwt+p[i].wtime;
tottat+=p[i].tatime;
}
avgwt=totwt/n;
avgtat=tottat/n;
printf("\n\nProcess no Priority Execution Time Wait Time Turn Around
Time\n");
for(i=1;i<=n;i++)
{
printf("\n");
printf("\t%d\t%d\t%d\t\t%d\t
%d",p[i].pid,p[i].prty,p[i].extime,p[i].wtime,p[i].tatime);
}
printf("\n\nAverage Wait Time\t:\t%f",avgwt);
printf("\nAverage Turn Around Time:\t%f",avgtat);
getch();
}

Output:
./a.out
Enter the number of process:5
Enter the priority for the process#1:3
Enter the execution time of process#1:10
Enter the priority for the process#2:1
Enter the execution time of process#2:1
Enter the priority for the process#3:4
Enter the execution time of process#3:2
Enter the priority for the process#4:5
Enter the execution time of process#4:1
Enter the priority for the process#5:2
Enter the execution time of process#5:5

42

Processno priority executiontime


2
1
1
5
2
5
1
3
10
3
4
2
4
5
1
Average waiting time:8.2
Average turn around time:12.0

waittime
0
1
6
16
18

turnaroundtime
1
6
16
18
19

Result:
Thus the program for scheduling in Priority based order is written and executed
successfully.

43

P
a
g
e

Expt. No : 7b
Date
:

N
o
.
:

Round Robin CPU Scheduling


Aim:
To write a C program for scheduling the CPU processes in Round Robin fashion.

Algorithm:
1. Declare necessary variables and process id, process execution time process Waiting
time and Turnaround time of process.
Page No. :
2. Get the number of process to be scheduled in CPU from user.
3. Get the Time Quantum for each process.
4. Get the process number and execution time of each process from the user.
5. Calculate the total execution time of all processes.
6. If the execution time of each process is less than or equal to time quantum the
turnaround time is equal to the execution time of previous process.
7. Else the execution time of current process is the difference between the execution
time and sum of time quantum.
8. The waiting time of current process is the difference between the turn around time
of current process.
9. Assign t1 as the sum of turnaround time of all processes.
10. Assign the sum of waiting time of all processes to t2.
11. Divide total waiting time by no of processes to get average waiting time.
12. Divide total turnaround time by no of processes to get average turnaround time.
13. Display process id, execution time, turnaround time and waiting time for each
process.
14. Display average turnaround time and average waiting time of all processes

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,ch,t,tq,pid[50],extime[50],totextime=0;
int p,p1,tt[50],wt[50],r[50],t1,t2,att,awt;
clrscr();
printf("\n\t\t\tROUND ROBIN SCHEDULING\n\n");
printf("\nEnter the Time Quantum:");
scanf("%d",&tq);
44

printf("\nEnter the total number of processes:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the process number:");
scanf("%d",&pid[i]);
printf("\nEnter the CPU Burst Time:");
scanf("%d",&extime[i]);
r[i]=extime[i];
totextime+=extime[i];
}
p=t1=t2=0;
printf("\n\nGrantt Chart\n");
printf("------------\n");
do
{
for(i=0;i<n;i++)
{
t=extime[i];
if(extime[i]==0)
continue;

Page No. :

if(extime[i]<=tq)
{
p1=p;
p+=extime[i];
printf("\nFrom %d to %d process %d
completed\n",p1,p,i+1);
tt[i]=p;
extime[i]=0;
}
else
{
p1=p;
p+=tq;
printf("\nFrom %d to %d process %d
waiting...\n",p1,p,i+1);
extime[i]=t-tq;
}
}
}while(p!=totextime);
printf("\n Process No Turn Around Time Waiting Time\n");

45

printf(" ----------------------------------------\n");
for(i=0;i<n;i++)
{
wt[i]=tt[i]-r[i];
printf("\n Process %d: \t%d\t\t%d",i+1,tt[i],wt[i]);
t1+=tt[i];
t2+=wt[i];
}
att=t1/n;
printf("\n\nAverage Turn Aroud Time (ATT) = %d",att);
awt=t2/n;
printf("\nAverage Waiting Time (AWT) = %d",awt);
getch();
}

Output:
ROUND ROBIN SCHEDULING
Enter the time quantum:3
Enter the total number of process:5
Enter the process number:1
Enter the CPU burst time:7
Enter the process number:2
Enter the CPU burst time:8
Enter the process number:3
Enter the CPU burst time:4
Enter the process number:4
Enter the CPU burst time:1
Enter the process number:5
Enter the CPU burst time:6
From 0 to 3 process 1 waiting
From 3 to 6 process 2 waiting
From 6 to 9 process 3 waiting
From 9 to 10 process 4 waiting
From 10 to 13 process 5 waiting
From 13 to 16 process 1 waiting
From 16 to 19 process 2 waiting
From 19 to 20 process 3 waiting
From 20 to 23 process 5 waiting
From 23 to 24 process 1 waiting
From 24 to 26 process 2 waiting
Processno
Process1
Process2
Process3
Process4

turn around time waiting time


24
17
26
18
20
16
10
9

46

Process5
23
Average turn around time(ATT)=20
Average waiting time(AWT)=15

17

Result:
Thus the program for process scheduling in round robin order is written and
executed successfully.

47

ExNo:8

PRODUCER CONSUMER PROBLEM USING SEMAPHORE

Date :
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
PROGRAM :
#define BUFFERSIZE 10
int mutex,n,empty,full=0,item,item1;
int buffer[20];
int in=0,out=0,mutex=1;
void wait(int s)
{
while(s<0)
{
printf(\nCannot add an item\n);
exit(0);
}
s--;
}
void signal(int s)
{
s++;
}
void producer()
{
do
{
wait (empty);
wait(mutex);
printf(\nEnter an item:);

48

scanf(%d,&item);
buffer[in]=item;
in=in+1;
signal(mutex);
signal(full);
}
while(in<n);
}
void consumer()
{
do
{
wait(full);
wait(mutex);
item1=buffer[out];
printf(\nConsumed item =%d,item1);
out=out+1;
signal(mutex);
signal(empty);
}
while(out<n);
}
void main()
{
printf(Enter the value of n:);
scanf(%d ,&n);
empty=n;
while(in<n)
producer();
while(in!=out)
consumer();
}
OUTPUT:
$ cc prco.c
$ a.out
Enter the value of n :3
Enter the item:2
Enter the item:5
Enter the item:9
consumed item=2
consumed item=5
consumed item=9
$
RESULT:
Thus the program for solving producer and consumer problem using semaphore was executed
successfully.

49

Page No. :

Expt. No : 9a
Date
:
IPC using Pipes
Aim:
To write a C program to implement Interprocess communication using Pipes.

Algorithm:
1.
2.
3.
4.
5.

Declare an array one for pipe descriptor (pd) and one for data to be sent (data).
Create a pipe using pipe().
Page No. :
If the pipe(pd<0) print Pipe cannot be created
Create a duplicate process and assign its ID to the pid
In parent process, get the typed information from the user and store it in character
array data
6. Close the read end of the pipe and write the data into the pipe.
7. Wait for the child to complete.
8. In the child process, close the right end of the pipe, read the data from the pipe and
print it.
9. Exit from the child process.
10. Parent exits after child exits.

Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
void error(const char *msg)
{
printf("%s\n",msg);
exit(1);
}
int main()
{
int pid,pd[2];
char data[100];
printf("\n\t\t\tExp 6-A IPC using Pipes\n");
printf("\n\t\t\tOpen,read & write using pipes\n");
if(pipe(pd)<0)
50

error("Pipe error\n");
pid=fork();
if(pid>0)
{
printf("Parent : Type few chars and press return to exit....\n");
scanf("%s",&data);
close(pd[0]);
write(pd[1],&data,100);
wait(0);
printf("\nParent exiting....\n\n");
}
else if(pid==0)
{
close(pd[1]);
read(pd[0],&data,100);
printf("\nChild received %s from parent",data);
printf("\nChild exiting....\n");
}
}

Output:
Open,read&write using pipes
PARENT:type few char end press return to exit
Good morning
Child received good from parent
Child existing..
Parent existing

Result:
Thus the program for Inter Process Communication using Pipes was implemented.

51

Page No. :

Expt. No : 9b
Date
:
IPC using Message Queues
Aim:
To write a C program to implement Inter process communication using Message
Queue.

Algorithm:
1.
2.
3.
4.
5.

Set the permission as 0666


Set the message key as 1234
Initialize exflag = 0.
Create a function error to display error message
Create a structure of type my_msg containing integer variable msg_type and character
array data of size 30.
6. In main() method, create a message queue and assign its ID to the variable msgid.
7. If msgid = -1 display error message Message queue not created
8. Using fork create child process and assign its ID to pid
9. If pid < 0 print Cannot fork
10. For parent process if pid > 0, create a structure variable message of type my_msg
11. get the input message from the user in the character array message.data
12. Set message.type=1
13. If msgnd(msgid,&message,sizeof(message.data),0) < 0
14. print error in sending message.
15. Else, send the message to the child through message queue.
16. Wait for the child to complete
17. In child process suspend the child until it receives message from parent
18. Receive message from the parent using msg_receive()
19. If msgrcv (msgid,&message,sizeof(message.data),0,0) < 0
20. print error in receiving data.
21. Else receive the data and print it.
22. Remove message queue and exit.

Progam:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
const int PERMS=0666;
const key_t MSGKEY=1234;
52

Page
No. :

char exflag=0;
void error(const char *msg)
{
printf("%s\n",msg);
exit(1);
}
struct my_msg
{
long int msgtype;
char data[30];
};
int main()
{
int msgid,pid;
printf("\n\t\t\tIPC using Message Queue\n\n");
msgid=msgget(MSGKEY,IPC_CREAT|PERMS);
if(msgid==-1)
error("Can't create message queue\n");
pid=fork();
if(pid<0)
{
perror("\nCan't fork\n");
exit(0);
}
if(pid>0)
{
struct my_msg message;
printf("\nParent process: Sending message to child process\n");
printf("\nType the message. Press Enter key to stop....\n");
scanf("%s",&message.data);
message.msgtype=1;
if(msgsnd(msgid,&message,sizeof(message.data),0)<0)
error("\nChild: Error in sending message\n");
printf("\nParent existing....\n");
wait(0);
}
else if(pid==0)
{
sleep(5);struct my_msg message;
printf("\nChild Process : Receiving meassage from Parent Process\n");
if(msgrcv(msgid,&message,sizeof(message.data),0,0)<1)
error("\nParent : Error in receiving data\n");

53

P
a
g
e

N
o
.
:

printf("\nReceived %s from parent\n",message.data);


msgctl(msgid,IPC_RMID,0);
printf("\nChild existing....\n");
exit(0);
}
}

Output:
IPC using message queue
Parent process:Sending message to child process
Type the message.press enter key to stop..
Hai
Parent existing..
Child process:receiving message from parent process
Received hai from parent
Child existing..

Result:
Thus the program for implement Inter Process Communication using Message Queue
was Implemented.

54

P
a
g
e

Expt. No : 10ba
Date

N
o
.
:

Memory Management I
FIRST FIT

Aim:
To write a C program to implement first fit under Memory Management policy.

Algorithm:
1.Declare a structure with the string memory address no of holes for allocation,memory
address at end of allocation.
2.Create the memory or holes for allocation a process using create() function.
3.Initialize no of blocks,block size,process.
4.Get the no of blocks from the user.
5.Set the size of each block.
6.Get the no of process and compare blocksize with the processsize.
7.If process size is less than or equal to block size,that process is allocated with block size.
8.If the process is greater check block or display process is not allowed.
9.Display the result.

Program:

Page No. :

#include<stdio.h>
main()
{
int i,n,m,ps[100],bs[150],j;
printf(enter the no of blocks\n);
scanf(%d,&n);
printf(enter the block size\n);
for(i=1;i<=n;i++)
{
printf(%d,&bs[i]);
}
printf(enter the no of process\n);
scanf(%d,&m);
for(i=1;i<=m;i++)
{
printf(enter process %d size :\n,i);
55

scanf(%d,&ps[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(ps[j]<=bs[i])
{
printf(process %d is allocated to block of siize %d \n,j,bs[i]);
ps[j]=10000;
break;
}}}
for(i=1;i<=m;i++)
{
if(ps[i]!=10000)
{
printf(\n process %d is not allowed,i+1);
}}}
Output:

Enter the no of blocks:4


Enter the block size:
1
400
2
500
3
300
4
600
Enter the no of process:4
Enter process1 size:200
Enter process2 size:250
Enter process3 size:350
Enter process4 size:550
Process 1 is allocated to block of size:400
Process 2 is allocated to block of size:500
Process 3 is allocated to block of size:600
Process 4 is not allowed.

56

Result:
Thus the first fit under Memory Management policy was implemented in c program.

57

P
a
g
e

Expt. No : 10b
Date

N
o
.
:

Memory Management I
Page Replacement - FIFO
Aim:
To write a C program to implement Page Replacement technique using FIFO under
Memory Management policy.

Algorithm:
1. Get the number of pages and their sequence from the user.
2. Get the number of available page frames from the user.
3. In FIFO, on the basics of first in first out, replace the pages respectively, then find
number of page faults occurred .
4. Compare all frames with incoming page
- If the incoming page is already available in page frame, set the match flag to
indicate no need of page replacement.
- If the incoming page is not available in all frames, then remove the page which
is loaded into the memory long back and give space for new incoming page.
- Increment the number of Page faults counter
5. Print the number of page faults.
6. Stop the program

58

Program:

Page No. :

#include <stdio.h>
#include <conio.h>
void main()
{
int pgseq[15], totnop, frseq[4];
int nfr, i, j, pgflt = 0;
int q, match, frindex = 0;
clrscr();
printf(\n\n\n\t\t\tPage Replacement FIFO\n);
printf("\n Enter the total number of Pages referred : \n");
scanf("%d", &totnop);
printf("\n Enter the page sequence\n");
for (i=0; i<totnop; i++)
scanf("%d", &pgseq[i]);
printf ("\nEnter the total number of Frames Available : \n");
scanf("%d", &nfr);
for (i=0;i<nfr; i++)
frseq[i] = pgseq[i];
for (i=nfr;i<totnop; i++)
{
match = 0;
printf("For page %d : \t", pgseq[i]);
for (j=0; j < nfr; j++)
if (frseq[j] == pgseq[i])
{
match = 1;
printf( "No need of Page Replacement\n");
}
if (match == 0)
{
frseq[frindex] = pgseq[i];
frindex = (frindex + 1) % nfr;
pgflt ++;
for (q=0; q<nfr; q++)
printf ("%d\t", frseq[q]);
}
printf ("\n");
}
printf ("\n The total no. of Page Faults = %d\n",pgflt);
getch();
}

Output:
Enter the total no of pages refered:16
59

Enter the page sequence:


1974563147712322
For page 4
For page 5
For page 6
For page 3
For page 1
For page 4
For page 7
For page 7
For page 1
For page 2
For page 3
For page 2
For page 2

4 9 7
4 5 7
4 5 6
3 5 6
3 1 6
3 1 4
7 1 4
no need for page replacement
no need for page replacement
7 2 4
7 2 3
no need for page replacement
no need for page replacement

Result:
Thus the Page Replacement technique using FIFO under Memory Management policy
was implemented in C.

60

ExNo:11

Implementation of memory management scheme-11 Paging

Date :
AIM:
To write a C program for memory allocation with pages.
ALGORITHM:
1.Start the program.
2.Initialize the variable pageno,offset,phymem,framesize.
3.Get the size of physical memory to get the frame along with pageno.
4.Get the sizes of page or frame.
5.Specify the no of frames available in memory.
6.Specify the pageno and offset value to find out the physical address of the specified page.
7.End the program.
PROGRAM :
#include<stdio.h>
void main()
{
int i,arr[100],pagetable[20],val,pageno,offset,nof,phymem,framesize;
int noframes;
printf(enter the size of the physical memory);
scanf(%d,&phymem);
for(i=1;i<=phymem;i++)
arr[i];
printf(enter the size of the framesize);
scanf(%d,&framesize);
nof=phymem/framesize;
printf(the no of frames available are %d,nof);
printf(\n enter the page table\n);
printf(pageno\tframeno\n);
for(i=1;i<=nof;i++)
{
printf(\n%d\t,i);
scanf(%d,&pagetable[i]);
}
printf(enter the pagenumber:);
scanf(%d,&pageno);
printf(enter the offset);
scanf(%d,&offset);
val=(framesize*pagetable[pageno])+offset;
printf(the physical address is %d,arr[val]);
}
OUTPUT:
Enter the size of physical memory:64

61

Enter the size of framesize:8


The no of frames available are:8
Enter the page table:
Pageno frameno
1
5
2
8
3
9
4
4
5
2
6
6
7
4
8
5
Enter the page number:5
Enter the offset:6
The physical address is:22
RESULT:
Thus the program for memory allocation with pages was executed successfully.

62

ExNo:12

CONTIGUOUS FILE ALLOCATION

Date :
AIM:
To implement file allocation technique for contiguous file allocation sysyem.
ALGORITHM:
1.Start the program.
2.Declare a structure with length of filename,length and string address.
3.Get the number of files.
4.Get filename,starting address,length of files,till the limit attain no of files to block according
to the length of the file
5.Allocates file to block according to the length of the file.
6.And allocate block to each other.
7.Display the result.
8.End the program.
PROGRAM :
#include<stdio.h>
struct file
{
char fname[30];
int length,start;
}s[100];
int main()
{
int a=0,i,j,f;
printf(\n enter the no of files:);
scanf(%d,&f);
for(i=0;i<f;i++)
{
printf(\n enter the filename:);
scanf(%s,&s[i].fname);
printf(\n enter the starting address);
scanf(%d,&s[i].start);
printf(\n enter the length of the file:);
scanf(%d,&s[i].length);
}
for(i=0;i<f;i++)
{
printf(\nfilename%s\t starting address%d\t length%d\n,s[i].fname,s[i].start,s[i].length);
printf(\n block address are\n);
a=s[i].start;

63

for(j=0;j<s[i].length;j++)
{
printf(%d\n,a);
a++;
}
}
}
OUTPUT:
Enter the no of files:
Enter the filename:
Enter the starting address:
Enter the length of the file:
Enter the filename:
Enter the starting address:
Enter the length of the file:
Enter the filename:
Enter the starting address:
Enter the length of the file:
Filename os starting address 100 length 3
Block address are
100
101
102
RESULT:
Thus the program was executed successfully.

64

Você também pode gostar