Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h> //memset
  5.  
  6.  
  7. #define MEM_SIZE 512
  8.  
  9. void init_arr(int* arr, int size, int seed, int max_chunk);
  10. void print_arr(int* arr, int size);
  11. double malloc_percentage(int* arr, int size);
  12. int get_best_fit(int* arr, int size, int chunk_size);
  13. int get_first_fit(int* arr, int size, int chunk_size);
  14. int get_worst_fit(int* arr, int size, int chunk_size);
  15. int get_next_fit(int* arr, int size, int chunk_size);
  16.  
  17. int main(){
  18.     const int mem_size=MEM_SIZE;
  19.     int my_mem[mem_size];
  20.     memset(my_mem,0,mem_size*sizeof(int));
  21.     int memory_seed=25;
  22.     int max_chunk=32;
  23.     int read_size;
  24.     int best_fit, first_fit, worst_fit, next_fit;
  25.    
  26.     srand(time(NULL));
  27.     init_arr(my_mem, mem_size, memory_seed, max_chunk);
  28.     print_arr(my_mem, mem_size);
  29.     printf("Malloc percentage: %f\n",malloc_percentage(my_mem, mem_size));
  30.     printf("Enter malloc size: ");
  31.     scanf("%d",&read_size);
  32.     best_fit=get_best_fit(my_mem, mem_size, read_size);
  33.     first_fit=get_first_fit(my_mem, mem_size, read_size);
  34.     worst_fit=get_worst_fit(my_mem, mem_size, read_size);
  35.     next_fit=get_next_fit(my_mem, mem_size, read_size);
  36.     printf("Best Fit: %d\n",best_fit);
  37.     printf("First Fit: %d\n",first_fit);
  38.     printf("Worst Fit: %d\n",worst_fit);
  39.     printf("Next Fit: %d\n",next_fit);
  40.    
  41.  
  42.    
  43.  
  44.  
  45.  
  46.  
  47.    
  48.  
  49.     return EXIT_SUCCESS;
  50. }
  51.  
  52. void init_arr(int* arr, int size, int seed, int max_chunk){
  53.     int random_seed;
  54.     int random_size;
  55.     for (int x=0; x<size; x++){
  56.         random_seed=rand()%seed;
  57.         if (random_seed==0){
  58.             random_size=rand()%max_chunk+1;
  59.             for (int y=0; y<random_size && x<size;y++){
  60.                 arr[x]=1;
  61.                 x++;
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. void print_arr(int* arr, int size){
  68.     for (int x=0; x < size; x++){
  69.         printf("%d   ",arr[x]);
  70.         if ((x+1)%10==0) printf("\n");
  71.     }
  72.     printf("\n");
  73. }
  74.  
  75. double malloc_percentage(int* arr, int size){
  76.     int count=0;
  77.     for (int x=0; x < size; x++){
  78.         if (arr[x]==1) count++;
  79.     }
  80.     return (double)count/size;
  81. }
  82.  
  83. int get_best_fit(int* arr, int size, int chunk_size){
  84.     int current_fit_index=-1;
  85.     int temp_fit_index;
  86.     int current_fit_size=size+1;
  87.     int fragment_size=0;
  88.     for (int x=0; x<size; x++){
  89.         if(arr[x]!=1){
  90.             temp_fit_index=x;
  91.             while(x<size && arr[x]==0){
  92.                 fragment_size++;
  93.                 x++;
  94.             }
  95.             if (fragment_size>=chunk_size && fragment_size<current_fit_size){
  96.                 current_fit_index=temp_fit_index;
  97.                 current_fit_size=fragment_size;
  98.             }
  99.             fragment_size=0;
  100.         }
  101.     }
  102.     return current_fit_index;
  103. }
  104.  
  105. int get_first_fit(int* arr, int size, int chunk_size){
  106.     int current_fit_index=-1;
  107.     int temp_fit_index;
  108.     int fragment_size=0;
  109.     for (int x=0; x<size; x++){
  110.         if(arr[x]!=1){
  111.             temp_fit_index=x;
  112.             while(x<size && arr[x]==0){
  113.                 fragment_size++;
  114.                 x++;
  115.             }
  116.             if (fragment_size>=chunk_size ){
  117.                 current_fit_index=temp_fit_index;
  118.                 break;
  119.             }
  120.             fragment_size=0;
  121.         }
  122.     }
  123.     return current_fit_index;
  124. }
  125.  
  126. int get_worst_fit(int* arr, int size, int chunk_size){
  127.     int current_fit_index=-1;
  128.     int temp_fit_index;
  129.     int current_fit_size=0;
  130.     int fragment_size=0;
  131.     for (int x=0; x<size; x++){
  132.         if(arr[x]!=1){
  133.             temp_fit_index=x;
  134.             while(x<size && arr[x]==0){
  135.                 fragment_size++;
  136.                 x++;
  137.             }
  138.             if (fragment_size>=chunk_size && fragment_size>current_fit_size){
  139.                 current_fit_index=temp_fit_index;
  140.                 current_fit_size=fragment_size;
  141.             }
  142.             fragment_size=0;
  143.         }
  144.     }
  145.     return current_fit_index;
  146. }
  147.  
  148. int get_next_fit(int* arr, int size, int chunk_size){
  149.     int current_fit_index=-1;
  150.     int temp_fit_index;
  151.     int fragment_size=0;
  152.     int last_fit_index=0;
  153.     for (int x=0; x<size;x++){
  154.         if (arr[x]==1) last_fit_index=x;
  155.     }
  156.  
  157.     for (int x=last_fit_index; x<size; x++){
  158.         if(arr[x]!=1){
  159.             temp_fit_index=x;
  160.             while(x<size && arr[x]==0){
  161.                 fragment_size++;
  162.                 x++;
  163.             }
  164.             if (fragment_size>=chunk_size ){
  165.                 current_fit_index=temp_fit_index;
  166.                 break;
  167.             }
  168.             fragment_size=0;
  169.         }
  170.     }
  171.    
  172.     if (current_fit_index!=-1) return current_fit_index;
  173.  
  174.     for (int x=0; x<last_fit_index; x++){
  175.         if(arr[x]!=1){
  176.             temp_fit_index=x;
  177.             while(x<last_fit_index && arr[x]==0){
  178.                 fragment_size++;
  179.                 x++;
  180.             }
  181.             if (fragment_size>=chunk_size ){
  182.                 current_fit_index=temp_fit_index;
  183.                 break;
  184.             }
  185.             fragment_size=0;
  186.         }
  187.     }
  188.     return current_fit_index;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement