Professional Documents
Culture Documents
Parika 1220260 Os File
Parika 1220260 Os File
#include<stdio.h>
int main()
int P,bt[20],wt[20],tat[20],avg_wt=0,avg_tat=0,i,j;
scanf("%d",&P);
for(i=0;i<P;i++)
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
for(i=1;i<P;i++)
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
for(i=0;i<P;i++){
tat[i]=bt[i]+wt[i];
avg_wt+=wt[i];
avg_tat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
avg_wt/=i;
avg_tat/=i;
return 0;
OUTPUT:
#include<stdio.h>;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf(“Enter Total Number of Process:”);
scanf(“%d”,&n);
printf(“\nEnter Burst Time and Priority\n”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\n”,i+1);
printf(“Burst Time:”);
scanf(“%d”,&bt[i]);
printf(“Priority:”);
scanf(“%d”,&pr[i]);
p[i]=i+1;
}
printf(“\nEntered Processes with their Burst Times and Priorities are:\n”);
printf(“\nProcess\t\t Burst Time\t\t\t Priority”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\t\t\t %d\t\t\t \t%d”,p[i],bt[i], pr[i]”);
}
//sorting burst time, priority and process number in ascending order using selection
sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
printf(“\n\nAfter Sorting…”);
printf(“n\nProcess\t\t Burst Time\t\t\t Priority”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\t\t\t %d\t\t\t \t%d”,p[i],bt[i], pr[i]);
}
wt[0]=0; //waiting time for first process is zero
for(i=1;i<n;i++) //calculate waiting time
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf(“\n\nProcess\t\t Burst Time\t\t \tWaiting Time\t\t\tTurnaround Time”);
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf(“\nP[%d]\t\t\t %d\t\t\t %d\t\t\t \t%d”,p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf(“\n\nAverage Waiting Time=%d”,avg_wt);
printf(“\nAverage Turnaround Time=%d\n”,avg_tat);
return 0;
}
OUTPUT:
4. Implement a program for Round Robin CPU
Scheduling.
#include<iostream>;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] < 0)
{
done = false; // There is a pending process
if (rem_bt[i] < quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true) // If all processes are done
break;
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime(int processes[], int n, int bt[], int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
OUTPUT:
5. Implement a program for First Fit Memory
Allocation.
#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int allocation[n]; // Stores block id of the block allocated to a process
for (int i=0; i<n; i++)
{
allocation[i]=-1; // Initially no block is assigned to any process
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j; // allocate block j to p[i] process
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\t\t",i+1,processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main()
{
int blockSize[20],processSize[20],m,n,i;
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &m);
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &n);
printf("\nEnter the Size of the Blocks:\n");
for(i = 0; i < m; i++)
{
printf("Block No.[%d]:\t", i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter the Size of the Process:\n");
for(i = 0; i < n; i++)
{
printf("Process No.[%d]:\t", i + 1);
scanf("%d", &processSize[i]);
}
firstFit(blockSize, m, processSize, n);
return 0 ;
}
Output:
#include<stdio.h>
using namespace std;
void bestfit(int blockSize[], int m, int processSize[], int n);
{
int allocation[n];
for(int i=0; i<n; i++)
{
allocation[i]=-1;
}
for(int i=0; i<n; i++)
{
int bestIdx = -1;
for(int j=0; j<m; j++)
{
if(blockSize[j] >= processSize[i])
{
if(bestIdx == -1)
bestIdx = j;
else if(blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
if(bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\n Process No.\t Process Size\t Block No.\n ");
for(int i=0; i<n; i++)
{
printf("%d\t\t%d\t\t", i+1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated \n");
}
}
int main()
{
int blockSize[20], processSize[20], m,n,i;
printf("\n Enter Total No. of Blocks: \t");
scanf("%d", &m);
printf("\n Enter Total No. of Process: \t");
scanf("%d", &n);
printf("\n Enter Size of Blocks: \t");
for(i=0; i<m; i++)
{
printf("Block No.[%d]: \t",i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter Size of Process: \n");
for(i=0; i<n; i++)
{
printf("Process No.[%d]: \t",i + 1);
scanf("%d", &processSize[i]);
}
bestfit(blockSize, m, processSize, n);
return 0;
}
Output:
#include<stdio.h>
}
}
int main()
{
int blockSize[20],processSize[20],m,n,i;
clrscr();
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &m);
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &n);
printf("\nEnter the Size of the Blocks:\n");
for(i = 0; i < m; i++)
{
printf("Block No.[%d]:\t", i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter the Size of the Process:\n");
for(i = 0; i < n; i++)
{
printf("Process No.[%d]:\t", i + 1);
scanf("%d", &processSize[i]);
}
return 0 ;
}
OUTPUT:
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];
clrscr();
f1=open("data",O_RDWR | O_CREAT | O_TRUNC);
while((c=getchar())!='\n'){
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0);
printf("\n%s\n",strin);
close(f2);
getch();
return 0;
}
Output:
TotalMemBlock=(num*Max_Size)/Block_Size;
printf(“\nEnter process number and respective size(<%d)\n”,Max_Size);
for(ctr=0;ctr<num;ctr++)
{
scanf(“%d%d”,p[ctr],&m[ctr]);
if(m[ctr]>Max_Size)
{
printf(“Enter the valid size(<%d)”,Max_Size);
ctr--;
printf(“\nEnter process number and respective size(<%d) again\n”,Max_Size);
}
}
for(ctr=0;ctr<num;ctr++)
{
temp[ctr]=m[ctr];
}
for(ctr=0;ctr<num;ctr++)
{
UseBlock=0;
while(temp[ctr]>0)
{
temp[ctr]=temp[ctr]-Block_Size;
UseBlock++;
TotalUseBlock++;
}
int_frag=abs(temp[ctr]);
Total_int_frag+=int_frag;
printf(“\n\n Process p%d uses %d blocks and %d units internal fragmentation”,p[ctr],UseBlock,int_frag);
}
RemainBlock=TotalMemBlock-TotalUseBlock;
Total_ex_frag=RemainBlock*Block_Size;
printf(“\n\n\nResults are -------------------------\n\n”);
printf(“\n--------------------------------");
printf(“\nTotal memory blocks are %d”,TotalMemBlock);
printf(“\n---------------------------------");
printf(“\nTotal used blocks are %d”,TotalUseBlock);
printf(“\n-------------------------------------");
printf(“\nTotal unused blocks are %d”,RemainBlock);
printf(“\n----------------------------");
printf(“\nTotal internal fragmentation is %d units”,Total_int_frag);
printf(“\n------------------------------");
printf(“\nTotal external fragmentation is %d units”,Total_ex_frag);
getch();
}
Output:
10. Implement a program for Banker’s
Algorithm.
#include<iostream>
#include<conio.h>
int main()
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
f[k] = 0;
int need[5][3];
int y = 0;
if (f[i] == 0) {
int flag = 0;
flag = 1;
break;
if (flag == 0) {
ans[ind++] = i;
avail[y] += alloc[i][y];
f[i] = 1;
getch();
return (0);
Output:
11. Implement a program for First in First Out
page replacement.
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
clrscr();
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE REFERENCE STRING :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
{
if(frame[k]==a[i])
avail=1;
}
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
getch();
return 0;
}
OUTPUT:
12.
Implement a program for Least Recently Used
page replacement.
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
Output:
13.
Implement a program for Optimal page
replacement.
#include<stdio.h>
int main()
{
//variable declaration and initialization
int frames_number, pages_number, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, miss = 0,faults=0;
//code to input the frame number
frames[pos] = pages[i];
miss++;
}
printf("\n");
for(j = 0; j < frames_number; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page miss = %d\nPage Fault=%d", miss,miss+faults);
return 0;
}
Output:
14. Implement a program for Producer-Consumer
Problem.
#include <stdio.h>
#include <stdlib.h>
// Initialize a mutex to 1
int mutex = 1;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
// Switch Cases
switch (n) {
case 1:
case 2:
// Exit Condition
case 3:
exit(0);
break;
}
}
}
Output: