Question

In this exercise we “reverse-engineer” some code to try to determine how the heap in a C program is managed. Consider the following C program (compiled as an executable called memory):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
   int chunk_sizes[4];
   

   if ((argc != 2) || 
       (sscanf(argv[1], "%d,%d,%d,%d", &chunk_sizes[0], &chunk_sizes[1], &chunk_sizes[2], &chunk_sizes[3]) != 4) ||
       (chunk_sizes[0] < 0) || (chunk_sizes[1] < 0) || (chunk_sizes[2] < 0) || (chunk_sizes[3] < 0) ) {
      fprintf(stderr,"Usage: %s a,b,c,d\n", argv[0]);
      fprintf(stderr,"       where a, b, c, and d above must be >=0 integers (chunk sizes)\n");
      exit(1);
   }

   char *ptrs[9];
   unsigned long sizes[9] = {1024, 128, 1024, 128, 1024, 32, 1024, 128, 1024}; 


   for (int i=0; i < 9; i++) {
      ptrs[i] = (char *)calloc(sizes[i], sizeof(char));
      printf("%ld | ", (unsigned long)(ptrs[i]));
   }
   printf("\n--------------\n");

   free(ptrs[0]); free(ptrs[1]); free(ptrs[3]); free(ptrs[5]); free(ptrs[7]);

   for (int i=0; i < 4; i++) {
          if (chunk_sizes[i] > 0) { // If the user specified a 0-size chunk, do nothing
                char *chunk = (char *)calloc(chunk_sizes[i], sizeof(char));
                printf("The %d-byte chunk was allocated at address %ld\n", chunk_sizes[i], (unsigned long)(chunk));
          }
   }
   exit(0);
}

If invoking the program as /memory1100,100,199,20 produces this output: 0 1024 1152| 2176| 2304| 3328 3360 | 4384| 4512 The 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:-

(b) Best fit.

  • The best-fit deals with allocating the smallest free partition of memory which meets the requirement of the requesting process.
  • This algorithm searches for the smallest hole that is enough from the entire list of free partitions.

As per the question

At first, we need to allocate 1100 bytes of memory for which we choose 1024 and 128 consecutive blocks because no single block of ample space is available.

After that, we need to allocate 100-bytes memory for that we have chosen the best fitted 128-byte block, allocated at 4412. and similarly, following the best-fit approach, we also allocated remaining ones.

P.S. - If you find my answer useful, please take a second to give it a THUMBS UP.

Add a comment
Know the answer?
Add Answer to:
In this exercise we “reverse-engineer” some code to try to determine how the heap in a C program ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT