[go: up one dir, main page]

0% found this document useful (0 votes)
17 views71 pages

Os Lab Manual PDF

The document outlines the objectives and experiments for the Operating Systems Laboratory course (CS8461), focusing on Unix commands, CPU scheduling algorithms, process creation, inter-process communication, deadlock avoidance, and memory management techniques. It includes a detailed list of experiments, including basic Unix commands, shell programming, CPU scheduling algorithms, and various memory allocation methods. Additionally, it provides specific tasks and examples for using Unix commands and shell programming operations.

Uploaded by

atchayap14
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)
17 views71 pages

Os Lab Manual PDF

The document outlines the objectives and experiments for the Operating Systems Laboratory course (CS8461), focusing on Unix commands, CPU scheduling algorithms, process creation, inter-process communication, deadlock avoidance, and memory management techniques. It includes a detailed list of experiments, including basic Unix commands, shell programming, CPU scheduling algorithms, and various memory allocation methods. Additionally, it provides specific tasks and examples for using Unix commands and shell programming operations.

Uploaded by

atchayap14
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/ 71

SUB CODE: CS8461

SUB NAME: OPERATING SYSTEMS


LABORATORY
CS8461 OPERATING SYSTEMS LABORATORY

OBJECTIVES

To learn Unix commands and shell programming

To implement various CPU Scheduling Algorithms

To implement Process Creation and Inter Process Communication.

To implement Deadlock Avoidance and Deadlock Detection Algorithms

To implement Page Replacement Algorithm

LIST OF EXPERIMENTS

1. Basics of UNIX commands

2. Write programs using the following system calls of UNIX operating system

fork, exec, getpid, exit, wait, close, stat, opendir, readdir

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

4. Shell Programming

5. Write C programs to implement the various CPU Scheduling Algorithms

6. Implementation of Semaphores

7. Implementation of Shared memory and IPC

8. Bankers Algorithm for Deadlock Avoidance

9. Implementation of Deadlock Detection Algorithm

10. Write C program to implement Threading & Synchronization Applications

11. Implementation of the following Memory Allocation Methods for fixed

partition a) First Fit b) Worst Fit c) Best Fit

12. Implementation of Paging Technique of Memory Management

13. Implementation of the following Page Replacement Algorithms a) FIFO b)

LRU c) LFU

14. Implementation of the various File Organization Techniques

15. Implementation of the following File Allocation Strategies a) Sequential b) Indexed c) Linked
Ex No. 1. BASIC COMMANDS IN LINUX

Commands: The following are the commands to be studied:

1. date

i. date +%m

ii. date +%h

iii. date +%d

iv. date +%y &date +%Y

v. date +%H

vi. date +%M

vii. date +%S

2. cal

i. cal year

3. echo “message”

4. who

i. whoami

5. mkdir

6. cd directory name

7. rmdir directory name

8. cd

9. ls

10. cat>filename

11. cat filename

12. cp source file destination file

13. mv source file destination file

14. cut -c column number file name

15. rm file name


16. grep

17. chmod

18. sort file name

19. wc file name

20. man

21.clear

Explanation for the above commands is given below.

1. TASK : To display the system date and time

COMMAND : date

SYNTAX : date

EXPLANATION: This command displays the current system date and time on the screen.

OUTPUT :

[exam1@local host ~]$date

Thu Nov 27 10:37:05 IST 2013

i. TASK : To display the current month.

COMMAND : date

SYNTAX : date +%m

EXPLANATION: This command displays the current month on the screen.

OUTPUT :

[exam1@local host ~]$date +%m

11

ii. TASK : To display the name of the current month.

COMMAND : date

SYNTAX : date +%h

EXPLANATION: This command displays the name of the current month on the screen

OUTPUT :
[exam1@local host ~]$date +%h

Nov

iii. TASK : To display the current system date.

COMMAND : date

SYNTAX : date +%d

EXPLANATION: This command displays the current system date on the screen.

OUTPUT :

[exam1@local host ~]$date +%d

27

iv. TASK : To display the current system (year)

COMMAND : date

SYNTAX : date +%y & date +%Y

EXPLANATION: This command displays the current system year on the screen.

OUTPUT :

[exam1@local host ~]$date +%y

13

[exam1@local host ~]$date +%Y

2013

v. TASK : To display the current system time.

COMMAND : date

SYNTAX : date +%H

EXPLANATION: This command displays the current system time (in hour) on the screen.

OUTPUT :

[exam1@local host ~]$date +%H

10

vi. TASK : To display the current system time.

COMMAND : date
SYNTAX : date +%M

EXPLANATION: This command displays system time (in min) on the screen.

OUTPUT :

[exam1@local host ~]$date +%M

49

vii. TASK : To display the current system time.

COMMAND : date

SYNTAX : date +%S

EXPLANATION: This command displays the current system time (in sec) on the screen.

OUTPUT :

[exam1@local host ~]$date +%S

50

2. TASK : To display the calendar of the current month.

COMMAND : calendar

SYNTAX : cal (or)cal -1

EXPLANATION: This command displays the calendar of the current month on the screen.

-1 Display single month output. (This is the default.)

-3 Display prev/current/next month output.

-s Display Sunday as the first day of the week. (This is the

default.)

-m Display Monday as the first day of the week.

-j Display Julian dates (days one-based, numbered from January 1).

-y Display a calendar for the current year.

OUTPUT :

[exam1@local host ~] $ cal

November 2013
SuMo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

[exam1@localhost ~]$ cal -3 9 2013

August 2013 September 2013 October 2013

SuMo Tu We Th Fr Sa SuMo Tu We Th Fr Sa SuMo Tu We Th Fr Sa

1 2 3 1 2 3 4 5 6 7 1 2 3 4 5

4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12

11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19

18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26

25 26 27 28 29 30 31 29 30 27 28 29 30 31

i. TASK : To display the calendar of a year.

COMMAND : cal

SYNTAX : cal year

EXAMPLE : cal 2007

EXPLANATION: This command displays the calendar of the specified year.

OUTPUT :

[exam88@local host ~] $ cal 2012

It displays all the month of the year 2012

3. TASK : To display the user defined message

COMMAND : echo

SYNTAX : echo “message”

EXPLANATION: This command display on the screen the argument of the echo command.
OUTPUT :

[exam1@local host ~] $ echo “Hello”

Hello

4. TASK : To display the detail of all users.

COMMAND : who

SYNTAX : who

EXPLANATION : This command lists the information about all the users who have logged on that
system.

OUTPUT :

[exam88@local host ~] $ who

exam80 pts/18 2013-01-23 14.48 <172.17.3.51>

exam18 pts/12 2013-01-23 14.39 <172.17.3.51>

i. TASK : To display the user detail.

COMMAND : who

SYNTAX :whoami

EXPLANATION : This command displays the information about the current user of the
system on the screen.

OUTPUT :

[exam88@local host ~] $ whoami

exam88

5. TASK : To create directory.

COMMAND : make directory

SYNTAX :mkdir

EXPLANATION : This command is used to create a new directory with a specified name.

EXAMPLE : mkdir student

OUTPUT :
[exam88@local host ~] $ mkdir sun

[exam88@local host ~] $ ls

Palindrome.c palindrome.sh

Morn.text good sun

6. TASK : To change directory

COMMAND : change directory

SYNTAX : cd directory name

EXAMPLE : cd sun

EXPLANATION : This command is used to switch from one directory to another.

OUTPUT :

[exam88@local host ~] $ cd sun

[exam88@local host sun] $

7. TASK : To come out of sub-directory.

COMMAND : change directory

SYNTAX : cd

EXPLANATION : This command help in switching to the main directory.

OUTPUT :

[exam88@local host sun] $ cd

[exam88@local host ~] $

8. TASK : To delete a directory.

COMMAND : remove directory

SYNTAX : rmdir directory name

EXPLANATION : This command is used to delete the specified directory.

OUTPUT :
[exam88@local host ~] $ rmdir sun

[exam88@local host ~] $ cd sun

No such file or directory.

9. TASK : To list all the files and directories.

COMMAND : list

SYNTAX :ls

EXPLANATION : This command display all the files and directories of the system.

OUTPUT :

[exam88@local host ~] $ ls

Palindrome.c palindrome.sh

Morn.text good

10. TASK : To create a file.

COMMAND : cat

SYNTAX : cat > file name

EXAMPLE :cat> wind.txt

EXPLANATION : This command leads to the creation of a new file with the specified filename
content.

OUTPUT :

[exam88@local host ~] $ cat> wind.txt

Hai good morning

Have a Nice Day

1]+ stopped cat > wind.txt

[exam88@local host ~] $ cat> moon.txt

Hello how are you.

Take care of ur Health. (Press Ctrl+D to exit)

2]+ stopped cat > moon.txt


11. TASK : To view a file

COMMAND : cat

SYNTAX : cat filename

EXAMPLE : cat wind.txt

EXPLANATION: This command displays the content of the specified files.

OUTPUT :

[exam88@local host ~] $ cat wind.txt

Hai good morning

Have a Nice Day

12. TASK : To copy a file

COMMAND : copy

SYNTAX : cp source file destination file

EXAMPLE : cp wind.txt moon.txt

EXPLANATION: This command produces a copy of the source file to be stored in the specified
destination file of overwriting its previous content if exist.

OUTPUT :

[exam88@local host ~] $ cp wind.txt moon.txt

[exam88@local host ~] $ cat moon.txt

Hai good morning

Have a Nice Day

EXPLANATION: This command produces a copy of the source file to be stored in the specified
destination file(by creating it) .

[exam88@local host ~] $ cp wind.txt star.txt

[exam88@local host ~] $ cat star.txt

Hai good morning

Have a Nice Day


13. TASK : To move a file.

COMMAND : move

SYNTAX : mv source file destination file

EXAMPLE : mv wind.txt moon.txt

EXPLANATION : This command moves the content of the source file to destination file. The
content of the source file is deleted.

OUTPUT :

[exam88@local host ~] $ mv wind.txt moon.txt

[exam88@local host ~] $ cat wind.txt

No such file or directory.

14. TASK : To cut a column from a file.

COMMAND : cut

SYNTAX : cut -c column number filename

EXAMPLE : cut –c 5-10 star.txt

EXPLANATION : This command displays the character of particular column in the specified field.

OUTPUT :

[exam1@localhost ~]$ cut -c 5-10 star.txt

good m

a Nic

15. TASK : To delete a file.

COMMAND : remove

SYNTAX :rm filename

EXAMPLE : rm star.txt

EXPLANATION : This command deletes the specified file from the directory.

OUTPUT :

[exam88@local host ~] $ rm star.txt


[exam88@local host ~] $ cat star.txt

No such file or directory.

16. TASK : This command prints the line matching the pattern. grep(globally search
a regular expression and print)

COMMAND : grep

SYNTAX : grep „string‟ filename

EXAMPLE : grep „stat‟ stat1.c

OUTPUT :

[exam1@CSELAB1 ~]$ grep 'stat' stat1.c

#include<sys/stat.h>

struct stat st;

s=stat("fork1.c",&st);

printf("\n use of stat() is unsuccessful ");

17. TASK :To change file access permission.

COMMAND : chmod

EXPLANATION: To change the permission of a file.

18. TASK : To sort the content

COMMAND : sort

SYNTAX : sort file name

EXAMPLE : sort moon.txt

EXPLANATION: This command helps in sorting the content of a file in ascending order.

OUTPUT :

[exam88@local host ~] $ sort moon.txt

Hai good morning

Have a Nice Day


19. TASK : To display no.of characteristics in a file.

COMMAND : word count

SYNTAX : wc file name

EXAMPLE : wc moon.txt

EXPLANATION : This command displays the no.of rows, words and character in the file.

 wc -l <filename> print the line count

 wc -m <filename> print the character count

 wc -L <filename> print the length of longest line

 wc -w <filename> print the word count

OUTPUT :

[exam88@local host ~] $ wc –l –w –m moon.txt (or)

[exam88@local host ~] $ wc moon.txt

2 7 33 moon.txt

20.TASK : To display the manual pages.

COMMAND : man

SYNTAX : man command

EXAMPLE : man cat

EXPLANATION : This command formats and displays the online manual pages.

21. TASK : To clear the screen.

COMMAND : clear

Note:

EXPLANATION : This command is useful for creating or editing programs.

SYNTAX : vi filename

EXAMPLE : vi fork.c
Ex.No.2 SHELL PROGRAMMING

Assume that a =10 and b=20

Arithmetic Operators:

Operator Description Example

Addition - Adds values on either side of the


+ `expr $a + $b` will give 30
operator

Subtraction - Subtracts right hand operand from


- `expr $a - $b` will give -10
left hand operand

Multiplication - Multiplies values on either side


* `expr $a \* $b` will give 200
of the operator

Division - Divides left hand operand by right


/ `expr $b / $a` will give 2
hand operand

Modulus - Divides left hand operand by right


% `expr $b % $a` will give 0
hand operand and returns remainder

Assignment - Assign right operand in left


= a=$b would assign value of b into a
operand

Equality - Compares two numbers, if both are


== [ $a == $b ] would return false.
same then returns true.

Not Equality - Compares two numbers, if both


!= [ $a != $b ] would return true.
are different then returns true.

Relational Operators:

Operator Description Example

Checks if the value of two operands are equal or


-eq [ $a -eq $b ] is not true.
not, if yes then condition becomes true.

-ne Checks if the value of two operands are equal or [ $a -ne $b ] is true.
not, if values are not equal then condition becomes
true.

Checks if the value of left operand is greater than


-gt the value of right operand, if yes then condition [ $a -gt $b ] is not true.
becomes true.

Checks if the value of left operand is less than the


-lt value of right operand, if yes then condition [ $a -lt $b ] is true.
becomes true.

Checks if the value of left operand is greater than or


-ge equal to the value of right operand, if yes then [ $a -ge $b ] is not true.
condition becomes true.

Checks if the value of left operand is less than or


-le equal to the value of right operand, if yes then [ $a -le $b ] is true.
condition becomes true.

Note:All the conditional expressions would be put inside square braces with one space around them

[ $a == $b ] is correct where as [$a==$b] is incorrect

[ $a <= $b ] is correct where as [$a <= $b] is incorrect.

Boolean Operators:

Operator Description Example

This is logical negation. This inverts a true


! [ ! false ] is true.
condition into false and vice versa.

This is logical OR. If one of the operands is true


-o [ $a -lt 20 -o $b -gt 100 ] is true.
then condition would be true.

This is logical AND. If both the operands are true


-a then condition would be true otherwise it would be [ $a -lt 20 -a $b -gt 100 ] is false.
false
Ex.No. 2a EVEN OR ODD NUMBER

PROGRAM:

echo "ENTER THE NUMBER"

read n

r=$(($n%2))

if [ $r -eq 0 ]

then

echo "EVEN"

else

echo "ODD"

fi

OUTPUT:

[exam1@localhost ~]$ sh ex01.sh

ENTER THE NUMBER

450

EVEN

[exam1@localhost ~]$
Ex.No.2b LINUX USING ECHO COMMAND

PROGRAM:

echo "1.WHO AM I?"

echo "2.WHO IS LOGGED ON?"

echo "3.DATE"

read n

case $n in

1) whoami;;

2) who;;

3) date;;

esac

OUTPUT:

[exam1@localhost ~]$ sh ex02.sh

1.WHO AM I?

2.WHO IS LOGGED ON?

3.DATE

exam1

[exam1@localhost ~]$
Ex.No.2c FACTORIAL OF A NUMBER

PROGRAM:

echo “FACTORIAL OF A GIVEN NUMBER”

echo "ENTER THE NUMBER"

read num

i=1

fact=1

while [ $i -le $num ]

do

fact=$(($fact*$i))

i=$(($i+1))

done

echo "FACTORIAL OF $num IS $fact"

OUTPUT:

[exam1@CSELAB1 ~]$ sh ex3.sh

ENTER THE NUMBER

FACTORIAL OF 5 IS 120
Ex.No.2c GREATEST OF THREE NUMBERS

PROGRAM:

echo "ENTER THE VALUES:"

read a b c

if [ $a -gt $b -a $a -gt $c ]

then

echo "$a IS GREATEST"

elif [ $b -gt $c ]

then

echo "$b IS GREATEST"

else

echo "$c IS GREATEST"

fi

OUTPUT:

[exam1@localhost ~]$ sh ex04.sh

ENTER THE VALUES:

23 98 -1

98 IS GREATEST
Ex.No.3a IMPLEMENTATION OF FCFS SCHEDULING

PROGRAM:
#include<stdio.h>
#define MAX 10
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// main program
int main()
{
int i,j,n,totwt,tottt;
float avgwt,avgtt;
int prevtt;
printf("\n\t\t First Come First Serve \n");
printf("\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~ \n");
printf("\n\t Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\tEnter burst time:");
scanf("%d",&pr[i].brt);
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ~~~~~~~~~~ \t ~~~~~~~~~~ \t ~~~~~~~~~~~~ \t ~~~~~~~~~~~~~~~~");
//Sum of Waiting time and Turn Aruond Time
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f",avgtt);
printf("\n\n");
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}

OUTPUT:
First Come First Serve
~~~~~ ~~~~ ~~~~~ ~~~~~
Enter the no of process:5
Process no: 1 Enter burst time:8
Process no: 2 Enter burst time:3
Process no: 3 Enter burst time:4
Process no: 4 Enter burst time:2
Process no: 5 Enter burst time:6

PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME


~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
1 8 0 8
2 3 8 11
3 4 11 15
4 2 15 17
5 6 17 23

Average Waiting time: 10.20


Average Turnaround Time: 14.80
-----------------------
0 8 11 1517 23
-----------------------
RESULT:
Ex.No.3b IMPLEMENTATION OF SJF SCHEDULING

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
printf("\n\t\t\t SHORTEST JOB FIRST");
printf("\n\n Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
}
// Sorting the Burst Time in Ascending order
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].brt> pr[j].brt)
{
t= pr[j].pro;
pr[j].pro= pr[i].pro;
pr[i].pro=t;

t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
}
}
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
//Sum of Waiting time and Turn Aruond Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f\n\n",avgtt);
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
SHORTEST JOB FIRST
Enter the no of process:4
Process no: 1
Enter burst time:6
Process no: 2
Enter burst time:8
Process no: 3
Enter burst time:7
Process no: 4
Enter burst time:3

PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME


------------- ------------- ---------- --------------
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24

Average Waiting time: 7.00


Average Turnaround Time: 13.00
------------------------
0 3 9 16 24
------------------------

RESULT:
Ex.No.3c IMPLEMENTATION OF PRIORITY SCHEDULING

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int pri;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
printf("\n\t\t\t PRIORITY SCHEDULING");
printf("\n\n Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
printf("\nEnter Priority:");
scanf("%d",&pr[i].pri);
}
// Sorting the Priority in Ascending order
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].pri> pr[j].pri)
{
t= pr[j].pro;
pr[j].pro= pr[i].pro;
pr[i].pro=t;
t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
t= pr[j].pri;
pr[j].pri= pr[i].pri;
pr[i].pri=t;
}
}
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
//Sum of Waiting time and Turn Aruond Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f\n\n",avgtt);
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
PRIORITY SCHEDULING
Enter the no of process:5
Process no: 1
Enter burst time:10
Enter Priority:3
Process no: 2
Enter burst time:1
Enter Priority:1
Process no: 3
Enter burst time:2
Enter Priority:4
Process no: 4
Enter burst time:1
Enter Priority:5
Process no: 5
Enter burst time:5
Enter Priority:2

PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME


------------- ------------- ---------- --------------
2 1 0 1
5 5 1 6
1 10 6 16
3 2 16 18
4 1 18 19

Average Waiting time: 8.20


Average Turnaround Time: 12.00
-------------------
01 6 161819
-------------------
RESULT:
Ex.No.3d IMPLEMENTATION OF RR SCHEDULING

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int remtime;
}pr[MAX];
// Main program
void main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int ts;
int totbrt;
printf("\n Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the time slice:");
scanf("%d",&ts);
//Getting Process no. and Burst Time
totbrt = 0;
for(i=1;i<=n;i++)
{
printf("\nEnter process no: %d \n", i);
pr[i].pro = i;
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
pr[i].remtime = pr[i].brt;
totbrt = totbrt + pr[i].brt;
}
//calculating Waiting time and Turn Around Time
tottt = 0;
while (1)
{
if(tottt == totbrt)
{
break;
}
for(i=1;i<=n;i++)
{
if(pr[i].remtime == 0)
{
continue;
}
if(pr[i].remtime > ts)
{
tottt = tottt + ts;
pr[i].remtime = pr[i].remtime - ts;
}
else
{
tottt = tottt + pr[i].remtime;
pr[i].remtime = 0;
}
pr[i].wt = tottt-pr[i].brt;
pr[i].tt = tottt;
}
}
//Sum of Waiting time and Turn Around Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Around Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f",avgtt);
getch();
}
}

OUTPUT:
Enter the no of process:
3
Enter the time slice:4
Enter process no: 1
Enter burst time:24
Enter process no: 2
Enter burst time:3
Enter process no: 3
Enter burst time:3
PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME
------------- ------------- ---------- --------------
1 24 6 30
2 3 4 7
3 3 7 10

Average Waiting time: 5.67


Average Turnaround Time: 15.67
RESULT:
Ex.No.4a SEQUENTIAL ALLOCATION

PROGRAM:
#include<stdio.h>
int main()
{
inti,j,n,block[20],start;
printf("Enter the no. of file : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number of blocks needed for file %d: ", i);
scanf("%d",&block[i]);
}
start=0;

for(i=1;i<=n;i++)
{
printf("File %d Start %d Size %d\n",i,start,block[i]);
start=start+block[i];
}
}

Output:
Enter the no of file:2
Enter the number of blocks needed for file1:3
Enter the no of blocks needed for file2: 2
File1 start 0 size 3
File 2 start 3 size 2

Result:
Ex.No.4b INDEXED ALLOCATION

PROGRAM:
#include<stdio.h>
main()
{
int f[50],i,n,st,len,k;
for(i=0;i<50;i++)
{
f[i]=1;
}
printf("How many blocks are already allocated ?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the allocated block numbers ");
scanf("%d",&k);
f[k]=0;
}
printf("Enter the starting index block & length");
scanf("%d %d",&st,&len);
i=0;
k=st;
while(i<len)
{
if(f[k]==1)
{
f[k]=0;
printf("\n %d-> file allocating",k);
i++;
}
else
{
printf("\n %d->file is already allocated",k);
}
k++;
}
}
OUTPUT:
[itstu003@localhost ~]$ ./a.out
How many blocks are already allocated ?2
Enter the allocated block numbers 2
Enter the allocated block numbers 4
Enter the starting index block & length2 3
2->file is already allocated
3-> file allocating
4->file is already allocated
5-> file allocating
6-> file allocating
How many blocks are already allocated? 2
Enter the allocated block numbers 2
Enter the allocated block numbers 4
Enter the starting index block & length 2 3
2->file is already allocated
3->file allocating
4-> file is already allocated
5-> file allocating
6-> file allocating

Result:
Ex.No.4c LINKED ALLOCATION

PROGRAM:
#include<stdio.h>
typedefstruct file
{
char name[10];
intstart,size,block[10];
}fil;
main()
{
fil f[10];
inti,j,n;
printf("Enter number of files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the filename: ");
scanf("%s",f[i].name);
printf("Enter the starting block of the file: ");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter the size of the file: ");
scanf("%d",&f[i].size);
for(j=1;j<f[i].size;j++)
{
printf("Enter the next block number:");
scanf("%d",&f[i].block[j]);
}
}
printf("File \t Start \t Size \t Blocks \n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].name,f[i].start,f[i].size);
for(j=0;j<f[i].size;j++)

printf("%d-->",f[i].block[j]);
printf("-1 \n");
}
printf("\n");
}

Output:
Enter number of files2
Enter the filename: a
Enter the starting block of the file: 5
Enter the size of the file: 5
Enter the next block number: 11
Enter the next block number: 12
Enter the next block number: 14
Enter the next block number: 13

Enter the filename: b


Enter the starting block of the file: 2
Enter the size of the file: 3
Enter the next block number: 3
Enter the next block number: 1

File Start Size Blocks


a 5 5 5->11->12->14->13->-1
b 2 2 2->3->1->-1

Result:
Ex.No.5 DINING PHILOSOPHER'S PROBLEM USING SEMAPHORE

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define N 5
#define LEFT (j-1)%N
#define RIGHT (j+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[N];
void test(int);
intlt, rt;
void main()
{
intf,ch=0,i,a,b;
inttake_sticks(int);
intput_sticks(int);
clrscr();
printf("\n \t *** DINING PHILOSOPHER PROBLEM**\n");
printf("ENTER THE NUMBER OF PHILOSOPHER IN THE TABLE IS %d:",N);
printf("\n STATUS OF THE PHILOSOPHER:\n");
for(i=0;i<N;i++)
{
state[i]=THINKING;
printf("\n P%d IS THINKING",i);
}
do
{
printf("\n\t\t MENU\n\n");
printf("\n\t1.START EATING\n\n");
printf("\n\t2.STOP EATING\n\n");
printf("\n\t3.EXIT\n\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE PHILOSOPHER WHO IS GOING TO EAT:");
scanf("%d",&a);
f=take_sticks(a);
if(f==EATING)
{
printf("\n THE PHILOSOPHER HAS STARTED EATING...\n\n");
}
else
{
printf("\nTHE PHILOSOPHER IS HUNGRY!!..\n\n");
printf("\n THE CHOPSTICK IS ENGAGED\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 2:
printf("\n ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK");
scanf("%d",&b);
if(state[b]==EATING)
{
f=put_sticks(b);
printf("\n THE PHILOSOPHER HAS STOPPED EATING!!..\n\n");
}
else
{
printf("\n THE PHILOSOPHER IS NOT EATING!!\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
getch();
}

inttake_sticks(int j)
{
state[j]=HUNGRY;
test(j);
return state[j];
}
intput_sticks(int j)
{
state[j]=THINKING;
if(j==0) test(N-1); else test(LEFT);
if(j==N-1) test(0); else test(RIGHT);
return state[j];
}
void test(int j)
{
if(j==0) rt = N-1; else rt = LEFT;
if(j==N-1) lt = 0; else lt = RIGHT;
if(state[j]==HUNGRY && state[lt]!=EATING && state[rt]!=EATING)
{
state[j]=EATING;
}
}

OUTPUT:

*** DINING PHILOSOPHER PROBLEM**


ENTER THE NUMBER OF PHILOSOPHER IN THE TABLE IS 5:
STATUS OF THE PHILOSOPHER:

P0 IS THINKING
P1 IS THINKING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU

1.START EATING
2.STOP EATING
3.EXIT

ENTER YOUR CHOICE:1


ENTER THE PHILOSOPHER WHO IS GOING TO EAT:1
THE PHILOSOPHER HAS STARTED EATING...

P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT

ENTER YOUR CHOICE:1


ENTER THE PHILOSOPHER WHO IS GOING TO EAT:3
THE PHILOSOPHER HAS STARTED EATING...

P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU

1.START EATING
2.STOP EATING
3.EXIT

ENTER YOUR CHOICE:1


ENTER THE PHILOSOPHER WHO IS GOING TO EAT:0
THE PHILOSOPHER IS HUNGRY!!..
THE CHOPSTICK IS ENGAGED

P0 IS HUNGRY
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT

ENTER YOUR CHOICE:2


ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK 1
THE PHILOSOPHER HAS STOPPED EATING!!..

P0 IS EATING
P1 IS THINKING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
RESULT:
Ex.No.6a SINGLE LEVEL DIRECTORY

Program:
#include<stdio.h>
#include<conio.h>
main()
{
intmaster,s[20];
char f[20][20][20];
char d[20][20];
inti,j;
clrscr();
printf("enter number of directories:");
scanf("%d",&master);
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]);
printf("enter size of 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\tfilenames\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
b
enter size of directories:2
2
enter the file names:ab
bc
cd
ef
directory size filenames
*****************************
a 2 ab
bc
b 2 cd
ef

Result:
Ex.No.6b TWO LEVEL DIRECTORIES

Program:
#include<stdio.h>
#include<conio.h>
structst
{
chardname[10];
charsdname[10][10];
charfname[10][10][10];
intds,sds[10];
}dir[10];
void main()
{
inti,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter 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();
}
Output:
enter number of directories:2
enterdirectory 1 names:a
enter size of directories:1
enter subdirectory name and size:b 1
enter file name:b
enterdirectory 2 names:c
enter size of directories:1
enter subdirectory name and size:d 1
enter file name:e
dirname size subdirname size files
*******************************************
a 1 b 1 b

c 1 d 1 e
Result:
Ex.No.7 BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE

PROGRAM:
#include<stdio.h>
#include<conio.h>
void matrix_get(int[5][5],int,int);
void main()
{
int max[5][5],all[5][5],total[1][5];
int avail[1][5],f[5],need[5][5],state[5];
int s,i,j,p,r,c,ch,ch1,k=0;
clrscr();
printf("\t\t DEADLOCK -BANKER'S ALGORITHM \n");
printf("ENTER THE NUMBER OF PROCESS:");
scanf("%d",&p);
ch=p;
printf("ENTER THE NUMBER OF RESOURCE TYPE:");
scanf("%d",&r);
printf("ENTER THE MAX MATRIX:\n");
matrix_get(max,p,r);
printf("\nENTER THE ALLOCATED MATRIX:\n");
matrix_get(all,p,r);
printf("ENTER THE TOTAL RESOURCE PRESENT:\n");
matrix_get(total,1,r);
for(i=0;i<r;i++)
{
for(j=0;j<p;j++)
{
if(max[j][i]>total[0][i]||all[j][i]>total[0][i])
{
printf("\nINPUT IS > TOTAL RESOURCE PRESENT\n");
exit(0);
}
}
s=total[0][i];
for(j=0;j<p;j++)
{
s=s-all[j][i];
}
avail[0][i]=s;
}
printf("\n\n AVAILABLE MATRIX \n\n");
for(i=0;i<r;i++)
{
printf("%d\t",avail[0][i]);
}
printf("\n\n NEED MATRIX:\n\n");
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-all[i][j];
if(need[i][j]<0)
{
need[i][j]=0;
}
printf("%d\t",need[i][j]);

}
printf("\n");
}
while(ch>0)
{
ch1=ch;
for(i=0;i<p;i++)
{
if(f[i]==0)
{
c=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[0][j])
{
c++;
}
}
if(c==r)
{
f[i]=1;
state[k++]=i;
ch--;
for(j=0;j<r;j++)
{
avail[0][j]=avail[0][j]+all[i][j];
need[i][j]=0;
}
}
}
}
if(ch==p)
{
printf("\n ALREADY DEADLOCK OCCURRED");
exit(0);
}
if(ch1==ch)
{
printf("\n ALLOCATION LEADS TO DEADLOCK");
exit(0);
}
}

printf("\n\n SAFE STATE S:{");


for(i=0;i<p;i++)
{
printf("P%d ",state[i]);
}
printf("}");
}
void matrix_get(int a[5][5],intp,int r)
{
inti,j;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&a[i][j]);
}
}
}

OUTPUT:
DEADLOCK -BANKER'S ALGORITHM
ENTER THE NUMBER OF PROCESS:5
ENTER THE NUMBER OF RESOURCE TYPE:3
ENTER THE MAX MATRIX:
753
322
902
222
433

ENTER THE ALLOCATED MATRIX:


010
200
302
211
002
ENTER THE TOTAL RESOURCE PRESENT:
10 5 7

AVAILABLE MATRIX

3 3 2

NEED MATRIX:

7 4 3
1 2 2
6 0 0
0 1 1
4 3 1

SAFE STATE S:{P1 P3 P4 P0 P2 }

RESULT:
Ex.No.8 IMPLEMENTATION OF DEADLOCK DETECTION

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,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",&amp;tp);
printf("enter clain matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;c[i][j]);
}
printf("enter allocation matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;r[i]);
}
printf("enter availability vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;a[i]);
temp[i]=a[i];
}
for(i=1;i&lt;=4;i++)
{
sum=0;
for(j=1;j&lt;=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i&lt;=4;i++)
{
for(l=1;l&lt;k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j&lt;=5;j++)
if(c[i][j]&gt;temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j&lt;=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j&lt;=tp;j++)
{
found=0;
for(i=1;i&lt;k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}

INPUT:
enter total no. of processes : 4
enter claim matrix :
01001
00101
00001
10101
enter allocation matrix :
10110
11000
00010
00000
enter resource vector :
21121
enter the availability vector :
00001

OUTPUT :
deadlock causing processes are : 1 2

Result:
Ex.No.9a FIFO PAGE REPLACEMENT

PROGRAM:
#include<stdio.h>
#include<conio.h>
intnf;
int ne;
inthit,miss;
int ref[50];
intfr[15];
void getdata();
void fifo();
int large(int []);
void getdata()
{
inti;
printf("\nEnter number of frames:");
scanf("%d",&nf);
printf("\nEnter number of elements in the reference string");
scanf("%d",&ne);
printf("\nEnter the reference string");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void fifo()
{
inti,j,found=0,rear=-1;
miss=hit=0;
for(i=0;i<nf;i++)
{
fr[i]=0;
}
for(i=0;i<ne;i++)
{
printf("\nThe no to be inserted :%d",ref[i]);
found=search(ref[i]);
if(found==0)
{
miss++;
if(rear==nf-1)
{
rear=-1;
}
rear++;
fr[rear]=ref[i];
}
else
{
hit++;
}
printf("\n");
for(j=0;j<nf;j++)
printf("%d ",fr[j]);
}
printf("\n No. of page faults=%d",miss);
printf("\n No. of Hits=%d",hit);
}
int search(int item)
{
inti;
for(i=0;i<nf;i++)
{
if(fr[i]==item)
{
return(1);
}
}
return 0;
}
void main()
{
getdata();
printf("\n FIFO PAGE REPLACEMENT");
fifo();
getch();
}

Output:
[itstu003@localhost ~]$ cc fifo.c
fifo.c:2:18: conio.h: No such file or directory
fifo.c: In function `main':
fifo.c:93: warning: return type of 'main' is not `int'
[itstu003@localhost ~]$ ./a.out
Number of blocks in memory :3
Size of block:2
Number of jobs :3
Size of job 1:4
Size of job 2:1
Size of job 3:2
job 1 stored in memory block 1
job 1 stored in memory block 2
job 2 stored in memory block 3

RESULT:
Ex.No.9b LRU PAGE REPLACEMENT

PROGRAM:
#include<stdio.h>
intnf;
inti,ne;
int miss=0;
int ref[50];
intfr[15];
void getdata();
void lru();
void getdata()
{
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void lru()
{
intcheck,j,k;
int count=0,index[5];
printf("\n\n Simulation of LRU page replacement technique!...........\n");
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
check=0;
for(j=0;j<nf;j++)
{
if(fr[j]==ref[i])
{
check=1;
}
}
if(check==0)
{
miss+=1;
if(count<nf)
{
fr[count++] = ref[i];
}
else
{
for(j=0;j<i;j++)
{
for(k=0;k<nf;k++)
{
if(ref[j]==fr[k])
{
index[k] = j;
}
}
}
for(j=0;j<nf;j++)
{
if(index[check]>index[j])
{
check=j;
}
}
fr[check]=ref[i];
}
printf("\n");
for(j=0;j<nf;j++)
{
printf("%d\t",fr[j]);
}
}
}
printf("\n Number of page faults:%d",miss);
}
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT");
getdata();
printf("\t\t\t LRU PAGE REPLACEMENT");
lru();
getch();
}

OUTPUT:
LRU PAGE REPLACEMENT
Enter the number of frames:3
Enter the number of elements in the reference string:
6
Enter the reference string:
1
2
3
4
2
1
LRU PAGE REPLACEMENT
Simulation of LRU page replacement technique!...........
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 2 1
Number of page faults:5
RESULT:
Ex.No.9c LFU PAGE REPLACEMENT

PROGRAM:
#include<stdio.h>
int n;
main()
{
intseq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0;
int count=1,p=0;
float pfr;
clrscr();
printf("enter max limit of the sequence:");
scanf("%d",&max);
printf("enter the sequence:");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("enter the no of frames:");
scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count] );
count++;
pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{

pos[j]=k;
break;
}
else
pos[j]=-1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==-1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==-1)
{
s=k;
break;
}
}
}
pf++;
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\n theno of page faults are:%d",pf);
printf("\n page fault rate:%f",pfr);
getch();
}
intfindmax(int a[])
{
intmax,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
Output:
enter the limit of the sequence:10
enter the sequence: 2 1 3 4 2 1 1 2 3 4
enter the no of frames:2
2 1
2 3
2 4
2 1
3 1
4 1
the no of page faults are:7
page fault rate:0.70000

RESULT:
Ex.No: 10 INTERPROCESS COMMUNICATION USING SHARED MEMORY

PROGRAM:
Server Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 25
void main()
{
char c;
intshmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY SERVER PROGRAM \n");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");
exit(0);
}
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
s = shm;
for(c='A';c<='Z';c++)
{
*s++ = c;
*s = '\0';
}
exit(0);
}
Client Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 0
void main()
{
char c;
intshmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY CLIENT PROGRAM\n ");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");

exit(0);
}
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
for(s=shm;*s!='\0';s++)
{
printf("\t");
putchar(*s);
}
exit(0);
}
Output:
[exam01@localhost ~]$ cc ser_shamem.c
[exam01@localhost ~]$ cc cli_shamem.c
[exam01@localhost ~]$ ./a.out
SHARED MEMORY CLIENT SERVER PROGRAM
A B C D E F G H I
J K L M N O P Q R S
T U V W X Y Z

Result:
Ex.No.10b INTERPROCESS COMMUNICATION USING PIPES

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

OUTPUT:
Enter the string to enter into the pipe: Operating system
The string retrieved from the pipe is Operating system

Result:
Ex.No.11 Memory Management Scheme – PAGING

PROGRAM:
#include<stdio.h>
int main()
{
intnb,sb,nj,i,j;
intjs[10];
printf("Number of blocks in memory :");
scanf("%d",&nb);
printf("Size of block:");
scanf("%d",&sb);
printf("Number of jobs :");
scanf("%d",&nj);
for(j=1;j<=nj;j++)
{
printf("Size of job %d:", j);
scanf("%d",&js[j]);
}
j=1;
for(i=1;i<=nb;i++)
{
printf("job %d stored in memory block %d\n ", j,i);
js[j] = js[j] - sb;
if(js[j] <= 0)
{
j++;
}
if(j >nj)
{
break;
}
}
if(i>nb)
{
printf("Out of memory");
}
return 0;
}

Output:
Number of blocks in memory:2
Size of each block:50
Number of jobs:3
Size of job 1:10
Size of job 2:20
Size of job 3:10
Job1 stored in memory block 1
Job2 stored in memory block 2

Result:

You might also like