// I need this whole program complete please #include <cstdlib> #include <ctime>
ID: 1813238 • Letter: #
Question
// I need this whole program complete please
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <list>
#include <vector>
using namespace std;
// Allocates num_units units of memory to a process whose id is process_id.
// If successful, it returns the number of nodes traversed in the linked list.
// Otherwise, it returns -1.
// This memory allocator implements FIRST FIT.
list < Node *> memSim;
class Node{
public:
int startOfBlockAddress;
int blockSize;
bool used;
int processID;};
int allocate_mem(int process_id, int num_units)
{
// TODO: Implement
if(num_units <= 3 && num_units >= 10){
Node *temp = new Node;
temp->blockSize = num_units;
temp->processID = process_id;
temp->used = true;
memSim.push_front(temp); // getting error here
return 1;
}
return -1;
}
// Deallocates the memory allocated to the process whose id is process_id.
// It returns 1, if successful, otherwise -1.
int deallocate_mem(int process_id)
{
// TODO: Implement
return -1;
}
// returns the number of holes (fragments of sizes 1 or 2 units).
int fragment_count()
{
// TODO: Implement
return 0;
}
// BEGIN Simulation Code (Which is already complete and does not need
// modification)
// The number of allocations (the definitions below sum to 10000):
#define NUM_INIT_ALLOCS 20
#define NUM_REM_ALLOCS 9980
#define MIN_NUM_UNITS 3
#define MAX_NUM_UNITS 10
static int next_alloc_process_id = 0;
static int next_dealloc_process_id = 0;
// Statistics
static int total_fragments = 0;
static int total_traversals = 0;
static int num_alloc_successes = 0;
static int num_alloc_failures = 0;
static int num_dealloc_successes = 0;
static void print_stats()
{
double avg_fragments;
double avg_traversals;
double denied_percent;
int num_request_successes = num_alloc_successes + num_dealloc_successes;
int num_alloc_attempts = num_alloc_successes + num_alloc_failures;
if (num_request_successes != 0) {
avg_fragments = (double)total_fragments / (double)num_request_successes;
} else {
avg_fragments = 0.0;
}
if (num_alloc_successes != 0) {
avg_traversals = (double)total_traversals / (double)num_alloc_successes;
} else {
avg_traversals = 0.0;
}
if (num_alloc_attempts != 0) {
denied_percent = (double)num_alloc_failures * 100.0 /
(double)num_alloc_attempts;
} else {
denied_percent = 0.0;
}
cout.setf(ios::fixed);
cout.precision(2);
cout << "Main Memory Allocation" << endl
<< "----------------------" << endl
<< "average number of external fragments = " << avg_fragments << endl
<< "average allocation time = " << avg_traversals << endl
<< "percentage of allocation denials = " << denied_percent << endl;
}
void perform_allocate(void)
{
int rand_num_units = rand() % (MAX_NUM_UNITS - MIN_NUM_UNITS + 1) +
MIN_NUM_UNITS;
int traversals = allocate_mem(next_alloc_process_id, rand_num_units);
if (traversals != -1) {
total_traversals += traversals;
total_fragments += fragment_count();
num_alloc_successes++;
} else {
num_alloc_failures++;
}
next_alloc_process_id++;
}
void perform_deallocate(void)
{
int result = -1;
while ((result == -1) && (next_dealloc_process_id < next_alloc_process_id)) {
result = deallocate_mem(next_dealloc_process_id);
next_dealloc_process_id++;
}
if (result != -1) {
total_fragments += fragment_count();
num_dealloc_successes++;
}
}
int main(int argc, char* argv[])
{
int init_allocs_left = NUM_INIT_ALLOCS;
int requests_left = NUM_REM_ALLOCS;
bool request_deallocate = true;
// Seed random number generator.
srand(time(NULL));
// Perform initial allocations.
while (init_allocs_left != 0) {
perform_allocate();
init_allocs_left--;
}
// Alternate deallocate then allocate until all requests have been performed.
while (requests_left != 0) {
if (request_deallocate) {
perform_deallocate();
} else {
perform_allocate();
}
request_deallocate = !request_deallocate;
requests_left--;
}
print_stats();
return EXIT_SUCCESS;
}
Explanation / Answer
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <list>
#include <vector>
using namespace std;
// Allocates num_units units of memory to a process whose id is process_id.
// If successful, it returns the number of nodes traversed in the linked list.
// Otherwise, it returns -1.
// This memory allocator implements FIRST FIT.
class Node{
public:
int startOfBlockAddress;
int blockSize;
bool used;
int processID;};
list < Node *> memSim;
int allocate_mem(int process_id, int num_units)
{
// TODO: Implement
if(num_units <= 3 && num_units >= 10){
Node *temp = new Node;
temp->blockSize = num_units;
temp->processID = process_id;
temp->used = true;
memSim.push_front(temp); // getting error here
return 1;
}
return -1;
}
// Deallocates the memory allocated to the process whose id is process_id.
// It returns 1, if successful, otherwise -1.
int deallocate_mem(int process_id)
{
// TODO: Implement
return -1;
}
// returns the number of holes (fragments of sizes 1 or 2 units).
int fragment_count()
{
// TODO: Implement
return 0;
}
// BEGIN Simulation Code (Which is already complete and does not need
// modification)
// The number of allocations (the definitions below sum to 10000):
#define NUM_INIT_ALLOCS 20
#define NUM_REM_ALLOCS 9980
#define MIN_NUM_UNITS 3
#define MAX_NUM_UNITS 10
static int next_alloc_process_id = 0;
static int next_dealloc_process_id = 0;
// Statistics
static int total_fragments = 0;
static int total_traversals = 0;
static int num_alloc_successes = 0;
static int num_alloc_failures = 0;
static int num_dealloc_successes = 0;
static void print_stats()
{
double avg_fragments;
double avg_traversals;
double denied_percent;
int num_request_successes = num_alloc_successes + num_dealloc_successes;
int num_alloc_attempts = num_alloc_successes + num_alloc_failures;
if (num_request_successes != 0) {
avg_fragments = (double)total_fragments / (double)num_request_successes;
} else {
avg_fragments = 0.0;
}
if (num_alloc_successes != 0) {
avg_traversals = (double)total_traversals / (double)num_alloc_successes;
} else {
avg_traversals = 0.0;
}
if (num_alloc_attempts != 0) {
denied_percent = (double)num_alloc_failures * 100.0 /
(double)num_alloc_attempts;
} else {
denied_percent = 0.0;
}
cout.setf(ios::fixed);
cout.precision(2);
cout << "Main Memory Allocation" << endl
<< "----------------------" << endl
<< "average number of external fragments = " << avg_fragments << endl
<< "average allocation time = " << avg_traversals << endl
<< "percentage of allocation denials = " << denied_percent << endl;
}
void perform_allocate(void)
{
int rand_num_units = rand() % (MAX_NUM_UNITS - MIN_NUM_UNITS + 1) +
MIN_NUM_UNITS;
int traversals = allocate_mem(next_alloc_process_id, rand_num_units);
if (traversals != -1) {
total_traversals += traversals;
total_fragments += fragment_count();
num_alloc_successes++;
} else {
num_alloc_failures++;
}
next_alloc_process_id++;
}
void perform_deallocate(void)
{
int result = -1;
while ((result == -1) && (next_dealloc_process_id < next_alloc_process_id)) {
result = deallocate_mem(next_dealloc_process_id);
next_dealloc_process_id++;
}
if (result != -1) {
total_fragments += fragment_count();
num_dealloc_successes++;
}
}
int main(int argc, char* argv[])
{
int init_allocs_left = NUM_INIT_ALLOCS;
int requests_left = NUM_REM_ALLOCS;
bool request_deallocate = true;
// Seed random number generator.
srand(time(NULL));
// Perform initial allocations.
while (init_allocs_left != 0) {
perform_allocate();
init_allocs_left--;
}
// Alternate deallocate then allocate until all requests have been performed.
while (requests_left != 0) {
if (request_deallocate) {
perform_deallocate();
} else {
perform_allocate();
}
request_deallocate = !request_deallocate;
requests_left--;
}
print_stats();
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.