OS Lab Manual: CSE IV Sem Experiments
OS Lab Manual: CSE IV Sem Experiments
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
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.
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.
3
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
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 (_).
4
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
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.
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
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.
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
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
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:
8
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
if(pid<0)
printf("fork failed");
exit(1);
else if(pid==0)
execlp("whoami","ls",NULL);
exit(0);
else
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
ALGORITHM:
11
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
int fd[2];
char buf2[50];
fd[0]=open("file1",O_RDWR);
fd[1]=open("file2",O_RDWR);
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
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
#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();
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i].pid=i;
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;
while(i<=n){
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:
processid bt wt tt
1 2 0 2
2 4 2 6
3 6 6 12
AVG1=2.000000 AVG2=6.000000
RESULT:
18
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
PRIORITY SCHEDULING
AIM : To write a ‘C’ program to perform priority scheduling.
ALGORITHM:
19
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
#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:
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):
ALGORITHM:
23
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
#include<stdio.h>
struct process
int wt,tt;
p[10];
int main()
int i,n,totwt,tottt,avg1,avg2;
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i].pid=i;
");
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:
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
4(a) SEQUENTIAL:
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:
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;
}
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
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:
32
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
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:
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
35
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
AIM :To implement Worst fit ,best fit ,first fit, algorithm for memory management.
ALGORITHM:
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:
Block 1: 5
Block 2: 2
Block 3: 7
File 1: 1
File 2: 4
OUTPUT
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
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:
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
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:
Memory is Full
PROCESS MEMORY
ALLOCATED 1 400
2 275
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 :
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()
{
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
50
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
ALGORITHM:
51
ITS ENGINEERING COLLEGE OS LAB(KCS-451) CSE IVth Sem
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:
345
561
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
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
#include<stdio.h>
void main()
{
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
break;
{
consume = buffer[out];
break;
OUTPUT:
choice: 2
57
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem
Buffer is Empty
choice: 1
choice: 2
choice: 3
RESULT:
58
ITS ENGINEERING COLLEGE OS LAB (KCS-451) CSE IVth Sem
59