0% found this document useful (0 votes)
51 views59 pages

OS Lab Manual: CSE IV Sem Experiments

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)
51 views59 pages

OS Lab Manual: CSE IV Sem Experiments

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

LAB MANUAL

Branch : BTech. - CSE

Year & Semester : II Year / IV Semester

KCS-451 OPERATING SYSTEM LABORATORY


ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OPERATING SYSTEMS LABORATORY(KCS – 451)

LIST OF EXPERIMENTS:

Objective: To understand the Operating System and create various program using C language.
1. Study of hardware and software requirements of different
operating systems
(UNIX,LINUX,WINDOWS,XP,WINDOWS7/8)
2. Execute various system calls for i. Process Management ii. File management
iii. Input/output system call
3. Implement FCFS CPU Scheduling Policies: i. SJF ii. Priority iii. FCFS iv. Multilevel
Queue
4. Write Implement file storage allocation technique:
1. Contiguous(using array)
2. Linked-List(using linked -list)
3. Indirect allocation(indexing)

5. Implementation of contiguous allocation techniques: i. First fit ii. Best fit iii. Worst
fit
6. Calculation of external and internal fragmentation
1. i. List process file from the system
2. Free space list of blocks from system
7. Implementation of compaction for the continually memory layout and calculate
total movement of data.
8. Implementation of resource allocation graph(RAG
9. Implementation Bankers Algorithm
10. Simulate Conversion of resource allocation 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 communication techniques-Semaphores

Operating System

Introduction

A computer system can be divided into 4 components:

- Hardware (CPU, memory, input/output devices, etc.),


- Operating system,
- System programs (word processors, spread sheets, accounting software’s,
compilers,)
- Application programs.

2
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
In 1960’s definition of an operating system is “software that controls the
hardware”. However, today, due to microcode we need a better definition. We see an
operating system as the programs that make the hardware useable. In brief, an operating
system is the set of programs that controls a computer.

An Operating system is software that creates a relation between the User, Software
and Hardware. It is an interface between the all. All the computers need basic software
known as an Operating System (OS) to function.

The OS acts as an interface between the User, Application Programs, Hardware


and the System Peripherals. The OS is the first software to be loaded when a computers
starts up. The entire application programs are loaded after the OS.

Types of Operating System (Based of No. of user):

1. Single User: If the single user Operating System is loaded in computer’s memory;
the
computer would be able to handle one user at a time.

Ex: MS-Dos, MS-Win 95-98, Win-ME

1. Multi user: If the multi-user Operating System is loaded in computer’s memory;


the
computer would be able to handle more than one user at a time.
Ex: UNIX, Linux, XENIX
2. Network: If the network Operating System is loaded in computer’s memory; the
computer would be able to handle more than one computer at time.
Ex: Novel Netware, Win-NT, Win-2000-2003

3
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

Command Prompt Interface:

Operating System provides a text based interface called command prompt. From the command
prompt commands can be issued to perform file and disk management and to run program. Results of these
commands are presented to the user as text message.

C:\>-

The command prompt can be an alphabet followed by one colon (:), one back slash (\), one greater
than sign (>) and one blinking element called cursor (_).

Where C: represents the Drive letter (Current Drive)

\ represents the current folder / Directory

> represents the end of the Prompt and

_ blinking element (represents the Cursor)

4
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

Operating Systems Types

Single- And Multi-Tasking Operating Systems


A single-tasking system can only run one program at a time, while a multi-tasking operating system
allows more than one program to be running in concurrency. This is achieved by time-sharing, dividing the
available processor time between multiple processes that are each interrupted repeatedly in time slices by a
task-scheduling subsystem of the operating system.

Multi-tasking may be characterized in preemptive and co-operative types. In preemptive multitasking, the
operating system slices the CPU time and dedicates a slot to each of the programs. Unix-like operating
systems, e.g., Solaris, Linux, as well as AmigaOS support preemptive multitasking.

Single- And Multi-User Operating Systems


Single-user operating systems have no facilities to distinguish users, but may allow multiple programs to run
in tandem. A multi-user operating system extends the basic concept of multi-tasking with facilities that
identify processes and resources, such as disk space, belonging to multiple users, and the system permits
multiple users to interact with the system at the same time. Time-sharing operating systems schedule tasks for
efficient use of the system and may also include accounting software for cost allocation of processor time,
mass storage, printing, and other resources to multiple users.

Distributed Operating Systems


A distributed operating system manages a group of distinct computers and makes them appear to be a single
computer. The development of networked computers that could be linked and communicate with each other
gave rise to distributed computing. Distributed computations are carried out on more than one machine.
When computers in a group work in cooperation, they form a distributed system.

Embedded Operating Systems


Embedded operating systems are designed to be used in embedded computer systems. They are designed to
operate on small machines like PDAs with less autonomy. They are able to operate with a limited number of
resources. They are very compact and extremely efficient by design. Windows CE and “Minix 3”are some
examples of embedded operating systems.
Real-Time Operating Systems :A real-time operating system is an operating system that guarantees to process
events or data by a specific moment in time. A real-time operating
system may be single- or multi-tasking, but when multitasking, it uses specialized scheduling algorithms so that a
deterministic nature of behavior is achieved. An event-driven system switches between tasks based on their
priorities or external events while time-sharing operating systems switch tasks based on clock interrupts

Process Scheduling

Processes are the Small Programs those are executed by the user according to their Request. CPU
Executes all the Process according to Some Rules or Some Schedule. Scheduling ist hat in which
each process have Some Amount of Time of CPU. Scheduling Provides Time of CPU to the Each
Process.

5
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

Types of Process Scheduling

1. FCFS Scheduling Algorithm


The First Come First Served (FCFS) Scheduling Algorithm is the simplest one. In this algorithm the set of
ready processes is managed as FIFO (first-in-first-out) Queue. The processes are serviced by the CPU until
completion in order of their entering in the FIFO queue.

A process once allocated the CPU keeps it until releasing the CPU either by terminating or requesting I/O.
For example, interrupted process is allowed to continujre running after interrupt handling is done with.

2. SJF Scheduling Algorithm


The Shortest Job First Scheduling Algorithm chooses the process that has the smallest next CPU burst.

3.SRTF: Shortest Remaining Time First


This is the preemptive version of SJF. The currently executing process will be preempted from the CPU if a
process with a shorter CPU burst time is arrived.

4.Round Robin Scheduling


This scheduling algorithm is designed especially for time sharing systems. It is similar to FCFS scheduling,
but preemption is added to switch between processes.

6
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO. 1
AIM :Study of hardware and software requirements of different operating systems
(UNIX,LINUX,WINDOWS,XP,WINDOWS 7/8)

Theory:
Hardware requirement

Processor: intel Core i5 and above(such as Intel Core i7, or Intel Core i9)
HARD DRIVE: 128 Gigabytes
Memory: 8GB
Wired Networking: Ethernet LAN Port or USB Ethernet
Adapter/Dongle Wireless Networking: 802.11n

Operating system requirement:

Supported Operating System(s)


1. Windows 8,
2. Windows 8.1
3. Windows 10
4. macOS 10.14 (Mojave)
5. macOS 10.15 (Catalina).

Unsupported Operating System(s)

1. macOS 10.16 (Big Sur), as well as other BETA and newly released versions of any
OperatingSystem(s) cannot be guaranteed to work with the Business School resources, including
the wired and wireless networks.
2. Windows RT,
3. Windows 7 and
4. older versionsUnix, Linux, macOS 10.13 (High Sierra) and older.

Software requirement:

Office Suite Microsoft Office 365 (includes Office 365 for Mac and Office 365 ProPlus for
Windows) is the version deployed by ITG. For Windows, you may be able to use an earlier
version, 2016 or higher, if you cannot upgrade your current version.

Your Office Suite must be in English. Full installation is provided free of charge by ITG if
needed. Computer Security Antivirus and Spyware Protection Must be updated with the latest

7
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

virus definitions/updates. Required for both Mac and Windows

EXP NO: 2
Execute various system calls for i. Process Management ii. File
management iii. Input/output system call
2(A):
AIM: To write c program to implement the Process system calls.

ALGORITHM:

• Start the program.


• Declare the pid and get the pid by using the getpid() method.
• Create a child process by calling the fork() system call
• Check if(pid==0) then print the child process id and then print the parent
process value. Otherwise print
• Stop the program

8
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM: (PROCESS SYSTEM CALLS)

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

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

int pid; pid=fork();

if(pid<0)

printf("fork failed");

exit(1);

else if(pid==0)

execlp("whoami","ls",NULL);

exit(0);

else

printf("\n Process id is -%d\n",getpid());

wait(NULL);

exit(0);

9
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

RESULT:

Thus the process system call program was executed and verified successfully.

10
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP.NO : 2 (B)
IO SYSTEM CALLS

AIM: To write a ‘c’ program for I/O system calls.

ALGORITHM:

1. Start the program.


2. open a file for O_RDWR for R/W,O_CREATE for creating a file , O_TRUNC for
truncate a file
3. Using getchar(), read the character and stored in the string[] array
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and displayed It and
close the file
6. Stop the program

11
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM :( IO SYSTEM CALLS)

#include<stdio.h>

#include<unistd.h>

#include<string.h>

#include<fcntl.h>

main( )

int fd[2];

char buf1[25]= "just a test\n";

char buf2[50];

fd[0]=open("file1",O_RDWR);

fd[1]=open("file2",O_RDWR);

write(fd[0], buf1, strlen(buf1));

printf("\n Enter the text

now…."); scanf("\n %s",buf1);

printf("\n Cat file1 is \n hai");

write(fd[0], buf1, strlen(buf1));

lseek(fd[0], SEEK_SET, 0);

read(fd[0], buf2, sizeof(buf1));

write(fd[1], buf2, sizeof(buf2));

close(fd[0]);

close(fd[1]);

printf("\n");

return 0;

12
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

RESULT:

Thus the I/O system call program was executed and verified successfully.

13
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO: 3 Implement CPU Scheduling Policies: i. SJF ii. Priority iii. FCFS
iv. Multilevel Queue

EXP. NO:3 (i) SHORTEST JOB FIRST SCHEDULING

AIM : To write a program to implement cpu scheduling algorithm for shortest job first scheduling.

ALGORITHM:

1. Start the program. Get the number of processes and their burst time.
2. Initialize the waiting time for process 1 as 0.
3. The processes are stored according to their burst time.
4. The waiting time for the processes are calculated a follows:
for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1].
5. The waiting time of all the processes summed and then the average time is calculate
6. The waiting time of each processes and average time are displayed.
7. Stop the program.

14
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM: (SHORTEST JOB FIRST SCHEDULING)

#include<stdio.h>

#include<conio.h>

struct process

int pid;

int bt;

int wt;

int tt;

}p[10],temp;

int main()

int

i,j,n,totwt,tottt;

float avg1,avg2;

clrscr();

printf("\nEnter the number of process:\t");

scanf("%d",&n);

for(i=1;i<=n;i++)

p[i].pid=i;

printf("\nEnter the burst time:\t");

scanf("%d",&p[i].bt);

for(i=1;i<n;i++){

for(j=i+1;j<=n;j++)

if(p[i].bt>p[j].bt)

15
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

temp.pid=p[i].pid;

16
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

p[i].pid=p[j].pid;

p[j].pid=temp.pid;

temp.bt=p[i].bt;p[i].bt=p[j].bt;

p[j].bt=temp.bt;

}}}

p[1].wt=0;

p[1].tt=p[1].bt+p[1].wt;

i=2;

while(i<=n){

p[i].wt=p[i-1].bt+p[i-1].wt;

p[i].tt=p[i].bt+p[i].wt; i+

+;

i=1;

totwt=tottt=0;

printf("\nProcess id \tbt \twt \ttt");

while(i<=n){

printf("\n\t%d \t%d \t%d t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);

totwt=p[i].wt+totwt;

tottt=p[i].tt+tottt; i+

+;

} avg1=totwt/n;

avg2=tottt/n;

printf("\nAVG1=%f\t AVG2=

%f",avg1,avg2); getch();

return 0; }

17
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

enter the number of process 3

enter the burst time: 2

enter the burst time: 4

enter the burst time: 6

processid bt wt tt

1 2 0 2

2 4 2 6

3 6 6 12

AVG1=2.000000 AVG2=6.000000

RESULT:

Thus the SJF program was executed and verified successfully

18
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP. NO:3 (ii)

PRIORITY SCHEDULING
AIM : To write a ‘C’ program to perform priority scheduling.

ALGORITHM:

1. Start the program.


2. Read burst time, waiting time, turn the around time and priority.
3. Initialize the waiting time for process 1 and 0.
4. Based up on the priority process are arranged
5. The waiting time of all the processes is summed and then the average waiting time
6. The waiting time of each process and average waiting time are displayed based on the
priority.
7. Stop the program.

19
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM: (PRIORITY SCHEDULING)

#include<stdio.h>
#include<conio.h>
struct process
{

int pid;
int bt;
int wt;
int tt;
int prior;
}

p[10],temp;
int main()
{

int i,j,n,totwt,tottt,arg1,arg2;
printf("Enter the number of process: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("Enter the burst time: ");
scanf("%d",&p[i].bt);
printf("Enter the priority: ");
scanf("%d",&p[i].prior);
}

for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{

if(p[i].prior>p[j].prior)

temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;

p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
}

p[i].wt=0;

p[1].tt=p[1].bt+p[1].wt;

20
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

i=2;
while(i<=n)

p[i].wt=p[i-1].bt+p[i-1].wt;

p[i].tt=p[i].bt+p[i].wt;
i++;
}

i=1;

totwt=tottt=0;

printf("process id\tbt\twt\ttt");
while(i<=n)
{
printf("\n%d\t%d\t%d\t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;

i++;

arg1=totwt/n;
arg2=tottt/n;
printf("\nAvg1 = %d",arg1);
printf("\nAvg2 = %d\n",arg2);
return 0;

21
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

enter the no of process:3

enter the burst time:2

enter the priority:3

enter the burst time:4

enter the priority:1

enter the burst time:6

enter the priority:2

process to Bt wt tt

1 4 0 4 4

2 6 4 10 14

3 2 10 12 22

avg1=4 avg2=8

RESULT:

Thus the priority scheduling program was executed and verified successfully

22
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO 3(iii):

FIRST COME FIRST SERVE SCHEDULING


AIM :To write the program to implement CPU & scheduling algorithm for first come first serve
scheduling.

ALGORITHM:

1. Start the program.


2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 and 0.
4. Process for(i=2;i<=n;i++),wt.p[i]=p[i-1]+bt.p[i-1].
5. The waiting time of all the processes is summed then average value time is calculated.
6. The waiting time of each process and average times are displayed
7. Stop the program

23
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM :( FIRST COME FIRST SERVE SCHEDULING)

#include<stdio.h>

struct process

int pid; int bt;

int wt,tt;

p[10];

int main()

int i,n,totwt,tottt,avg1,avg2;

printf("Enter the no of process: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

p[i].pid=i;

printf("Enter the burst time:

");

scanf("%d",&p[i].bt);

p[1].wt=0;

p[1].tt=p[1].bt+p[1].wt;

i=2;

while(i<=n)

p[i].wt=p[i-1].bt+p[i-1].wt;

p[i].tt=p[i].bt+p[i].wt;

i ++;

24
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

i=1;

totwt=tottt=0;

printf("process id\tbt\twt\ttt");

while(i<=n)

printf("\n%d\t%d\t%d\t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);

totwt=p[i].wt+totwt;

tottt=p[i].tt+tottt;

i++;

avg1=totwt/n;

avg2=tottt/n;

printf("\nAvg1 = %d",avg1);

printf("\nAvg2 = %d\n",avg2);

return 0;

OUTPUT:

enter the no of process 3

enter the burst time 2

enter the burst time 4

enter the burst time 6

Process sid bt wt tt

1 2 0 2

2 4 2 6

25
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

3 6 6 12

avg1=2 avg2=6

RESULT:

Thus the FIFO process scheduling program was executed and verified successfull

26
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP. NO. 4: Implement file storage allocation technique:


1. Contiguous (using array)
2. Linked-List (using linked -list)
3. Indirect allocation (indexing)

4(a) SEQUENTIAL:

AIM: To write a C program for implementing sequential file allocation method

DESCRIPTION:
The most common form of file structure is the sequential file in this type of file, a
fixed format is used for records. All records (of the system) have the same length, consisting of
the same number of fixed length fields in a particular order because the length and position of
each field are known, only the values of fields need to be stored, the field name and length for
each field are attributes of the file structure.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order a).
Randomly select a location from availablelocation s1= random(100);

a) Check whether the required locations are free from the selected
location.
if(b[s1].flag==0){
for (j=s1;j<s1+p[i];j++){
if((b[j].flag)==0)count++;
}
if(count==p[i]) break;
}
b) Allocate and set flag=1 to the allocated locations. for(s=s1;s<(s1+p[i]);s++)
{
k[i][j]=s; j=j+1; b[s].bno=s;
b[s].flag=1;
}

Step 5: Print the results file no, length, Blocks allocated.


Step 6: Stop the program

27
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

SOURCE CODE :

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

28
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:
Enter the starting block & length of file
4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk.

29
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO 4(B) LINKED:

AIM: To implement linked file allocation technique.

DESCRIPTION:
In the chained method file allocation table contains a field which points to starting block of
memory. From it for each bloc a pointer is kept to next successive block. Hence, there is no external
fragmentation

ALGORTHIM:
Step 1: Start the program. Step
2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q=
random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations.
While allocating next location address to attach it to previous location

for(i=0;i<n;i++)
{
for(j=0;j<s[i];j++)
{
q=random(100);
if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
if(j>0)
{
}
}
p=r[i][j-1]; b[p].next=q;}
Step 5: Print the results file no, length ,Blocks allocated.
Step 6: Stop the progra

30
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

SOURCE CODE :
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++) f[i]=0;
printf("Enter how many blocks that are already allocated: ");
scanf("%d",&p);
printf("Enter the blocks no's that are already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length: ");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d->%d\n",j,f[j]);
}
else
{
printf("%d->file is already allocated\n",j);
k++;
}
}
printf("If u want to enter one more file? (yes-1/no-0): ");
scanf("%d",&c);
if(c==1)
goto X;
}

31
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

Enter how many blocks that are already allocated


3
Enter the blocks no.s that are already allocated
47
Enter the starting index block & length
379
3->1
4->1 file is already allocated
5->1
6->1
7->1 file is already allocated
8->1
9->1file is already allocated
10->1
11->1
12->1

32
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO. 4(C) INDEXED:

AIM: To implement allocation method using chained method

DESCRIPTION:
In the chained method file allocation table contains a field which points to starting block of
memory. From it for each bloc a pointer is kept to next successive block. Hence, there is no external
fragmentation.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations.

q=random(100);
{
if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
Step 5: Print the results file no, length ,Blocks allocated.
Step 6: Stop the program

33
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

SOURCE CODE :

#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
int main()
{
for(i=0;i<50;i++)
f[i]=0;
x:
printf("Enter index block: ");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("Enter no of files on index: ");
scanf("%d",&n);
}
else
{
goto x;
}
printf("Enter the files: ");
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated\n");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("Allocated");
printf("\nFile indexed\n");
for(k=0;k<n;k++)
printf("%d->%d:%d\n",p,inde[k],f[inde[k]]);
printf("Enter 1 to enter more files and 0 to exit: ");
scanf("%d",&c);
if(c==1)
goto x;
}

34
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT: enter index block 9


Enter no of files on index 3 1 2
3
Allocated File
indexed 9-
>1:1
9->2;1
9->3:1 enter 1 to enter more files and 0 to exit

35
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO 5: Implementation of contiguous memory allocation


techniques:
(i) Worst fit (ii) Best Fit (iii) First Fit

AIM :To implement Worst fit ,best fit ,first fit, algorithm for memory management.

ALGORITHM:

1. Start the program.


2. Get the segment size, number of process to be allocated and their corresponding size.
3. Get the options. If the option is ‘2’ call first fit function.
4. If the option is ‘1’ call best fit function. Otherwise exit.
5. For first fit, allocate the process to first possible segment which is free and set the
personnel slap as ‘1’. So that none of process to be allocated to segment which is already
allocated and vice versa.
6. For best fit, do the following steps,.
7. Sorts the segments according to their sizes.
8. Allocate the process to the segment which is equal to or slightly greater than the process
size and set the flag as the ‘1’ .So that none of the process to be allocated to the segment
which is already allocated and vice versa. Stop the program.
9. Stop the program

36
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{

int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("Enter the number of blocks: ");
scanf("%d",&nb);
printf("Enter the number of files: ");
scanf("%d",&nf);
printf("Enter the size of the blocks\n");
for(i=1;i<=nb;i++)
{
printf("Block %d: ",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files\n");
for(i=1;i<=nf;i++)
{
printf("File %d: ",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("File_no\tFile_size\tBlock_no\tBlock_size\tFragement\n");
for(i=1;i<=nf;i++)
printf("%d\t%d\t%d\t%d\t%d\n", i,f[i],ff[i],b[ff[i]],frag[i]);
}

37
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

Enter the number of blocks: 3

Enter the number of files: 2

Enter the size of the blocks:-

Block 1: 5

Block 2: 2

Block 3: 7

Enter the size of the files:-

File 1: 1

File 2: 4

OUTPUT

File No File Size Block No Block Size Fragment

1 1 3 7 6

2 4 1 5 1

RESULT:

Thus the First Bit and Best Fit program was executed and verified successfully.
38
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

39
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXPERIMENT.NO 6
Calculation of external and internal fragmentation
i. Free space list of blocks from system
ii. List process file from the system

A). MEMORY MANAGEMENT WITH FIXED PARTITIONING TECHNIQUE (MFT)

AIM: To implement and simulate the MFT algorithm.

DESCRIPTION:

In this the memory is divided in two parts and process is fit into it. The process which is best suited
will be placed in the particular memory where it suits. In MFT, the memory is partitioned into fixed size
partitions and each job is assigned to a partition. The memory assigned to a partition does not change. In
MVT, each job gets just the amount of memory it needs. That is, the partitioning of memory is dynamic
and changes as jobs enter and leave the system. MVT is a more ``efficient'' user of resources. MFT suffers
with the problem of internal fragmentation and MVT suffers with external fragmentation.

ALGORITHM:

Step1: Start the process.


Step2: Declarevariables.
Step3: Enter total memory size ms.
Step4: Allocate memory for os.
Ms=ms-os
Step5: Read the no partition to be divided n Partition size=ms/n.
Step6: Read the process no and process size.
Step 7: If process size is less than partition size allot alse blocke the process. While allocating update
memory wastage-external fragmentation.
if(pn[i]==pn[j])
f=1;
if(f==0)
{
if(ps[i]<=size)
{
extft=extft+size-
ps[i];avail[i]=1;
count++;
}
}
Step 8: Print the results

40
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

SOURCE CODE :

#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
printf("Enter the total memory available (in Bytes): ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes): ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("Enter the number of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes): ",i+1);
scanf("%d",&mp[i]);
}
printf("No. of blocks available\tin memory %d\n",nob);
printf("Process\tMemory Required\tAllocated\tInternal Fragmentation\n");
for(i=0;i<n && p<nob;i++)
{
printf("%d\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\tNO\t------\n");
else
{
printf("\tYES\t%d\n",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("Memory is Full, Remaining Processes cannot be accomodated\n");
printf("Total Internal Fragmentation is %d\n",tif);
printf("Total External Fragmentation is %d\n",ef);
}

41
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

INPUT
Enter the total memory available (in Bytes) -- 1000
Enter the block size (in Bytes)-- 300
Enter the number of processes – 5
Enter memory required for process 1 (in Bytes) -- 275
Enter memory required for process 2 (in Bytes) -- 400
Enter memory required for process 3 (in Bytes) -- 290
Enter memory required for process 4 (in Bytes) -- 293
Enter memory required for process 5 (in Bytes) -- 100
No. of Blocks available in memory -- 3

OUTPUT
PROCESS ALLOCAT INTERNAL
MEMORY REQUIRED ED FRAGMENTATION
1 275 YES 25
2 400 NO -----
3 290 YES 10
4 293 YES 7
Memory is Full, Remaining Processes cannot be accommodated Total
Internal Fragmentation is 42
Total External Fragmentation is 100

42
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO. 6 B) MEMORY VARIABLE PARTIONING TYPE (MVT)

AIM: To write a program to simulate the MVT algorithm

ALGORITHM:
Step1: start the process.
Step2: Declare variables.
Step3: Enter total memory size ms.
Step4: Allocate memory for os.
Ms=ms-os
Step5: Read the no partition to be divided n Partition size=ms/n.
Step6: Read the process no and process size.
Step 7: If process size is less than partition size allot alse blocke the process. While allocating update
memory wastage-external fragmentation.
if(pn[i]==pn[j])
f=1;
if(f==0)
{
if(ps[i]<=size)
{
extft=extft+size- ps[i];
avail[i]=1;
count++;
}
}
Step 8: Print the results
Step 9: Stop the process.

43
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
int main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';
printf("Enter the total memory available (in Bytes): ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("Enter memory required for process %d (in Bytes): ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("Memory is allocated for Process %d\n",i+1);
temp = temp - mp[i];
}
else
{
printf("Memory is Full\n");
break;
}
printf("Do you want to continue(y/n): ");
scanf(" %c", &ch);
}
printf("Total Memory Available %d\n", ms);
printf("Process\tMemory Allocated\n");
for(i=0;i<n;i++)
printf("%d\t%d\n",i+1,mp[i]);
printf("Total Memory Allocated is %d\n",ms-temp);
printf("Total External Fragmentation is %d\n",temp);
}

44
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

OUTPUT:

Enter the total memory available (in Bytes) – 1000


Enter memory required for process 1 (in Bytes) –
400 Memory is allocated for Process 1
Do you want to continue(y/n) -- y
Enter memory required for process 2 (in Bytes) -- 275
Memory is allocated for Process 2
Do you want to continue(y/n) – y
Enter memory required for process 3 (in Bytes) – 550

Memory is Full

Total Memory Available – 1000

PROCESS MEMORY

ALLOCATED 1 400
2 275

Total Memory Allocated is


675
Total External
Fragmentation is 325

45
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

Experiment No 7

AIM : Implementation of Compaction for the continually changing memory layout and calculate
total movement of data.

IMPEMENTATION DETAILS:
Compaction is a technique used to remove internal fragmentation. Assumption that has to be taken
into consideration is that the files must be inserted and deleted continuously. User must provide
memory image at different instances of time.
In the experiment no 6, we have obtained total internal and external fragmentation. To the above
practical, we continue performing the compaction.

46
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO. 8
AIM : Implementation of resource allocation graph (RAG)

Theory :

Resource Allocation Graph


The resource allocation graph is the pictorial representation of the state of a system. As its name
suggests, the resource allocation graph is the complete information about all the processes which are
holding some resources or waiting for some resources.
It also contains the information about all the instances of all the resources whether they are available or
being used by the processes.
In Resource allocation graph, the process is represented by a Circle while the Resource is represented by
a rectangle. Let's see the types of vertices and edges in detail.

Vertices are mainly of two types, Resource and process. Each of them will be represented by a
different shape. Circle represents process while rectangle represents resource.
A resource can have more than one instance. Each instance will be represented by a dot inside
the rectangle.

47
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

Edges in RAG are also of two types, one represents assignment and other represents the wait
of a process for a resource. The above image shows each of them.
A resource is shown as assigned to a process if the tail of the arrow is attached to an instance
to the resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached to the process
while the head is pointing towards the resource.

Example
Let'sconsider 3 processes P1, P2 and P3, and two types of resources R1 and R2. The resources
are having 1 instance each.
According to the graph, R1 is being used by P1, P2 is holding R2 and waiting for R1, P3 is
waiting for R1 as well as R2.
The graph is deadlock free since no cycle is being formed in the graph.

48
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM CODE :

#include<stdio.h>
#include<conio.h>
#include<window.h>
int proc,res,i,j,row=0,flag=0;
static int pro[3][3],req[3][3],st_req[3][3],st_pro[3][3];

void main()
{

printf("\nEnter the number of Processes:");


scanf("%d",&proc);
printf("\nEnter the number of Resources:");
scanf("%d",&res);

printf("\nEnter the Process Matrix:");


for(i=0;i<proc;i++)
for(j=0;j<res;j++)
scanf("%d",&pro[i][j]);

printf("\nEnter the Request Matrix:");


for(i=0;i<res;i++)
for(j=0;j<proc;j++)
scanf("%d",&req[i][j]);

row=0;
while(!kbhit())
{
for(i=0;i<res;i++)
{
if(pro[row][i]==1)
{
if(st_pro[row][i]>1 && flag==1)
{
printf("\nDeadlock Occured");
getch();
exit(0);
49
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

}
st_pro[row][i]++;
row=i;
break;
}
}

for(i=0;i<proc;i++)
{

if(req[row][i]==1)
{
if(st_req[row][i]>1)
{
printf("\nDeadlock Occured");
getch();
exit(0);
}
st_req[row][i]++;
row=i;
flag=1;
break;
}
}
}
printf("\nNo Deadlock Detected");
}

OUTPUT
Enter the number of Processes: 2
Enter the number of Resources: 2

Enter the Process Matrix :


0 1
1 0
Enter the Request Matrix :
1 0
0 1
Deadlock Occured

50
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

EXP NO 9: Implementation of Banker’s Algorithm

SIMULATE ALGORITHM FOR DEADLOCK PREVENTION

AIM :To Simulate Algorithm for Deadlock prevention

ALGORITHM:

1. Start the program


2. Attacking Mutex condition: never grant exclusive access. But this may not be possible for
several resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait condition: make a process hold at the most 1 resource
5. At a time. Make all the requests at the beginning. Nothing policy. If you feel, retry.
6. Attacking circular wait: Order all the resources. Make sure that the requests are issued in
the
7. Correct order so that there are no cycles present in the resource graph. Resources
numbered 1 ... n.
8. Resources can be requested only in increasing
9. Order. i.e. you cannot request a resource whose no is less than any you may be holding.
10. Stop the program

51
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

PROGRAM: (SIMULATE ALGORITHM FOR DEADLOCK PREVENTION)


#include<stdio.h>
#include<conio.h>
int max[10][10], alloc[10][10], need[10][10];
int avail[10],i,j,p,r,finish[10]={0},flag=0;
int main()
{
printf("Enter no. of processes: ");
scanf("%d",&p);
printf("Enter no. of resources: ");
scanf("%d",&r);
printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter max matrix");
for(i=0;i<p;i++) /*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("Enter available matrix\n");
for(i=0;i<r;i++) scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun(); /*calling function*/
if(flag==0)
{
if(finish[i]!=1)
{
printf("Failing: Mutual exclusion\n");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("By allocating required resources to process %d deadlock is prevented\n",i);
printf("Lack of preemption\n");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("Daedlock is prevented by allocating needed resources\n");
printf("Failing: Hold and Wait condition\n");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("Avoiding any one of the condition, you can prevent deadlock\n");
}
}

52
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem

return 0;
}
fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
else break;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
return 0;
}

53
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

OUTPUT:

SIMULATION OF DEADLOCK PREVENTION

Enter no. of processes, resources 3, 2

Enter allocation matrix 2 4 5

345

Enter max matrix4 3 4

561

Enter available matrix2

Failing: Mutual Exclusion

By allocating required resources to process dead is prevented

Lack of no preemption deadlock is prevented by allocating needed resources

Failing: Hold and Wait condition

RESULT:
Thus the program deadlock was executed successfully.

54
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

EXPERIMENT.NO 10

AIM: Simulate Conversion of resource allocation graph (RAG) to wait for graph(WFG) for each
type of method used for storing graph.

Implementation details
One such deadlock detection algorithm makes use of a wait for graph to track which other processes a
process is currently blocking on. In a wait for graph, processes are represented as nodes, and an edge
from process Pi to Pj implies Pj is holding a resource that Pi needs
and thus Pi is waiting for Pj to release its lock on that resource. There may be processes waiting
for more than a single resource to become available. Graph cycles imply the possibility of a
deadlock.

INPUT/s:
Output of experiment 8 as Resource allocation graph (RAG) through adjacency matrices/list.

STEPS TO PERFORM:
(i)Identifythe waiting processes in the RAG.
(ii)Accordingly draw Wait for graph for the given RAG.
(iii) To draw it as graphical representation, we introduce graphics.h and work in graphics mode.
(iv)Geometric images are entered by entering graphics, providing with parameter and closing graphics.
(v) We now identify circular chain of dependency (i.e., appearance of loops in the graph)

OUTPUT:
(i) The wait for graph (graphical representation).
(ii) Also, check presence of loop to detect if loop is present

55
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

EXPERIMENT.NO 11 :

Implement the solution for Producer Consumer Problem using inter process
communication techniques -Semaphores

PRODUCER-CONSUMER PROBLEM USING SEMOPHERES


AIM:

To implement producer/consumer problem using semaphore.

DESCRIPTION
Producer consumer problem is a synchronization problem. There is a fixed size buffer where the
producer produces items and that is consumed by a consumer process. One solution to the
producer- consumer problem uses shared memory. To allow producer and consumer processes to
run concurrently, there must be available a buffer of items that can be filled by the producer and
emptied by the consumer. This buffer will reside in a region of memory that is shared by the
producer and consumer processes. The producer and consumer must be synchronized, so that the
consumer does not try to consume an item that has not yet been produced.

ALGORITHM:

1. Declare variable for producer & consumer as pthread-t-tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.

56
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

PROGRAM: (PRODUCER-CONSUMER PROBLEM)

#include<stdio.h>
void main()
{

int buffer[10], bufsize, in, out, produce, consume, choice=0;


in = 0;
out = 0;

bufsize = 10;

while(choice !=3)

printf("1.Produce\t2.Consume\t3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{

case 1: if((in+1)%bufsize==out)
printf("Buffer is Full\n");
else

printf("Enter the value: ");


scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;

break;

case 2: if(in == out)


printf("Buffer is empty\n");
else

{
consume = buffer[out];

printf("The consumed value is %d\n", consume);


out = (out+1)%bufsize;
}

break;

OUTPUT:

1. Produce 2. Consume 3. Exit Enter your

choice: 2

57
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

Buffer is Empty

1. Produce 2. Consume 3. Exit Enter your

choice: 1

Enter the value: 100

1. Produce 2. Consume 3. Exit Enter your

choice: 2

The consumed value is 100

1. Produce 2. Consume 3. Exit Enter your

choice: 3

RESULT:

Thus the producer consumer program was executed

58
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem

59

You might also like