-3

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


  • where did you get this script from and have you contacted the author of said script for help? the error messages indicate line numbers that are greater than the number of lines of code you've posted (ie, this does not appear to be all of the code); I'd recommend you cut-n-paste the entire script (along with correct shebang) into [shellcheck.net](https://www.shellcheck.net/) and make any suggested code changes – markp-fuso May 31 '22 at 18:18
  • Please take a look at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). – Cyrus May 31 '22 at 18:40
  • markp-fuso Yes you already recommended it and you helped me. Now i have edited my code. Can you have a look at this please? I would be very grateful if you give me some suggestions for improvement. – codecreater_512Z May 31 '22 at 19:47
  • A question for you: what do you think `declare -a blocksize=100` does? What is the "100" for? – glenn jackman May 31 '22 at 19:54
  • I thought that are the possible number of block size from a user. How would you program such a best fit algorithm with inputs of number and size of blocks and processes? – codecreater_512Z May 31 '22 at 20:03
  • See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) (MCVE). – pjh May 31 '22 at 21:06
  • See the "Before asking about problematic code" and "How to turn a bad script into a good question" sections of the ['bash' tag wiki - Stack Overflow](https://stackoverflow.com/tags/bash/info). – pjh May 31 '22 at 21:07
  • [Shellcheck](https://www.shellcheck.net/) reports many issues with the code. There seems to be at least one [Shellcheck](https://www.shellcheck.net/) issue associated with all of the reported errors. Fixing the [Shellcheck](https://www.shellcheck.net/) issues would be a good way to make progress on fixing the code. – pjh May 31 '22 at 21:09
  • pjh Thanks for your advices. I always looked on Shellcheck and it did not help really much. – codecreater_512Z May 31 '22 at 21:10
  • @codecreater_512Z, [Shellcheck](https://www.shellcheck.net/) only helps if you fix the issues that it reports. The [Shellcheck](https://www.shellcheck.net/) report includes links to explanations of the issues and how to fix them. Most of the issues reported for your code are trivial to fix. A few are more difficult, but they highlight some of the more significant problems with the code. Fixing the [Shellcheck](https://www.shellcheck.net/) issues would be a good way to make progress on fixing the code. – pjh May 31 '22 at 21:29
  • 1
    _i get some errors like command not found_ is not a very helpful problem description. The error message will tell you the line number, so you could point out exactly which statement goes wrong. Why do you keep this information for your self and expect us to guess the error? – user1934428 Jun 01 '22 at 06:29
  • Regarding the error messages, see [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551). – pjh Jun 02 '22 at 12:42

2 Answers2

0
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

This does not assign values to individual array elements. In each loop iteration, you're overwriting the entire array.

Demo:

for i in 1 2; do
  printf '%d: ' $i
  read -a blocksize
  declare -p i blocksize
done

I enter "10" for i=1 and "20" for i=2:

1: 10
declare -- i="1"
declare -a blocksize=([0]="10")
2: 20
declare -- i="2"
declare -a blocksize=([0]="20")

Inside the loop, you need to

read -r "blocksize[$i]"      # those quotes are necessary
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

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
pjh
  • 6,388
  • 2
  • 16
  • 17
  • pjh Thank you very much for your help. The program works fine. :) – codecreater_512Z Jun 03 '22 at 16:12
  • pjh. I have a question what does the code snippet bidx=${process_block_indexes[pidx]-} and (( psize > bsize )) && continue mean? – codecreater_512Z Jul 06 '22 at 12:16
  • @codecreater_512Z, the [Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html) documents all of the Bash features used. In particular, see [Arrays](https://www.gnu.org/software/bash/manual/bash.html#Arrays), [Shell Parameter Expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion) (for `${var-}`), [Conditional Constructs](https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs) (for `(( ... ))`), and [continue](https://www.gnu.org/software/bash/manual/bash.html#index-continue). – pjh Jul 08 '22 at 18:45