0% found this document useful (0 votes)
33 views79 pages

39

The document outlines a lab course on Operating Systems at VIT-Bhopal, detailing various lab assessments focused on concepts like CPU scheduling, file storage allocation, and inter-process communication. It also provides hardware and software requirements for different operating systems including Windows, UNIX, and Linux. The aim is to study and implement key operating system functionalities through practical exercises.

Uploaded by

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

39

The document outlines a lab course on Operating Systems at VIT-Bhopal, detailing various lab assessments focused on concepts like CPU scheduling, file storage allocation, and inter-process communication. It also provides hardware and software requirements for different operating systems including Windows, UNIX, and Linux. The aim is to study and implement key operating system functionalities through practical exercises.

Uploaded by

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

lOMoARcPSD|54340024

20MIP10015 OS LAB - To study and apply concepts relating


to operating systems, such as concurrency
Operating Systems (Vellore Institute of Technology)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by SAMARTH SONI 22BCE11354 ([email protected])
lOMoARcPSD|54340024

LAB WORK (OPERATING


SYSTEMS)

Submitted by SIDHARTH E S (20MIP10015)


Student from VIT-Bhopal
Integrated MTech CSE Specialization in
Computational and Data Science

Submitted to Professor Ashwin M.


Slot: A11+DA1
Course Code: CSE3003

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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.

What is an Operating System?


The 1960’s definition of an operating system is “the 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. Some examples of operating systems are UNIX,
Mach, MS-DOS, MS-Windows, Windows/NT, Chicago, OS/2, MacOS, VMS,
MVS, and VM.
Controlling the computer involves software at several levels. We will
differentiate kernel services, library services, and application-level
services, all of which are part of the operating system. Processes run
Applications, which are linked together with libraries that perform
standard services. The kernel supports the processes by providing a path
to the peripheral devices. The kernel responds to service calls from the
processes and interrupts from the devices. The core of the operating
system is the kernel, a control program that functions in privileged
state (an execution context that allows all hardware instructions to be
executed), reacting to interrupts from external devices and to service
requests and traps from processes. Generally, the kernel is a permanent
resident of the computer. It creates and terminates processes and
responds to their request for service.
Operating Systems are resource managers. The main resource is
computer hardware in the form of processors, storage, input/output
devices, communication devices, and data. Some of the operating system
functions are: implementing the user interface, sharing hardware among
users, allowing users to share data among themselves, preventing users
from interfering with one another, scheduling resources among users,
facilitating input/output, recovering from errors, accounting for resource
usage, facilitating parallel operations, organizing data for secure and rapid
access, and handling network communications.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Topics for LAB Assessment


Lab 1
a) Study of hardware and software requirements of different operating
systems (UNIX, LINUX, WINDOWS XP, WINDOWS 7/8).
b) Execute various UNIX system calls

Lab 2
Implement CPU scheduling policies: (2-3
Lab sessions)
a) SJF
b) Priority
c) FCFS
d) Multi-level queue

Lab 3
Implement file storage allocation techniques:
a) Contiguous (using array)
b) Linked –list (using linked list)
c) Indirect allocation (indexing)

Lab 4
Implementation of Contiguous allocation techniques:
a) Worst-Fit
b) Best-Fit
c) First-Fit

Lab 5
Calculation of external and internal fragmentation.

Lab 6
External and Internal Fragmentation: Implementation of Compaction for
the continually changing memory layout and calculate total movement of
data.

Lab 7
Implementation of resource allocation graph (RAG).

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab 8
Implementation of Banker’s Algorithm.

Lab 9
Wait Graph: Conversion of resource allocation graph (RAG) to wait-for-
graph (WFG) for each type of method used for storing graph.

Lab 10
Inter process Communication – Semaphore
a) Implement the solution for Bounded Buffer (Producer-Consumer)
problem using inter process communication technique –
Semaphores.
b) Implement the solution for Readers-Writer’s problem using inter
process communication technique – Semaphores.
c) Implement the solution for Dining-Philosopher problem using inter
process communication technique – Semaphores.

Lab 11
FORK and JOIN construct
a) Write a program where parent process take average of the odd
numbers and child process will take the average of even numbers
present in a given Aadhar number of a person. Use FORK and JOIN
construct.
b) Write a program where parent process finds additive primes and
child process finds circular prime for a given prime list array. Use
FORK and JOIN construct.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab:1
a) Study of hardware and software requirements of different operating
systems (UNIX, LINUX, WINDOWS XP, WINDOWS 7/8).
b) Execute various UNIX system calls for—
- Process management
- File management
- Input / Output System calls

IMPLEMENTATION DETAILS AND ASSUMPTIONS:


(i) The OBJECTIVE of this practical is to obtain general overview of various
popular OS.
a) Their development and distribution
b) Compatibility
c) Security issues and Thread detection and its solution
d) The GUI etc..
(ii) Along with the above-mentioned activities, execution of various UNIX
commands is also helpful: cat, cd, cp, chmod, df, less, ls, mkdir, more, mv,
pwd, rmdir, rm, man, uname, who, ps, vi, cal, date, echo, bc, grep

AIM-
Studying the hardware and software requirements of various
operating systems.
Windows Requirements
The following topics provide the hardware and software requirements
for running Web Logic Integration - Business Connect on computers
with Microsoft Windows operating systems.
▪ Hardware on Windows

▪ Software on Windows

Hardware on Windows
A Windows computer must have the following minimum configuration.
▪ A Pentium-class processor running at 400 MHz or faster

▪ Random Access Memory (RAM)

▪ 250MB available hard drive space


Note: We recommend at least a 1 GB hard drive for

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

both the application and the documents you exchange.


▪ SVGA monitor

▪ CD-ROM drive (for installation)

▪ TCP/IP network interface

Software on Windows
A Windows computer must have the following minimum
software configuration -
Operating System
WebLogic Integration - Business Connect supports the
following Windows operating systems: Windows NT 4.0 and
Windows 2000.
To use Windows NT 4.0, you also must have installed the domestic
United States version of Service Pack 4 or later with the associated
bug fixes.
Note: Apache as the mediated web server is not supported when
configured on Windows NT or Windows 2000.

Recommended Software for Windows


Although Internet Explorer 4.0.1 or later must be installed to
successfully install WebLogic Integration - Business Connect, after
installation you can use Internet Explorer 4.0.1 or later or Netscape
Navigator 4.0 or later. You use a browser to read WebLogic
Integration - Business Connect online help files and to obtain third-
party certificates from certificate authorities such as VeriSign, Inc.

In addition, you need Adobe Acrobat Reader to view and print Using
WebLogic Integration - Business Connect, which is provided in
portable document format (PDF) files. The PDFs are in the userdoc
directory on the installation CD and in the doc subdirectory after you
install the application. Acrobat Reader is available free from Adobe
Systems Inc., www.adobe.com.

UNIX Requirements
The following topics provide the hardware and software
requirements for running WebLogic Integration - Business Connect
on UNIX computers.
▪ Hardware on UNIX
▪ Software on UNIX

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Hardware on UNIX
Your computer must have the following minimum configuration to
successfully install WebLogic Integration - Business Connect on a
UNIX platform.
▪ Random Access Memory (RAM)

▪ 250MB available hard drive space (see note)

▪ CD-ROM drive

▪ TCP/IP network interface

▪ A persistent Internet connection.

Note: We recommend at least a 1 GB hard drive for both the


application and the documents you exchange.

Software on UNIX
This topic lists the operating systems and other software necessary to
install and configure WebLogic Integration - Business Connect on UNIX
platforms
X Windows and Operating Systems
Your system requires X Windows and any of the following
UNIX operating systems:
▪ Hewlett-Packard HP-UX 11.0
▪ IBM AIX 4.3.

Linux client system requirements


Ensure that the system onto which you install the Discovery Studio client
meets these minimum requirements:
• 32-bit Intel® Pentium® 4 or compatible processor running at 2 GHz or
greater
• 512 MB RAM
• Graphics card: NVIDIA® Quadro™ FX 1100, FX1400, FX540 or
ATI FireGL™ V3200, V5100
• Disk space: 350 MB for client components
• A DVD-ROM drive

Note. The total hard disk space required for installation can vary between

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

350 MB and 2 GB, depending on the components selected for installation.


Additional space is required if system updates are necessary.

Operating system
The client is supported on workstations running:
• Red Hat® Enterprise Linux WS 3.0, updates 4-7
• Red Hat® Enterprise Linux WS 4.0, base and updates 1-3

Note. Discovery Studio is designed for use on 32-bit operating systems


only. It does not support 64-bit versions of Red Hat Enterprise running
natively on Intel® Itanium®, Intel® EM64T, or AMD64.

Additional software required


• GNOME Desktop - Discovery Studio optimized for the GNOME
Desktop. We recommend that this software is installed and
configured. It should be available on your Linux distribution media.
• System libraries - On Red Enterprise Linux WS 4.0, the Standard C++
libraries for backwards compatibility are required for correct operation
of the Discovery Studio client. These are typically provided in a default
installation of the operating system.
• Graphics Drivers - Discovery Studio is optimized to provide
hardware-accelerated graphics with supported NVIDIA and ATI
hardware when
certified drivers are installed. For example, on Linux, Accelrys
recommends version 1.0-8174 or higher for use on NVIDIA Quadro
systems. 3D stereo support is also available with hardware that
provides quad-buffered OpenGL stereo capabilities. Further details on
Accelrys-certified graphics drivers canbe obtained from
http://www.nvidia.com and http://www.ati.com.

Linux server system requirements


Ensure that the system onto which you install Discovery Studio
meets these minimum requirements:

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

• 32-bit Intel-compatible processor running at 2 GHz or greater


• 512 MB RAM
• Disk space: 2.5 GB for Pipeline Pilot server plus components
• A DVD-ROM drive

Note. The hard disk space required for installation can vary between
300 MB and 2.6 GB (total), depending on the components selected for
installation.

Operating system
The server is supported on workstations running:
• Red Hat® Enterprise Linux WS 3.0, updates 4-7
• Red Hat® Enterprise Linux WS 4.0, base and update 1-3

Note. Discovery Studio is designed for use on 32-bit operating systems


only. It does not support 64-bit versions of Red Hat® Enterprise running
natively on Intel® Itanium®, Intel EM64T, or AMD64.

Additional software required


• System libraries - On Red Hat® Enterprise Linux 4.0, the Standard C+
+ libraries for backwards compatibility (compat-libstdc++-33) are
required for correct operation of the Discovery Studio server. These are
typically provided in a default installation of the operating system.
• MPICH - Discovery Studio includes a version of CHARMm that
runs on systems with multiple processors. To use this version,
MPICH must be installed and configured. We recommend building
MPICH 1.2.7 from http://www-unix.mcs.anl.gov/mpi/mpich1/
using GCC 3.2.3 for C/C++ and Intel 8.1 for Fortran.

Windows XP

The minimum hardware requirements for Windows XP Home Edition are:


• Pentium 233-megahertz (MHz) processor or faster (300
MHz is recommended)

• At least 64 megabytes (MB) of RAM (128 MB is recommended)

• At least 1.5 gigabytes (GB) of available space on the hard disk

• CD-ROM or DVD-ROM drive

• Keyboard and a Microsoft Mouse or some other compatible pointing

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

device

• Video adapter and monitor with Super VGA (800 x 600)or higher
resolution

• Sound card

Speakers or headphones

The minimum hardware requirements for Windows XP


Professional include:

• Pentium 233-megahertz (MHz) processor or faster (300


MHz is recommended)

• At least 64 megabytes (MB) of RAM (128 MB is recommended)

• At least 1.5 gigabytes (GB) of available space on the hard disk

• CD-ROM or DVD-ROM drive

Software Requirements
The following software is required:
• On 32-bit Windows, one of:

• Windows Vista
• Windows Server 2008

• Windows Server 2003 R2, Windows Server 2003


• Windows XP Professional SP2 or later

• On 64-bit Windows for X64, one of:

o Windows Vista
o Windows Server 2008
o Windows Server 2003
o Windows XP Professional

• Microsoft Visual Studio 2008 Service Pack 1 or Microsoft Visual


Studio Shell 2008. Note that Microsoft Visual Studio Express
Edition is not supported.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

• Microsoft Windows SDK is required if you are using Visual Studio


Shell 2008. See the Microsoft Download site and search for
Windows SDK.

• Microsoft .NET Framework 3.5. This is included with the above


versions of Visual Studio.

• The Java Development Kit (JDK) is required for compiling Java.


The JDK is downloadable from the Sun Web site. After installing
the JDK, you must put the tools.jar file for the JDK on your
classpath, using a command similar to:

set classpath=jdk-install-directory\lib\tools.jar

• A Web browser, such as Microsoft Internet Explorer 6.0 or


Netscape Navigator 6.1., is required for Enterprise Server
Administration in reUZE Server.

• Enterprise Server Help requires the Sun Java Run-time


Environment on some Windows systems to enable the Search
facility to work. See Third Party Software

AIM-
Execute various UNIX system calls
In computing, a system call is the programmatic way in which a
computer program requests a service from the kernel of the operating
system it is executed on. A system call is a way for programs to interact
with the operating system. A computer program makes a system call
when it makes a request to the operating system’s kernel. System
call provides the services of the operating system to the user programs
via Application Program Interface (API). It provides an interface between
a process and operating system to allow user-level processes to request
services of the operating system. System calls are the only entry points
into the kernel system. All programs needing resources must use system
calls.
Services Provided by System Calls:
1. Process creation and management
2. Main memory management
3. File Access, Directory and File system management
4. Device handling(I/O)
5. Protection
6. Networking, etc.
Types of System Calls : There are 5 different categories of
system calls –
1. Process control: end, abort, create, terminate,
allocate and free memory.
2. File management: create, open, close, delete, read
file etc.
3. Device management

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

4. Information maintenance
5. Communication
Examples of Windows and Unix System Calls –

Windows Unix

CreateProcess() fork()
ExitProcess() exit()
Process Control WaitForSingleObject() wait()

CreateFile() open()
ReadFile() read()
WriteFile() write()
File Manipulation
CloseHandle() close()

SetConsoleMode() ioctl()
ReadConsole() read()
Device Manipulation WriteConsole() write()

GetCurrentProcessID() getpid()
SetTimer() alarm()
Information Maintenance Sleep() sleep()

CreatePipe() pipe()
CreateFileMapping() shmget()
Communication MapViewOfFile() mmap()

SetFileSecurity() chmod()
InitlializeSecurityDescriptor() umask()
Protection SetSecurityDescriptorGroup() chown()

1. The prototype for the creat() system call is:

2. The prototype for the open() system call is:

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

3. To close a channel, use the close() system call. The prototype for
the close() system call is:

4. The read() system call does all input and the write() system call
does all output.

5. The prototype for the lseek() system call is:

6. The prototype for the dup() system call is:

7. The prototype for the link() system call is:

8. The prototype for the unlink() system call is:

9. The prototype for the exec() system call is:

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

10. The prototype for the fork() system call is:

11. The prototype for the wait() system call is:

12. The prototype for the exit() system call is:

13. The prototype for the signal() system call is:

14. The prototype for the kill() system call is:

15. The prototype for the alarm() system call is:

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab:2
Implement CPU scheduling policies : (2-3
Lab sessions)
a) SJF
b) Priority
c) FCFS
d) Multi-level queue

IMPLEMENTATION DETAILS AND ASSUMPTIONS: INPUT/s:


(i) The number of processes/jobs in the system (computed through random
functions in C)
(ii) The CPU Burst (based on past history), priority (initially, compute through
random function), arrival time of process.

STEPS TO PERFORM:
(a) For SJF algorithm, (i) We randomly generate the number of jobs. There
must be a limit on the number of jobs in a system. (ii) The execution time of
the generated jobs is also not known. Here, we are generating the CPU burst
of each job making use of the past history. (iii) All the jobs are then arranged
in a queue where searching is done to find the one with the least CPU burst.
There may be two jobs in queue with the same execution time then FCFS
approach is to be performed. Case a) If the algorithm is non preemptive in
nature, then the newly arriving job is to be added to the job queue even
though it is of lesser execution time than the one running on the processor.
Case b) Otherwise preemption is performed.
(b) For Priority scheduling, (i) We again prefer to compute the CPU burst from
past history. (ii) Priority may be assigned on the basis of their CPU burst
(simplest approach) (iii)Priority may be assigned through some random
function (within a specific range say 1 to 100). (iv) Priority has to be changed
dynamically (to perform aging in order to avoid starvation). Priority
(preemption) and priority (non preemption) nature of priority scheduling is
performed.
(c) The FCFS scheduling is performed on the basis of arrival time irrespective
of their other parameters.
(d) In multi-level queue scheduling, different queues are to be created.
OUTPUT/s: The average throughput, waiting time of process/s

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

DESCRIPTION

Assume all the processes arrive at the same time.

FCFS CPU SCHEDULING ALGORITHM

For FCFS scheduling algorithm, read the number of processes/jobs


in the system, their CPU burst times. The scheduling is performed
on the basis of arrival time of the processes irrespective of their
other parameters. Each process will be executed according to its
arrival time. Calculate the waiting time and turnaround time of
each of the processes accordingly.

SJF CPU SCHEDULING ALGORITHM

For SJF scheduling algorithm, read the number of processes/jobs


in the system, their CPU burst times. Arrange all the jobs in order
with respect to their burst times. There may be two jobs in queue
with the same execution time, and then FCFS approach is to be
performed. Each process will be executed according to the length
of its burst time. Then calculate the waiting time and turnaround
time of each of the processes accordingly.

PRIORITY CPU SCHEDULING ALGORITHM


For priority scheduling algorithm, read the number of
processes/jobs in the system, their CPU burst times, and the
priorities. Arrange all the jobs in order with respect to their
priorities. There may be two jobs in queue with the same priority,
and then FCFS approach is to be performed. Each process will be
executed according to its priority. Calculate the waiting time and
turnaround time of each of the processes accordingly.

MULTI-LEVEL QUEUE SCHEDULING ALGORITHM

Multi-level queue scheduling algorithm is used in scenarios where


the processes can be classified into groups based on property like
process type, CPU time, IO access, memory size, etc. In a multi-level
queue scheduling algorithm, there will be 'n' number of queues,
where 'n' is the number of groups the processes are classified into.
Each queue will be assigned a priority and will have its own
scheduling algorithm like round-robin scheduling or FCFS. For the
process in a queue to execute, all the queues of priority higher than
it should be empty, meaning the process in those high priority

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

queues should have completed its execution. In this scheduling


algorithm, once assigned to a queue, the process will not move to
any other queues.

FIRST COME FIRST SERVE:

ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and
the burst time Step 4: Set the waiting of the first process as ‗0‘and its
burst time as its turnaround time.
Step 5: for each process in the Ready Q calculate
Waiting time (n) = waiting time (n-1) + Burst time (n-1) b). Turnaround
time (n)= waiting time(n)+Burst time(n)
Step 6: Calculate:
a) Average waiting time = Total waiting Time / Number of
process
b) Average Turnaround time = Total Turnaround Time / Number of
process
Step 7: Stop the process.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

INPUT SOURCE CODE:


Enter the number of processes -- 3 #include<stdio.h>
Enter Burst Time for Process 0 -- 2 #include<conio.h>
4 main()
Enter Burst Time for Process 1 -- 3
{
Enter Burst Time for Process 2 -- 3
int bt[20], wt[20], tat[20], i,
n;
float wtavg, tatavg;
clrscr();

printf("\nEnter the number of processes --");


scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];

wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i];


}

printf("\t PROCESS \tBURST TIME \t WAITING TIME\t


TURNAROUND TIME\n"); for(i=0;i<n;i++)

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i,


bt[i], wt[i], tat[i]); printf("\nAverage Waiting
Time -- %f", wtavg/n); printf("\nAverage
Turnaround Time -- %f", tatavg/n); getch();}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30

Average Waiting Time--17.000000

Average Turnaround Time – 27.000000

SHORTEST JOB FIRST:

ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept
the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting
according to lowest to highest burst time.

Step 5: Set the waiting time of the first process as ‗0 and its turnaround
time as its burst time.
Step 6: Sort the processes names based
on their Burt time Step 7: For each
process in the ready queue, calculate
Waiting time(n)= waiting time (n-1) +
Burst time (n-1) Turnaround time (n)=
waiting time(n)+Burst time(n)
Step 8: Calculate
Average waiting time = Total waiting Time / Number of process
Average Turnaround time = Total Turnaround Time / Number of process.
Stop the process.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{

int p[20], bt[20], wt[20], tat[20], i, k, n, temp;


float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);

for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)

if(bt[i]>bt[k])
{
temp=bt[i]; bt[i]=bt[k];
bt[k]=temp;

temp=p[i];
p[i]=p[k]; p[k]=temp;
}
wt[0] = wtavg = 0;

tat[0] = tatavg = bt[0];


for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

OUTPUT tat[i]
TURN
=
BURST WAITING AROUND tat[i-
PROCESS TIME TIME TIME 1]

P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24

Average Waiting Time -- 7.000000


Average Turnaround Time
-- 13.000000
+bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}

printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t


TURNAROUND TIME\n"); for(i=0;i<n;i++)

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
INPUT printf("\nAverage Waiting
Time -- %f", wtavg/n);
Enter the number of processes --
Enter Burst Time for Process 0 -- 6 printf("\nAverage
Enter Burst Time for Process 1 -- 8 Turnaround Time -- %f",
tatavg/n);
Enter Burst Time for Process 2 -- 7
getch();
Enter Burst Time for Process 3 -- 3
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

PRIORITY SCHEDULING:

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept
the CPU burst time

Step 4: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as 0 and its burst time as its
turnaround time

Step 6: Arrange the processes based on process priority

Step 7: For each process in the Ready Q calculate.

Step 8: for each process in the Ready Q calculate

a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)


b) Turnaround time (n)= waiting time(n)+Burst time(n)

Step 9: Calculate
Average waiting time = Total waiting Time / Number of process

Average Turnaround time = Total Turnaround Time / Number of process


Print the results in an order.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Step10: Stop.

SOURCE CODE:

#include<stdio.h>
main()
{
int p[20],bt[20],pri[20],
wt[20],tat[20],i, k, n, temp; float
wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)

{
p[i] = i;

printf("Enter the Burst Time & Priority of


Process %d --- ",i); scanf("%d %d",&bt[i],
&pri[i]);

}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])

{
temp=p[i];
p[i]=p[k];
p[k]=temp;

temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];

pri[i]=pri[k];

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

pri[k]=temp;

wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];


tatavg = tatavg + tat[i];
}

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING
TIME\tTURNAROUND TIME"); for(i=0;i<n;i++)

printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d


",p[i],pri[i],bt[i],wt[i],tat[i]); printf("\nAverage
Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is ---
%f",tatavg/n); getch();

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

INPUT
Enter the number of processes
-- 5
Enter the Burst Time & Priority of
Process 0 --- 10 3
Enter the Burst Time & Priority of
Process 1---1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of
Process 3---1 5
Enter the Burst Time & Priority of
Process 4---5 2

OUTPUT
PROCESS PRIORITY BURST TIME WAITIN G TURNARO
TIME 0 -UND TIME
1 1 1 1 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19

Average Waiting Time is --- 8.200000


Average Turnaround Time is --- 12.000000

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

MULTI-LEVEL QUEUE SCHEDULING-

SOURCE CODE

main(){

int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;


float wtavg, tatavg;
clrscr();

printf("Enter the number of processes --- ");


scanf("%d",&n);

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

{
p[i] = i;

printf("Enter the Burst Time of


Process %d --- ", i);
scanf("%d",&bt[i]);

printf("System/User Process (0/1) ? --- ");


scanf("%d", &su[i]);

}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])

{
temp=p[i]; p[i]=p[k]; p[k]=temp;
temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;
temp=su[i]; su[i]=su[k]; su[k]=temp;

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];


tatavg = tatavg + tat[i];
}

printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING


TIME\tTURNAROUND TIME");

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

{
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage


Turnaround Time is --- %f",tatavg/n);
getch();
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab:3
Implement file storage allocation techniques:
a) Contiguous (using array)
b) Linked –list (using linked list)
c) Indirect allocation (indexing)
IMPLEMENTATION DETAILS: INPUT/s :
(i) Free storage blocks to allocate storage to files.
STEPS TO PERFORM: Before performing storage allocation, we need to
perform the following common steps:
(i) During program execution, we need an illusion that we have a large
disk space for allocation strategies.
(ii) We take a big array of structure (representing storage space in terms
of sectors). This space is equal to the disk size available for storage.
(iii) Next step is to see free space list. The free space list is not entirely
free. Some portion of this list is occupied by some OS or user files/data.
(iv) The free-space is to be maintained in a suitable manner as per
allocation strategy.
(v) Upon the above formed disk space and free space list, we then
implement the above mentioned techniques.
(vi) Symbolic file name and size will be inputted by the user and
accordingly the assumed disk space as well as free space list will be
updated.

(a) Contiguous allocation strategy is implemented using array data


structure.
(b) Linked list allocation technique is implemented using linked list.
(c) Indirect allocation is performed using indexing concept.
OUTPUT/s: Files/ Programs are allocated storage space through
appropriate storage allocation techniques.

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.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

ALGORITHM:
Step 1: Start the program.

Step 2: Get the number of files.

Step 3: Get the memory requirement of each file.

Step 4: Allocate the required locations to


each in sequential order

a). Randomly select a location from


available location s1= random(100);

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;
}
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

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

SOURCE CODE:
#include<stdio.h>

main()
{
int f[50],i,st,j,len,c,k;

clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("\n Enter the starting block & length of file");

scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{

f[j]=1
;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1) goto X;
else exit();
getch();

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

INDEXED ALLOCATION

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.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Step 6: Stop the program


SOURCE CODE:
#include<stdio.h>
Int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();

for(i=0;i<50;i++)
f[i]=0;
printf("enter index block\t");
scanf("%d",&p);

if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");

scanf("%d",&n);
}
else
{

printf("Block already allocated\n");


goto x;
}

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");


goto x;

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

}
for(j=0;j<n;j++) f[inde[j]]=1;

printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);

printf(" Enter 1 to enter more files and 0 to exit\t");

scanf("%d",&c);
if(c==1) goto x;
else exit();
getch();

OUTPUT:
enter index block 9

Enter no of files on index


31
2 3
Alloca
ted
File
index
ed 9-
>1:1
9-
>2;1
9->3:1 enter 1 to enter more files and 0 to exit

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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++)
{

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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 program

SOURCE CODE:
#include<stdio.h>
main()
{

int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;

printf("Enter how many blocks that are


already allocated"); scanf("%d",&p);

printf("\nEnter the blocks no.s that are


already allocated"); for(i=0;i<p;i++)

{
scanf("%d",&a);
f[a]=1;
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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("\n%d->%d",j,f[j]);
}
else
{

printf("\n %d->file is already allocated",j);


k++;
}
}

printf("\n If u want to enter one more file?


(yes-1/no-0)"); scanf("%d",&c);
if(c==1)
goto X;
else exit();

getch( );}

OUTPUT
Enter how many blocks that are already allocated 3 Enter the blocks
no.s that are already allocated 4 7 Enter the starting index block &
length 3 7 9

3->1

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab: 4
AIM:
Implementation of Contiguous allocation techniques:
a) Worst-Fit
b) Best-Fit
c) First-Fit

IMPLEMENTATION DETAILS: INPUT/s:


(i) Free space list of blocks from system (as created in experiment 3)
(ii) List processes and files from the system (as in experiment 3)

STEPS TO PERFORM:
(i) We consider the same free space list and files/processes as created in
experiment 3 for our system.
(ii) Implement the above mentioned three contiguous allocation
techniques. Also, the free space list is updated from the free blocks left
out after performing allocation.

(a) Worst-fit: In worst fit technique largest available block/partition which


will hold the page is selected. Blocks are sorted according to their size in
descending order.
(b) Best-fit: Best-fit is one of the optimal techniques in which page is
stored in the block/partition which is large enough to hold it. Blocks are
sorted according to their size in ascending order.
(c) First-fit: In first-fit technique page is stored in the block which is
encountered first that is big enough to hold it.

DESCRIPTION
One of the simplest methods for memory allocation is to divide
memory into several fixed-sized partitions. Each partition may contain
exactly one process. In this multiple-partition method, when a
partition is free, a process is selected from the input queue and is
loaded into the free partition. When the process terminates, the
partition becomes available for another process. The operating
system keeps a table indicating which parts of memory are available
and which are occupied. Finally, when a process arrives and needs
memory, a memory section large enough for this process is provided.
When it is time to load or swap a process into main memory, and if
there is more than one free block of memory of sufficient size, then
the operating system must decide which free block to allocate. Best-

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

fit strategy chooses the block that is closest in size to the request.
First-fit chooses the first available block that is large enough. Worst-fit
chooses the largest available block.

SOURCE CODES

WORST-FIT:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()

{
int frag[max],b[max],f[max],i,j,nb,nf,t emp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme
-First Fit"); printf("\nEnter the number of
blocks:"); scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);
printf("\nEnter thesize 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]);

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

}
for(i=1;i<=nf;i++)
{

for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{

temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j; break;

}
}
}
frag[i]=temp;

bf[ff[i]]=1;
}

printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");

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

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

getch();

INPUT:
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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 1 5 4

2 4 3 7 3

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

BEST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()

{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();

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


scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");


for(i=1;i<=nb;i++)
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)
{

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

temp=b[j]-f[i]; if(temp>=0)
if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}}
frag[i]=lowest; bf[ff[i]]=1;
lowest=10000;

printf("\nFileNo\tFileSize\tBlockNo\tBlock
Size\tFragment"); for(i=1;i<=nf && ff[i]!
=0;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

INPUT

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

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

File 2: 4

OUTPUT

File No File Size Block No Block Size Fragment

1 1 2 2 1

2 4 1 5 1

FIRST-FIT
#include<stdio.h>

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

int frag[max],b[max],f[max],i,j,nb,nf,temp,highes t=0;


static int bf[max],ff[max]; clrscr();

printf("\n\tMemory Management
Scheme - Worst Fit"); printf("\nEnter the
number of blocks:"); scanf("%d",&nb);

printf("Enter the number of files:");


scanf("%d",&nf);
printf("\nEnter 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);

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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)
{
}
}
frag[i]=highest; bf[ff[i]]=1;
highest=0;
}
ff[i]=j;
highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

INPUT

Enter the number of blocks: 3

Enter the number of files: 2

Enter the size of the blocks:-

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab: 5
AIM
Calculation of external and internal fragmentation.

DESCRIPTION
IMPLEMENTATION DETAILS: INPUT/s:
(i) Free space list of blocks from system (as in experiment 3).
(ii) List processes and files from the system (as in experiment 3).

STEPS TO PERFORM:
(i) Completing experiment 4, we end up getting list of allotted files,
remaining part of allotted block and blocks which cannot be allotted
(ii) After implementing each allocation algorithm, list the amount of free
space blocks left out after performing allocation.
(iii) When a block which is not at all allocated to any process or file, it adds
to external fragmentation.
(iv) When a file or process is allocated the free block and still some part of
it is left unused, we count such unused portion into internal
fragmentation. OUTPUT/s: Processes and files allocated to free blocks.
From the list of unused blocks, we determine the count of total internal
and external fragmentation.

Internal Fragmentation
When a process is assigned to a memory block and if that process is
smaller than the memory requested, it creates a free space in the
assigned memory block. Then the difference between assigned and
requested memory is called the internal fragmentation.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

External Fragmentation

When the process is loaded or removed from the memory, a free space is
created. This free space creates an empty space in the memory which is
called external fragmentation.

Following are the important differences between Internal Fragmentation


and External Fragmentation.

Key Internal Fragmentation External Fragmentation

Definition When there is a difference When there are small and non-
between required memory contiguous memory blocks
space vs allotted memory which cannot be assigned to
space, problem is termed as any process, the problem is
Internal Fragmentation. termed as External
Fragmentation.

Memory Internal Fragmentation External Fragmentation occurs


Block Size occurs when allotted when allotted memory blocks
memory blocks are of fixed are of varying size.
size.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Key Internal Fragmentation External Fragmentation

Occurrenc Internal Fragmentation External Fragmentation occurs


e occurs when a process when a process is removed
needs more space than the from the main memory.
size of allotted memory
block or use less space.

Solution Best Fit Block Search is the Compaction is the solution for
solution for internal external fragmentation.
fragmentation.

Process Internal Fragmentation External Fragmentation occurs


occurs when Paging is when Segmentation is
employed. employed.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

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

ASSUMPTION:
the files must be inserted and deleted continuously.

DESCRIPTION:
External and Internal Fragmentation: Implementation of Compaction for
the continually changing memory layout and calculate total movement of
data. the dynamic partitioning suffers from external fragmentation.
However, this can cause some serious problems.

To avoid compaction, we need to change the rule which says that the
process can't be stored in the different places in the memory.

We can also use compaction to minimize the probability of external


fragmentation. In compaction, all the free partitions are made contiguous
and all the loaded partitions are brought together.

By applying this technique, we can store the bigger processes in the


memory. The free partitions are merged which can now be allocated
according to the needs of new processes. This technique is also called
defragmentation.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

As shown in the image above, the process P5, which could not be loaded
into the memory due to the lack of contiguous space, can be loaded now
in the memory since the free partitions are made contiguous.

Problem with Compaction


The efficiency of the system is decreased in the case of compaction due to
the fact that all the free spaces will be transferred from several places to a
single place.

Huge amount of time is invested for this procedure and the CPU will
remain idle for all this time. Despite of the fact that the compaction avoids
external fragmentation, it makes system inefficient.

Let us consider that OS needs 6 NS to copy 1 byte from one place to


another.

1. 1 B transfer needs 6 NS
2. 256 MB transfer needs 256 X 2^20 X 6 X 10 ^ -9 secs

hence, it is proved to some extent that the larger size memory transfer
needs some huge amount of time that is in seconds.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Lab:7
AIM
Implementation of resource allocation graph (RAG).

IMPLEMENTATION DETAILS:
INPUT/s:
(i) List of resources
(ii) Instance of each resource (for case 2 only)
(iii) List of processes
(iv) Resource allocated by each process

STEPS TO PERFORM:
(i) List the processes and resources.
(ii) We read input from user for each [Pi , Ri] and also how many instances
of each resource to a particular process (for multiple instances case).
(iii) While user completes the input, we end up constructing adjacency
matrices/ list. Two cases to be considered:

Case 1– Each resource has single instance (simpler problem).


Case 2– Multiple instances of resources (complex problem). a. Methods
used for representing graph:

(i) Adjacency matrix: A 2-D array of size N x N where N is the number of


vertices in the graph (includes processes and resources). For each adj[i][j]
= 1 indicates that there is an edge from vertex i to vertex j. Since
resource allocation graph is directed graph, hence it is not necessary to be
symmetric.
(ii) Adjacency list: An array of linked list is used. Size of the array is equal
to number of vertices (processes) in the graph. An entry arr[i] represents
the linked list of vertices (resources requested by process) adjacent to the
ith vertex.

OUTPUT/s:
Output is a Resource allocation graph through matrices/list.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

DESCRIPTION

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. It is also two type –
• Single instance type resource – It represents as a box,
inside the box, there will be one dot. So, the number of dots
indicate how many instances are present of each resource type.
• Multi-resource instance type resource – It also represents
as a box, inside the box, there will be many dots present.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Now coming to the edges of RAG.There are two types of edges in RAG –

1. Assign Edge – If you already assign a resource to a process then it


is called Assign edge.
2. Request Edge – It means in future the process might want
some resource to complete the execution, that is called request
edge.

So, if a process is using a resource, an arrow is drawn from the resource


node to the process node. If a process is requesting a resource, an arrow
is drawn from the process node to the resource node.

Example 1 (Single instances RAG)

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

If there is a cycle in the Resource Allocation Graph and each resource in the
cycle provides only one instance, then the processes will be in deadlock.
For example, if process P1 holds

resource R1, process P2 holds resource R2 and process P1 is waiting for


R2 and process P2 is waiting for R1, then process P1 and process P2 will
be in deadlock.

Here’s another example, that shows Processes P1 and P2 acquiring


resources R1 and R2 while process P3 is waiting to acquire both
resources. In this example, there is no deadlock because there is no
circular dependency.

So cycle in single-instance resource type is the sufficient


condition for deadlock.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Example 2 (Multi-instances RAG)

From the above example, it is not possible to say the RAG is in a safe
state or in an unsafe state. Thus, to see the state of this RAG, let’s
construct the allocation matrix and request matrix.

• The total number of processes are three; P1, P2 & P3 and the
total number of
resources are two; R1 & R2.
Allocation matrix –

• For constructing the allocation matrix, just go to the resources


and see to which process it is allocated.
• R1 is allocated to P1, therefore write 1 in allocation matrix and
similarly, R2 is
allocated to P2 as well as P3 and for the remaining
element just write 0

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Request matrix –

• In order to find out the request matrix, you have to go to the


process and see the outgoing edges.
• P1 is requesting resource R2, so write 1 in the matrix
and similarly, P2 requesting R1 and for the remaining
element write 0.
So now available resource is = (0, 0).

Checking deadlock (safe or not) –

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

So, there is no deadlock in this RAG. Even though there is a cycle, still
there is no deadlock. Therefore in multi-instance resource cycle is not
sufficient condition for deadlock.

Above example is the same as the previous example except


that, the process P3 requesting for resource R1.

So, the table becomes as shown in below.

So, the Available resource is = (0, 0), but requirement are (0, 1), (1,
0) and (1, 0). So, you can’t fulfil any one requirement. Therefore, it
is in deadlock.

Therefore, every cycle in a multi-instance resource type graph is not a


deadlock, if there has to be a deadlock, there has to be a cycle. So, in
case of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

LAB:8
AIM

To Simulate banker’s algorithm for Dead Lock Avoidance (Banker’s


Algorithm)

DESCRIPTION

Deadlock is a situation where in two or more competing actions


are waiting f or the other to finish, and thus neither ever does.
When a new process enters a system, it must declare the
maximum number of instances of each resource type it needed.
This number may exceed the total number of resources in the
system. When the user requests a set of resources, the system
must determine whether the allocation of each resources will
leave the system in safe state. If it will the resources are
allocation; otherwise the process must wait until some other
process release the resources.

SAFETY ALGORITHM

Work and Finish be the vector of length m and n respectively,


Work=Available and Finish[i]=False.
Find an i such that both Finish[i]=False Need<=Work If no such I exists go
to .
work= work + Allocation, Finish[i] =True;
if Finish[1]=True for all I, then the system is in safe state. Resource
request algorithm
Let Request i be request vector for the process Pi, If request i=[j]=k, then
process Pi wants k instances of resource type Rj.
if Request<=Need I go to step 2. Otherwise raise an error condition.
if Request<=Available go to step 3. Otherwise Pi must since the resources
are available
Have the system pretend to have allocated the requested resources to
process Pi by modifying the state as follows;

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

Available=Available-Request I; Allocation I=Allocation +Request I; Need


i=Need i- Request I. If the resulting resource allocation state is safe, the
transaction is completed and process Pi is allocated its resources.
However, if the state is unsafe, the Pi must wait for Request i and the old
resource-allocation state is restored.

ALGORITHM:

1. Start the program.


2. Get the values of resources and processes.

3. Get the avail value.

4. After allocation find the need value.

5. Check whether its possible to allocate.


6. If it is possible then the system is in safe state.

7. Else system is not in safety state.


8. If the new request comes then check that the system is in
safety.
9. or not if we allow the request.

10. stop the program.

11. End

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()

{
int alloc[10][10],max[10][10];
int avail[10],work[10],total[10];
int i,j,k,n,need[10][10];
int
m;
int count=0,c

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

=0;
char finish[10];
clrscr();
printf("Ente processe resource
r the no. of s and s:");
scanf("%d
%d",&n,&m);
for(i=0;i<=n;i+
+)
finish[i]='n
';
printf("Ente clai
r the m matrix:\n");
for(i=0;i<n;
i++)
for(j=0;j<m;j++)
scanf("%d",&max
[i][j]);
printf("Ente matrix:\n"); for(i=0;i<n;i+
r the allocation +)

for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]); printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);

for(i=0;i<m;i++)
avail[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
work[i]=avail[i]; for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)

for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

A:
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n'))
c++;
if(c==m)
{

printf("All the resources can be allocated to


Process %d", i+1); printf("\n\nAvailable
resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess %d executed?:%c \n",i+1,finish[i]);
count++;
}
}
if(count!=n)
goto A;
else
printf("\n System is in safe mode"); printf("\n The given
state is safe state"); getch();

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

OUTPUT
Enter the no. of processes and resources: 4 3 Enter the claim matrix:
322

613
314
422
Enter the allocation matrix:

100
612
211
002

Resource vector:9 3 6

All the resources can be allocated to Process 2


Available resources are: 6 2 3 Process 2 executed?:y

All the resources can be allocated to Process 3


Available resources are: 8 3 4 Process 3 executed?:y

All the resources can be allocated to Process 4


Available resources are: 8 3 6 Process 4 executed?:y

All the resources can be allocated to Process 1


Available resources are: 9 3 6 Process 1 executed?:y
System is in safe mode The given state is safe state

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

LAB: 9
AIM

Conversion of resource allocation graph (RAG) to wait-for-graph (WFG)


for each type of method used for storing graph.

DESCRIPTION

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. The possibility of a deadlock is implied by
graph cycles in the conjunctive case, and by knots in the disjunctive case.
The wait-for-graph scheme is not applicable to a resource allocation
system with multiple instances of each resource type.

CONVERSION OF SINGLE INSTANCES R.A.G. DRAWN


IN EXPERIMENT 7 TO WAIT FOR GRAPH

1.

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

The wait for graph can be drawn as following –

P1
P2

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

LAB: 10
AIM
To Write a C program to simulate producer-consumer problem using
semaphores.

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.

PROGRAM

#include<stdio.>
void main()
{

int buffer[10], bufsize, in, out, produce,


consume, choice=0; in = 0;

out = 0;
bufsize =
10;
while(choice !=3)
{
Exit”
printf(“\n1. Produce \t 2. Consume \t3. );
printf(“\nEnter your choice: ”);

scanf(“%d”,&choice);
switch(choice)

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

{
case 1: if((in+1)%bufsize==out)

printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;;;

case 2:
if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume); out
= (out+1)%bufsize; }
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

OUTPUT

1. Produce
Consume 2.
Exit Enter your
3. choice: 2
Buffer is
Empty
1. Produce 2.
Exit Enter your
Consume 3. choice: 1
Enter the
value: 100
1. Produce 2.
Exit Enter your
Consume 3. choice: 2
The consumed value
is 100
1. Produce 2.
Exit Enter your
Consume 3. choice: 3

AIM: To Write a C program to simulate the concept of


Dining-Philosophers problem.

DESCRIPTION

The dining-philosophers problem is considered a classic synchronization


problem because it is an example of a large class of concurrency-
control problems. It is a simple representation of the need to allocate
several resources among several processes in a deadlock-free and
starvation-free manner. Consider five philosophers who spend their
lives thinking and eating. The philosophers share a circular table
surrounded by five chairs, each belonging to one philosopher. In the
center of the table is a bowl of rice, and the table is laid with five single

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

chopsticks. When a philosopher thinks, she does not interact with her
colleagues. From time to time, a philosopher gets hungry and tries to
pick up the two chopsticks that are closest to her (the chopsticks that
are between her and her left and right neighbors). A philosopher may
pick up only one chopstick at a time. Obviously, she cam1ot pick up a
chopstick that is already in the hand of a neighbor. When a hungry
philosopher has both her chopsticks at the same time, she eats without
releasing her chopsticks. When she is finished eating, she puts down
both of her chopsticks and starts thinking again. The dining-
philosophers problem may lead to a deadlock situation and hence some
rules have to be framed to avoid the occurrence of deadlock.
PROGRAM
int tph, philname[20], status[20],
howhung, hu[20], cho; main()

{
int i; clrscr();

printf("\n\nDINING PHILOSOPHER
PROBLEM"); printf("\nEnter the total
no. of philosophers: ");
scanf("%d",&tph);

for(i=0;i<tph;i++)
{
philname[i]=(i+1);
status[i]=1;

}
printf("How many are hungry : ");
scanf("%d", &howhung); if(howhung==tph)
{

printf(“\n All are hungry..\nDead lock


stage will occur”); printf(\n”Exiting\n”);
else
{

for(i=0;i<howhung;i++)

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

{
printf(“Enterphilosopher%dposition:”,(i+1));

scanf(“%d”,&hu[i]);

status[hu[i]]=2;
}
do
{

printf("1.One can eat at a time\t2.Two


can eat at a time \t3.Exit\nEnter your
choice:"); scanf("%d", &cho);

switch(cho)

{
case 1:
one();
break;;;

CASE 2
two();
break; case 3: exit(0);
default: printf("\nInvalid option..");
}
}
one()
{

}
}while(1);
int pos=0, x, i;

printf("\nAllow one philosopher to eat


at any time\n"); for(i=0;i<howhung; i+
+, pos++) {

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

printf("\nP %d is granted to eat", philname[hu[pos]]);


for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}

}
two()
{
int i, j, s=0, t, r, x;

printf("\n Allow two philosophers to eat at same time\n");


for(i=0;i<howhung;i++)
for(j=i+1;j<howhung;j++)
{

if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];

r=hu[j]; s++;

printf("\nP %d and P %d are granted to eat", philname[hu[i]],


philname[hu[j]]);
for(x=0;x<howhung;x++)
{

if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}}}}

Lab: 11

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

FORK and JOIN construct


a) Write a program where parent process take average of the odd
numbers and child process will take the average of even numbers
present in a given Aadhar number of a person. Use FORK and JOIN
construct.
b) Write a program where parent process finds additive primes and
child process finds circular prime for a given prime list array. Use
FORK and JOIN construct.
IMPLEMENTATION DETAILS:
(i) Parent process and child process can communicate with each other
with the help of shared memory. Parent process and child process both will
work on the data available in that shared memory and according to them
provide their outputs. Two cases will arise according to their sequence of
termination:

Case a) Child process waits until parent process terminates.


Case b) Parent process waits until child process terminates.
(ii) If child process terminates before parent process, process execution
will be unsuccessful.
(iii) At a time only one process is executing. Control will not be transferred
to another process until one process does not complete its execution i.e.
in nonpreemptive manner. According to their termination proper message
must be displayed

AIM
Write a program where parent process takes average of the odd numbers
and child process will take the average of even numbers present in a
given Aadhar number of a person. Use FORK and JOIN construct.

DESCRIPTION
System call fork() is used to create processes. The purpose of fork() is to
create a new process, which becomes the child process of the caller. After
a new child process is created, both processes will execute the next
instruction following the fork() system call.

The fork-join is a basic method of expressing concurrency


within a computation. This is implemented in the UNIX
operating system

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

as system calls. The fork() call creates a new child process which runs
concurrently with the parent. The child process is an exact copy of the
parent except that it has a new process id.

The join() is called by both the parent and the child. The child
process calls join() after it has finished execution. This operation is
done implicitly. The parent process waits until the child joins and
continues later.
There are two join scenarios:
1. The child joins first and then the parent joins without waiting
for any process.

2. The parent process joins first and wait, the child process joins,
and the parent continues thereafte.

SOURCE CODE:

/ C++ program to demonstrate calculation in parent and


/ child processes using fork()
#include <iostream>

#include <unistd.h>
using namespace std;
/
Driv
er
cod
e
int
mai
n()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sumOdd = 0, sumEven = 0,
SEA, SOA, n, i; n = fork();
/ Checking if n is
not 0 if (n > 0) {
for (i = 0; i < 10;
i++) { if

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

(a[i] % 2 ==
0)
sumEven = sumEven + a[i]
}

SEA = sumEven/i;
cout << "Parent process \n";
cout << "Sum of even numbers in adhar number is "
<< sumEven << endl; cout<< "The average of sum of even
numbers is:"<< SEA << endl;

}
/ If n is 0 i.e. we are in child
process else {
for (i = 0; i < 10;
i++) { if
(a[i] % 2 !=
0)
sumOdd = sumOdd + a[i];
}
SOA = sumOdd/n;
cout << "Child process \n";
cout << "\nSum of odd number in adhar number is "
<< sumOdd << endl; cout<<"\n average of sum of odd
numbers is"<< SOA <<endl;
}
return 0;
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

OUTPUT
Parent process
Sum of even numbers in adhar number is 30
The average of sum of even numbers is:6
Child process
Sum of even numbers in the adhar number is 25
The average of sum of add numbers is: 5

AIM
Write a program where parent process finds additive primes and child
process finds circular prime for a given prime list array, using fork and
Join.

Source code

/ Program to check
if a number is circular //
prime or not.
#include
<iostream>
#include
<cmath>
using
namespace
std;

/ Function to check if a
number is prime or not. bool
isPrime(int n)
{
/ Corner cases
if (n <= 1)
return false;

if (n <= 3)
return true;

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])


lOMoARcPSD|54340024

/ This is checked so that we can skip


/ middle five numbers in
below loop if (n % 2 == 0
|| n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)


if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

Downloaded by SAMARTH SONI 22BCE11354 ([email protected])

You might also like