OS Lab
OS Lab
OS Lab
#include <stdio.h>
#include <unistd.h>
intmain()
{
fork();
printf("Operating system\n");
}
output
#include <stdio.h>
#include <unistd.h>
intmain()
intpid;
pid=fork();
if(pid==0)
else
Output
1
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;
}
}
Output
4. Write a program to simulate first come first serve (FCFS) process scheduling.
#include<stdio.h>
void main()
{
intn,bt[20],wt[20],tat[20],i,j;
float avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20) ");
scanf("%d",&n);
printf("Enter Process Burst Time\n");
for(i=0;i<n;i++)
{
2
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
for(i=1;i<n;i++) //calculating waiting time
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++) //calculating turnaround time
{
tat[i]=bt[i]+wt[i];
avwt=avwt+wt[i];
avtat=avtat+tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt=avwt/i;
avtat=avtat/i;
printf("\n\nAverage waiting time:%.2f",avwt);
printf("\nAverage turnaround time:%.2f",avtat);
}
Output
3
intn,bt[20],wt[20],tat[20],i,j,p[20],temp;
float avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20) ");
scanf("%d",&n);
printf("Enter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
wt[0]=0; //waiting time for first process is 0
for(i=1;i<n;i++) //calculating waiting time
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++) //calculating turnaround time
{
tat[i]=bt[i]+wt[i];
avwt=avwt+wt[i];
avtat=avtat+tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avwt=avwt/i;
avtat=avtat/i;
printf("\n\nAverage waiting time:%.2f",avwt);
printf("\nAverage turnaround time:%.2f",avtat);
4
}
Output
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pri[i]>pri[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=bt[i];
bt[i]=bt[j];
5
bt[j]=temp;
temp=pri[i];
pri[i]=pri[j];
pri[j]=temp;
}
}
}
6
7. Write a program to simulate Round Robin (RR) scheduling.
#include<stdio.h>
void main()
{
inti,j,n,bt[10],wt[10],tat[10],t,ct[10],max;
float avwt=0,avtat=0,temp=0;
printf("Enter total number of processes(maximum 20) ");
scanf("%d",&n);
printf("Enter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
ct[i]=bt[i];
}
printf("Enter the size of time slice ");
scanf("%d",&t);
max=bt[0];
for(i=1;i<n;i++)
if(max<bt[i])
max=bt[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bt[i]!=0)
if(bt[i]<=t)
{
tat[i]=temp+bt[i];
temp=temp+bt[i];
bt[i]=0;
}
else
{
bt[i]=bt[i]-t;
temp=temp+t;
}
7
printf("\n\nAverage waiting time:%.2f",avwt);
printf("\nAverage turnaround time:%.2f",avtat);
}
Output
8
if(in==out)
printf("\nBuffer is Empty");
else
{
consume=buffer[out];
printf("\nThe consumed value is %d",consume);
out=(out+1)%bufsize;
}
break;
}
}
}
Output
9
9. Write a program to simulate Banker’s algorithm for the purpose of deadlock avoidance.
// Banker's Algorithm
#include <stdio.h>
intmain()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
intalloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
10
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
11
}
for(i=0;i<pno;i++) //allocation as per first fit
for(j=0;j<bno;j++)
if(flags[j]==0&&bsize[j]>=psize[i])
{
allocation[j]=i;
flags[j]=1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i=0;i<bno;i++)
{
printf("\n%d\t\t%d\t\t",i+1,bsize[i]);
if(flags[i]==1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
Output
12
11. Write a program to simulate best fit algorithm
#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static intbarray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
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]],fragment[i]);
}
13
12. Write a program to implement first come first serve (FCFS) disk scheduling algorithm.
#include<stdio.h>
intmain()
{
inti,j,sum=0,n;
intar[20],tm[20];
int disk;
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
tm[i]=disk-ar[i];
if(tm[i]<0)
{
tm[i]=ar[i]-disk;
}
disk=ar[i];
sum=sum+tm[i];
}
14
printf("\nmovement of total cylinders %d",sum);
getch();
return 0;
}
Output
13. Write a program to implement shortest seek time first (SSTF) disk scheduling algorithm.
#include<conio.h>
#include<stdio.h>
struct di
{
intnum;
int flag;
};
intmain()
{
inti,j,sum=0,n,min,loc,x,y;
struct di d[20];
int disk;
intar[20],a[20];
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i].num);
d[i]. flag=0;
}
15
for(i=0;i<n;i++)
{ x=0; min=0;loc=0;
for(j=0;j<n;j++)
{
if(d[j].flag==0)
{
if(x==0)
{
ar[j]=disk-d[j].num;
if(ar[j]<0){ar[j]=d[j].num-disk;
}
min=ar[j];loc=j;x++;
}
else
{
ar[j]=disk-d[j].num;
if(ar[j]<0)
{
ar[j]=d[j].num-disk;
}
}
if(min>ar[j])
{
min=ar[j]; loc=j;
}
}
}
d[loc].flag=1;
a[i]=d[loc].num-disk;
if(a[i]<0)
{
a[i]=disk-d[loc].num;
}
disk=d[loc].num;
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("\nmovement of total cylinders %d",sum);
return 0;
}
16
Output
17
Output
18
{
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(0);
}
Output
19