有没有办法解决bash中的bestfit分配问题?

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

我在 Bash 中的应用程序 bestfit 分配动态分区有一些问题,我想知道如何创建 bestfit 算法,该算法可以与块和进程一起使用,并在块适合时通过输入进行分配?

我尝试启动它,但我收到了一些错误,如命令未找到,尽管我分配了我的值。

我给你看我的整个程序。

The errors

提前致谢

#!/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


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

这不会为单个数组元素赋值。在每次循环迭代中,您将覆盖整个数组。

演示:

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

我为 i=1 输入“10”,为 i=2 输入“20”:

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

在循环内部,你需要

read -r "blocksize[$i]"      # those quotes are necessary

这个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

给定黑名单

100 500 200 300 600

和进程列表

212 417 112 426 170 50 100 100

它产生输出

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