OS algorithms
First fit
#include <iostream>
using namespace std;
int main() {
int blocks[] = {100, 500, 200, 300};
int processes[] = {120, 50, 230};
int m = 4, n = 3;
int allocation[3];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blocks[j] >= processes[i]) {
allocation[i] = j;
blocks[j] -= processes[i];
break;
for (int i = 0; i < n; i++) {
cout << "P" << i+1 << " -> ";
if (allocation[i] != -1)
cout << "Block " << allocation[i]+1 << endl;
else
cout << "Not Allocated\n";
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Best fit
#include <iostream>
using namespace std;
int main() {
int blocks[] = {100, 500, 200, 300};
int processes[] = {120, 50, 230};
int m = 4, n = 3;
int allocation[3];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < n; i++) {
int best = -1;
for (int j = 0; j < m; j++) {
if (blocks[j] >= processes[i]) {
if (best == -1 || blocks[j] < blocks[best]) {
best = j;
if (best != -1) {
allocation[i] = best;
blocks[best] -= processes[i];
for (int i = 0; i < n; i++) {
cout << "P" << i+1 << " -> ";
if (allocation[i] != -1)
cout << "Block " << allocation[i]+1 << endl;
else
cout << "Not Allocated\n";
}
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Banker algorithm
#include <iostream>
using namespace std;
int main() {
int n = 3; // (processes number)
int m = 3; // (resources type number)
int alloc[3][3] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2}
};
int max[3][3] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2}
};
int avail[3] = {3, 3, 2};
int need[3][3];
int finish[3] = {0};
int safeSeq[3];
// need = max - alloc
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - alloc[i][j];
int count = 0;
while (count < n) {
bool found = false;
for (int i = 0; i < n; i++) {
if (finish[i] == 0) {
bool canRun = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > avail[j]) {
canRun = false;
break;
if (canRun) {
for (int j = 0; j < m; j++) {
avail[j] += alloc[i][j];
safeSeq[count++] = i;
finish[i] = 1;
found = true;
if (!found) {
cout << "System is NOT SAFE\n";
return 0;
cout << "System is SAFE\nSafe sequence: ";
for (int i = 0; i < n; i++) {
cout << "P" << safeSeq[i] << " ";
return 0;
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
FCFS
#include <iostream>
using namespace std;
int main() {
int n = 3;
int burst[] = {5, 3, 8};
int waiting[3], turnaround[3];
waiting[0] = 0;
for (int i = 1; i < n; i++) {
waiting[i] = waiting[i-1] + burst[i-1];
for (int i = 0; i < n; i++) {
turnaround[i] = waiting[i] + burst[i];
for (int i = 0; i < n; i++) {
cout << "P" << i+1 << " WT=" << waiting[i]
<< " TAT=" << turnaround[i] << endl;
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
SJF
#include <iostream>
using namespace std;
int main() {
int n = 3;
int burst[] = {5, 3, 8};
int process[] = {1, 2, 3};
// sort (simple)
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (burst[i] > burst[j]) {
int temp = burst[i];
burst[i] = burst[j];
burst[j] = temp;
int temp2 = process[i];
process[i] = process[j];
process[j] = temp2;
}
int waiting[3], turnaround[3];
waiting[0] = 0;
for (int i = 1; i < n; i++) {
waiting[i] = waiting[i-1] + burst[i-1];
for (int i = 0; i < n; i++) {
turnaround[i] = waiting[i] + burst[i];
for (int i = 0; i < n; i++) {
cout << "P" << i+1 << " WT=" << waiting[i]
<< " TAT=" << turnaround[i] << endl;
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
[Link]
سطر سطرdebug موقع بيعمل
Banker's Algorithm Simulator
Memory Allocation Simulator: First-Fit, Best-Fit & Worst-Fit with Fragmentation Tracking | Persona
500
OS خاصة بالalgorithms مواقع فيها شوية