Upload
aims4success
View
223
Download
1
Embed Size (px)
Citation preview
7/30/2019 OSLab Final
1/49
SWCET Date :
Op erating Systems 30
Week 9
1. Simulate the following CPU scheduling algorithms
(a) Round Robin#include
#include
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i
7/30/2019 OSLab Final
2/49
SWCET Date :
Op erating Systems 31
for(i=0;its)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else
if((et[i]
7/30/2019 OSLab Final
3/49
SWCET Date :
Op erating Systems 32
Output
Enter the no of processes : 2
Enter the time quantum: 3
Enter the process name & estimated time : p1 12
Enter the process name & estimated time : p2 15
p1 ->3
p2 ->3
p1 ->3
p2 ->3
p1 ->3
p2 ->3
p1 ->3
p2 ->3
p2 -> 3
Total estimated time : 27
7/30/2019 OSLab Final
4/49
SWCET Date :
Op erating Systems 33
(b) SJF
#include
#include
#include
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i
7/30/2019 OSLab Final
5/49
SWCET Date :
Op erating Systems 34
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i
7/30/2019 OSLab Final
6/49
SWCET Date :
Op erating Systems 35
getch();
}
Output
Enter the number of processes: 3
Enter the process name, arrival time and burst time: p1 4 6
Enter the process name, arrival time and burst time: p2 5 15
Enter the process name, arrival time and burst time: p3 6 11
Pname Arrival Time Execution Time Waiting Time Turnaround Time
p1 4 6 0 6
p3 6 11 4 15
p2 5 15 16 31
Average waiting time : 6.6667
Average turn around time : 17.3333
7/30/2019 OSLab Final
7/49
SWCET Date :
Op erating Systems 36
(c) FCFS
#include
#include
#include
main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
int time_passed=0;
//clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i
7/30/2019 OSLab Final
8/49
SWCET Date :
Op erating Systems 37
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0;itime_passed)
time_passed=arr[i];
star[i]=time_passed;
time_passed += bur[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nPName Arrtime Burtime WaitTime Start\tTAT");
for(i=0;i
7/30/2019 OSLab Final
9/49
SWCET Date :
Op erating Systems 38
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
getch();
return 0;
}
Output
Enter the number of processes: 3
Enter the process name, arrival time and burst time: p1 6 5
Enter the process name, arrival time and burst time: p2 4 15
Enter the process name, arrival time and burst time: p3 8 10
PName Arrival Time Burst Time Wait Time Start TAT
p2 4 15 0 4 15
p1 6 5 13 19 18
p3 8 10 16 24 26
Average waiting time : 9.666667
Average Turn around time: 19.666667
7/30/2019 OSLab Final
10/49
SWCET Date :
Op erating Systems 39
(d) Priority
#include
#include
#include
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i
7/30/2019 OSLab Final
11/49
SWCET Date :
Op erating Systems 40
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
for(i=0;i
7/30/2019 OSLab Final
12/49
SWCET Date :
Op erating Systems 41
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i
7/30/2019 OSLab Final
13/49
SWCET Date :
Op erating Systems 42
Output
Enter the number of processes: 3
Enter the process name, arrival time, execution time and priority: p1 2 3 1
Enter the process name, arrival time, execution time and priority: p2 4 5 2
Enter the process name, arrival time, execution time and priority: p3 5 6 3
Pname Arrival Time Execution Time Priority Waiting Time Turnaround Time
p1 2 3 1 0 3
p3 4 5 2 1 6
p2 5 6 3 5 11
Average waiting time : 2.0000
Average turn around time : 6.6667
7/30/2019 OSLab Final
14/49
SWCET Date :
Op erating Systems 43
Week 10
2. Simulate the following file allocation strategies
(a) Sequential#include
#include
main()
{
int a[50],n,i,j,k,fname,startblock,length,currentfilename;
printf("Enter the number of files:\t");
scanf("%d", &n);
for(i=0;i
7/30/2019 OSLab Final
15/49
SWCET Date :
Op erating Systems 44
if(j==startblock+length-1)
for(k=startblock;k
7/30/2019 OSLab Final
16/49
SWCET Date :
Op erating Systems 45
for(j=0;j
7/30/2019 OSLab Final
17/49
SWCET Date :
Op erating Systems 46
The memory is:
0 0 1 1 1 0 0 0 0 2
2 2 2 2 0 0 0 0 3 3
3 3 3 3 3 3 0 0 0 0
4 4 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
After Compaction:
1 1 1 2 2 2 2 2 3 3
3 3 3 3 3 3 4 4 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
7/30/2019 OSLab Final
18/49
SWCET Date :
Op erating Systems 47
(b) Linked
#include
#include
#include
main()
{
int a[50][3],n,i,j,k,fname,startblock,length,currentfilename;
int blocksallocated,currentlocation,previouslocation;
printf("Enter the number of files:\t");
scanf("%d", &n);
srand(time(NULL));
for(i=0;i
7/30/2019 OSLab Final
19/49
SWCET Date :
Op erating Systems 48
a[currentlocation][0]= fname;
if(blocksallocated!=0)
a[previouslocation][1] = currentlocation;
else
a[currentlocation][2] = 1;
previouslocation = currentlocation;
blocksallocated++;
if(blocksallocated==length)
a[currentlocation][1] = -1;
}
}
printf("\n\nThe memory is:\n");
for(j=0;j
7/30/2019 OSLab Final
20/49
SWCET Date :
Op erating Systems 49
Output
Enter the number of files: 2
Enter the filename: 1
Enter the length of the file: 4
Enter the filename: 2
Enter the length of the file:3
0 2 18 0 1 1 4 6 0 2 0 0 0 3 0 0 0
4 1 1 1 5 0 0 0 6 0 0 0 7 0 0 0
8 0 0 0 9 0 0 0 10 0 0 0 11 0 0 0
12 0 0 0 13 1 -1 0 14 0 0 0 15 2 0 1
16 0 0 0 17 0 0 0 18 2 -1 0 19 0 0 0
20 0 0 0 21 0 0 0 22 0 0 0 23 0 0 0
24 0 0 0 25 0 0 0 26 0 0 0 27 0 0 0
28 0 0 0 29 0 0 0 30 0 0 0 31 0 0 0
32 0 0 0 33 0 0 0 34 0 0 0 35 0 0 0
36 0 0 0 37 0 0 0 38 0 0 0 39 0 0 0
40 0 0 0 41 0 0 0 42 0 0 0 43 0 0 0
44 0 0 0 45 0 0 0 46 1 13 0 47 0 0 0
48 0 0 0 49 0 0 0
7/30/2019 OSLab Final
21/49
SWCET Date :
Op erating Systems 50
Week 11
3. Simulate MVT and MFT
MVT#include
#include
void main()
{
int m=0,m1=0,m2=0,p,count=0,i;
clrscr();
printf("enter the memory capacity:");
scanf("%d",&m);
printf("enter the no of processes:");
scanf("%d",&p);
for(i=0;i
7/30/2019 OSLab Final
22/49
SWCET Date :
Op erating Systems 51
}
else
{
printf("memory is not allocated for process%d",i+1);
}
printf("\nexternal fragmentation for this process is:%d",m2);
}
getch();
}
Output
Enter the memory capacity: 80
Enter no of processes: 2
Enter memory req for process1: 23
The memory allocated for process1 is: 80
Remaining memory is: 57
External fragmentation for this process is: 57
Enter memory req for process2: 52
The memory allocated for process2 is: 57
Remaining memory is: 5
External fragmentation for this process is: 5
7/30/2019 OSLab Final
23/49
SWCET Date :
Op erating Systems 52
MFT
#include
#include
int main()
{
int m,p,s,p1;
int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos;
clrscr();
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
7/30/2019 OSLab Final
24/49
SWCET Date :
Op erating Systems 53
printf("\nEnter the memory req for process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]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\tallocatedmemory");
7/30/2019 OSLab Final
25/49
SWCET Date :
Op erating Systems 54
for(i=0;i
7/30/2019 OSLab Final
26/49
SWCET Date :
Op erating Systems 55
4. Simulate all file organization techniques
(a) Single level directory
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i
7/30/2019 OSLab Final
27/49
SWCET Date :
Op erating Systems 56
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j
7/30/2019 OSLab Final
28/49
SWCET Date :
Op erating Systems 57
(b) Two level directory
#include
#include
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
7/30/2019 OSLab Final
29/49
SWCET Date :
Op erating Systems 58
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;ilink[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("hoe many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
7/30/2019 OSLab Final
30/49
SWCET Date :
Op erating Systems 59
else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;inc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;inc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
7/30/2019 OSLab Final
31/49
SWCET Date :
Op erating Systems 60
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;inc;i++)
{
display(root->link[i]);
}
} }
7/30/2019 OSLab Final
32/49
SWCET Date :
Op erating Systems 61
Output
enter name of dir/file(under null):sld
How many users(forsld):2
enter name of dir/file(under sld):tld
hoe many files(fortld):2
enter name of dir/file(under tld):hir
enter name of dir/file(under tld):dag
enter name of dir/file(under sld):bin
hoe many files(forbin):2
enter name of dir/file(under bin):exe
enter name of dir/file(under bin):obj
bintld
hirda ex
ob
7/30/2019 OSLab Final
33/49
SWCET Date :
Op erating Systems 62
Week 12
5. Simulate Bankers Algorithm for deadlock avoidance
#include
#include
void main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t,total=0;
count=0;
for(i=1;i
7/30/2019 OSLab Final
34/49
SWCET Date :
Op erating Systems 63
for(j=1;j
7/30/2019 OSLab Final
35/49
SWCET Date :
Op erating Systems 64
{ printf("%d",clm[i][j]); }
printf("\t\t\t");
for(j=1;j
7/30/2019 OSLab Final
36/49
SWCET Date :
Op erating Systems 65
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
7/30/2019 OSLab Final
37/49
SWCET Date :
Op erating Systems 66
Output
Enter the number of processes : 4
Enter the number of resources : 1
Enter the claim matrix for each process:
For process 1: 6
For process 2: 5
For process 3 : 4
For process 4 : 7
Enter the allocation matrix for each process:
For process 1: 1
For process 2: 2
For process 3 : 2
For process 4 : 4
Enter the total number of resources : 10
Available resource is : 1
Claim matrix 6 5 4 7 Allocation matrix 1 2 2 4
The system is in an unsafe state.
7/30/2019 OSLab Final
38/49
SWCET Date :
Op erating Systems 67
6. Simulate Bankers Algorithm for deadlock prevention
Deadlock Prevention:o Preventing deadlocks by constraining how requests for resources can be made
in the system and how they are handled (system design).
o The goal is to ensure that at least one of the necessary conditions for deadlockcan never hold.
Deadlock Avoidance:o The system dynamically considers every request and decides whether it is safe
to grant it at this point,
oThe system requires additional apriori information regarding the overallpotential use of each resource for each process.
o Allows more concurrency.
Similar to the difference between a traffic light and a police officer directing traffic.
Difference from avoidance is that here, the system itself is build in such a way that
there are no deadlocks. The difference between the two is very subtle. The above
program can be used as an implementation of deadlock prevention.
Conditions for deadlock .
Mutual exclusion: resources cannot be shared. Hold and wait: processes request resources incrementally, and hold on to what
they've got.
No preemption: resources cannot be forcibly taken from processes. Circular wait
: circular chain of waiting, in which each process is waiting for aresource held by the next process in the chain.
For Deadlock prevention ,
Make sure atleast one of the 4 deadlock conditions is never satisfied. This may however be even more conservative than deadlock avoidance strategy.
7/30/2019 OSLab Final
39/49
SWCET Date :
Op erating Systems 68
Week 13
7. Simulate all page replacement algorithms
(a) FIFO
#include
#include
#define PAGES_IN_WINDOW 3
void main()
{
int window[PAGES_IN_WINDOW], window_old[PAGES_IN_WINDOW];
int pages[50];
int number_of_pages;
int page_faults = 0;
int i,j,k,l;
clrscr();
printf("\nEnter the number of pages:\t");
scanf("%d", &number_of_pages);
printf("\nEnter the pages in order:\t");
for(l=0;l
7/30/2019 OSLab Final
40/49
SWCET Date :
Op erating Systems 69
for(i=0;i
7/30/2019 OSLab Final
41/49
SWCET Date :
Op erating Systems 70
printf("%d\t",window[j]);
window_old[j]=window[j];
}
}
printf("\n\nNumber of page faults: %d",page_faults);
getch();
}
Output
Enter the number of pages : 12
Enter the 12 page numbers:
2 3 2 1 5 2 4 5 3 2 5 2
Referring 2
Page Fault
Window : 2
Referring 3
Page Fault
Window : 2 3
Referring 2
Window : 2 3
Referring 1
Page Fault
Window : 2 3 1
Referring 5
7/30/2019 OSLab Final
42/49
SWCET Date :
Op erating Systems 71
Page Fault
Window : 3 1 5
Referring 2
Page Fault
Window : 1 5 2
Referring 4
Page Fault
Window : 5 2 4
Referring 5
Window : 5 2 4
Referring 3
Page Fault
Window : 2 4 3
Referring 2
Window : 2 4 3
Referring 5
Page Fault
Window : 4 3 5
Referring 2
Page Fault
7/30/2019 OSLab Final
43/49
SWCET Date :
Op erating Systems 72
Window : 3 5 2
Number of page faults : 9
7/30/2019 OSLab Final
44/49
SWCET Date :
Op erating Systems 73
(b) LRU
#include
#include
#define PAGES_IN_WINDOW 3
void main()
{
int window[PAGES_IN_WINDOW];
int pages[50];
int number_of_pages;
int page_faults = 0;
int lru[PAGES_IN_WINDOW];
int i,j,k,l;
clrscr();
printf("\nEnter the number of pages:\t");
scanf("%d", &number_of_pages);
printf("\nEnter the pages in order:\t");
for(l=0;l
7/30/2019 OSLab Final
45/49
SWCET Date :
Op erating Systems 74
int page_number = pages[i];
int maximum_u=0;
int maximum_u_location=0;
int found=0;
printf("\n\nReferring %d",page_number);
for(k=0;kmaximum_u)
{
maximum_u=lru[k];
maximum_u_location=k;
}
if(page_number == window[k])
{
found=1;
lru[k]=0;
}
else
{
lru[k]++;
}
if((found==0)&(k==PAGES_IN_WINDOW-1))
{
window[maximum_u_location] = page_number;
lru[maximum_u_location] = 0;
page_faults++;
7/30/2019 OSLab Final
46/49
SWCET Date :
Op erating Systems 75
printf("\nPage Fault");
}
}
printf("\nWindow:\t");
for(j=0;j0)&&(window[j]
7/30/2019 OSLab Final
47/49
SWCET Date :
Op erating Systems 76
Referring 1
Page Fault
Window : 2 3 1
Referring 5
Page Fault
Window : 2 5 1
Referring 2
Window : 2 5 1
Referring 4
Page Fault
Window : 2 5 4
Referring 5
Window : 2 5 4
Referring 3
Page Fault
Window : 3 5 4
Referring 2
Page Fault
Window : 3 5 2
7/30/2019 OSLab Final
48/49
SWCET Date :
Op erating Systems 77
Referring 5
Window : 3 5 2
Referring 2
Window : 3 5 2
Number of page faults : 7
7/30/2019 OSLab Final
49/49
SWCET Date :
8. Simulate paging technique of memory management.
#include
#include
main()
{
int np,ps,i;
int *sa;
printf("enter how many pages\n");
scanf("%d",&np);
printf("enter the page size \n");
scanf("%d",&ps);
sa=(int*)malloc(2*np);
for(i=0;i