Question

Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...

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;
}

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in C that takes a file name as the argument on the command...
Write a program in C that takes a file name as the argument on the command line, and, if the file is a symbolic link, prints out the contents of that link (i.e. the file name that the link points to). If it is not a symbolic link, the program should print an error int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR :...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to be output immediately. (For space reasons, we are not checking error return codes, so assume that all functions return normally.) int main () {    if (fork() == 0) {        if (fork() == 0) {           printf("3"); fflush(stdout);        }        else {           pid_t pid; int status;           if ((pid = wait(&status)) > 0) {                  printf("4"); fflush(stdout);           }        }...
In C language: Partially finished program is given below. The second and third parameters of the...
In C language: Partially finished program is given below. The second and third parameters of the count() function need to be pointer parameters. However, the programmer forgot to write the necessary ampersands and asterisks in the prototype, call, function header, and function code. Add these characters where needed so that changes made to the parameters adults and teens actually change the underlying variables in the main program. #include #define MAX 10 void count( int ages[], int adults, int teens );...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT