Lab4
Lab4
2) RR
3) SJF
4)SRT
4.4 Hướng dẫn thực hành
4.4.1 Phân tích bài toán
4.4.2 Lưu đồ giải thuật
4.4.3 Hiện thực giải thuật FCFS trên C
#include <stdio.h>
#include <stdlib.h>
#define SORT_BY_ARRIVAL 0
#define SORT_BY_PID 1
#define SORT_BY_BURST 2
#define SORT_BY_START 3
typedef struct {
int iPID;
int iArrival, iBurst;
int iStart, iFinish, iWaiting, iResponse, iTaT;
} PCB;
// Function prototypes
void inputProcess(int n, PCB P[]);
void printProcess(int n, PCB P[]);
void exportGanttChart(int n, PCB P[]);
void pushProcess(int *n, PCB P[], PCB Q);
void removeProcess(int *n, int index, PCB P[]);
void quickSort(PCB P[], int low, int high, int iCriteria);
void calculateAWT(int n, PCB P[]);
void calculateATaT(int n, PCB P[]);
// Main function
int main() {
PCB Input[10];
PCB ReadyQueue[10];
PCB TerminatedArray[10];
int iNumberOfProcess;
printf("Please input number of Processes: ");
scanf("%d", &iNumberOfProcess);
ReadyQueue[0].iStart = ReadyQueue[0].iArrival;
ReadyQueue[0].iFinish = ReadyQueue[0].iStart + ReadyQueue[0].iBurst;
ReadyQueue[0].iResponse = ReadyQueue[0].iStart - ReadyQueue[0].iArrival;
ReadyQueue[0].iWaiting = ReadyQueue[0].iResponse;
ReadyQueue[0].iTaT = ReadyQueue[0].iFinish - ReadyQueue[0].iArrival;
printf("\nReady Queue:\n");
printProcess(1, ReadyQueue);
if (iReady > 0) {
pushProcess(&iTerminated, TerminatedArray, ReadyQueue[0]);
removeProcess(&iReady, 0, ReadyQueue);
if (iReady > 0) {
ReadyQueue[0].iStart = TerminatedArray[iTerminated - 1].iFinish;
ReadyQueue[0].iFinish = ReadyQueue[0].iStart + ReadyQueue[0].iBurst;
ReadyQueue[0].iResponse = ReadyQueue[0].iStart -
ReadyQueue[0].iArrival;
ReadyQueue[0].iWaiting = ReadyQueue[0].iResponse;
ReadyQueue[0].iTaT = ReadyQueue[0].iFinish - ReadyQueue[0].iArrival;
}
}
}
if (compare) {
i++;
PCB temp = P[i];
P[i] = P[j];
P[j] = temp;
}
}
typedef struct {
int iPID;
int iArrival, iBurst;
int iStart, iFinish, iWaiting, iResponse, iTaT;
int completed;
} PCB;
// Function prototypes
void generateProcesses(int n, PCB P[]);
void printProcess(int n, PCB P[]);
void sortByArrivalAndBurst(int n, PCB P[]);
void calculateSJF(int n, PCB P[]);
void calculateAWT(int n, PCB P[]);
void calculateATaT(int n, PCB P[]);
int main() {
int n = 5;
PCB processes[n];
generateProcesses(n, processes);
printf("Generated Processes:\n");
printProcess(n, processes);
sortByArrivalAndBurst(n, processes);
calculateSJF(n, processes);
calculateAWT(n, processes);
calculateATaT(n, processes);
return 0;
}
// Sort processes by Arrival Time, then by Burst Time if Arrival Time is the same
void sortByArrivalAndBurst(int n, PCB P[]) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (P[i].iArrival > P[j].iArrival || (P[i].iArrival == P[j].iArrival && P[i].iBurst
> P[j].iBurst)) {
PCB temp = P[i];
P[i] = P[j];
P[j] = temp;
}
}
}
}
if (shortestIndex == -1) {
time++; // No process ready to execute, increment time
continue;
}
time = P[shortestIndex].iFinish;
P[shortestIndex].completed = 1;
completed++;
}
}
P1 P2 P3 P4 P5 P1 P2 P3 P4 P1
0 4 8 12 16 18 22 23 27 31
P1 P2 P3 P4 P1 P5 P3 P4 P1
P3 P4 P1 P5 P3 P4 P1
P4 P1 P5 P3 P4 P1
P1 P5 P3 P4 P1
#include <stdio.h>
#include <stdbool.h>
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int waitingTime;
int turnaroundTime;
bool isCompleted;
};
// Nếu không có tiến trình nào có thể được thực hiện, tăng thời gian hiện tại
if (!processExecuted) {
currentTime++;
}
}
}
int main() {
int n, timeQuantum;
calculateTimes(processes, n, timeQuantum);
printTable(processes, n);
return 0;
}
2.3) chạy 3 test case với code trên:
Test 1:
Output:
Test2:
Output:
Test 3:
Output:
4.6) ôn tập
1) sơ đồ giải thuật:
2) chạy tay:
Testcase:
P1 P2 P5 P4 P3 P1
0 1 5 7 13 21 31
Test 2:
Output:
Test 3:
Output: