Professional Documents
Culture Documents
OS Lab File
OS Lab File
PRACTICAL FILE
OF
OPERATING SYSTEM(KCS-451)
ROLLNO:1901430100016
PROGRAM-1
2
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
PROGRAM-02
4
}
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:
21 3 5 6 2
2143
1 3 1 0 3
2 21 2 3 24
3 2 3 24 26
4 6 4 26 32
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++)
{
}
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
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
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
OUTPUT-:
Enter a number of process:
5134
P1 5 8 13
P2 1 2 3
P3 3 7 10
P4 4 8 12
PROGRAM-05
10
#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
013
010
200
302
211
002
753
3229
902
222
433
332
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:
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
break;
if(j==(st+len))
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;
scanf("%d",&p);
for(i=0;i<p;i++)
scanf("%d",&a);
f[a]=1;
X:
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
k++;
}
17
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
PROGRAM-09
20
}
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
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
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
#include<stdio.h>
clrscr();
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;
51
Input:
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>
clrscr();
25
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;
55
Input:
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: