简体   繁体   English

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

[英]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?我对 Bash 中的应用程序最佳匹配分配动态分区有一些问题,我想知道如何创建可以与块和进程一起使用的最佳匹配算法,并通过输入块是否合适来进行分配?

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


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:我为 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")

Inside the loop, you need to在循环内部,您需要

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

This Shellcheck -clean code is a Bash implementation for the example in Program for Best Fit algorithm in Memory Management - GeeksforGeeks :Shellcheck -clean 代码是Program for Best Fit algorithm in Memory Management - GeeksforGeeks 中示例的 Bash 实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM