0% found this document useful (0 votes)
1 views

Lab4

Uploaded by

baominh5x2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab4

Uploaded by

baominh5x2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Báo cáo thực hành

Họ tên Nguyễn Minh Bảo Lớp: TTNT2023


MSSV 23520123 Môn: IT007
Bài thực hành Lab4
CBHD Trần Ngọc Đức

4.3 Sinh viên chuẩn bị


4.3.5 Câu hỏi chuẩn bị
- Vẽ sơ đồ giải thuật của các giải pháp lập lịch tiến trình
1) FCFS

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);

int iRemain = iNumberOfProcess, iReady = 0, iTerminated = 0;

// Input processes and sort by arrival time


inputProcess(iNumberOfProcess, Input);
quickSort(Input, 0, iNumberOfProcess - 1, SORT_BY_ARRIVAL);

// Initialize the ReadyQueue


pushProcess(&iReady, ReadyQueue, Input[0]);
removeProcess(&iRemain, 0, Input);

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);

while (iTerminated < iNumberOfProcess) {


while (iRemain > 0) {
if (Input[0].iArrival <= ReadyQueue[0].iFinish) {
pushProcess(&iReady, ReadyQueue, Input[0]);
removeProcess(&iRemain, 0, Input);
continue;
} else {
break;
}
}

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;
}
}
}

printf("\n===== FCFS Scheduling =====\n");


exportGanttChart(iTerminated, TerminatedArray);

quickSort(TerminatedArray, 0, iTerminated - 1, SORT_BY_PID);


calculateAWT(iTerminated, TerminatedArray);
calculateATaT(iTerminated, TerminatedArray);
return 0;
}

// Function to input process details


void inputProcess(int n, PCB P[]) {
for (int i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for Process %d: ", i + 1);
scanf("%d %d", &P[i].iArrival, &P[i].iBurst);
P[i].iPID = i + 1;
}
}

// Function to print process details


void printProcess(int n, PCB P[]) {
printf("PID\tArrival\tBurst\tStart\tFinish\tWaiting\tResponse\tTaT\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", P[i].iPID, P[i].iArrival, P[i].iBurst,
P[i].iStart, P[i].iFinish, P[i].iWaiting, P[i].iResponse, P[i].iTaT);
}
}

// Function to export Gantt Chart


void exportGanttChart(int n, PCB P[]) {
printf("Gantt Chart:\n");
for (int i = 0; i < n; i++) {
printf("| P%d ", P[i].iPID);
}
printf("|\n");
}
// Function to push a process into a queue
void pushProcess(int *n, PCB P[], PCB Q) {
P[*n] = Q;
(*n)++;
}

// Function to remove a process from a queue


void removeProcess(int *n, int index, PCB P[]) {
for (int i = index; i < *n - 1; i++) {
P[i] = P[i + 1];
}
(*n)--;
}

// Function to calculate Average Waiting Time (AWT)


void calculateAWT(int n, PCB P[]) {
int totalWaiting = 0;
for (int i = 0; i < n; i++) {
totalWaiting += P[i].iWaiting;
}
printf("Average Waiting Time: %.2f\n", (float)totalWaiting / n);
}

// Function to calculate Average Turnaround Time (ATaT)


void calculateATaT(int n, PCB P[]) {
int totalTaT = 0;
for (int i = 0; i < n; i++) {
totalTaT += P[i].iTaT;
}
printf("Average Turnaround Time: %.2f\n", (float)totalTaT / n);
}

// Function for quicksort (sorting based on criteria)


int partition(PCB P[], int low, int high, int iCriteria) {
PCB pivot = P[high];
int i = low - 1;

for (int j = low; j < high; j++) {


int compare = 0;
if (iCriteria == SORT_BY_ARRIVAL) compare = P[j].iArrival < pivot.iArrival;
else if (iCriteria == SORT_BY_PID) compare = P[j].iPID < pivot.iPID;
else if (iCriteria == SORT_BY_BURST) compare = P[j].iBurst < pivot.iBurst;
else if (iCriteria == SORT_BY_START) compare = P[j].iStart < pivot.iStart;

if (compare) {
i++;
PCB temp = P[i];
P[i] = P[j];
P[j] = temp;
}
}

PCB temp = P[i + 1];


P[i + 1] = P[high];
P[high] = temp;
return i + 1;
}

void quickSort(PCB P[], int low, int high, int iCriteria) {


if (low < high) {
int pi = partition(P, low, high, iCriteria);
quickSort(P, low, pi - 1, iCriteria);
quickSort(P, pi + 1, high, iCriteria);
}
}
Sau khi chạy code:

Phần 4.5 Bài tập ôn tập


4.5.1) Viết chương trình mô phỏng giải thuật SJF với các yêu cầu trên.
a)
b)
| P1 | P5 | P2 | P4 | P3 |
0 8 10 14 19 28
- Average Waiting Time = (0 + 9 + 17 + 11 + 4) / 5 = 8.2
- Average Turnaround Time = (8 + 13 + 26 + 16 + 6) / 5 = 13.8
c)
#include <stdio.h>
#include <stdlib.h>

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);

printf("\n===== SJF Scheduling =====\n");


printProcess(n, processes);

calculateAWT(n, processes);
calculateATaT(n, processes);

return 0;
}

// Generate user-defined Arrival Time and Burst Time


void generateProcesses(int n, PCB P[]) {
for (int i = 0; i < n; i++) {
P[i].iPID = i + 1;
printf("Enter Arrival Time for Process %d: ", P[i].iPID);
scanf("%d", &P[i].iArrival);
printf("Enter Burst Time for Process %d: ", P[i].iPID);
scanf("%d", &P[i].iBurst);
P[i].completed = 0;
}
}

// Print process details


void printProcess(int n, PCB P[]) {
printf("PID\tArrival\tBurst\tStart\tFinish\tWaiting\tResponse\tTaT\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", P[i].iPID, P[i].iArrival,
P[i].iBurst, P[i].iStart, P[i].iFinish, P[i].iWaiting, P[i].iResponse, P[i].iTaT);
}
}

// 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;
}
}
}
}

// Calculate SJF scheduling


void calculateSJF(int n, PCB P[]) {
int time = 0;
int completed = 0;

while (completed < n) {


int minBurst = 1000;
int shortestIndex = -1;
// Find the next process with the shortest Burst Time that has arrived
for (int i = 0; i < n; i++) {
if (!P[i].completed && P[i].iArrival <= time && P[i].iBurst < minBurst) {
minBurst = P[i].iBurst;
shortestIndex = i;
}
}

if (shortestIndex == -1) {
time++; // No process ready to execute, increment time
continue;
}

// Execute the selected process


P[shortestIndex].iStart = time;
P[shortestIndex].iFinish = P[shortestIndex].iStart + P[shortestIndex].iBurst;
P[shortestIndex].iResponse = P[shortestIndex].iStart -
P[shortestIndex].iArrival;
P[shortestIndex].iWaiting = P[shortestIndex].iResponse;
P[shortestIndex].iTaT = P[shortestIndex].iFinish - P[shortestIndex].iArrival;

time = P[shortestIndex].iFinish;
P[shortestIndex].completed = 1;
completed++;
}
}

// Calculate Average Waiting Time (AWT)


void calculateAWT(int n, PCB P[]) {
int totalWaiting = 0;
for (int i = 0; i < n; i++) {
totalWaiting += P[i].iWaiting;
}
printf("Average Waiting Time: %.2f\n", (float)totalWaiting / n);
}

// Calculate Average Turnaround Time (ATaT)


void calculateATaT(int n, PCB P[]) {
int totalTaT = 0;
for (int i = 0; i < n; i++) {
totalTaT += P[i].iTaT;
}
printf("Average Turnaround Time: %.2f\n", (float)totalTaT / n);
}
d)
test 1:
| P1 | P5 | P2 | P4 | P3 |
0 8 10 14 19 28
- Average Waiting Time = (0 + 9 + 17 + 11 + 4) / 5 = 8.2
- Average Turnaround Time = (8 + 13 + 26 + 16 + 6) / 5 = 13.8
Test 2:
| P1 | P3 | P5 | P2 | P4 |
0 3 4 9 15 22
- Average Waiting Time = (0 + 8 + 1 + 12 + 0) / 5 = 4.2
- Average Turnaround Time = (3 + 14 + 2 + 19 + 5) / 5 = 8.6
Test3:
4.5.2) Chọn một trong hai hai giải thuật gồm SRTF và RR để thực hiện các yêu cầu
trên.
Giải thuật RR
1) Sơ đồ giải thuật:
2.2) chạy tay test case:
Testcase:

Tiến trình Arrival time Burst time


P1 0 10
P2 1 5
P3 2 8
P4 3 6
P5 4 2

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;
};

void calculateTimes(struct Process processes[], int n, int timeQuantum) {


int currentTime = 0, completed = 0;

while (completed < n) {


bool processExecuted = false;

for (int i = 0; i < n; i++) {


if (processes[i].arrivalTime <= currentTime && !
processes[i].isCompleted) {
if (processes[i].remainingTime > timeQuantum) {
currentTime += timeQuantum;
processes[i].remainingTime -= timeQuantum;
processExecuted = true;
} else {
currentTime += processes[i].remainingTime;
processes[i].remainingTime = 0;
processes[i].isCompleted = true;
processes[i].turnaroundTime = currentTime -
processes[i].arrivalTime;
processes[i].waitingTime = processes[i].turnaroundTime -
processes[i].burstTime;
completed++;
processExecuted = true;
}
}
}

// 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++;
}
}
}

void printTable(struct Process processes[], int n) {


printf("ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id,
processes[i].arrivalTime,
processes[i].burstTime,
processes[i].waitingTime,
processes[i].turnaroundTime);
}
}

int main() {
int n, timeQuantum;

printf("Nhập số tiến trình: ");


scanf("%d", &n);

struct Process processes[n];


for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Nhập thời gian đến (Arrival Time) của tiến trình %d: ", i + 1);
scanf("%d", &processes[i].arrivalTime);
printf("Nhập thời gian xử lý (Burst Time) của tiến trình %d: ", i + 1);
scanf("%d", &processes[i].burstTime);
processes[i].remainingTime = processes[i].burstTime;
processes[i].isCompleted = false;
}

printf("Nhập time quantum: ");


scanf("%d", &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:

Tiến trình Arrival time Burst time


P1 0 10
P2 1 5
P3 2 8
P4 3 6
P5 4 2
Sau khi chạy tay:

P1 P2 P5 P4 P3 P1
0 1 5 7 13 21 31

3)chạy thử với 3 testcase:


Output:

Test 2:
Output:

Test 3:
Output:

You might also like