Operating Sys. Lab Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

4.

Wap to demonstrate

a. First Come First Serve CPU scheduling Algorithm

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
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();
}

Prepare By: Sabina Ghimire


INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000

Prepare By: Sabina Ghimire


b. Shortest Job First CPU scheduling Algorithm

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];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
Prepare By: Sabina Ghimire
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]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}

INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 – 7

Enter Burst Time for Process 3 – 3


OUTPUT BURST WAITING TURNARO
PROCESS TIME TIME UND TIME
3
P3 3 0
9
P0 6 3
16
P2 7 9
24
P1 8 16

Average Waiting Time -- 7.000000


Average Turnaround Time -- 13.000000

Prepare By: Sabina Ghimire


c. Round Robin CPU scheduling Algorithm

SOURCE CODE
#include<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t) {
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else {
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++){
wa[i]=tat[i]-
ct[i]; att+=tat[i];
awt+=wa[i];}
printf("\nThe Average Turnaround time is -- %f",att/n);
Prepare By: Sabina Ghimire
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();}

INPUT:
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3
Enter the size of time slice – 3

OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2347
3 3 7 10
The Average Turnaround time is – 15.666667
TheAverage Waiting time is------------ 5.666667

Prepare By: Sabina Ghimire


a.FCFS DISK SCHEDULING ALGORITHM

SOURCE CODE
#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0; float avhm;
clrscr();
printf(“enter the no.of tracks”);
scanf(“%d”,&n);
printf(“enter the tracks to be traversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);
for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverage header movements:%f",avhm);
getch();
}

Prepare By: Sabina Ghimire


INPUT
Enter no.of tracks:9 45
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT 3
Tracks traversed Difference between tracks 2
55
58 10
60 52
70
72
18
90 60
150
10
160
184 24
Average header movements:30.888889

Prepare By: Sabina Ghimire


b. SCAN DISK SCHEDULING ALGORITHM

SOURCE CODE:
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}}}
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j]; j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)

Prepare By: Sabina Ghimire


atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();}

INPUT
Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184

OUTPUT
Tracks traversed Difference between tracks
50
150 10
160 24
184 94
20
90 10
70 2
60 3
37
58
55
18

Average header movements: 27.77

Prepare By: Sabina Ghimire


c. C-SCAN DISK SCHEDULING ALGORITHM

SOURCE CODE:
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter the tracks");
for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp
}
for(i=0;i<=n+2;i++)
if(t[i]==h);
j=i;break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
Prepare By: Sabina Ghimire
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i]; i++;
p++;
}
Page 67
for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}

INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100
OUTPUT
Tracks traversed Difference Between tracks
150 50
160 10
24
184 240
18 37
55 3
2
58 10
60 20
70
90

Average seek time : 35.7777779

Prepare By: Sabina Ghimire


Lab report on memory management
a. FIFO page replacement algorithm

SOURCE CODE :
#include<stdio.h>
#include<conio.h> int fr[3];
void main() {
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int
flag1=0,flag2=0,pf=0,frsize=3,top=0;
clrscr();
for(i=0;i<3;i++) {
fr[i]=
-1;
}
for(j=0;j<12;j++) {
flag1=0; flag2=0; for(i=0;i<12;i++) {
if(fr[i]==page[j]) {
flag1=1; flag2=1; break; }}
if(flag1==0) {
for(i=0;i<frsize;i++) {
if(fr[i]==
-1)
{
fr[i]=page[j]; flag2=1; break; }}}
if(flag2==0) {
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0; }
display(); }
Page 31
printf("Number of page faults : %d ",pf+frsize);
getch();
}
void display()
{
Prepare By: Sabina Ghimire
int i; printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}

OUTPUT:
2 -1 -1
2 3 -1
2 3 -1
231
531
521
524
524
324
324
354
352
Number of page faults: 9

Prepare By: Sabina Ghimire


b. LRU page replacement

SOURCE CODE :
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1; break;
}
}
if(flag1==0)
Page 33
{
for(i=0;i<3;i++) {
if(fr[i]==
-1)
{
fr[i]=p[j]; flag2=1;
break; }}}
if(flag2==0) {
for(i=0;i<3;i++)
fs[i]=0;
Prepare By: Sabina Ghimire
for(k=j
-1,l=1;l<=frsize
-1;l++,k--
)
{
for(i=0;i<3;i++) {
if(fr[i]==p[k]) fs[i]=1;
}}
for(i=0;i<3;i++) {
if(fs[i]==0)
index=i; }
fr[index]=p[j];
pf++; }
display(); }
printf("
\n no of page faults :%d",pf+frsize);
getch(); }
void display() {
int i; printf("\n");
for(i=0;i<3;i++)
printf(" \ t%d",fr[i]);
}

OUTPUT:
2 -1 -1
2 3 -1
2 3 -1
231
251
251
254
254
354
352
352
352
No of page faults: 7
Prepare By: Sabina Ghimire
c. optimal page replacement

SOURCE CODE:
/* Program to simulate optimal page replacement */
#include<stdio.h>
#include<conio.h>
int fr[3], n, m;
void
display();
void main()
{
int i,j,page[20],fs[10];
int
max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
float pr;
clrscr();
printf("Enter length of the reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&page[i]);
printf("Enter no of frames: ");
scanf("%d",&m);
for(i=0;i<m;i++)
fr[i]=-1; pf=m;
for(j=0;j<n;j++) {
flag1=0; flag2=0;
for(i=0;i<m;i++) {
if(fr[i]==page[j]) {
flag1=1; flag2=1;
break; }}
if(flag1==0) {
for(i=0;i<m;i++) {
if(fr[i]==-1)
{
fr[i]=page[j]; flag2=1;
break; }}}
if(flag2==0) {
for(i=0;i<m;i++)
Prepare By: Sabina Ghimire
lg[i]=0;
for(i=0;i<m;i++) {
for(k=j+1;k<=n;k++) {
if(fr[i]==page[k]) {
lg[i]=k-j;
break; }}}
found=0;
for(i=0;i<m;i++) {
if(lg[i]==0) {
index=i;
found = 1;
break;
}
}
if(found==0)
{
max=lg[0]; index=0;
for(i=0;i<m;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=page[j];
pf++;
}
display();
}
printf("Number of page faults : %d\n", pf);
pr=(float)pf/n*100;
printf("Page fault rate = %f \n", pr); getch();
}
void display()
{
int i; for(i=0;i<m;i++)
printf("%d\t",fr[i]);
printf("\n"); getch();
Prepare By: Sabina Ghimire
}

OUTPUT:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
123
124
124
124
125
125
125
325
425
425
Number of page faults : 7 Page fault rate = 58.333332

Prepare By: Sabina Ghimire


d. WORST-FIT MEMORY MANAGEMENT ALGORITHM

SOURCE CODE:
#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 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]);
Page 25
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
Prepare By: Sabina Ghimire
{
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:-
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
11154
24373
Page 26

Prepare By: Sabina Ghimire


e. BEST-FIT MEMORY MANAGEMENT ALGORITHM

SOURCE CODE:
#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)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}}
Prepare By: Sabina Ghimire
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\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();
}
Page 27

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
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
11221
24151

Prepare By: Sabina Ghimire


f. FIRST-FIT MEMORY MANAGEMENT ALGORITHM

SOURCE CODE:
#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);
scanf("%d",&f[i]);
}
Page 28
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)
Prepare By: Sabina Ghimire
{
}
}
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:-
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
11376
24151

Prepare By: Sabina Ghimire


a. CREATE OF SINGLE PROCESS AND DISPLAY THEIR ID.

SOURCE CODE:

#include<stdio.h>
#include<unistd.h> /*contains forks prototype */
Int main(void)
{
Printf(“hello world!\n”);
Fork();
Printf(“I am after forking\n”);
Printf(“\tl am process %d.\n”,getpid());
}

When this program is executed,it first prints Hello World!. When the fork is
executed, an identical process called the called the child is created. Then both the
parent and the child process begin execution at the next statement.

Prepare By: Sabina Ghimire


b. GETTING AN ID OF A PROCESS.

SOURCE CODE:

#include<stdio.h>
#include<unistd.h> /*contains forks prototype */
Int main(void)
{
Intpid;
Printf(“hello world!\n”);
Printf(“I am the parent process and pids is: %d.\n”,getpid());
Printf(“Here I am before use of forking\n”);
Pid= fork();
Printf(“Here I am just after forking\n”);
If(Pid== 0)
Printf(“I am the child process and pids is: %d.\n”,getpid());
Else
Printf(“I am the parent process and pids is: %d.\n”,getpid());
}

c. USE OF MULTIPLE FORK().

SOURCE CODE:
#include<stdio.h>
#include<unistd.h> /*contains forks prototype */
Int main(void)
{
Printf(“Here I am just before first forking statemement\n”);
fork();
Printf(“Here I am just after first forking statement\n”);
Fork();
Printf(“Here I am just after second forking statement\n”);
Printf(“\t\t Hello World from process %d!\n”,getpid());
}

Prepare By: Sabina Ghimire


d. THREAD CREATION

SOURCE CODE:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
Pthread_ttid[2];
Void*dosomeThing(void*arg)
{
Unsigned long i=0;
Pthread_t id=pthread_self();
If(pthread_equal(id,tid[0]))
{
Printf(“\n first thread processing\n”);
}
Else
{
Printf(“\n second thread processing\n”);
}
For(i=0;i<(0Xffffffff);i++);
Return NULL;
}
Int main(void)
{
Inti=0;
Int err;
While(i<2)
{
Err=pthread_create(&(tid[i]),NULL,&dosomething,NULL);
If(err !=0)
Printf(“\n can’t create thread :[%s]”,strerror(err));
else
printf(“\nThread created successfully\n”);
i++;
}
Sleep(5);
Return0;
Prepare By: Sabina Ghimire
}
e. IMPLEMENT IPC TECHNIQUES.

SOURCE CODE:
#include<stdib.h>
#include<unistd.h>
#include<pthread.h>
#include<stdio.h>
Void*thread1f(void*arg);
Void*thread2f(void*arg);
Int turn=1
Int main()
{
Pthread_t thid1;
Pthread_t thid2;
Pthread_create(&thid1,NULL,&thread1f,NULL);
Pthread_create(&thid2,NULL,&thread2f,NULL);
Pthread_join(thid1,NULL);
Pthread_join(thid2,NULL);
return 0;
}
Void*thread1f(void*arg)
{
Int a=0;
While(a++<20)
{
While(turn!=1);
fputc(‘b’,stderr);
turn=0;
}
}
Void*thread2f(void*arg)
{
Int b=0;
While(b++<20)
{
While(turn!=0);
fputc(‘a’,stderr);
turn=1;
}
Prepare By: Sabina Ghimire
}

Prepare By: Sabina Ghimire

You might also like