bashshellbest-fit-curvebest-fit

Is there a way to solve the bestfit allocation problem in bash?


I have some problems with the application bestfit allocation dynamic partition in Bash and i want to know how do i create the bestfit algorithm that can work with blocks and processes and allocate by entering if a block fits?

I tried to launch it but i get some errors like command not found although i assigned my values.

I show you my whole program.

The errors

Thank you in advance

#!/bin/bash                                          

# 1) Indroduction.  Best-Fit program with bash.

echo "==================================================================================================="  

echo "======= Welcome by Best-Fit program allocation. ======="  

# ================================================================================================================

# 1.1) Declaration

declare -i numblocks               

declare -i numprocess         

declare -a blocksize=100     

declare -a processsize=100  

declare -i i                   

declare -i j                   

declare -a alloc=100            

declare -a avail=100            

declare -i min               

# ================================================================================================================  

# 2.2) Input Number of blocks.

echo -e "\nPlease enter the number of Blocks:"

# 2.2.1) Variable numblocks

read numblocks

# ================================================================================================================

# 1. For-Loop.

for ((i=1; i<=$numblocks; i++));                                         

    do
   
        echo -e "\nPlease enter the $i. size of the block:"        
    
        read -a blocksize                                              
        
        echo -e "\nThe $i. blocksize is: ${blocksize[*]}"      

    done
    
# ================================================================================================================

# 2.2) Input Number of processes.

echo -e "\nPlease enter the number of processes "

# 2.2.1) Variable numprocess

read numprocess
    
# ================================================================================================================                      

# 2. For-Loop.

for ((i=1 ; i<=$numprocess; i++));                                       

    do

        echo -e "\nPlease enter the $i. size of the process:"     
    
        read -a processsize                                           
        
        echo -e "\nThe $i. processsize is: ${processsize[*]}"   

    done
    
# ================================================================================================================

# Initialize alloc vector to -1 and avail to 9999.

  for((i=0; i<$numprocess; i++));                   
  
    do
  
        alloc[i]=-1                                                  

    done


  for((i=0; i<$numblocks; i++));                   
  
    do
  
        avail[i]=9999                                                

    done

# ================================================================================================================

# Check for each process if a block is available.

  for((i=0; i<$numprocess; i++));
  
  do
  
        for((j=0; j<$numblocks; j++));
        
        do
        
            if [ ${blocksize[j]} -gt ${processsize[i]} ];    # Problems. !!!!!!!!   -gt means --> > (upper like)
        
                then
            
                avail[j]= ${blocksize[j]} - ${processsize[i]}

            fi
        
        done

done

# ================================================================================================================

    min=0
    
    for ((j=0; j<$numblocks; j++));
    
    do
    
        if [ ${avail[min]} -gt ${avail[j]} ];
    
            then
        
            min=$j 
        
        fi
        
    done

# ================================================================================================================

        
        alloc[i]= $min

        if [ ${avail[$min]} -ge 9999 ];
        
            then
        
            alloc[i]=-1
        
        fi
        
# ================================================================================================================

    blocksize[min]=-1
    

    # Initialize avail to 9999.

    for ((j=0; j<$numprocess; j++));
    
    do
    
        avail[j]=9999

    done
    
# ================================================================================================================

# Print the Output.
    
    echo -e "\n================================ Results ================================"
        
    for ((i=1; i<$numprocess; i++));
    
    do
    
        if [ ${alloc[i]} -ne -1 ];
    
            then
        
                echo "Process $i of ${processsize[*]} --> Block . ${alloc[*]+}"
        
            else
        
                echo "Process $i of ${processsize[*]} --> is not allocated"
 
 
        fi
 
 done



Solution

  • This Shellcheck-clean code is a Bash implementation for the example in Program for Best Fit algorithm in Memory Management - GeeksforGeeks:

    #! /bin/bash -p
    
    read -r -p 'Enter line of block sizes: ' -a block_sizes
    read -r -p 'Enter line of process sizes: ' -a process_sizes
    
    process_block_indexes=()
    for pidx in "${!process_sizes[@]}"; do
        psize=${process_sizes[pidx]}
        best_block_idx='' best_block_size=''
        for bidx in "${!block_sizes[@]}"; do
            bsize=${block_sizes[bidx]}
            (( psize > bsize )) && continue
            if [[ -z $best_block_idx ]] || (( bsize < best_block_size )); then
                best_block_idx=$bidx
                best_block_size=$bsize
            fi
        done
    
        [[ -z $best_block_idx ]] && continue
        process_block_indexes[pidx]=$best_block_idx
        block_sizes[best_block_idx]=$(( best_block_size - psize ))
    done
    
    echo 'Process No.    Process Size        Block no.'
    for pidx in "${!process_sizes[@]}"; do
        bidx=${process_block_indexes[pidx]-}
        [[ -n $bidx ]] && bnum=$(( bidx+1 )) || bnum='Not Allocated'
        printf '%11d    %12d    %13s\n'  \
            "$(( pidx+1 ))" "${process_sizes[pidx]}" "$bnum"
    done
    

    Given the block list

    100 500 200 300 600
    

    and the process list

    212 417 112 426 170 50 100 100
    

    it produces the output

    Process No.    Process Size        Block no.
              1             212                4
              2             417                2
              3             112                3
              4             426                5
              5             170                5
              6              50                2
              7             100                1
              8             100    Not Allocated