C language Bankers Algorithm

Source: Internet
Author: User

// Banker algorithm
/**
* Author: Thank you!
* Last modification date:, 21
* Email: do_while@sohu.com
* Function: Bankers Algorithm Implementation
**/
# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>

# Define M 5/* Number of processes */
# Define N 3/* Number of Resources */

/**
* Used in TC
# Define true 1
# Define false 0
Typedef int bool
**/
// System available resource Vector
Int available [N] = {3, 2 };
// Maximum Demand Vector
Int MAX [m] [N] = {
{7, 5, 3 },
{3, 2, 2 },
{9, 0, 2 },
{2, 2 },
{4, 3 },
};
// Resource Allocation Vector
Int allocation [m] [N] = {
{0, 1 },
{2, 0, 0 },
{3, 0, 2 },
{2, 1, 1 },
{0, 0, 2 },
};
// Demand Vector
Int need [m] [N] = {
{7, 4, 3 },
{1, 2 },
{6, 0, 0 },
{0, 1 },
{4, 3, 1 },
};

// Compare two one-dimensional arrays
// Judge a> = B?
Bool compare (int * a, int * B, int N)
{
Int I = 0;
For (I = 0; I <n; I ++)
{
If (A [I] <B [I])
{
Return false;
}
}

Return true;

}

// One-dimensional array Addition
// A = a + B
Void add (int * a, int * B, int N)
{
Int I = 0;
For (I = 0; I <n; I ++)
{
A [I] + = B [I];
}
}

// One-dimensional array Subtraction
// A = A-B
Void subtract (int * a, int * B, int N)
{
Int I = 0;
For (I = 0; I <n; I ++)
{
A [I]-= B [I];
}
}

// Assign the value of array B to A, and N is the size of the array.
Void assign (int * a, int * B, int N)
{
Int I = 0;
For (I = 0; I <n; I ++)
{
A [I] = B [I];
}
}

// Determine whether the security status is correct.
// AV available resource Matrix
// Security path of SL record
Bool safe (int * SL)
{
Int I;
Int COUNT = 0;/* Number of records for finish [I] = true */
Int n = 0;
Int work [N];
Bool finish [m];
// Work = Av;
Assign (work, available, N );
// Initialize mark finish
For (I = 0; I <m; I ++)
{
Finish [I] = false;
}
 
// N indicates the number of processes.
// The loop can be executed up to n times
N = m;
While (n --)
{
For (I = 0; I <m; I ++)
{
// Determine security
If (count> = m)
{
// All processes can be executed securely (finish = true)
Return true;
}

// Determine whether process I can be satisfied
// Work> = need [I]?
If (finish [I] = false & compare (work, need [I], n ))
{
// Allocate and release after the process is completed
Add (work, allocation [I], n );
Finish [I] = true;

 
// Record the security path
SL [count] = I;
// Number of processes that can be satisfied + 1
Count ++;
}
}
}
If (count> = m)
{
Return true;
}
Else
{
Return false;
}
}

// Request allocation
// PID process, r Request vector, N resource count
Bool request (int pid, int * r, int N)
{
Int I;
// Record the security path
Int SL [5];
If (compare (need [pid], R, n) = true &&
Compare (availavle, R, n ))
{
// Try to allocate resources
Subtract (available, R, N );
Add (allocation [pid], R, N );
Subtract (need [pid], R, N );

// Determine whether the security status is correct.
If (safe (SL ))
{
// Print the security path
Printf ("Secure Path:/n/t ");
For (I = 0; I <m; I ++)
{
Printf ("P % d", SL [I]);
}
Printf ("/N ");
// Allocable
Return true;
}
Else
{
// Not allocated
// Restore to the status before allocation
Add (available, R, N );
Subtract (allocation [pid], R, N );
Add (need [pid], R, N );

Return false;
}
}
Else
{
// Error
Return false;
}
}

// Print a one-dimensional array
Void print (int * a, int N)
{
Int I;
For (I = 0; I <n; I ++)
{
Printf ("% 4D", a [I]);
}
Printf ("/N ");
}

// Prompt message
Char help ()
{
Printf ("----- operation prompt -----/N ");
Printf ("A -- automatically apply for resources randomly/N ");
Printf ("s -- manually enter the requested resource/N ");
Return getch ();
}

// Display System Information
Void Init ()
{
Int I;
Printf ("the system has five processes,/n its resource requirements and allocation are:/N ");
For (I = 0; I <m; I ++)
{
Printf ("/t process % d maximum Resource requirements:", I );
Print (MAX [I], n );
}
Printf ("/N ");
For (I = 0; I <m; I ++)
{
Printf ("/t process % d allocated resources:", I );
Print (allocation [I], n );
}

Printf ("number of available system resources:/n/t ");
Print (available, N );
}

// Input
Void input (int * r, int N, int * ID)
{
Char ch;
Printf ("Enter the process ID (0 ~ 4):/N ");
Ch = getche ();
* Id = CH-0x30;

Printf ("/n please enter the number of requests for Class 0 resources (INT):/N ");
Ch = getche ();
R [0] = CH-0x30;
 
Printf ("/n please enter the number of requests for Class 1 resources (INT):/N ");
Ch = getche ();
R [1] = CH-0x30;
 
Printf ("/n please enter the number of requests for two types of resources (INT):/N ");
Ch = getche ();
R [2] = CH-0x30;

Printf ("/N: request [% d] (% d, % d, % d)/n", * ID, R [0], R [1], R

[2]);
}

// Check input
Bool check (int id, int R1, int R2, int R3)
{
If (ID> 4 | id <0 | R1 <0 | R2 <0 | R3 <0)
{
Return false;
}
Else
{
Return true;
}
}

Int main ()
{
// Process ID
Int ID;
// Control characters
Char control;
// Resource request Vector
Int R [3];
// Display the start information
Init ();
// Random number Initialization
Srand (null );
// Master Process
While (1)
{
// Prompt
Control = help ();
If (control = 'A' | control = 'A ')
{
// Randomly apply for resources
Id = rand () % 5;
R [0] = rand () % 5;
R [1] = rand () % 5;
R [2] = rand () % 5;
// Display application information
Printf ("/trequest [% d] (% d, % d, % d)/n", ID, R [0], R [1], R [2]);

If (Request (ID, R, n ))
{
Printf ("allocated successfully! /N ");
}
Else
{
Printf ("failed to allocate! /N ");
}
}
Else
{
// Enter the application information
Input (R, N, & ID );
// Check input
If (check (ID, R [0], R [1], R [2]) = false)
{
Printf ("/N input error, check and re-enter/N ");
Continue;
}
// Execute
If (Request (ID, R, n ))
{
Printf ("request secceed! /N ");
}
Else
{
Printf ("request fail! /N ");
}
}
// Displays the current system resources and processes
Printf ("available system resources:/N ");
Print (available, N );

Printf ("maximum resource requirement of process % d:/N", ID );
Print (MAX [ID], n );

Printf ("process % d allocated resources:/N", ID );
Print (allocation [ID], n );

// The system prompts whether to continue N or N to exit.
Printf ("/ncontinue? (Y/n )! /N ");
Control = getch ();
If (control = 'n' | control = 'n ')
{
Break;
}
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.