0% found this document useful (0 votes)
66 views54 pages

Os - Lab Experiments - Merged

Meraj

Uploaded by

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

Os - Lab Experiments - Merged

Meraj

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

(ARTIFICIAL INTELLIGENCE)

LAB MANUAL

SUBJECT: OPERATING SYSTEM LAB


[BCS-451]

Prepared by Approved By
Mr. Vishal Yadav Dr. Ajay Gupta
Asst. Professor HoD
Dept. of CSE(AI) Dept. of CSE(AI)
Index

SI. NO. Particulars Page


1 Vision, Mission (Institute) i
2 Vision, Mission (Department) ii
3 COs iii
4 POs and PSOs iv
5 CO-PO mapping v
6 Syllabus as per AKTU vi
7 List of Experiments vii
8 DO’s & Don’ts viii
VISION AND MISSION OF THE INSTITUTE

Vision:
To be among the renowned institutions providing engineering and management education of
excellence, empowered with research, innovation and entrepreneurship with high ethical
values, catering to the needs of industry and society.

Mission:

1. To offer state of the art undergraduate and postgraduate program.


2. To be a learner centric institute imbibing experimental innovation and lifelong
learning skills, addressing societal problems with high ethical values for nation
building.
3. To deliver changing demands of industry and academia through our stakeholders.
4. To contribute as a professional leader in the growing field of entrepreneurship and
research.

i
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(ARTIFICIAL INTELLIGENCE)

VISION AND MISSION OF THE DEPARTMENT

Vision:

To produce creative engineers and leaders through excellence in education, innovation, and
research in artificial intelligence globally, to serve as valuable assets for industry and society.

Mission:

 To provide deep knowledge through state-of-the art concepts in artificial intelligence.


 To transform the students to competent professionals and great leaders.
 To support research, innovation and entrepreneurship to meet regional and country’s
technological need.
 To inspire and inculcate the moral and ethical values for lifelong.

ii
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(ARTIFICIAL INTELLIGENCE)

Operating System Lab [BCS -451]

Course Outcomes (COs)


At the end of this course students will demonstrate the ability to:

CO 1: Understand and implement basic services and functionalities of the operating system
using system calls.

CO 2: Use modern operating system calls and synchronization libraries in


software/hardware interfaces.

CO 3: Analyze and simulate CPU Scheduling algorithm like FCFS, Round Robin, SJF, and
Priority.

Implement memory management schemes and page replacement schemes and


CO 4:
simulate file allocation and organization techniques.

Understand the concepts of deadlock in operating systems and implement them in


CO 5:
multiprogramming system.

iii
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERNG
(ARTIFICIAL INTELLIGENCE)

Program Outcomes (POs):

PO-1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
PO-2: Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
PO-3: Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
PO-4: Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
PO-5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
PO-6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
PO-7: Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development
PO-8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO-9: Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO-10: Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
PO-11: Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
PO-12: Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

iv
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING (ARTIFICIAL
Program Specific Outcomes (PSOs):
PSO-1: Understanding Human Cognition, Artificial Intelligence, Data Engineering and
Machine Learning for designing Intelligent Systems.
PSO-2: Apply computational knowledge and skills to provide innovative solutions to solve
problems in AI & ML.
CO-PO Mapping:

Course
PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO
Outcome 1 2 3 4 1 2
5 6 7 8 9 10 11 12
s
CO1 2 1 1 1 - - - - - - - 2 2 -
CO2 3 2 2 2 3 3 - - - - - 3 3 -
CO3 3 3 2 2 3 3 - - - - - 3 3 -
CO4 2 3 3 3 - - - - - - - 2 2 2
CO5 2 3 3 2 - - - - - - - 2 2 2
Avg 2.4 2.4 2.2 2 3 3 - - - - - 2.4 2.4 2
3 – High; 2 – Medium; 1 – Low

v
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING (ARTIFICIAL
Operating System Lab [BCS -451]

SUGGESTIVE LIST OF EXPERIMENTS AS PER AKTU

1. Study of Hardware andSoftware requirementsof different Operating Systems


(UNIX, LINUX, WINDOWS XP and Windows 7/8).
2. Execute various UNIXSystem Calls for
i. Process Management
ii. File Management
iii. I/O System Calls
3. Implement CPU Scheduling Policies
i. SJF
ii. Priority
iii. FCFS
iv. Multi-Level
4. Implement file storageallocation technique:
i. Contiguous
ii. Linked –list
iii. Indirect allocation
5. Implement contiguousallocation technique.
i. Worst-Fit
ii. Best-Fit
iii. First-Fit
6. Calculation of externaland internal fragmentation
i. Free space list of blocks from the system
ii. List process filesfrom the system.
7. Implementation of compaction for the continually changingmemory layout and
Calculate the total movement of data.
8. Implementation of resource allocation graph (RAG)Implementation of Banker’s
algorithm.
9. Implementation of Banker’s algorithm.
10. Conversion of resourceallocation graph (RAG) to wait for graph (WFG) for each
type of method used for storing graph.
11. Implement the solution for Bounded Buffer(producer-consumer) problem using inter
process communication techniques-Semaphores.
12. Implement the solutions for Readers-Writers problem using inter process
communication techniques-Semaphores.

vi
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(ARTIFICIAL INTELLIGENCE)
Operating System Lab [BCS -451]
LIST OF EXPERIMENTS:

S.NO. Experiment name Page no.


Study of Hardware andSoftware requirementsof different Operating
1 1
Systems (UNIX, LINUX, WINDOWS XP and Windows 7/8).
Execute various UNIXSystem Calls for
a. Process Management
2 4
b. File Management
c. I/O System Calls
Implement CPU Scheduling Policies
a. SJF
3 b. Priority 12
c. FCFS
d. Multi-Level
Implement file storageallocation technique:
a. Contiguous
4 16
b. Linked –list
c. Indirect allocation
Implement contiguousallocation technique.
a. Worst-Fit
5 20
b. Best-Fit
c. First-Fit
Calculation of externaland internal fragmentation
6 a. Free space list of blocks from the system 23
b. List process filesfrom the system.
Implementation of compaction for the continually changing
7 memory layout and Calculate the total movement of data. 29

Implementation of resource allocation graph (RAG)Implementation of


8 Banker’s algorithm. 33

Implementation of Banker’s algorithm.


9 37
Conversion of resourceallocation graph (RAG) to wait for graph
10 (WFG) for each type of method used for storing graph. 39

vii
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(ARTIFICIAL INTELLIGENCE)

Lab Instructions

 Lab should be maintained clean, organized, and properly lighted.


 Keep bags outside the lab.
 Don’t make noise and Grouping in lab.
 Follow lab manuals and Faculty instructions to perform lab Experiments strictly.
 Follow discipline in lab during performing experiments.
 Reach on time in lab.
 Return all materials and equipment to their designated places after the lab.
 Don’t use Mobile in Lab.

viii
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(ARTIFICIAL INTELLIGENCE)
Operating System Lab [BCS -451]

Do’s and Dont’s

Do’s:
i. Entry of student should be made with terminal number.
ii. Student must enter the lab in uniform.
iii. Properly shut down the system before leaving.
iv. Student should arrange the chair after leaving.
v. Student must maintain discipline in the lab.
vi. Printing schedule should be followed.
vii. Student should come with proper study materials in labs.

Dont’s:
i. Spitting, smoking and chewing is not allowed.
ii. Student should not play games in the computer.
iii. Use of mobile is strictly prohibited in the lab.
iv. Do not come with bag and baggage in the lab.
v. Do not install the software without permission.
vi. Do not insert pen drive in computer without permission.

ix
EXPERIMENT – 1
Objective – Study of Hardware and Software requirements of different Operating
Systems (UNIX, LINUX, WINDOWS XP and Windows 7/8)
Theory –
Hardware requirements: The most common set of requirements defined by any operating
system or software application is the physical computer resources, also known as hardware.
A hardware requirements list is often accompanied by a hardware compatibility list (HCL),
especially in case of operating systems.

1. Windows 7/8
Windows 7 is a major release of the Windows NT operating system developed
by Microsoft. It was released to manufacturing on July 22, 2009, and became generally
available on October 22, 2009.[9] It is the successor to Windows Vista, released nearly
three years earlier. It remained an operating system for use on personal computers,
including home and business desktops, laptops, tablet PCs and media centre PCs, and itself
was replaced in November 2012 by Windows 8, the name spanning more than three years
of the product.

Hardware and Software Requirements


i. Processor: 1 GHz or faster processor/ SoC
ii. RAM: 1 GB for 32-bit OS or 2 GB for 64-bit OS
iii. Hard Disk Space: 16 GB for 32-bit OS or 20 GB for 64-bit OS
iv. Graphics Card: DirectX9 or later with WDDM 1.0 driver
v. Display: 800 X 609 with WDDM Driver

2. Windows XP
Windows XP is a version of the Microsoft Windows operating system for personal
computers. The letters "XP" stand for eXPerience. Microsoft released Windows XP on
October 25, 2001. Windows XP replaced Windows 2000 and Windows ME, which helped
bring the NT and 9x versions of Windows together. It was replaced by Windows Vista in
2006.[4] Windows XP was the second most used computer operating system in the world
as late as April 2012.

Hardware and Software Requirements


i. Processor: 233 MHz or faster processor (300 MHz is recommended)
ii. RAM: 64 MB (minimum) 128 MB (recommended)
iii. Hard Disk Space: 1.5 GB minimum space
iv. CD-ROM or DVD-ROM drive
v. Video Adapter and monitor with Super VGA (800 x 600) or higher resolution.
vi. Sound Card

3. UNIX OS
UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis
Ritchie at AT&T Bell laboratories in 1970. It is prevalent among scientific, engineering,
and academic institutions due to its most appreciative features like multitasking, flexibility,
and many more. In UNIX, the file system is a hierarchical structure of files and directories
where users can store and retrieve information using the files.
Hardware and Software Requirements
i. Processor: IBM 604e processor with a clock speed of 375 MHz or faster
ii. RAM: 1 GB
iii. Hard Disk Space: /tmp – 1 GB, WAS_HOME – 800 MB, /var – 300 MB, /itim45
– 500 MB

4. LINUX OS
Linux is a Unix-like, open source and community-developed operating system (OS) for
computers, servers, mainframes, mobile devices and embedded devices. It is supported on
almost every major computer platform, including x86, ARM and SPARC, making it one of
the most widely supported operating systems.
Hardware and Software Requirements
i. Processor: 32-bit Intel Compatible processor at 2 GHz or faster
ii. RAM: 512 MB
iii. Hard Disk Space: 2.5 GB
iv. A DVD-ROM Drive
EXPERIMENT – 2
Objective – Execute various system calls for
i. Process Management
ii. File Management
iii. Input/Output System Calls
Theory –

1. Process Management
Process management uses certain system calls. They are explained below.
1. To create a new process – fork () is used.
2. To run a new program = exec () is used.
3. To make the process to wait = wait () is used.
4. To terminate the process – exit () is used.
5. To find the unique process id – getpid () is used.
6. To find the parent process id – getppid () is used.
7. To bias the currently running process property – nice () is used.

2(i) Example program for example of fork ()

Figure: Flow of fork () system call

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main () {
int i =10;
int pid;
pid=fork ();
if (pid==0) {
for (i=0; i < 999999; i++) {
sleep (2);
printf (" from Child process %d\n", i);
}
}
else
{ for (i=0; i < 999999; i=i+2){
sleep (2);
printf (" from Parent process %d\n", i);
}

}
return 0;
}
Output 2(i)
user@node1: ~/oslab$. /chp
from Parent process 0
from Child process 0
from Parent process
2 from Child process
1 from Parent
process 4 from Child
process 2
…………………..
……………………

2. File Management
There are four system calls for file management,
1. open ():system call is used to know the file descriptor of user-created files. Since read
and write use file descriptor as their 1st parameter so to know the file descriptor open()
system call is used.
2. read ()system call is used to read the content from the file. It can also be used to read
the input from the keyboard by specifying the 0 as file descriptor.
3. write (): system call is used to write the content to the file.
4. close ():system call is used to close the opened file, it tells the operating system that
you are done with the file and close the file.

2(ii). Example program for file management


#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main() {
int n,fd;
char buff[50];
printf("Enter text to write in the file:\n");
n= read(0, buff, 50);
fd=open("file",O_CREAT | O_RDWR, 0777);
write(fd, buff, n);
write(1, buff, n);
int close(int fd);
return 0;
}
Output 2(ii)
Enter text to write in the file:
Hello world, welcome @
IncludeHelp Hello world, welcome
@ IncludeHelp

3. Input/Output System Calls


Basically there are total 5 types of I/O system calls:
 Create: Used to Create a new empty file.
 open: Used to Open the file for reading, writing or both.
 close: Tells the operating system you are done with a file descriptor and Close the file
which pointed by fd.
 read: From the file indicated by the file descriptor fd, the read() function reads cnt
bytes of input into the memory area indicated by buf. A successful read() updates the
access time for the file.
 write: Writes cnt bytes from buf to the file or socket associated with fd. cnt should not
be greater than INT_MAX (defined in the limits.h header file). If cnt is zero, write()
simply returns 0 without attempting any other action.
2(iii) Example program for Input/output System Calls
main(){
char c;
int a=1,
i;
while (a != 0) {
read(0, &a, 3);
i=a;
write(1, &i, 3);
write(1, "\n", 1);
}
}

Output 2(iii) user@node1:~/oslab$ ./io


2
2
33
33
44
44
EXPERIMENT – 3
Objective – Implement CPU Scheduling Policies
i. SJF
ii. Priority
iii. FCFS
iv. Multi – Level Queue
Theory –
1. SJF
Algorithm:
Step 1. Sort all the process according to the arrival time.
Step 2. Then select that process which has minimum arrival time and minimum Burst
time. Step 3. After completion of process make a pool of process which after till the
completion of previous process and select that process among the pool which is
having
minimum Burst time.
Example: Sorting the process according to brust time. (Array sorting flowchart can be
used for logic understanding)

3(i). Example program for SJF scheduling implementation


#include<stdio.h>
struct process {
int id;
int brust;
};
typedef struct process proc;
int main() {
int n;
printf("enter the number of processs:
"); scanf("%d", &n);
proc p[n], temp;
int i,j;
for(i=0;i<n;i++) {
p[i].id=i;
printf("enter the brust time of process P %d :", i);
scanf("%d", &p[i].brust);
}
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(p[j].brust < p[i].brust){
temp.id = p[j].id;
temp.brust =
p[j].brust; p[j].id =
p[i].id;
p[j].brust = p[i].brust;
p[i].id = temp.id;
p[i].brust =
temp.brust;
}
}
}
int wt=0;
int twt=0;
printf("\n scheduling information \n");
for(i=0;i<n;i++) {
twt = twt+wt;
printf("process id: %d \t wating time: %d \n",p[i].id, wt );
wt=wt+p[i].brust;
}
printf("\n average waiting time: %1.2f :", ((float)twt/n));
return 0;
}
Input: Program 3(i):

Process Brust Time


P0 5
P1 3
P2 6

P3 4

Output:
scheduling information
process id: 1 wating time: 0
process id: 3 wating time: 3
process id: 0 wating time: 7
process id: 2 wating time: 12
average waiting time: 5.50

2. Priority Scheduling
Algorithm:
Step 1. First input the processes with their arrival time, burst time and priority.
Step 2. Sort the processes, according to arrival time if two process arrival times is same
then sort according process priority if two process priority are same then sort
according to process number.
Step 3. Now simply apply FCFS algorithm.

Example : Sorting the process according to priority. (Array sorting flowchart can be used
for logic understanding)
3(ii). Example program for Priority scheduling implementation
#include<stdio.h>
struct process {
int id;
int brust;
int pr;
};
typedef struct process proc;
int main() {
int n;
printf("enter the number of processs:
"); scanf("%d", &n);
proc p[n], temp;
int i,j;
for(i=0;i<n;i++) {
p[i].id=i;
printf("enter the brust time of process P %d :", i);
scanf("%d", &p[i].brust);
printf("enter the priority of process P %d :",
i); scanf("%d", &p[i].pr);
}
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(p[j].pr < p[i].pr){
temp.id = p[j].id;
temp.brust =
p[j].brust; temp.pr =
p[j].pr;
p[j].id = p[i].id;
p[j].brust = p[i].brust;
p[j].pr = p[i].pr;
p[i].id = temp.id;
p[i].brust =
temp.brust; p[i].pr =
temp.pr;
}
}
}
int wt=0;
int twt=0;
printf("\n scheduling information \n");
for(i=0;i<n;i++) {
twt = twt+wt;
printf("process id: %d priority %d \t wating time: %d \n",p[i].id,p[i].pr, wt
);
wt=wt+p[i].brust;
}
printf("\n avg waiting time: %1.2f :", ((float)twt/n));
return 0;
}
Input: Program 3(ii):

Process Brust Time Priority


P0 5 3
P1 3 1
P2 6 2
P3 4 4
Output:
Scheduling information
process id: 1 priority 1 wating time: 0
process id: 2 priority 2 wating time: 3
process id: 0 priority 3 wating time: 9
process id: 3 priority 4 wating time: 14
avg waiting time: 6.50

3. FCFS
Algorithm:
Step 1. First input the processes with their arrival time, burst time.
Step 2. Queue them in their arrival order i.e. the process that comes first then process it
first. Example:

3(iii). Example program for FCFS scheduling implementation


#include<stdio.h>
struct process {
int id;
int brust;
int at;
};
typedef struct process proc;
int main() {
int n;
printf("enter the number of processs:
"); scanf("%d", &n);
proc p[n], temp;
int i,j;
for(i=0;i<n;i++) {
p[i].id=i;
printf("enter the brust time of process P %d :", i);
scanf("%d", &p[i].brust);
printf("enter the arival time of process P %d :", i);
scanf("%d", &p[i].at);
}
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(p[j].at < p[i].at) {
temp.id = p[j].id;
temp.brust =
p[j].brust; temp.at =
p[j].at;
p[j].id = p[i].id;
p[j].brust = p[i].brust;
p[j].at = p[i].at;
p[i].id = temp.id;
p[i].brust =
temp.brust; p[i].at =
temp.at;
}}}
int wt=0;
int twt=0;
printf("\n scheduling information \n");
for(i=0;i<n;i++) {
twt = twt+wt;
printf("process id: %d arrival-time %d \t wating time: %d \
wt )
; n",p[i].id,p[i].at, wt=wt+p[i].brust;
}
printf("\n avg waiting time: %1.2f :", ((float)twt/n));
return 0;
}

Input: Program 3(iii):

Process Brust Time Arival time


P0 5 3
P1 3 1
P2 6 2
P3 4 4

Output:
Scheduling information
process id: 1 arrival-time 1 wating time: 0
process id: 2 arrival-time 2 wating time: 3
process id: 0 arrival-time 3 wating time: 9
process id: 3 arrival-time 4 wating time: 14
avg waiting time: 6.50

4. Multi-Level Queue
Algorithm:
Let, us suppose we have three level queue such that queue 1, 2 and 3. Queue 1 for system
process, queue 2 for interactive process and queue 3 for batch process. Input process
parameters from user Step 1. If process is system process then insert this process in
queue
1. Step 2. If process is interactive process the insert this process in queue 2.
Step 3. If process is batch process then insert this process in queue 3.
Step 4. Sort all queues using FCFS.
Step 5. Process Queue 1
Step 6. Process Queue 2
Step 7. Process Queue 3
Step 8. Repeat from step 1 for further processing

Example program for multilevel queue scheduling implementation


#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
int num;
int arrive;
int brust;
int pt;
int f;
int cpu;
int wait;
} process;
typedef struct queue {
process p[10];
int front;
int rear;
} queue;
void insert(queue *q,process t) {
q->p[q->rear]=t;
q->rear++;
}
void pstack(queue *que) {
int i,j;
for(i=0;i<3;i++) {
printf("\n queue %d is \n",i+1); printf("Process\tarive\
tbrust\t type\t I/O\t cpu \twait\n");
for(j=0;j<que[i].rear;j++) {
printf(" %d \t %d \t %d \t %d \t %d \t %d \t %d\
n",que[i].p[j].num,que[i].p[j].arrive,que[i].p[j].brust,que[i].p[
j].pt,que[i].p [j].f,que[i].p[j].cpu,que[i].p[j].wait);
}}}
void fcfs_sort(queue *que)
{ process *temp;
int i,j,k,n;
for(k=0;k<3;k++){
n=que[k].rear;
for(i=1;i<n;i++){
for(j=0;j<n-i;j++){
if(que[k].p[j].arrive > que[k].p[j+1].arrive){
temp=&que[k].p[j];
que[k].p[j]=que[k].p[j+1];
que[k].p[j+1]=*temp;

}}}}}
void pque(queue *que){
int c=0,cp=0;
int w=0;
int j=0,k=0,l=0,t=0,i;
process *temp;
int max=0;
for(i=0;i<3;i++) {
max=max+que[i].rear;
}
for(i=0;i<max;i++){
if(que[0].p[j].arrive <= cp && j< que[0].rear)
{ que[0].p[j].cpu=c;
que[0].p[j].wait=c-que[0].p[j].arrive;
if(que[0].p[j].wait < 1)
que[0].p[j].wait=0;
c=c+que[0].p[j].brust;
j++;
cp=c;
continue;
}
else if(que[1].p[k].arrive < cp && k < que[1].rear ){
que[1].p[k].cpu=c;
que[1].p[k].wait=c-que[1].p[k].arrive;
if(que[1].p[k].wait < 1)
que[1].p[k].wait=0;
c=c+que[1].p[k].brust;
k++;
cp=c;
continue;
}
else if(que[2].p[l].arrive < cp && l < que[2].rear){
que[2].p[l].cpu=c;
que[2].p[l].wait=c-que[2].p[l].arrive;
if(que[2].p[l].wait < 1)
que[2].p[l].wait=0;
c=c+que[2].p[l].brust;
l++;
}
else
c=0;
}}
int main(){
queue que[3];
int j;
for(j=0;j<3;j++){
que[j].front=-1;
que[j].rear=0;
}
process temp;
int n;
printf("enter the number of process");
scanf("%d",&n);
int i;
for(i=0;i<n;i++){
temp.num=i+1;
temp.cpu=0;
temp.wait=0;
printf("\n enter the ariive time of process %d
=",i+1); scanf("%d",&temp.arrive); printf("\
n enter the brust time of process %d
=",i+1); scanf("%d",&temp.brust);
printf("\n enter the type of(1-back,2-forground,3-other) process %d
=",i+1); scanf("%d",&temp.pt);
printf("\n enter the feedback of process(0-for normal,1-I/O) %d =",i+1);
scanf("%d",&temp.f);
if(temp.pt>=2 && temp.f==1)
insert(&que[temp.pt-2],temp);
else if(temp.f==0)
insert(&que[temp.pt-1],temp);
else
insert(&que[temp.pt-1],temp);
}
pstack(que);
fcfs_sort(que);
printf("\n\nafter sorting of process list is \n\n");
pstack(que);
pque(que);
printf("\n\nafter processing cpu and wait is \n\n");
pstack(que);
return 0;
}

Input: Program 3(iv):


Process Arival Time Brust TimeProcess_TypeI/O
P1 0 2 1 0
P2 2 5 1 0
P3 1 3 1 0
P4 1 3 2 0
P5 0 4 2 0
P6 2 4 2 0
P7 1 3 3 0
P8 3 4 3 0
P9 2 6 3 0
Output:
queue 1 is
Process arive brust type I/O cpu wait
1 0 2 1 0 0 0
2 2 5 1 0 0 0
3 1 3 1 0 0 0
queue 2
is arive brust type I/O cpu wait
Process
4 1 3 2 0 0 0
5 0 4 2 0 0 0
6 2 4 2 0 0 0

queue
3 is arive brust type I/O cpu wait
Proce
ss
7 1 3 3 0 0 0
8 3 4 3 0 0 0
9 2 6 3 0 0 0

after sorting of process list is queue 1 is


Process arive brust type I/O cpu wait
1 0 2 1 0 0 0
3 1 3 1 0 0 0
3 1 3 1 0 0 0

queue 2 is
Process arive brust type I/O cpu wait
5 0 4 2 0 0 0
5 0 4 2 0 0 0
6 2 4 2 0 0 0

queue 3 is
Process arive brust type I/O cpu wait
7 1 3 3 0 0 0
9 2 6 3 0 0 0
9 2 6 3 0 0 0

after processing cpu and wait isqueue 1 is


Process arive brust type I/O cpu wait
1 0 2 1 0 0 0
3 1 3 1 0 2 1
3 1 3 1 0 5 4

queue 2
is Process arive brust type I/O cpu wait
5 0 4 2 0 8 8
5 0 4 2 0 12 12
6 2 4 2 0 16 14

queue 3
is Process arive brust type I/O cpu wait
7 1 3 3 0 20 19
9 2 6 3 0 23 21
9 2 6 3 0 29 27
EXPERIMENT – 4
Objective - Implement file storage allocation technique:
i. Contiguous(using array)
ii. Linked –list(using linked-list)
iii. Indirect allocation (indexing)
Theory –
1. Contiguous Allocation
About Contiguous allocation technique:
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a file
requires n blocks and is given a block b as the starting location, then the blocks assigned to the file
will be: b, b+1, b+2,……b+n-1. This means that given the starting block address and the length of
the file (in terms of blocks required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation
contains Address of starting block
Length of the allocated portion.

4(i). Example program for Contiguous(using array)file storage allocation technique


#include<stdio.h>
struct filep {
int id;
int start;
int count;
};
typedef struct filep files;
int main() {
int disk[100];
int i,j,k;
for(i=0;i<100;i+
+) disk[i]=-1;
files f[3];
for(i=0;i < 3;i++) {
f[i].id=i;
printf("enter the number of blocks for file : %d", i);
scanf("%d", &f[i].count);
}
int flag = 0;
for(i=0;i<3;i++) {
flag=0;
for(j=0;j<100;j++){
if(disk[j] == -1) {
for(k=j;k< j+f[i].count && disk[k] == -1;k++) {
flag++;
printf("\n flag for file %d : %d\n", i, flag);
}
if(flag == f[i].count) {
for(k=j;k< j+f[i].count;k++) {
f[i].start = j;
disk[k]=f[i].id;
}
j=j+f[i].count;
break;
}
else
{ j=j+flag;
flag=0;

}}
if(j==99 && flag == 0)
printf("\n disk is full \n");
}}
printf("\n here is the disk allocation details \n");
for(i=0;i<100;i++)
printf("%d ", disk[i]);
return 0;
}
Input: Program 4(i):
Disk Size = 100 (Block (all of equal size))

File ID Block_Count
File 0 5
File 1 3
File 2 2

Output:
here is the disk allocation details
0 0 0 0 0 1 1 1 2 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -
1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

2. Linked List
About Linked List Allocation technique:
In this scheme, each file is a linked list of disk blocks which need not be contiguous. The disk blocks
can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each block contains a
pointer to the next block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last block (25)
contains -1 indicating a null pointer and does not point to any other block.

Figure: Linked List file allocation technique


Example program for file storage allocation technique (Linked List Allocation)
#include<stdio.h>
#include<stdlib.h
> struct file{
int id;
int block;
struct file *next_block;
};
typedef struct file f;
int main(){
f *file1, *temp, *temp2;
int i=1,j, count;
printf("enter the number of blocks for file : %d", i);
scanf("%d", &count);
for(j=0;j<count;j++){
if(j==0){
temp=(f*)malloc(sizeof(f))
; temp->id=i;
temp->block=j;
temp->next_block=NULL;
file1=temp;
printf("%d\t", i);
}
else
{ temp=file1;
while ( temp->next_block != NULL)
temp=temp->next_block;
temp2=(f*)malloc(sizeof(f));
temp2->id=i;
temp2-
>block=j;
temp2->next_block=NULL;
temp->next_block = temp2;
printf("%d\t", i);
}
}
printf("\n Here is the disk allocation for file : \n");
temp=file1;
while(temp!=NULL){
printf("file id: %d\t block number: %d\t block address %d\n", temp->id, temp-
>block,(int)temp);
temp=temp-
>next_block;
}
return 0;}
Input:

File ID Block_Count
File 1 5

Output:
Here is the disk allocation for file :
file id: 1 block number: 0 block address 38287408
file id: 1 block number: 1 block address 38287440
file id: 1 block number: 2 block address 38287472
file id: 1 block number: 3 block address 38287504
file id: 1 block number: 4 block address 38287536
file id: 1 block number: 5 block address 38287568

3. Indirect Allocation
About Indirect allocation (indexing) technique:
In this scheme, a special block known as the Index block contains the pointers to all the blocks occupied
by a file. Each file has its own index block. The ith entry in the index block contains the disk address of
the ith file block. The directory entry contains the address of the index block as shown in the image:

Figure: Indirect (index) file allocation technique


Example program for file storage allocation technique (Indirect (index))
#include<stdio.
h>
#include<stdlib
.h>struct file{
int id;
int block;
struct file *next_block;
};
typedef struct
file f;int main(){
f *files[3], *temp, *temp2;int
count, i, j, id; for(i=0;i<3;i++){
printf("enter the number of blocks for file : %d", i);scanf("%d",
&count);
for(j=0;j<count;j++)

{ if(j==0){
temp=(f*)malloc(sizeof(f));temp-
>id=i; temp->block=j;
temp->next_block=NULL;files[i]=temp;
printf("%d\t", i);
}
else{
temp=files[i];
while ( temp->next_block != NULL)
temp=temp-
>next_block;
temp2=(f*)malloc(
sizeof(f));temp2-
>id=i;
temp2->block=j;
temp2-
}}} >next_block=NU
temp=files[id]; LL; temp-
while(temp!=NULL){ >next_block =
temp2; printf("%d\
t", i);
printf("file id: %d\t block number: %d\t block address %d\n", temp->id, temp-
>block,(int)temp); temp=temp-
>next_block;
}
return 0;
}
Input: Program 4(iii):

File ID Block_Count
File 0 5
File 1 4
File 2 6
file id: block number: block address 35997744 Output:
0 0 disk allocation using
file id: block number: block address 35997776 index as file id
0 1
file id: block number: block address 3599780
0 2
file id: block number: block address 35997840
0 3
file id: block number: block address 35997872
0 4
file id: block number: block address 35997904
1 0
file id: block number: block address 35997936
1 1
file id: block number: block address 35997968
1 2
file id: block number: block address 35998000
1 3
file id: block number: block address 35998032
2 0
file id: block number: block address 35998064
2 1
file id: block number: block address 35998096
2 2
file id: block number: block address 35998128
2 3
file id: block number: block address 35998160
2 4
file id: block number: block address 35998192
2 5
EXPERIMENT – 5
Objective - Implementation of contiguous allocation techniques:
i. Worst-Fit
ii. Best-Fit
iii. First-Fit

Theory –
1. Worst Fit
Worst Fit Allocate the process to the partition which is the largest sufficient among the freely available
partitions available in the main memory.
Algorithm
Step 1. Input the total number of blocks and their size. Step 2.
Input the total number of processes and their size.Step 3. For
each process in list
Find free block having max size and greater than process sizeAllocate block to
process

Example program for implementation of contiguous allocation techniques: Worst-Fit


#include<stdio.h>
#include<stdlib.h> int
disk[20];
struct blocks{
int bid; int size; int
index;
}block;
struct processs{
int pid; int size; int
flag;
struct blocks b; int
fragment;
}process;

int main(){
int nb, np, i , j; int
disk_index=0;
printf(" Total disk size is 50 \n"); printf(" enter
numebr of blocks: ");scanf("%d", &nb);
struct blocks bls[nb], temp;
for(i=0;i<nb;i++)

{ bls[i].bid=i;
printf("enter the size of block %d ", i);scanf("%d",
&bls[i].size); bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;

}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);scanf("%d",
&ps[i].size);
ps[i].flag=-1;

}
for(i=0;i<nb-1;i++){
for(j=i+1;j<nb;j++){
if(bls[j].size > bls[i].size){ temp.bid = bls[j].bid; temp.size =
bls[j].size; temp.index = bls[j].index;bls[j].bid = bls[i].bid;
bls[j].size = bls[i].size; bls[j].index = bls[i].index;bls[i].bid =
temp.bid; bls[i].size = temp.size; bls[i].index = temp.index;
}}}
for(i=0;i<np;i++){
if(i >= nb || bls[i].size < ps[i].size)
printf("\n no block is available for the process \n");
else
ps[i].b=bls[i];
}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid,ps[i].size, ps[i].b.bid,
ps[i].b.size-ps[i].size);
}
return 0;
}
Input: Program 5(i):
Block ID Block_size
Block 0 5
Block 1 4
Block 2 8
Block 3 3

Process ID Process_size
Process 0 3
Process 1 2
Process 2 4

Output:
now process block allocation list is
process id: 0 process size 3 block id 2 free space 5
process id: 1 process size 2 block id 0 free space 3
process id: 2 process size 4 block id 1 free space 0
2. Best-Fit
Best Fit Allocate the process to the partition which is the first smallest sufficient partition among the
free available partition.
Algorithm
Step 1. Input the total number of blocks and their size.
Step 2. Input the total number of processes and their size.
Step 3. For each process in list
Find free block where, block_size - process _size = minimum
Allocate block to process

Example program for implementation of contiguous allocation techniques: Best-Fit


#include<stdio.h>
#include<stdlib.h
> int disk[20];
struct blocks{
int bid;
int size;
int index;
}block;
struct processs{
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i , j;
int
disk_index=0;
printf(" Total disk size is 20 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb];
for(i=0;i<nb;i++){
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
int temp,temp2=20, bindex, pindex;
for(i=0; i<nb;i++){
for(j=0;j<np;j++){
temp = bls[i].size - ps[j].size;
if(temp < 0){
continue;
}
else if(temp < temp2) {
if(ps[j].flag == -1){
pindex=j;
temp2=temp;
}}}
ps[pindex].b = bls[i];
ps[pindex].flag = 1;
temp2=20;
}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid,
ps[i].size, ps[i].b.bid, ps[i].b.size-ps[i].size);
}
return 0;
}

Input: Program 5(ii):

Block ID Blocksize
Block 0 5
Block 1 4
Block 2 8
Block 3 3

Process ID Process size


Process 0 3
Process 1 2
Process 2 4

Output:
now process block allocation list is
process id: 0 process size 3 block id 1 free space 1
process id: 1 process size 2 block id 3 free space 1
process id: 2 process size 4 block id 0 free space 1

3. First Fit
First Fit: In the first fit, the partition is allocated which is first sufficient block from the top of Main
Memory.
Algorithm
Step 1. Input the total number of blocks and their size.
Step 2. Input the total number of processes and their size.
Step 3. For each process in list
Find free block where, block size > process _size
Allocate block to process
Example program for implementation of contiguous allocation techniques: First-Fit
#include<stdio.h>
#include<stdlib.h
> int disk[20];
struct blocks{
int bid;
int size;
int index;
int status;
}block;
struct processs{
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i , j;
int
disk_index=0;
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb], temp;
for(i=0;i<nb;i++){
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
bls[i].status = -1;
}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++)

{
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
for(i=0;i<np;i++){
for(j=0;j<nb;j++) {
if(bls[j].size > ps[i].size && bls[j].status ==-1)

{ ps[i].b=bls[j];
bls[j].status = 1;
break;
}}}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid,
ps[i].size, ps[i].b.bid, ps[i].b.size-ps[i].size);
}
return 0;
}

Input: Program 5(iii):

Block ID Block_size
Block 0 5
Block 1 4
Block 2 8
Block 3 3

Process ID Process_size
Process 0 3
Process 1 2
Process 2 4

Output:
now process block allocation list is
process id: 0 process size 4 block id 0 free space 1
process id: 1 process size 2 block id 1 free space 2
process id: 2 process size 3 block id 2 free space 5
EXPERIMENT – 6
Objective – Calculation of Internal and External fragmentation
i. Free space list of blocks from system
ii. List process file from the system
Theory –

1. Fres space list of blocks from system


External Fragmentation:
External fragmentation happens when there’s a sufficient quantity of area within the memory to satisfythe
memory request of a method. However, the process’s memory request cannot be fulfilled because the
memory offered is during a non-contiguous manner. Either you apply first-fit or best-fit memory
allocation strategy it’ll cause external fragmentation.

Figure. External Fragmentation in System


Example program for Free space list of blocks from system
#include<stdio.h>
#include<stdlib.h>
int disk[20];
struct blocks{
int bid;
int size;
int index;
int status;
}block;
struct processs{
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i , j;
int
disk_index=0;
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb], temp;
for(i=0;i<nb;i++){
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
bls[i].status = -1;
}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
for(i=0;i<np;i++){
for(j=0;j<nb;j++){
if(bls[j].size > ps[i].size && bls[j].status ==-1){
ps[i].b=bls[j];
bls[j].status = 1;
break;
}}}
printf(" \n now Free space list of blocks is \n");
int free=0;
for(i=0;i<nb;i++)
if(bls[i].status == -1){
printf(" Block id: %d Block size %d block index %d \n", bls[i].bid, bls[i].size,
bls[i].index);
free= free + bls[i].size;
}}
printf(" \n Total external free space is: %d \n", free);
return 0;
}
Input: Program 6:
Disk size = 20

Block ID Blocksize
Block 0 5
Block 1 4
Block 2 8
Block 3 3

Process ID Process size


Process 0 4
Process 1 6
Process 2 2

Output:
now Free space list of blocks is
Block id: 2 Block size 3 block index 9
Total external free space is: 3
2. List process file from the system
Internal Fragmentation:
Internal fragmentation happens when the memory is split into mounted sized blocks. Whenever, a
method request for the memory, the mounted sized block is allotted to the method. Just in case, the
memory allotted to the method is somewhat larger than the memory requested, then the distinction
between allotted and requested memory is that the internal fragmentation.

Figure. Internal Fragmentation


Example program for List process file from the system
#include<stdio.h>
#include<stdlib.h>
int disk[20];
struct blocks{
int bid;
int size;
int index;
int status;
}block;
struct processs{
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i , j;
int
disk_index=0;
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb], temp;
for(i=0;i<nb;i++){
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
bls[i].status = -1;
}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
for(i=0;i<np;i++){
for(j=0;j<nb;j++){
if(bls[j].size > ps[i].size && bls[j].status ==-1){
ps[i].b=bls[j];
bls[j].status = 1;
break;
}}}
printf(" \n now process block allocation list is \n");
int t_free=0;
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid,
ps[i].size, ps[i].b.bid, ps[i].b.size-ps[i].size);
t_free = t_free + (ps[i].b.size-ps[i].size);
}
printf(" \n Total internal fragmentation space is: %d \n", t_free);
return 0;
}

Input: Program 6(ii):

Block ID Block_size
Block 0 5
Block 1 4
Block 2 8
Block 3 3

Process ID Process_size
Process 0 3
Process 1 2
Process 2 4

Output:
now process block allocation list is
process id: 0 process size 4 block id 0 free space 1
process id: 1 process size 2 block id 1 free space 2
process id: 2 process size 3 block id 2 free space 5

Total internal fragmentation space is: 8


EXPERIMENT – 7
Objective – Implementation of compaction for the continually changing memory layout and
calculate total movement of data

Theory –
Memory compaction
The longstanding memory fragmentation problem has been covered many times in these pages. In
short: as the system runs, pages tend to be scattered between users, making it hard to find groups of
physically-contiguous pages when they are needed. Much work has gone into avoiding the need
for higher-order (multi-page) memory allocations whenever possible, with the result that most
kernel functionality is not hurt by page fragmentation. But there are still situations where higher-
order allocations are needed; code which needs such allocations can fail on a fragmented system.

Example:

Figure: Memory status (where red part are the occupied block and white cells free memory)

Figure: Move the free space one side and occupied


space on another side

Figure: Memory after compaction

Example program for Implementation of compaction for the continually changing


memory layout and calculate total movement of data
#include<stdio.h>
#include<stdlib.h
> int disk[20];
struct blocks{
int bid;
int size;
int index;
int status;
}block;
struct processs{
int pid;
int size;
int flag;
struct blocks b;
int fragment;
}process;
int main(){
int nb, np, i , j;
int disk_index=0;
printf(" Total disk size is 50 \n");
printf(" enter numebr of blocks: ");
scanf("%d", &nb);
struct blocks bls[nb],
temp; for(i=0;i<nb;i++) {
bls[i].bid=i;
printf("enter the size of block %d ", i);
scanf("%d", &bls[i].size);
bls[i].index=disk_index;
disk_index=disk_index+bls[i].size;
bls[i].status = -1;
}
printf(" enter numebr of process: ");
scanf("%d",&np);
struct processs ps[np];
for(i=0;i<np;i++){
ps[i].pid=i;
printf("enter the size of process %d ", i);
scanf("%d", &ps[i].size);
ps[i].flag=-1;
}
for(i=0;i<np;i++){
for(j=0;j<nb;j++) {
if(bls[j].size > ps[i].size && bls[j].status ==-1){
ps[i].b=bls[j];
bls[j].status = 1;
break;
}}}
printf(" \n now process block allocation list is \n");
for(i=0;i<np;i++){
printf(" process id: %d process size %d block id %d free space %d\n", ps[i].pid, ps[i].size,
ps[i].b.bid, ps[i].b.size-ps[i].size);
}
printf(" \n now Free space list of blocks is \n");
int free=0;
for(i=0;i<nb;i++){
printf(" Block id: %d Block size %d block index %d \n", bls[i].bid, bls[i].size,
bls[i].index);
}
int sid, count=0;
for(i=0;i<nb-1;i++)
{
count = 0;
for(j=1; j<nb;j++){
if(bls[i].status == -1 && bls[j].status !=-1){
sid = bls[j].index;
bls[j].index= bls[i].index;
bls[i].index = sid;
}
else{
count = count +1;
i=i+count;
}
}
i=i-count;
}
printf(" \n now After compaction list of blocks is \n");
free=0;
for(i=0;i<nb;i++){
if(bls[i].status == -1){
printf(" Block id: %d Block size %d New block index %d \n", bls[i].bid, bls[i].size,
bls[i].index);
free= free + bls[i].size;
}
}
printf(" \n Total external free space is: %d \n", free);
return 0;
}

Input: Program 7:

Block ID Block_size
Block 0 3
Block 1 8
Block 2 5
Block 3 4

Process ID Process_size
Process 0 4
Process 1 2

Output:
now process block allocation list is

process id: 0 process size 4 block id 1 free space 4


process id: 1 process size 4 block id 2 free space 1

now Free space list of blocks is


Block id: 0 Block size 3 block index 0
Block id: 1 Block size 8 block index 3
Block id: 2 Block size 5 block index 11
Block id: 3 Block size 4 block index 16
now After compaction list of blocks is
Block id: 0 Block size 3 New block index 11
Block id: 3 Block size 4 New block index 16
Total external free space is: 7
EXPERIMENT – 8
Objective – Implementation of Resource Allocating Graph (RAG).

Theory –
Resource Allocation Graph (RAG)
As Banker’s algorithm using some kind of table like allocation, request, available all that thing to
understand what is the state of the system. Similarly, if you want to understand the state of the
system instead of using those table, actually tables are very easy to represent and understand it, but
then still you could even represent the same information in the graph. That graph is called Resource
Allocation Graph (RAG).
So, resource allocation graph is explained to us what is the state of the system in terms of processes
and resources. Like how many resources are available, how many are allocated and what is the
request of each process. Everything can be represented in terms of the diagram. One of the
advantages of having a diagram is, sometimes it is possible to see a deadlock directly by using RAG,
but then you might not be able to know that by looking at the table. But the tables are better if the
system contains lots of process and resource and Graph is better if the system contains less number
of process and resource.
We know that any graph contains vertices and edges. So RAG also contains vertices and edges. In
RAG vertices are two type –
1. Process vertex – Every process will be represented as a process vertex.Generally, the process
will be represented with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex.

Figure: Resource Allocation Graph


8. Example program for Implementation of resource allocation graph RAG)
#include<stdio.h>
int main(){
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++) {
for(j=0; j<np+nr;j++)

{ rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++){
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++){
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++){
for(j=0; j<np+nr;j++){
printf("%d ", rag[i][j]);
}
printf("\n ");
}
return 0;
}

Input: Program 8:

Process ID Resource Handling Resource Requesting


Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1

Output:
P0 P1 P2 R0 R1 R2
P0 0 0 0 0 0 1
P1 0 0 0 1 0 0
P2 0 0 0 0 1 0
R0 1 00 0 0 0
R1 0 1 0 0 0 0
R2 0 0 1 0 0 0
EXPERIMENT – 9
Objective – Implementation of Banker’s Algorithm
Theory –
Banker’s Algorithm
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety
bysimulating the allocation for predetermined maximum possible amounts of all resources, then makes
an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to
continue. Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available:
 It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
 Available[ j ] = k means there are ‘k’ instances of resource type
Rj Max:
 It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
 Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation:
 It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to
each process.
 Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need :
 It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
 Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
for its execution.
 Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional
resourcesthat process Pi may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm.
Safety Algorithm
1) Let Work and Finish be vectors of length ‘m’ and ‘n’
respectively. Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi<= Work
if no such i exists goto step (4)
3) Work = Work +
Allocation[i] Finish[i] =
truegoto
step (2)
4) if Finish [i] = true for all
i then the system is in a safe
stateResource-Request
Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of
resource type Rj. When a request for resources is made by process Pi, the following actions are taken:
1)
If Requesti<= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim.
2)
If Requesti<= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3)
Have the system pretend to have allocated the requested resources to process Pi by modifying
the state asfollows:
Available = Available – Requesti
Allocationi = Allocationi +
RequestiNeedi = Needi– Requesti
Example program for Implementation of Banker‟s algorithm
#include
<stdio.h>Int
main() {
intn, m, i, j, k;n =
5;
m = 3;
intalloc[5][3] = { { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 2 }, { 2, 1, 1 }, { 0, 0, 2 } };
intmax[5][3] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 }, { 4, 3, 3 } };
intavail[3] = { 3, 3, 2 };
intf[n], ans[n], ind = 0;
for(k = 0; k < n; k++)
f[k]
= 0;
intneed[n][m];
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
inty = 0;
for(k = 0; k < 5; k++) {
for(i = 0; i < n; i++) {
if(f[i] == 0) {
int flag = 0;
for(j = 0; j < m; j++) {
if(need[i][j] > avail[j])
{ flag = 1;break;
}}
if(flag == 0) {
ans[ind++] =
i;
for(y = 0; y < m; y++)
avail[y] += alloc[i] f[i] = 1;
[y];

}} } }
printf("Following is the SAFE Sequence\n");for(i
= 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
return0;
}
Input: Program 9:

Output:
Following is the SAFE
SequenceP1 -> P3 -> P4 ->
P0 -> P2
EXPERIMENT – 10
Objective - Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of
methodused for storing graph.
Theory –
Wait-for-Graph (WFG)
A Wait-For Graph (WFG) is the same as the SRAG with the resource elements stripped
out.Example:

Figure: Resource Allocation Graph

Figure: Equivalent Wait for graph of above resource allocation graph


10. Example program for Implementation of conversion of resource allocation graph (RAG) to wait
forgraph (WFG)
#include<stdio
.h>int main(){
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];int i,
j; for(i=0;i<np+nr;i++){
for(j=0; j<np+nr;j++){
rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);scanf("%d",
&temp);
for(j=0; j<temp;j++){
printf("enter the ressorce number process %d holding: ", j);scanf("%d",
&temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);scanf("%d",
&temp);
for(j=0; j<temp;j++){
printf("enter the ressorce number process %d requesting: ", i);scanf("%d",
&temp1);
rag[i][np+temp1]=1;
}}
for(i=0;i<np+nr;i++) {
for(j=0; j<np+nr;j++) {
printf("%d ", rag[i][j]);
}
printf("\n ");
}
int wfg[np][np];
for(i=0;i<np;i++){
for(j=0; j<np;j++){
wfg[i][j]=0;
}
}
int k;
for(i=0;i<np;i++){
for(j=np;j<np + nr; j++){if(rag[i][j]
== 1){
for(k=0;k<np;k++){
if(rag[j][k] == 1)
wfg[i][k] = 1;
}
}
}
}
for(i=0;i<np;i++){
for(j=0; j<np;j++){
printf("%d ", wfg[i][j]);
}
printf("\n ");
}
return 0;
}
Input: Program 10:
Process Resource Resource
ID Holding Requesting
Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1
Output:

P0 P1 P2 R0 R1 R2
P0 0 0 0 0 0 1
P1 0 0 0 1 0 0
P2 0 0 0 0 1 0
R0 1 0 0 0 0 0
R1 0 1 0 0 0 0
R2 0 0 1 0 0 0
WEG
P0 P1 P2
P0 0 0 1
P1 1 0 0
P2 0 1 0

You might also like