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

1

Department of Computer Science


IMS Engineering College, Ghaziabad (U.P.) India
(Affiliated to Dr. A.P.J. Abdul Kalam
TechnicalUniversity , Lucknow , India)

PRACTICAL FILE
OF
OPERATING SYSTEM(KCS-451)

Submitted To: Submitted By:


……………….. Akash Singh
B.tech 4th SEM

ROLLNO:1901430100016

PROGRAM-1
2

AIM-:WAP IN C TO PERFORM FCFS SCHEDULING


ALOGRITHM
#include<stdio.h>
#include<conio.h>
# define max 30
int main()
{

int n,j,bt[max],wt[max],tat[max];
float awt=0,att=0;
printf("Enter a number of process:\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter burst time of process %d\n",i);
scanf("%d",&bt[i]);

}
printf("Process\t Burst Time\t WaitingTime\t TurnarroundTime\n");
wt[0]=0;
for(int i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}

for(int i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
awt=awt+wt[i];
att=att+tat[i];
printf("%d\t %d\t\t %d\t\t %d\n",i,bt[i],wt[i],tat[i]);
}
awt=awt/n;
att=att/n;
printf("Averge waiting time %f\n:",awt);
printf("Averge TurnarroundTime %f:",att);

OUTPUT-:
Enter a number of process:
4
Enter a the sequence no of process:1
3

Enter a burst time of p1 :3


Enter a the sequence no of process:2
Enter a burst time of p2 :4
Enter a the sequence no of process:3
Enter a burst time of p3 :2
Enter a the sequence no of process:4
Enter a burst time of p4 :4
Process Burst Time WaitingTime TurnarroundTime
3 2 0 2
1 3 2 5
2 4 5 9
4 4 9 13
Averge waiting time 4.000000
:Averge TurnarroundTime 7.250000

PROGRAM-02
4

AIM-: WAP IN C TO PERFORM PRIORITY


SCHEDULING ALGORITHM
#include<stdio.h>
#include<conio.h>
#define max 30
int main()
{
int i,j,n,temp,bt[max],wt[max],tat[max],pro[max];
float awt=0,atat=0;\
printf("Vishal Gupta 1901430120061\n");
printf("Enter a number of process:\n");
scanf("%d",&n);
printf("Enter a burst time of processes:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);

}
printf("Enter priority of process\n");
for(int i=0;i<n;i++)
{
scanf("%d",&pro[i]);

}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(pro[j]>pro[j+1])
{
temp=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temp;

temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;

}
}
}
printf("Process\t Burst Time\t Prirotity\t WaitingTime\t TurnarroundTime\n");
wt[0]=0;
for(int i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
5

for(int i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
awt=awt+wt[i];
atat=atat+tat[i];
printf("%d\t %d\t\t %d\t\t %d\t\t %d\n",i+1,bt[i],pro[i],wt[i],tat[i]);
}
awt=awt/n;
atat=atat/n;
printf("Averge waiting time %f\n:",awt);
printf("Averge TurnarroundTime %f:",atat);
}
OUTPUT-:
Enter a number of process:

Enter a burst time of processes:

21 3 5 6 2

Enter priority of process

2143

Process Burst Time Priority WaitingTime TurnarroundTime

1 3 1 0 3

2 21 2 3 24

3 2 3 24 26

4 6 4 26 32

Averge waiting time 13.250000

:Averge TurnarroundTime 21.250000

PROGRAM-3
AIM-: WAP IN C TO PERFORM SJF SCHEDULING
6

ALGORITHM
#include<stdio.h>
#include<conio.h>
#define max 30
int main()
{
int i,j,n,temp,p[max],bt[max],wt[max],tat[max];
float awt=0,atat=0;
printf("Enter a number of process:\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{

printf("Enter a the sequence no of process:");


scanf("%d",&p[i]);
printf("Enter a burst time of p%d :",p[i]);
scanf("%d",&bt[i]);

}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(bt[j]>bt[j+1])
{
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;

temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
printf("Process\t Burst Time\t WaitingTime\t TurnarroundTime\n");
wt[0]=0;
for(int i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}

for(int i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
awt=awt+wt[i];
atat=atat+tat[i];
7

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


}
awt=awt/n;
atat=atat/n;
printf("Averge waiting time %f\n:",awt);
printf("Averge TurnarroundTime %f:",atat);
}
OUTPUT-:
Enter a number of process:

Enter a the sequence no of process:1

Enter a burst time of p1 :7

Enter a the sequence no of process:2

Enter a burst time of p2 :4

Enter a the sequence no of process:3

Enter a burst time of p3 :1

Enter a the sequence no of process:4

Enter a burst time of p4 :4

Process Burst Time WaitingTime TurnarroundTime

3 1 0 1

2 4 1 5

4 4 5 9

1 7 9 16
Averge waiting time 3.750000
:Averge TurnarroundTime 7.750000:
PROGRAM-04
8

AIM-: WAP IN C TO PERFORM ROUND ROBIN


SCHEDULING ALGORITHM
#include<stdio.h>
#include<conio.h>
#define max 30
int main()
{
int i,n,count=0,temp,temp2=0,p[max],bt[max],wt[max],tat[max],t,rem_bt[max];
float awt=0,atat=0;
printf("Enter a number of process:\n");
scanf("%d",&n);
printf("Enter burst time:\n");
for(i=0;i<n;i++)
{

scanf("%d",&bt[i]);
rem_bt[i]=bt[i];

}
printf("Enter the value of quantum time: ");
scanf("%d",&t);

while(1){
for(i=0,count=0;i<n;i++){
temp=t;
if(rem_bt[i]==0){
count++;
continue;

if(rem_bt[i]>t){
rem_bt[i]=rem_bt[i]-t;
}

else if(rem_bt[i]>=0){
temp=rem_bt[i];
rem_bt[i]=0;
}
temp2+=temp;
tat[i]=temp2;
}
if(n==count){
break;
}
}
9

printf("Process\t Burst Time\t WaitingTime\t TurnarroundTime\n");


for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
awt=awt+wt[i];
atat=atat+tat[i];
printf("P%d\t %d\t\t %d\t\t %d\n",i+1,bt[i],wt[i],tat[i]);
}
awt=awt/n;
atat=atat/n;
printf("Averge waiting time %f\n:",awt);
printf("Averge TurnarroundTime %f:",atat);
}

OUTPUT-:
Enter a number of process:

Enter burst time:

5134

Enter the value of quantum time: 2

Process Burst Time WaitingTime TurnarroundTime

P1 5 8 13

P2 1 2 3

P3 3 7 10

P4 4 8 12

Averge waiting time 6.250000

:Averge TurnarroundTime 9.500000

PROGRAM-05
10

AIM-: WAP IN C TO PERFORM BANKER ALGORITHM

#include<stdio.h>
#include<conio.h>
# define max 20
int main()
{
int n,r,i,j,total[max],maxi[max][max],alloc[max][max],need[max]
[max],avail[max],ans[max],res[max],v=0,c=0;
printf("Enter a number of process\n");
scanf("%d",&n);
printf("Enter a number of resource\n");
scanf("%d",&r);

for(i=0;i<n;i++)
{
printf("Enter number of allocation resource of P%d\n",i);
for(j=0;j<r;j++)
{

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

}
}
for(i=0;i<n;i++)
{
printf("Enter number of maximum resource of P%d\n",i);
for(j=0;j<r;j++)
{

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

}
}
printf("Enter Available resources\n");
for(i=0;i<r;i++)
{
scanf("%d",&avail[i]);
}
for(i=0;i<n;i++)
11

{
for(j=0;j<r;j++)
{
need[i][j]=maxi[i][j]-alloc[i][j];
}
}
for(i=0;i<n;i++)
{
res[i]=0;
}
int y=0;
for(int k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(res[i]==0)
{
int flag=0;
for(j=0;j<r;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0 && res[i]==0)
{
ans[v++]=i;
for(y=0;y<r;y++)
{
avail[y]+=alloc[i][y];
}
res[i]=1;
c+=1;
}
}

}
}
if(c==n)
12

{
printf("Safe state");
for(i=0;i<n;i++)
{
printf("P%d ->",ans[i]);
}
}
else
{
printf("Unsafe state");
}
}

OUTPUT-:
Enter a number of process

Enter a number of resource

013

Enter number of allocation resource of P0

010

Enter number of allocation resource of P1

200

Enter number of allocation resource of P2

302

Enter number of allocation resource of P3

211

Enter number of allocation resource of P4


13

002

Enter number of maximum resource of P0

753

Enter number of maximum resource of P1

3229

Enter number of maximum resource of P2

902

Enter number of maximum resource of P3

222

Enter number of maximum resource of P4

433

Enter Available resources

332

Safe stateP1 ->P3 ->P4 ->P0 ->P2 ->

PROGRAM-06
AIM-: WAP IN C TO PERFORM SEQUENTIAL FILE STORAGE
14

#include<stdio.h>

main()

int f[50],i,st,j,len,c,k;

clrscr();

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

f[i]=0;

X:

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

}
15

Output:
Enter the starting block & length of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
23
11->1
12->1
13->1
The file is allocated to disk
If you want to enter more files? (Y-1/N-0)

PROGRAM-07
AIM-: WAP IN C TO PERFORM LINKED FILE STORAGE
16

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

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

}
17

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

26

OUTPUT:
Enter how many blocks are already allocated 3
Enter the blocks no’s that are already allocated 4 7 9
Enter the starting index block & length 3
7
3-> 1
4-> File is already allocated
5->1
6->1
7-> File is already allocated
8->1
9-> File is already allocated
10->1
11->1
12->1
If u want to enter one more file? (yes-1/no-0)

PROGRAM-08
AIM-:WAP IN C PERFORM BOUNDED BUFFER
18

#include<stdio.h>
#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}

int wait(int s)
{
return (--s);
}

int signal(int s)
{
return(++s);
}

void producer()
19

{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}

Output-:

1.Producer
2.Consumer
3.Exit
Enter your choice:2
Buffer is empty!!
Enter your choice:1
Producer produces the item 1
Enter your choice:1
Producer produces the item 2
Enter your choice:2
Consumer consumes item 2

Enter your choice:3

PROGRAM-09
20

AIM-:WAP IN C TO PERFORM READERS WRITERS


ALGORITHM
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;

void *writer(void *wno)


{
sem_wait(&wrt);
cnt = cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);

}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
21

// Reader acquire the lock before modifying numreader


pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
sem_post(&wrt); // If this is the last reader, it will wake up the writer.
}
pthread_mutex_unlock(&mutex);
}

int main()
{

pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);

int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the producer and consumer

for(int i = 0; i < 10; i++) {


pthread_create(&read[i], NULL, (void *)reader, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_create(&write[i], NULL, (void *)writer, (void *)&a[i]);
}

for(int i = 0; i < 10; i++) {


pthread_join(read[i], NULL);
}
22

for(int i = 0; i < 5; i++) {


pthread_join(write[i], NULL);
}

pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);

return 0;

// FCFS PAGE REPLACEMENT//


23

#include<stdio.h>

#include<conio.h> void main()

int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1; char f='F';

clrscr();

printf("Enter the Number of Pages:"); scanf("%d",&n);

printf("Enter %d Page Numbers:",n); for(i=0;i<n;i++)

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

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

{if(p==0)

if(q>=3)

q=0;

a[q]=b[i];

q++;

if(q1<3)

q1=q;

printf("\n%d",b[i]);

printf("\t");

for(h=0;h<q1;h++)

printf("%d",a[h]);

if((p==0)&&(q<=3))

printf("-->%c",f);m++;

p=0;

for(k=0;k<q1;k++)

{
24

if(b[i+1]==a[k])

p=1;

printf("\nNo of faults:%d",m); getch();

51

SAMPLE INPUT AND OUTPUT:

Input:

Enter the Number of Pages: 12 Enter 12 Page Numbers:

232152453252

Output:

2 2-> F

323-> F

223

1231-> F

5531-> F

2521-> F

4524-> F

5524

3 324-> F

2 324

5 354-> F

2 352-> F

No of faults: 9

//LRU//

#include<stdio.h>

#include<conio.h> void main()

int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n; char f='F';

clrscr();
25

printf("Enter the number of pages:"); scanf("%d",&n);

printf("Enter %d Page Numbers:",n); for(i=0;i<n;i++)

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

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

{if(p==0)

if(q>=3)

q=0;

a[q]=b[i];

q++;

if(q1<3)

q1=q;

//g=1;

printf("\n%d",b[i]);

printf("\t");

for(h=0;h<q1;h++)

printf("%d",a[h]);

if((p==0)&&(q<=3))

printf("-->%c",f);m++;

p=0;

g=0;

if(q1==3)

for(k=0;k<q1;k++)

54

{
26

if(b[i+1]==a[k])

p=1;

for(j=0;j<q1;j++)

u=0;

k=i;while(k>=(i-1)&&(k>=0))

if(b[k]==a[j])

u++;k--;

if(u==0)

q=j;

else

for(k=0;k<q;k++)

if(b[i+1]==a[k])

p=1;

printf("\nNo of faults:%d",m); getch();

55

SAMPLE INPUT AND OUTPUT:

Input:

Enter the Number of Pages: 12 Enter 12 Page Numbers:

232152453252
27

Output:

22-> F

323-> F

2 23

1231-> F

5251-> F

2251

4254-> F

5254

3354-> F

2352-> F

5352

2352

No of faults: 7

RESULT:

Thus the program was executed successfully

You might also like