Sahil Shah - GL18
Sahil Shah - GL18
Sahil Shah - GL18
Assignment-1
Q1.
Commands used for the following question :
1.cat /proc/meminfo
2.cat /proc/cpuinfo
3.cat /proc/stat
4.cat /proc/vmstat
5.lscpu
Screenshots:-
1.
2.
3.
4.
5.
Q2
Bottleneck process of cpu.c where infinite loop exists and as the process
is not ending it is
categorized into one. Here the bottlenecked resource is clearly
the %CPU usage by the process.
Commands used :
1. gcc cpu.c
2. ./a.out
3. Top
Q3
Process Id(PID) 1 is assigned to the system that is operating in kernel
mode in the virtual file
system of /proc directory and some other allocated PID that other
application is using for
operation. Example : kworker temporarily using the PID 6471.
Command used :
1. ps -p 1
2. ps -p 4202
Screenshot :
Q4.
Involuntary switch context showing the cpu utilization to be at a
maximum and being labelled as
bottleneck process because of this consumption of resource.
Command used:
1. gcc cpu.c
2. ./a.out
3. top
4. Ps -p PID -o %cpu, %mem, cmd
Screenshots:
Q5.
Commands used:
A.
1. sudo bash
2. ps
B.
1. pstree p
Screenshots:
Q6.
Commands used :
1. Sudo bash
2. cd
3. ls
4. history
5. ps
Screenshot:-
Q7.
Commands used :
Commands Used :
1. gcc swap.c
2. ./a.out | grep keyword
3. sudo ls -l /proc /PID/ fd
Assignment 2
1. Write a C program to simulate the following non-preemptive CPU
scheduling algorithms to find turnaround time and waiting time. a)
FCFS b) SJF c) Round Robin d) Priority
FCFS Scheduling:-
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
Screenshot:-
SJF Scheduling:-
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
Screenshot:-
Round Robin Scheduling:-
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number
%d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-
bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Screenshot:-
Priority Scheduling :-
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
Screenshot:-
2. Write a C program to simulate multi-level queue scheduling
algorithm considering the following scenario. All the processes in the
system are divided into two categories system processes and user
processes. System processes are to be given higher priority than
user processes. Use FCFS scheduling for the processes in each
queue.
CODE:-
#include<stdio.h>
#define N 10
typedef struct
{
int process_id, arrival_time, burst_time, priority;
int q, ready;
}process_structure;
int main()
{
int limit, count, temp_process, time, j, y;
process_structure temp;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
process_structure process[limit];
for(count = 0; count < limit; count++)
{
printf("\nProcess ID:\t");
scanf("%d", &process[count].process_id);
printf("Arrival Time:\t");
scanf("%d", &process[count].arrival_time);
printf("Burst Time:\t");
scanf("%d", &process[count].burst_time);
printf("Process Priority:\t");
scanf("%d", &process[count].priority);
temp_process = process[count].priority;
process[count].q = Queue(temp_process);
process[count].ready = 0;
}
time = process[0].burst_time;
for(y = 0; y < limit; y++)
{
for(count = y; count < limit; count++)
{
if(process[count].arrival_time < time)
{
process[count].ready = 1;
}
}
for(count = y; count < limit - 1; count++)
{
for(j = count + 1; j < limit; j++)
{
if(process[count].ready == 1 && process[j].ready == 1)
{
if(process[count].q == 2 && process[j].q == 1)
{
temp = process[count];
process[count] = process[j];
process[j] = temp;
}
}
}
}
for(count = y; count < limit - 1; count++)
{
for(j = count + 1; j < limit; j++)
{
if(process[count].ready == 1 && process[j].ready == 1)
{
if(process[count].q == 1 && process[j].q == 1)
{
if(process[count].burst_time >
process[j].burst_time)
{
temp = process[count];
process[count] = process[j];
process[j] = temp;
}
else
{
break;
}
}
}
}
}
printf("\nProcess[%d]:\tTime:\t%d To %d\n",
process[y].process_id, time, time + process[y].burst_time);
time = time + process[y].burst_time;
for(count = y; count < limit; count++)
{
if(process[count].ready == 1)
{
process[count].ready = 0;
}
}
}
return 0;
}
Screenshot:-
3. Write a C program to simulate pre-emptive SJF CPU scheduling
algorithm.
CODE:-
include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Details of %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");#
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] <
burst_time[smallest] && burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end -
arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end -
arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("\n\nAverage Waiting Time:\t%lf\n",
average_waiting_time);
printf("Average Turnaround Time:\t%lf\n",
average_turnaround_time);
return 0;
}
Screenshot:-
3. Write a C program to simulate the following file allocation
strategies. a) Sequential b) Indexed c) Linked.
a. Sequential:-
#include<stdio.h>
main()
{
intf[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\nEnter 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();
}
Screenshot:-
b.Indexed:-
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
for(i=0;i<50;i++)
f[i]=0;
x:
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;
}
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();
}
Screenshot:-
c.Linked:-
#include<stdio.h>
int 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("\nEnter 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("\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();
}
Screenshot:-
Assignment 3
MVT:-
#include<stdio.h>
int main()
{
int m=0,m1=0,m2=0,p,count=0;
printf(enter the memory capacity);
scanf(%d,&m);
printf(enter the no of processes);
scanf(%d,&p);
for(i=0;i<p;i++)
{
printf(\nenter memory req for process%d is:%d,i+1,m);
M2=m-m1;
printf(\nremaining memory is:%d,m2);
M=m2;
}
else
{
printf(memory is not allocated for process%d,i+1);
}
printf(\nexternal fragmentation for this process is:%d,m2);
}
}
Screenshot:-
MFT:-
#include<stdio.h>
main()
{
intm,p,s,p1;
int
m1[4],i,f,f1=0,f2=0,fra1,f
ra2,s1,pos;
printf("Enter the
memory size:");
scanf("%d",&m);
printf("Enter the no of
partitions:");
scanf("%d",&p);
s=m/p;
printf("Each partn size
is:%d",s);
printf("\nEnter the no of
processes:");
scanf("%d",&p1);
pos=m;
for(i=0;i<p1;i++)
{
if(pos<s)
{
printf("\nThere is no
further memory for
process%d",i+1);
m1[i]=0;
break;
}
else
{
printf("\nEnter the
memory req for
process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess is
allocated in
partition%d",i+1);
fra1=s-m1[i];
printf("\nInternal
fragmentation for
process is:%d",fra1);
f1=f1+fra1;
pos=pos-s;
}
else
{
printf("\nProcess not
allocated in
partition%d",i+1);
s1=m1[i];
while(s1>s)
{
s1=s1-s;
pos=pos-s;
}
pos=pos-s;
fra2=s-s1;
f2=f2+fra2;
printf("\nExternal
Fragmentation for this
process is:%d",fra2);
}
}
}
printf("\nProcess\talloca
tedmemory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d",i+1,
m1[i]);
f=f1+f2;
printf("\nThe tot no of
fragmentation is:%d",f);
return 0;
}
Screenshot:-
2. Write a C program to simulate the following contiguous memory
allocation techniques a)
Worst-fit b) Best-fit c) First-fit
a. Best Fit:-
#include<stdio.h>
int main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment")
;
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragm
ent[i]);
}
Screenshot:-
b. First Fit:-
#include<stdio.h>
int main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
Screenshot:-
c. Worst Fit:-
#include<stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock
Size\tFragment");
for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m],
blocks[file_arr[m]], fragments[m]);
}
printf("\n");
return 0;
}
Screenshot:-
Write a C program to simulate paging technique of memory
3.
management.
Paging:-
#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
printf("\nEnter the no of pages in memory");
scanf("%d",&n);
printf("\nEnter page size");
scanf("%d",&ps);
printf("\nEnter no of frames");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any
frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no &
offset):%d,%d",page[pno],off);
getch();
return 0;
}
Screenshot:-
4.Write a C program to simulate segmentation memory management
technique.
Segmentation:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int
size,m,n,pgno,pageta
ble[3]={5,6,7},i,j,fram
eno;
double m1;
int ra=0,ofs;
clrscr();
printf(Enter process
size (in KB of max
12KB):);/*reading
memeory size*/
scanf(%d,&size);
m1=size/4;
n=ceil(m1);
printf(Total No. of
pages: %d,n);
printf(\nEnter
relative address (in
hexadecimal notation
eg.0XRA) \n);
//printf(The length of
relative Address is :
16 bits \n\n The size
of offset is :12
bits\n);
scanf(%d,&ra);
pgno=ra/1000;
/*calculating physical
address*/
ofs=ra%1000;
printf(page
no=%d\n,pgno);
printf(page table);
for(i=0;i<n;i++)
printf(\n %d
[%d],i,pagetable[i]);
frameno=pagetable[p
gno];
printf(\n Equivalent
physical
address : %d%d,fra
meno,ofs);
getch();
}
#include<stdio.h>
create(int,int);
del(int);
compaction();
display();
int fname[10],fsize[10],fstart[10],freest[10],freesize[10],m=0,n=0,start;
main()
{
int name,size,ch,i;
int *ptr;
ptr=(int *)malloc(sizeof(int)*100);
start=freest[0]=(int)ptr;
freesize[0]=500;
printf("\n\n");
printf(" Free start address Free Size \n\n");
for(i=0;i<=m;i++)
printf(" %d %d\n",freest[i],freesize[i]);
printf("\n\n");
while(1)
{
printf("1.Create.\n");
printf("2.Delete.\n");
printf("3.Compaction.\n");
printf("4.Exit.\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of file: ");
scanf("%d",&name);
printf("\nEnter the size of the file: ");
scanf("%d",&size);
create(name,size);
break;
case 2:
printf("\nEnter the file name which u want to delete: ");
scanf("%d",&name);
del(name);
break;
case 3:
compaction();
printf("\nAfter compaction the tables will be:\n");
display();
break;
case 4:
exit(1);
default:
printf("\nYou have entered a wrong choice.\n");
}
}
flag=1;
compaction();
display();
for(i=0;i<=m;i++)
if( freesize[i] >= size)
a=i,flag=0;
if(!flag)
{
for(j=0;j<n;j++);
n++;
fname[j]=name;
fsize[j]=size;
fstart[j]=freest[a];
freest[a]+=size;
freesize[a]-=size;
printf("\n The memory map will now be: \n\n");
display();
}
else
printf("\nNo enough space.\n");
}
}
void compaction()
{
int i,j,size1=0,f_size=0;
if(fstart[0]!=start)
{
fstart[0]=start;
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
else
{
for(i=1;i<n;i++)
fstart[i]=fstart[i-1]+fsize[i-1];
}
f_size=freesize[0];
for(j=0;j<=m;j++)
size1+=freesize[j];
freest[0]=freest[0]-(size1-f_size);
freesize[0]=size1;
m=0;
}
void display()
{
int i;
Screenshots-
Assignment 5
1. Write a C program to simulate Bankers algorithm for the purpose of
deadlock avoidance.
Banker
Algorithm:-
#include<stdio.h>
int main()
{
printf("\nEnter number of processes: ");
scanf("%d", &processes);
for (i = 0; i < processes; i++)
{
running[i] = 1;
counter++;
}
printf("\nAllocated resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", allocation[i]);
}
printf("\nAvailable resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
counter--;
safe = 1;
printf("\n");
}
}
return 0;
}
Screensho
t:=
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
input();
show();
cal();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
Screens
hot:-
2. Write a C program to simulate disk scheduling algorithms a) FCFS b)
SCAN
FCFS:-
#include<stdio
.h>
main()
{
int
queue[100],n,
head,i,j,k,seek
=0,diff;
avg;
printf("Enter
the size of
Queue\t");
scanf("%d",&n
);
printf("Enter
the Queue\t");
for(i=1;i<=n;i+
+)
{
sc
anf("%d",&que
ue[i]);
}
printf(
"Enter the
initial head
position\t");
scanf("%d",&h
ead);
queue[0]=hea
d;
printf("\n");
for(j=0;j<=n-
1;j++)
{
diff=abs(queu
e[j+1]-
queue[j]);
seek+=diff;
printf("Move fr
om %d to %d
with
Seek%d\n",qu
eue[j],queue[j
+1],diff);
}
printf("\nTotal
Seek Time
is %d\t",seek);
avg=s
eek/(float)n;
printf("\nAvera
ge Seek Time
is%f\t",avg);
Screens
hot:-
SCAN :-
#include<stdio.h>
void main()
{
int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;
printf("Enter head pointer position:");
scanf("%d",&a[0]);
s=a[0];
printf("\nEnter previous head position:");
scanf("%d",&p);
printf("\nEnter max track limit:");
scanf("%d",&m);
printf("\nEnter number of processes:");
scanf("%d",&n);
printf("\nEnter processes in request order");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[n+1]=m;
a[n+2]=0;
for(i=n+1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=1;i<=n+1;i++)
{
if(s==a[i])
x=i;
}
j=0;
if(s<p)
{
for(i=x;i>0;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
t+=a[x+1]-a[0];
b[j++]=a[0];
for(i=x+1;i<n+1;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
b[j++]=a[i];
}
else
{
for(i=x;i<n+2;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
t+=a[n+2]-a[x-1];
b[j++]=a[n+2];
for(i=x-1;i>1;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
b[j++]=a[i];
}
printf("\nProcessing order:");
for(i=0;i<=n+1;i++)
printf("\t%d",b[i]);
printf("\nTotal Head Movement:%d",t);
}
Screens
hot:-
3. Write a C program to simulate page replacement algorithms a) FIFO
b) LRU c) LFU
FIFO:-
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[i]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
Screens
hot:-
LRU:-
#include<stdio.h>
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0,
time[10], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
return 0;
}
Screens
hot:-
LFU:-
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] <
minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}
Screens
hot:-
Optimal:
-
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10],
flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
if(flag3 ==0){
max = temp[0];
pos = 0;
frames[pos] = pages[i];
faults++;
}
printf("\n");
return 0;
}
Screens
hot:-
5. Write a C program to simulate LRU-approximation page replacement
algorithm:- Second-chance algorithm
Second-Chance Algorithm:-
#include<stdio.h>
#define SIZE 3
int full=0;
int a[21];
int ref[SIZE];
int frame[SIZE];
int repptr=0;
int count=0;
int display()
{int i;
printf("\nThe elements in the frame are\n");
for(i=0;i<full;i++)
printf("%d\n",frame[i]);
}
int Pagerep(int ele)
{
int temp;
while(ref[repptr]!=0)
{ ref[repptr++]=0;
if(repptr==SIZE)
repptr=0;
}
temp=frame[repptr];
frame[repptr]=ele;
ref[repptr]=1;
return temp;
}
int Pagefault(int ele)
{if(full!=SIZE)
{ref[full]=1;
frame[full++]=ele;
}
Else
printf("The page replaced is %d",Pagerep(ele));
}
int Search(int ele)
{int i,flag;
flag=0;
if(full!=0)
{
for(i=0;i<full;i++)
if(ele==frame[i])
{ flag=1;ref[i]=1;
break;
}}
return flag;
}
int main()
{int n,i;
FILE *fp;
fp=fopen("Input.txt","r");
printf("The number of elements in the reference
string are :");
fscanf(fp,"%d",&n);
printf("%d",n);
for(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
printf("\nThe elements present in the string
are\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
if(Search(a[i])!=1)
{Pagefault(a[i]);
display();
count++;
}
}
printf("\nThe number of page faults are
%d\n",count);
getche();
return 0;