Write a C/C++ program that performs the tasks described below.
If there is just 1 command-line argument and it is
-hw
you should simply print
hello world
and then exit(0).
Otherwise, the program should provide a function named
mm_alloc.
This is the function prototype:
void *mm_alloc(int num_bytes_to_allocate)
mm_alloc function should obtain space for the user from a block
which you
obtain via mmap. You should obtain the block on the first
invocation
and the block size should be some number of pages. The number of
pages in
the block should be obtained from the environment variable
named:
P2_NUM_PAGES
I will write a set of test programs which invoke your function.
The program must align requests on 16-byte addresses.
If the user attempts to allocate more space than is available,
then
return NULL.
BUILDING AND TESTING:
Your program MUST NOT include a main procedure; that will be
supplied to
you in a source program named p2main.c
Your makefile should compile both your code and the source
program named
p2main.c linking them into into a single executable named p2
p2main.c:
#include
#include
#include
void *mm_alloc(int num_bytes_to_allocate);
int main(int argc, char *argv[])
{
if (argc > 1 && strcmp(argv[1],"-hw") == 0)
{
printf("hello world\n");
exit(0);
}
printf("DONE\n");
return 0;
}
CodeToCopy:
p3.cpp
#include <iostream> // for cout
#include <cstring> // for strcmp()
#include <cstdlib> // for exit()
#include <cmath> // for sqrt()
#include <csignal> // for signal()
#include <unistd.h> // for alarm()
using namespace std;
// This structure is used to pass arguments to thread function
struct range {
long low_value; // carries inclusive low value
long high_value; // carries inclusive high value
int count; // count of numbers in the range which match criteria
};
// Thread function computes the count of numbers in the range
// whose square root value decimal part starts with 7
void * compute(void *arg) {
// type-casting to structure
struct range * obj = (struct range *)arg;
// to store square root value
float sqrt_val;
// to store decimal part
float dec_part;
// to store first digit in the decimal part
int first_digit;
// using for loop to iterate over the range
for (long i = obj->low_value; i <= obj->high_value; ++i) {
// get square root value
sqrt_val = sqrt(i);
// get decimal part
dec_part = sqrt_val - (int)sqrt_val;
// get first digit in decimal part
first_digit = (int)(dec_part * 10 );
// check first digit is 7 or not
if (first_digit == 7) {
// if it is 7, increment the count
obj->count++;
}
}
// return the count
pthread_exit(arg);
}
// handling alarm
void terminate(int signal) {
cout << "Alarm: program terminated" << endl;
exit(0);
}
int main(int argc, char **argv) {
// setting program to terminate after 90 seconds
signal(SIGALRM, terminate);
alarm(90);
// check command line arguments
if (argc == 1) {
cout << "Usage: " << argv[0] << " <low-value> <high-value> <number-of-threads>" << endl;
exit(0);
}
// check for single command line arguments -hw
if (argc == 2) {
if (strcmp(argv[1], "-hw") == 0) {
cout << "hello world" << endl;
exit(0);
}
cout << "Usage: " << argv[0] << " <low-value> <high-value> <number-of-threads>" << endl;
exit(0);
}
// checking for 3 command line arguments
if (argc == 4) {
//command line arguments
long low_value = atol(argv[1]);
long high_value = atol(argv[2]);
int num_threads = atoi(argv[3]);
// creating thread objects
pthread_t threads[num_threads];
// create range-structure objects
struct range ranges[num_threads];
// calculate total count of numbers in the range
long total_numbers = high_value - low_value + 1;
// calculate parts based on the number of threads
long parts = total_numbers/num_threads;
// determine low value for each thread
long l = low_value;
// determine high value for each thread
long h = l + parts - 1;
// creating threads with their low and high values
for (int i = 0; i < num_threads; ++i) {
// assinging values thread arguments
ranges[i].low_value = l;
ranges[i].high_value = h;
ranges[i].count = 0;
// update low value for next thread
l = h+1;
// update high value for next thread
h = l + parts - 1;
// check if total count is odd and update high value accordingly
if ((i+2) == num_threads && total_numbers%2 == 1) {
h = l + parts;
}
// creating thread
pthread_create(&threads[i], NULL, compute, (void *)(&ranges[i]));
}
struct range *val = NULL;
long count=0;
// fetching count values from each thread and updating count
for (int i = 0; i < num_threads; ++i) {
pthread_join(threads[i], (void **)&val);
count += val->count;
}
// finally, printing count of numbers in the range which match
// the criteria
cout << "Count: " << count << endl;
}
return 0;
}
OutputScreenshot:
Get Answers For Free
Most questions answered within 1 hours.