1

I'm currently trying to simulate a simple Version of a Buddy algorithm. This code extraction should simulate the process of adding a process into a array List in which $speicher = the RAM which should be formatted. (The variables are german) $prozess = the process which is added into the RAM. When trying to run the script i get the error = to much recursion in line 19 =arraySpeicher+=($j/2). (again the error is in german so im not entirely sure if thats the correct error) $i should have the value of $prozess which needs to be formatted to fit the Buddy algorithm principal.

How do i fix the Problem of wanting to add the formatted values into my array?

#!/bin/bash
echo "Wie groß ist ihr Arbeitsspeicher?: " 
read speicher
#Einlesen des Arbeitsspeichers
echo "Wie groß ist ihr Prozess den sie dem Speicher hinzufügen wollen?: "
read prozess

if (( prozess < 0 )); then
   echo "Dein Prozess ist zu klein & hat keine relevante Größe." 
   exit
   fi

arraySpeicher=($speicher)
arrayName=()

for i in "${arraySpeicher[@]}"; do
    if (( $prozess <= $i )); then
        j=$i
        unset arraySpeicher[$i]
        while (( $prozess < $j/2 )); do
            arraySpeicher+=($j/2)
            ((j=j/2))
            done
        arraySpeicher+="p ${#ArrayName[@]}"
        arrayName+=$prozess

    fi
    done

echo "Dies ist ihr Speicher ${arraySpeicher[@]} nach hinzufügen des Prozesses."

while (( true )); do
    PS3="Möchten sie einen Prozess hinzufügen / entfernen / aufhören?"
    select eingabe in hinzfügen entfernen aufhören 
    do 

if (( $eingabe == hinzufügen )); then
    echo "wie groß ist der Prozess?:"
    read prozess

    if (( prozess < 0 )); then
        echo "Ihr Prozess ist zu klein." 
        exit
        fi

    for i in "${arraySpeicher[@]}"; do
        if (( $i == int )) #TODO: nur zahlen dürfen behandelt werden, prozesse nicht!!
        if (( $prozess <= $i )); then
            j=$i
            unset arraySpeicher[$i]
            while (( $prozess < $j/2 )); do
                arraySpeicher+=($j/2)
                ((j=j/2))
                done
            arraySpeicher+="p ${#ArrayName[@]}"
            arrayName+=$prozess

            break
        fi
        echo "Dies ist ihr Speicher ${arraySpeicher[@]} nach hinzufügen des Prozesses."
        fi
        done    


if (( $eingabe == entfernen )); then
echo "Welchen Prozess willst du entfernen:?"
read antwort
p=arrayName[$antwort]
z=1
while (( $z < $p )); do
z=((z*2))
done

#TODO: stelle muss die position von dem gewünschten Prozess("p"+$antwort) in dem arraySpeicher beinhalten!
arraySpeicher[$stelle]=z

h=1
while (( $h == 1 )); do
h=2
for i in "${arraySpeicher[@]}"; do
if (( i < "${#arraySpeicher[@]}"-1 )); then
if (( arraySpeicher[$i] == arraySpeicher[$i+1] )); then
unset arraySpeicher[$i+1]
arraySpeicher[$i]=((arrayspeicher[$i]*2))
h=1
fi
fi
done 
done

fi

if (( $eingabe == aufhören )); then
break
fi
done
done

edit: I just put in the whole code i have for now. still some work to do, still need to add comments and indent the code. hopefully this script gets done! thx for the help in advance!

1
  • Please provide the input so we can test the script. Commented Jun 20, 2016 at 15:31

1 Answer 1

1

The problem is that you don't assign a result at the following line

j=j/2

Change it to

((j=j/2))

When you assing the string j/2 to $j, using j/2 as an arithmetic expression (in the while condition) tries to expand $j, but it expands to j/2, so it tries to expand $j, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. it compiled. But now i have the Problem that my array has a list of values i dont want and they are even shown in a manner which i obviously dont want: echo "Dies ist ihr Speicher ${arraySpeicher[@]} nach hinzufügen des Prozesses." shows: Dies ist ihr Speicher 1024p 0 1024/2 512/2 nach hinzufügen des Prozesses. I want it to say the free space it has formatted into the proper form = 512 256 P1(Process 1)
Please provide a script we can run directly without inventing the input to simulate your problem.
speicher should be like 512,1024,2048 etc... prozess can be as high as wanted (noted that this isnt for use by a different user i just need to show how the script works so choosing a higher process than RAM isnt relevant here.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.