feat: implement EDF and RMS scheduling algorithms
Add a new RTOS scheduling implementation with EDF and RMS algorithms. Introduce a task structure and create task threads to simulate task execution. Optimize the Banker's Algorithm code comments for clarity. These changes enhance the understanding and functionality of the scheduling mechanisms in the system.
This commit is contained in:
parent
86ff7c0f35
commit
6699115e40
2 changed files with 194 additions and 26 deletions
|
@ -1,10 +1,11 @@
|
|||
{{REWRITTEN_CODE}}
|
||||
#include <stdio.h>
|
||||
|
||||
// C program for Banker's Algorithm (Safety & Resource Request)
|
||||
// C program for Banker's Algorithm (Safety & Resource Request Loop)
|
||||
// Optimized for minimal code size (e.g., for writing on paper)
|
||||
|
||||
int main() {
|
||||
int p, r, i, j, k, pid, req_pid = -1; // p=procs, r=res; req_pid: -1=initial, >=0 processing req
|
||||
int p, r, i, j, k, pid, req_pid = -1; // req_pid: -1=initial/between reqs, >=0 processing req
|
||||
printf("P R:"); scanf("%d%d", &p, &r); // Input num processes and resources
|
||||
int av[r], max[p][r], al[p][r], nd[p][r], req[r]; // av=avail, al=alloc, nd=need
|
||||
int w[r], fin[p], seq[p]; // w=work, fin=finish, seq=safe sequence
|
||||
|
@ -40,38 +41,53 @@ S:; // Safety Check Algorithm Label
|
|||
|
||||
// --- End Safety Check ---
|
||||
|
||||
// Handle result based on phase (initial check or request check)
|
||||
if(req_pid == -1) { // Phase 1: Initial State Check
|
||||
if(safe) {
|
||||
printf("SAFE. Seq:"); for(i=0; i<p; i++) printf(" P%d", seq[i]); puts("");
|
||||
} else { puts("UNSAFE"); goto end; } // If unsafe initially, exit
|
||||
|
||||
// Phase 2: Resource Request
|
||||
printf("PID Req:"); scanf("%d", &pid); req_pid = pid; // Get requesting proc ID
|
||||
printf("Req:"); for(j=0; j<r; j++) scanf("%d", &req[j]); // Get request vector
|
||||
|
||||
// Check 1: Request <= Need
|
||||
for(j=0; j<r; j++) if(req[j] > nd[pid][j]) { puts("Err:Req>Need"); goto end; }
|
||||
// Check 2: Request <= Available
|
||||
for(j=0; j<r; j++) if(req[j] > av[j]) { puts("Wait:Req>Avail"); goto end; }
|
||||
|
||||
// Tentatively allocate resources
|
||||
for(j=0; j<r; j++) { av[j]-=req[j]; al[pid][j]+=req[j]; nd[pid][j]-=req[j]; }
|
||||
puts("Checking req safety...");
|
||||
goto S; // Re-run safety check on the new state
|
||||
|
||||
} else { // Phase 3: Post-Request Safety Check Result
|
||||
if(safe) { // Request is granted if new state is safe
|
||||
// --- Post-Safety Check Decision Logic ---
|
||||
if (req_pid != -1) { // Phase 3: Result of a specific request check
|
||||
if (safe) { // Request granted
|
||||
printf("Req OK. Seq:"); for(i=0; i<p; i++) printf(" P%d", seq[i]); puts("");
|
||||
} else { // Request denied if new state is unsafe
|
||||
// State remains modified (available, alloc, need updated)
|
||||
} else { // Request denied
|
||||
puts("Req DENIED (unsafe)");
|
||||
// Rollback state to before tentative allocation
|
||||
pid = req_pid; // Restore pid for rollback
|
||||
for(j=0; j<r; j++) { av[j]+=req[j]; al[pid][j]-=req[j]; nd[pid][j]+=req[j]; }
|
||||
}
|
||||
// No further action needed after handling the single request
|
||||
req_pid = -1; // Reset for next request cycle
|
||||
goto R; // Go ask for the next request or exit
|
||||
} else { // Phase 1: Result of the initial state check
|
||||
if (safe) {
|
||||
printf("SAFE. Seq:"); for(i=0; i<p; i++) printf(" P%d", seq[i]); puts("");
|
||||
// Initial state is safe, proceed to handle requests
|
||||
} else {
|
||||
puts("UNSAFE"); // Initial state unsafe, cannot proceed
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
R:; // Phase 2: Resource Request Loop Label
|
||||
printf("PID Req (-1 to exit):"); scanf("%d", &pid);
|
||||
if (pid < 0) goto end; // Exit condition
|
||||
// Basic check if PID is valid, can add pid >= p check if needed
|
||||
if (pid >= p) { puts("Invalid PID."); goto R;}
|
||||
|
||||
printf("Req:"); for(j=0; j<r; j++) scanf("%d", &req[j]); // Get request vector
|
||||
|
||||
// Check 1: Request <= Need
|
||||
int check_fail = 0;
|
||||
for(j=0; j<r; j++) if(req[j] > nd[pid][j]) { check_fail = 1; break; }
|
||||
if (check_fail) { puts("Err:Req>Need"); goto R; } // Ask for next request
|
||||
|
||||
// Check 2: Request <= Available
|
||||
check_fail = 0;
|
||||
for(j=0; j<r; j++) if(req[j] > av[j]) { check_fail = 1; break; }
|
||||
if (check_fail) { puts("Wait:Req>Avail"); goto R; } // Ask for next request
|
||||
|
||||
// Tentatively allocate resources
|
||||
for(j=0; j<r; j++) { av[j]-=req[j]; al[pid][j]+=req[j]; nd[pid][j]-=req[j]; }
|
||||
req_pid = pid; // Set flag indicating we are checking this specific request
|
||||
puts("Checking req safety...");
|
||||
goto S; // Re-run safety check on the new tentative state
|
||||
|
||||
end: return 0; // End of program
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue