Php

Can Anyone help me make a PHP Banker’s Algorithm…im really got hard time making it can you please help me…
The need is you must input number of Columns and Rows…
the Out put must be Allocation, Max, Need, and Work

T_T

Welcome to the SP forums.

I don’t understand what you’re asking. Could you explain a bit more?


Banker’s Algorithm…
First you need to input number of Rows and Columns
the rows and columns must have its unique ID/name

While we wait for approval for the attachment, how about you show us what you’ve got so far?

We will help you get there, but we’re not here to do it for you.

And which part of the Banker’s Algorithm are you trying to demonstrate? A state’s safety?

it’s Deadlock Avoidance

So “Given Safe State A, and Request R, demonstrate the system’s correct response”.

Talk me through the logic of what the system should do.
(Yes, I know how to google, but I want you to spell it out, so you can take each step and put it into code)

1: You are going to input the Rows which is the Process
2: You are going to input the Columns which is the Reourse
3: You are going to Input the said Given Resource like if you enter 3 resource there must 3 reourse A= 9,B= 6,C= 4
4: If the Resource is 3 blocks/textarea there must be 3 textare in each Alocated , Max, Need, and Work/Availability
5: When it is displayed you are going to input value in the Allocated and Max Textfield
6: In the Need Textfield the Formula is that the Need is equal to Max minus(-) Allocated
7: In the Availability You need to find the first resource which you are going to add all the Resource A in the Allocated, Resource B in the Allocated, Resource C in the Allocated then subtracted to the Given Resource which is A is equal 9 subtracted to the total Resource A up to B
8: When you find the First Availability you are going to find the least value in the Need, if you find it you are going to add the first Availability to the value of the Allocated.

Okay, so, 1-6 describes your setup. Pretty simple stuff there.
You dont really seem to have a “If Process X requests Y resources of type Z, will the system accept or reject it” statement in there.
7-8 seem to be testing for a Safe State, but that doesnt demonstrate deadlock avoidance, it just tests the current state.

I’ve got a Code here but i can’t do the exact settings that must be display

<html>
<form name=“bank” method=‘post’ action=‘Start.php’>
Enter Resources: <input type=‘text’ name=‘resource’ size=‘3’><br>
Enter Process    : <input type=‘text’ name=‘process’ size=‘3’><br>
<input type=‘submit’ value=‘Submit’><br>
<?php
$x = $_POST[‘resource’];
$y =$_POST[‘process’];
//$count = 0;
for($i=0; $i<= $x; $i++)
{

echo “Instance $i = “.”<input type=‘text’ size=‘2’ name=‘r’>”." “;
}
if($y !=null){
echo”<br><b>Allocation</b><br>“;
echo”        ";
for ($ctr3=1;$ctr3<=$y;$ctr3++) {
echo "     R “.$ctr3.”     ";
}
for ($ctr=1;$ctr<=$y;$ctr++) {
echo "<br> P ".$ctr;
for ($ctr2=1;$ctr2<=$y;$ctr2++) {
//$array[$ctr1][$ctr2];
echo “<input type=‘text’ name=‘input’ size=‘5’>”;
//$count++;
//echo " R ".$ctr2;
}}
}

?>
</form>
</body>
</html>

The code is still lack the Max, Need and Availability
and I can figure out how to do it.

The easiest solution is going to be the following:

Display text boxes for #R, p, RequestR, and RequestP. (Number of Resources, Number of Processes, the test Request Resource, and the test Request Process. I’m assuming a request size of 1, but if you want to add that as a variable, make that a text box too)
Display text areas for ResourceMax Max and Allocated. The other two values are derived from these numbers (Need = Max - Allocated, Availability = ResourceMax - Sum(Allocated)) and should not be entered by the user.

Next is to take that form data, sanity check it (make sure the input is complete and valid), then run the Algorithm logic to determine whether the request should be granted or denied (and probably display WHY)

Yeah that’s it…

can you help me make the Syntax i got error on my syntax…

Everything in the first paragraph is pure HTML, no PHP involved. Generate that first. Then we’ll move on to step 2 (Sanity Check), and Step 3 (Algorithm).

This is the current HTML and PHP i have made and I cant figure out the Element of each textfield or its unique ID…
can you help me do it

<html>
<head></head>
<body>
<form method=“post” action=“<?php echo $_SERVER[‘PHP_SELF’]; ?>”>
Enter number of rows <input name=“rows” type=“text” size=“4”>
and columns <input name=“columns” type=“text” size=“4”> <input type=“submit” name=“submit” value=“Draw Table”>
</form>

<?php

$columns = $_POST['columns'];
for($i=1; $i&lt;= $columns; $i++)

{

echo “Instance $i = “.”<input type=‘text’ size=‘2’ name=‘r’>”." ";

}

if (isset($_POST[‘submit’])) {

// set variables from form input
$rows = $_POST['rows'];
$columns = $_POST['columns'];
// loop to create rows
for ($r = 1; $r &lt;= $rows; $r++) {
    echo "&lt;br&gt;";
    // loop to create columns
    for ($c = 1; $c &lt;= $columns;$c++) {
        echo "&lt;input type='text' name='input' size='5'&gt; ";
    }     echo "&lt;br&gt; ";
}
echo "&lt;/table&gt; ";

}

?>

</body>
</html>

Those text fields dont have a unique identifier, so it’s no wonder you cant figure them out.

What this code is trying to do is trying to generate the correct number of input boxes given input in a prior step (which may change in the following step, because those fields do not become uneditable) If you really want to do this, then you’ll have to start making your inputs into arrays (tacking onto the names, and probably $i in a couple of places too).

Awts T_T
can you help me about the syntax please badly needed for the assignment…

echo “Instance $i = “.”<input type=‘text’ size=‘2’ name=‘r’>”." ";

echo “Instance $i = “.”<input type=‘text’ size=‘2’ name=‘r[]’>”." ";

now it’s an array.
echo "<input type=‘text’ name=‘input’ size=‘5’> ";
echo "<input type=‘text’ name=‘input[“.$row.”][“.$column.”]’ size=‘5’> ";

Now it’s a two dimensional array. (You coudl reverse row and column if you want, the order isnt important, so long as you remember which order you did it in.)

i’ve got what you mention, my problem now is that how can make another textfield where i am going to the value of Max
and how can I make a textfield where the output of Need and Work be placed…

do I need to make the

for ($c = 1; $c <= $columns * 4;$c++) {
echo “<input type=‘text’ name='input[”.$rows.“][”.$columns."]’ size=‘5’> ";
} echo "<br> ";

for it to have a 4 parts textfield this the code so far

<html>
<head></head>
<body>
<form method=“post” action=“OS.php”>
Enter number of rows <input name=“rows” type=“text” size=“4”>
and columns <input name=“columns” type=“text” size=“4”>
<input type=“submit” name=“submit” value=“Draw Table”>
</form>

<?php

$columns = $_POST['columns'];
for($i=1; $i&lt;= $columns; $i++)

{

echo “Instance $i = “.”<input type=‘text’ size=‘2’ name=‘r’>”." ";

}
echo"&lt;br&gt;Alocation Max Need Work";

if (isset($_POST[‘submit’])) {

// set variables from form input
$rows = $_POST['rows'];
$columns = $_POST['columns'];
// loop to create rows
for ($r = 1; $r &lt;= $rows; $r++) {
    echo "&lt;br&gt;";
    // loop to create columns
            for ($c = 1; $c &lt;= $columns;$c++) {
        echo "&lt;input type='text' name='input[".$rows."][".$columns."]' size='5'&gt; ";
    }     echo "&lt;br&gt; ";
}
echo " ";

}

?>

<br>
<br><input type=“submit” name=“sumit” value=“Submit”>
<input type=“reset” name=“Reset”>
</body>
</html>

This is going to be what I call ‘gated’. Several pageloads of the same page.

Pageload 1: Request #Resources and #Processes in the current state
Pageload 2: Request the Available resources, and the current state’s Allocations & Max. Can also request to know what the test-Request is at this point.
Pageload 3: Do the algorithm for that Request, and output the results.

a nested For (row x column) will get you the correct number of text fields, you’ll just have to do it twice (once for Allocations, and once for Max). You’ll also need to run a For (column) to get the Available.

so like this

for ($r = 1; $r <= $rows; $r++)
{
for ($c = 1; $c <= $columns;$c++) {

echo" “.$input[input[”.$r.“][”.$c."]];
}