Tải bản đầy đủ (.docx) (27 trang)

Source Code Project Nguyên Lý Hệ Điều Hành CT178 CT104

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (782.86 KB, 27 trang )

TRƯỜNG ĐẠI HỌC CẦN THƠ
KHOA CÔNG NGHỆ THÔNG TIN & TRUYỀN THƠNG

BÁO CÁO CODE PROJECT
NGUN LÝ HỆ ĐIỀU HÀNH
HỌC KÌ I 2021-2022


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
Truy cập
/>Để Xem Thêm

2


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
3


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
PHẦN 1: XÂY DỰNG LINUX KERNEL
1. Biên dịch Kernel
Chỉnh sửa dòng CONFIG_SYSTEM_TRUSTED_KEYS=” debian/canonical-certs-pem”
thành CONFIG_SYSTEM_TRUSTED_KEYS=””
#nano .config



2. Thay đổi file cấu hình GRUB
Thay đổi file cấu hình grub:
# nano /etc/default/grub
Thực hiện các thay đổi sau:
GRUB_DEFAULT=0
GRUB_TIMEOUT=25

4


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
PHẦN 2: THÊM LỜI GỌI HỆ THỐNG VÀO LINUX KERNEL
1. Viết chương trình hello.c
#nano hello.c
Nội dung:
#include <linux/kernel.h>
asmlinkage long sys_hello(void){
printk(“Xin chao, Toi la TRAN VAN THINH – B1906773 \n”);
return 0;
}

2. Tạo tập tin Makefile
#nano Makefile
Obj-y := hello.o

3. Chỉnh sửa tập tin Makefile
Truy cập thư mục /root/linux-4.16.3/ và sửa đổi tập tin Makefile

#cd /root/linux4.16.3/ #nano Makefile
Thêm “hello/” vào cuối dòng:
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/
5


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
4. Thêm lời gọi mới vào bảng gọi hệ thống
Truy cập vào đường dẫn
#cd /root/linux-4.16.3/arch/x86/entry/syscalls/

Sau đó
Chỉnh sửa tập tin syscall_64.tbl
#nano syscall_64.tbl
Thêm 1 dịng như sau
548 64
hello
sys_hello
5. Thêm lời gọi hệ thống sys_hello() vào system call header file
#cd /root/linux-4.16.3/include/linux
#nano syscalls.h
Thêm dòng “asmlinkage long sys_hello(void);” vào trước dòng #endif cuối cùng

6


CT178-Nguyên Lý Hệ Điều Hành


Truy cập />
6. Thay đổi .config
#nano .config
Chỉnh sửa dòng CONFIG_SYSTEM_TRUSTED_KEYS=””

7


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
7. Chỉnh sửa cấu hình GRUB
#nano /etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT=25

8. Tạo lời gọi hệ thống
#nano uesrspace.c
#include<stdio.h>
#include<linux/kernel.h>
#include<sys/syscall.h>
#include<unistd.h>
int main(){
long int nmt = syscall(548);
printf(“Loi goi he thong return %ld\n”,nmt);
return 0;
}

8



CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
PHẦN 3: ĐỊNH THỜI CPU
1. SOURCE CODE
// TRAN VAN THNH
// B1906773
#include <stdio.h>
#include <stdlib.h>
#define Run 2
#define Wait 1
#define MAX 100
int q;
int RR =1;
typedef struct {
int PID;
int Arrive_Time;
int Burst_Time;
int Waiting_Time;
int Turnaround_Time;
int Response_Time;
int Start_Time;
int temp_Burst_Time;
9


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />

int sum_Burst_Time;
int Completion_Time;
int State[500];
}
Process;

// ham tim max
int Max(int a, int b) {
if (a > b)
return a;
return b;
}

// ham sap xep theo Arrive Time
void BubbleSort_Arrive(Process p[], int n) {
int i, j;
Process temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].Arrive_Time > p[j].Arrive_Time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
return;
10



CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
}
// ham sap xep theo Burst Time
void BubbleSort_Burst(Process p[], int n) {
int i, j;
Process temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].Burst_Time > p[j].Burst_Time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
return;
}
// ham dinh thoi tien trinh
void Output(Process p[], int n, int tongbursttime) {

int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < tongbursttime; j++) {
if (p[i].State[j] == Run)
printf("R");
else if (p[i].State[j] == Wait)
printf("W");
else printf("-");

11


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
}
printf("\n");
}
printf("\n");
}
// ham dinh thoi FCFS
void FCFS(Process p[], int n) {
Process p2[n], p3[n];
int i, j, sumWT = 0, sumTT = 0, sumRT;

//sao chep du lieu cua P vao P1 de khong lam thay doi du lieu da nhap vao P
for (i = 0; i < n; i++) {
p[n].sum_Burst_Time = p[i].Burst_Time;
}

for (i = 0; i < n; i++) {
p2[i] = p[i];
}

BubbleSort_Arrive(p2, n);

for (i = 0; i < n; i++) {
if (i == 0)
p2[i].Start_Time = p2[i].Arrive_Time;

else p2[i].Start_Time = Max(p2[i - 1].Completion_Time, p2[i].Arrive_Time);

p2[i].Completion_Time = p2[i].Start_Time + p2[i].Burst_Time;
p2[i].Turnaround_Time = p2[i].Completion_Time - p2[i].Arrive_Time;
12


Truy cập />p2[i].Waiting_Time = p2[i].Turnaround_Time - p2[i].Burst_Time;

CT178-Nguyên Lý Hệ Điều Hành

p2[i].Response_Time = p2[i].Start_Time - p2[i].Arrive_Time;
}
//Tao mang p3 de tra ve dung thu tu nhap cua Process
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (i == p2[j].PID - 1)
p3[i] = p2[j];

//tinh tong turnaround time, Waiting times, response time
for (i = 0; i < n; i++) {
sumTT += p3[i].Turnaround_Time;
sumWT += p3[i].Waiting_Time;
sumRT += p3[i].Response_Time;
}

// dat State = 0 cho tat ca cac tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < p2[n - 1].Completion_Time; j++)
p3[i].State[j] = 0;


// thiet lap State cho tat ca tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < p2[n - 1].Completion_Time; j++) {
if (p3[i].Start_Time <= j && j < p3[i].Completion_Time)
p3[i].State[j] = Run;
else if (j >= p3[i].Arrive_Time && j < p3[i].Start_Time)
p3[i].State[j] = Wait;
13


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
}
// in output cua chuong trinh
printf("\n");
printf("********************************\n");
printf("DINH THOI FCFS\n");
Output(p3, n, p2[n - 1].Completion_Time);
printf("AVGW= %-2.2lf\t", (double) sumWT / (double) n);
printf("AVGR= %-2.2lf \t", (double) sumWT / (double) n);
printf("AVGT= %-2.2lf\t", (double) sumTT / (double) n);
printf("\n");
}

// ham dinh thoi SJF trung dung
void SJF_TrungDung(Process p[], int n) {

int sumWT = 0, sumTT = 0, sumRT = 0;

int i, j, smallest, count = 0, time, end = 0, time2;
int rtemp[n], rtemp2[n];

//Tao mang rtemp de nho thoi gian lan dau tien tien trinh duoc phan hoi
//Tao mang rtemp 2 de danh dau tien trinh nao da duoc phan hoi
for (i = 0; i < n; i++) {
rtemp[i] = 0;
rtemp2[i] = 0;
}

// dat State = 0 cho tat ca cac tien trinh
for (i = 0; i < n; i++)
14


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
for (j = 0; j < p[n].sum_Burst_Time; j++)
p[i].State[j] = 0;

//thiet lap gia tri cho temp burst time
for (i = 0; i < n; i++) {
p[i].temp_Burst_Time = p[i].Burst_Time;
}

//tinh Waiting time and turn around time
p[9].temp_Burst_Time = 9999;
for (time = 0; count != n; time++) {
smallest = 9;

for (i = 0; i < n; i++)
if (p[i].Arrive_Time <= time && p[i].temp_Burst_Time <
p[smallest].temp_Burst_Time && p[i].temp_Burst_Time > 0)
smallest = i;

p[smallest].temp_Burst_Time--;
p[smallest].State[time] = Run;

if (rtemp2[smallest] == 0) {
rtemp[smallest] = time;
rtemp2[smallest] = 1;
}
if (p[smallest].temp_Burst_Time == 0) {
count++;
end = time + 1;
p[smallest].Waiting_Time = end - p[smallest].Arrive_Time - p[smallest].Burst_Time;
p[smallest].Turnaround_Time = end - p[smallest].Arrive_Time;
15


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
}
time2 = time;
}
// thiet lap State cho tat ca cac tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < time2; j++)
if (j >= p[i].Arrive_Time && j < p[i].Turnaround_Time + p[i].Arrive_Time)

if (p[i].State[j] != Run)
p[i].State[j] = Wait;

//tinh response time
for (i = 0; i < n; i++)
p[i].Response_Time = rtemp[i] - p[i].Arrive_Time;

for (i = 0; i < n; i++) {
sumWT += p[i].Waiting_Time;
sumTT += p[i].Turnaround_Time;
sumRT += p[i].Response_Time;
}

// in output cua chuong trinh
printf("\n");
printf("********************************\n");
printf("DINH THOI SJF TRUNG DUNG\n");
Output(p, n, time2);
printf("AVGW= %-2.2lf\t", (double) sumWT / (double) n);
printf("AVGR= %-2.2lf \t", (double) sumRT / (double) n);
printf("AVGT= %-2.2lf\t", (double) sumTT / (double) n);
16


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
printf("\n");
}


// ham dinh thoi SJF khong trung dung
void SJF_khongTrungDung(Process p[], int n) {
Process p2[n], p3[n];
int i, j, sumWT = 0, sumTT = 0, sumRT = 0;

for (i = 0; i < n; i++) {
p[n].sum_Burst_Time = p[i].Burst_Time;
}

for (i = 0; i < n; i++) {
p2[i] = p[i];
}

BubbleSort_Burst(p2, n);
for (i = 0; i < n; i++) {
if (i == 0)
p2[i].Start_Time = p2[i].Arrive_Time;
else p2[i].Start_Time = Max(p2[i - 1].Completion_Time, p2[i].Arrive_Time);

p2[i].Completion_Time = p2[i].Start_Time + p2[i].Burst_Time;
p2[i].Turnaround_Time = p2[i].Completion_Time - p2[i].Arrive_Time;
p2[i].Waiting_Time = p2[i].Turnaround_Time - p2[i].Burst_Time;
p2[i].Response_Time = p2[i].Start_Time - p2[i].Arrive_Time;
}
17


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />

for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (i == p2[j].PID - 1)
p3[i] = p2[j];

//calculate sum turnaround time, Waiting times, response time
for (i = 0; i < n; i++) {
sumTT += p3[i].Turnaround_Time;
sumWT += p3[i].Waiting_Time;
sumRT += p3[i].Response_Time;
}
// set State = 0 for all process
for (i = 0; i < n; i++)
for (j = 0; j < p2[n - 1].Completion_Time; j++)
p3[i].State[j] = 0;

// thiet lap State cho tat ca cac tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < p2[n - 1].Completion_Time; j++) {
if (p3[i].Start_Time <= j && j < p3[i].Completion_Time)
p3[i].State[j] = Run;
else if (j >= p3[i].Arrive_Time && j < p3[i].Start_Time)
p3[i].State[j] = Wait;
}
// in output cua chuong trinh
printf("\n");
printf("********************************\n");
18



CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
printf("DINH THOI SJF KHONG TRUNG DUNG \n");
Output(p3, n, p2[n - 1].Completion_Time);
printf("AVGW= %-2.2lf\t", (double) sumWT / (double) n);
printf("AVGR= %-2.2lf \t", (double) sumWT / (double) n);
printf("AVGT= %-2.2lf\t", (double) sumTT / (double) n);
printf("\n");
}

// ham dinh thoi Round Robin
void Round_Robin(Process p[], int n, int q) {
int i, j, pro, time, remain, flag = 0, k;
int sumWT = 0, sumTT = 0, sum_Burst_Time = 0, sumRT = 0;
remain = n;
int rtemp[n], rtemp2[n];

if(RR == 0) {
printf("\n");
printf("********************************\n");
printf("DINH THOI ROUND ROBIN \n ");
printf("quantum =0 ");
return 0;
}
//Tao mang rtemp de nho thoi gian lan dau tien tien trinh duoc phan hoi
//Tao mang rtemp 2 de danh dau tien trinh nao da duoc phan hoi
for (i = 0; i < n; i++) {
rtemp[i] = 0;
rtemp2[i] = 0;

}
19


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
//Tinh tong burst time
for (i = 0; i < n; i++)
sum_Burst_Time += p[i].Burst_Time;

// dat State = 0 cho tat ca tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < p[n].sum_Burst_Time; j++)
p[i].State[j] = 0;

for (i = 0; i < n; i++) {
p[i].PID = i;
p[i].temp_Burst_Time = p[i].Burst_Time;
}

BubbleSort_Arrive(p, n);

for (time = 0, i = 0; remain != 0;) {
if (p[i].temp_Burst_Time <= q && p[i].temp_Burst_Time > 0) {
for (k = time; k < time + p[i].temp_Burst_Time; k++)
p[i].State[k] = Run;
if (rtemp2[i] == 0) {
rtemp[i] = time;
rtemp2[i] = 1;

}
time = time + p[i].temp_Burst_Time;
p[i].temp_Burst_Time = 0;
flag = 1;
20


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
} else if (p[i].temp_Burst_Time > 0) {
p[i].temp_Burst_Time = p[i].temp_Burst_Time - q;
for (k = time; k < time + q; k++)
p[i].State[k] = Run;
if (rtemp2[i] == 0) {
rtemp[i] = time;
rtemp2[i] = 1;
}
time = time + q;

}
if (p[i].temp_Burst_Time == 0 && flag == 1) {
remain--;
p[i].Turnaround_Time = time - p[i].Arrive_Time;
p[i].Waiting_Time = time - p[i].Arrive_Time - p[i].Burst_Time;
flag = 0;
}
if (i == n - 1)
i = 0;
else if (p[i + 1].Arrive_Time <= time)

i++;
else i = 0;
}
for (i = 0; i < n; i++)
p[i].Response_Time = rtemp[i] - p[i].Arrive_Time;

for (i = 0; i < n; i++) {
sumWT += p[i].Waiting_Time;
21


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
sumTT += p[i].Turnaround_Time;
sumRT += p[i].Response_Time;
}

// thiet lap State cho tat ca cac tien trinh
for (i = 0; i < n; i++)
for (j = 0; j < sum_Burst_Time; j++)
if (j >= p[i].Arrive_Time && j < p[i].Turnaround_Time + p[i].Arrive_Time)
if (p[i].State[j] != Run)
p[i].State[j] = Wait;

// in output cua chuong trinh
printf("\n");
printf("********************************\n");
printf("DINH THOI ROUND ROBIN \n ");
Output(p, n, sum_Burst_Time);

printf("AVGW= %-2.2lf\t", (double) sumWT / (double) n);
printf("AVGR= %-2.2lf \t", (double) sumRT / (double) n);
printf("AVGT= %-2.2lf\t", (double) sumTT / (double) n);
printf("\n");

}
int main() {
Process p[MAX];
int i, j, n;
int sumWT = 0, sumTT = 0;
printf("Nhap so tong so tien trinh: ");
scanf("%d", & n);
22


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
if(n <1){
printf("So tien trinh phai >0");
return 0;
}
int temp[2 * n + 1];

for (i = 0; i < 2 * n + 1; i++)
scanf("%d", & temp[i]);

q = temp[0];
for (i = 1; i < 2 * n + 1; i++) {
if (i % 2 == 1)

p[i / 2].Arrive_Time = temp[i];
else p[i / 2 - 1].Burst_Time = temp[i];
}
if(q < 1) RR= 0;
for (i = 0; i < n; i++) {
p[i].PID = i + 1;
p[i].Waiting_Time = p[i].Turnaround_Time = 0;
}

// goi ham
FCFS(p, n);
SJF_khongTrungDung(p, n);
SJF_TrungDung(p, n);
Round_Robin(p, n, q);

return 0;
23


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
}
2. KẾT QUẢ

PHẦN 4: QUẢN LÝ BỘ NHỚ
1. SOURCE CODE
#include <stdio.h>
#include <math.h>
int BinToDec(long long binaryNum) {

int p = 0;
int decNumber = 0;
while (binaryNum > 0) {
decNumber += (binaryNum % 10) * pow(2, p);
++p;
binaryNum /= 10;
}
return decNumber;
}

24


CT178-Nguyên Lý Hệ Điều Hành

Truy cập />
long long Dec2Bin(int decimalNum) {
long long binaryNum = 0;
int p = 0;
while (decimalNum > 0) {
binaryNum += (decimalNum % 2) * pow(10, p);
++p;
decimalNum /= 2;
}
return binaryNum;
}
int QuanLy(int n, int pagesize) {
int p = n / pagesize;
int d = n % pagesize;
int khung = 0;

long long realBin = 0;
int phyA = 0;
switch (p) {
case 0:
khung = 6;
break;
case 1:
khung = 5;
break;
case 2:
khung = 4;
break;
case 3:
khung = 2;
break;
case 4:
khung = 7;
25


×