Please add in my c++ ( Highlight the added coding for guidance) 1) Total memory
ID: 3842224 • Letter: P
Question
Please add in my c++ ( Highlight the added coding for guidance)
1) Total memory size available
2) show in step by step how much memory will be allocated and remained for each request and release in a Buddy System
#include <iostream>
#include <cstdlib>
using namespace std;
class sched //scheduling class
{
public: //datamembers
int Nprocesses, BurstT[10], ArrivalT[10], TurnAT[10], WaitingT[10], RemainingT[10], finish[10], TotalWaitingT, TotalTurnAT, TotalBurstT;
void readData();
void SPN();
void computeSRT();
void Init();
void dispTime();
int getNextProcess(int);
void computeRR();
};
void sched::readData() //function of readdata
{
cout<<"Enter no. of processes: ";
cin>>Nprocesses;
cout<<"Enter the burst times in order: ";
for (int i = 0; i < Nprocesses; i++)
cin>>BurstT[i];
cout<<"Enter the arrival times in order: ";
for (int i = 0; i < Nprocesses; i++)
cin >> ArrivalT[i];
}
void sched::Init() //function of init
{
TotalBurstT = 0;
TotalWaitingT = 0;
TotalTurnAT = 0;
for (int i = 0; i < Nprocesses; i++)
{
RemainingT[i] = BurstT[i];
finish[i] = 0;
WaitingT[i] = 0;
TurnAT[i] = 0;
TotalBurstT += BurstT[i];
}
}
void sched::SPN() //function of shortest process next
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
float wavg=0,tavg=0,tsum=0,wsum=0;
cout<<"********** Shortest Process Next (SPN) ************ ";
cout<<" Enter the No. of processes : ";
cin>>n;
cout<<" ";
for(i=0;i<n;i++)
{
cout<<" Process ["<<i+1<<"]";
cout<<" Arrival time : ";
cin>>at[i];
cout<<" Service time : ";
cin>>bt[i];
cout<<" ";
}
/*Sorting According to Arrival Time*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}
/*Arranging the table according to Burst time,
Execution time and Arrival Time
Arrival time <= Execution time
*/
for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
}
k++;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}
wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}
tavg=(tsum/n);
cout<<" ****************************************************************";
cout<<" Process Burst Arrival Turn-around";
for(i=0;i<n;i++)
{
cout<<" "<<""<<p[i]<<" "<<bt[i]<<" "<<at[i]<<" "<<tt[i]<<" ";
// cout<<" p%d %d %d %d %d"<<p[i]<<bt[i]<<at[i]<<tt[i];
}
cout<<" Average Turnaround Time : "<<tavg<<" ";
cout<<"**************************************************************** ";
system("PAUSE");
}
void sched::computeSRT() //shortest remaining time function
{
readData();
Init();
int time,next=0,old,i;
cout<<"Gantt Chart ";
for(time=0;time< TotalBurstT;time++)
{
old=next;
next=getNextProcess(time);
if(old!=next || time==0) cout<<"("<<time<<")|==P"<<next+1<<"==|";
RemainingT[next]=RemainingT[next]-1;
if( RemainingT[next]==0) finish[next]=1;
for(i=0;i<Nprocesses;i++)
if(i!=next && finish[i]==0 && ArrivalT[i]<=time)
WaitingT[i]++;
}
cout<<"("<< TotalBurstT <<")"<<endl;
for(i=0;i<Nprocesses;i++)
if(!finish[i]) {cout<<"Scheduling failed, cannot continue "; return;}
dispTime();
}
void sched::dispTime()
{
for (int i = 0; i<Nprocesses; i++)
{
TotalWaitingT += WaitingT[i];
TurnAT[i] = WaitingT[i] + BurstT[i];
TotalTurnAT += TurnAT[i];
cout << "Waiting time for P" << (i + 1) << " = " << WaitingT[i] << ", Turnaround time = " << TurnAT[i] << endl;
}
cout << "Avg Waiting time = " << (double)TotalWaitingT / Nprocesses << " and Avg Turnaround time = " << (double)TotalTurnAT / Nprocesses << endl;
cout << "Scheduling complete ";
}
int sched::getNextProcess(int time) {
int i, low;
for (i = 0; i<Nprocesses; i++)
if (finish[i] == 0) { low = i; break; }
for (i = 0; i<Nprocesses; i++)
if (finish[i] != 1)
if (RemainingT[i]<RemainingT[low] && ArrivalT[i] <= time)
low = i;
return low;
}
void sched::computeRR() //function of roundrobin
{
readData();
Init();
int time;
int finishprocessinqueue[64];
for (int j = 0; j < Nprocesses; j++)
{
finishprocessinqueue[j] = TotalBurstT;
}
int m = -1, q, i, k = -1, decrease = 0;
int queue[64] = { 0 };
cout << "Enter the time quantum: ";
cin >> q;
cout << "Gantt Chart ";
for (time = 0; time < TotalBurstT;)
{
//cout << "time is " << time << endl;
for (i = Nprocesses - 1; i >= 0; i--)
{
if (ArrivalT[i] <= time && finish[i] == 0 && BurstT[i] == RemainingT[i])
{
//cout << "Process " << i + 1 << " arrive is arrive " << endl;
//cout << "This process have " << RemainningT[i] << " Remainning Time " << endl;
if (RemainingT[i] < q)
{
//cout << "this process remainning time is less than quantum" << endl;
decrease = RemainingT[i];
}
else
{
//cout << "this process remainning time is equal or more than quantum" << endl;
decrease = q;
}
//cout << "now place into queue" << endl;
for (int l = 1; l <= decrease; l++)
{
k++;
queue[k] = i + 1;
//cout << "place " << i + 1 << " into queue " << k << endl;
}
RemainingT[i] = RemainingT[i] - decrease;
if (RemainingT[i] == 0)
{
//cout << "this processes is finish" << endl;
finish[i] = 1;
finishprocessinqueue[i] = k;
}
}
else if (ArrivalT[i] <= time && finish[i] == 0 && BurstT[i] != RemainingT[i])
{
bool foundinqueue = false;
for (int n = m + 1; n <= k; n++)
{
if (queue[n] == i + 1)
{
foundinqueue = true;
break;
}
}
if (!foundinqueue)
{
//cout << "This process have " << RemainningT[i] << " Remainning Time " << endl;
if (RemainingT[i] < q)
{
//cout << "this process remainning time is less than quantum" << endl;
decrease = RemainingT[i];
}
else
{
//cout << "this process remainning time is equal or more than quantum" << endl;
decrease = q;
}
//cout << "now place into queue" << endl;
for (int l = 1; l <= decrease; l++)
{
k++;
queue[k] = i + 1;
//cout << "place " << i + 1 << " into queue " << k << endl;
}
RemainingT[i] = RemainingT[i] - decrease;
if (RemainingT[i] == 0)
{
//cout << "this processes is finish" << endl;
finish[i] = 1;
finishprocessinqueue[i] = k;
}
}
}
}
m++;
for (int j = 0; j <= Nprocesses - 1; j++)
{
if (queue[m] != j + 1 && m < finishprocessinqueue[j] && ArrivalT[j] <= time)
{
WaitingT[j]++;
}
}
if (queue[m - 1] != queue[m])
cout << "(" << time << ")|==P" << queue[m] << "==|";
time++;
}
cout << "(" << TotalBurstT << ")" << endl;
dispTime();
}
int main()
{
sched s;
int ch = 0;
while (ch != 3)
{
cout << "1.Shortest Remaining time 2.Round Robin 3.Exit Enter choice: ";
cin >> ch;
switch (ch)
{
case 1: s.computeSRT(); break;
case 2: s.computeRR(); break;
case 3: break;
default: cout << "Invalid Option ";
}
}
return 0;
}
Explanation / Answer
namespace MemoryManager
{
const int MM_POOL_SIZE = 65536;
const int MM_POOL_NO = 10;
char MM_pool[MM_POOL_NO][MM_POOL_SIZE];
struct info
{
bool freememory;
unsigned int chunkofSize;
info(size_t size):
freememory(true),chunkofSize(size) {}
};
void initializeMemoryManager(void)
{
for( int i(0); i < MM_POOL_NO; ++i )
{
memset(&MM_pool[i][0],0,MM_POOL_SIZE);
info first(MM_POOL_SIZE-sizeof(info));
memcpy(&MM_pool[i][0],&first,sizeof(info));
}
}
void* allocate(unsigned int anSize, bool AllowUnfragmentation )
{
for( int j(0); j < MM_POOL_NO; ++j )
{
for(int i=0;i<MM_POOL_SIZE;
{
info* iter = (info*)&MM_pool[j][i];
if(iter->freememory&&iter->chunkofSize>=anSize)
{
iter->freememory=false;
if(iter->chunkofSize>anSize+sizeof(info))
{
info nextHead(iter->chunkofSize-anSize-sizeof(info));
memcpy(&MM_pool[j][i+anSize+sizeof(info)],&nextHead,sizeof(info));
iter->chunkofSize=anSize;
}
return &MM_pool[j][i+sizeof(info)];
}
else
{
i+=iter->chunkofSize+sizeof(info);
}
}
}
if( AllowUnfragmentation )
{
unfrag();
return allocate( anSize, false );
}
onOutOfMemory( anSize );
}
void deallocate(void* aPointer)
{
info* start = (info*)((char*)aPointer-sizeof(info));
start->freememory=true;
info* next = (info*)((char*)aPointer+sizeof(info)+start->chunkofSize);
}
void unfrag()
{
for( int j(0); j < MM_POOL_NO; ++j )
{
int i=0;
while(i<MM_POOL_SIZE)
{
info* iter = (info*)&MM_pool[j][i];
info* next = (info*)&MM_pool[j][i+iter->chunkofSize+sizeof(info)];
if(iter->freememory&&next->free)
{
iter->chunkofSize+=next->chunkofSize+sizeof(info);
memset(next,0,sizeof(info));
}
i+=iter->chunkofSize+sizeof(info);
}
}
}
int freeRemaining(void)
{
unfrag();
int tot=0;
for( int j(0); j < MM_POOL_NO; ++j )
{
for(int i=0;i<MM_POOL_SIZE;)
{
info* iter = (info*)&MM_pool[j][i];
if(iter->freememory)
{
tot+=iter->chunkofSize;
}
i+=iter->chunkofSize+sizeof(info);
}
}
return tot;
}
int largestFree(void)
{
unfrag();
int large=0;
for( int j(0); j < MM_POOL_NO; ++j )
{
for(int i=0;i<MM_POOL_SIZE;)
{
info* iter = (info*)&MM_pool[j][i];
if(iter->free&&iter->chunkofSize>large)
{
large = iter->chunkofSize;
}
i+=iter->chunkofSize+sizeof(info);
}
}
return large;
}
int smallestFree(void)
{
unfrag();
int small = MM_POOL_SIZE;
for( int j(0); j < MM_POOL_NO; ++j )
{
for(int i=0;i<MM_POOL_SIZE;)
{
info* iter = (info*)&MM_pool[j][i];
if(iter->free&&iter->chunkofSize<small)
{
small = iter->chunkofSize;
}
i+=iter->chunkofSize+sizeof(info);
}
}
return small;
}
void onOutOfMemory( unsigned int Size )
{
std::cout<< "The Allocation:" << Size << " The Largest:"<< MemoryManager::largestFree() << " The Smallest:" << MemoryManager::smallestFree() << "Remaining: " << MemoryManager::freeRemaining() << std::endl;
std::cerr << "The Memory pool out of the memory" << std::endl;
system("PAUSE");
exit( 1 );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.