Добавил:
інстаграм _roman.kob, курсові роботи з тєрєхова в.в. для КІ Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабораторна робота №7

.docx
Скачиваний:
1
Добавлен:
31.05.2020
Размер:
231.24 Кб
Скачать

Міністерство освіти і науки України

Технічний коледж НУ "Львівська політехніка"

Лабораторна робота №7 З ОС

Студент групи 31КІ Мартинюк Р.

Викладач

Різник О. Я.

Львів 2020

Мета: створення алгоритму організації сторінок за кількістю запитів, віком і пріоритетом з використанням алгоритму заміщення сторінок.

Етапи розробки:

  • створення в класі «введення даних» алгоритму введення розмірів і кількості запиту процесів процесу.

  • створення в трьох класах-організаторах алгоритмів поділу процесів і їх організації в пам’яті за параметром, вибраним користувачем. У програмі використовується алгоритм вибору сторінки жертви і її заміщення.

  • створення в класі «виведення результату» алгоритму виведення «вмісту оперативної пам’яті».

Принцип роботи:

  • При запуску програми на екран виводиться номер поточного процесу.

  • Після цього користувачу буде доступна для введення розмір процесу. Після введення розміру, буде доступним для введення поле «кількість запитів», «вік», «пріоритет».

  • Після введення останніх даних, програма дає на вибір три алгоритми, після вибору організовує дані у пам’яті.

  • Результат виводиться у вигляді списку.

Код програми:

public class Main { public static void main(String[] args) { UI ui = new UI(); ui.ui(); } }

public class DataBase { public int processNum; public int time; public int request; public int age; public int priority; public DataBase (int p, int t, int r, int a, int pr) { this.processNum = p; this.time = t; this.request = r; this.age = a; this.priority = pr; } }

import java.util.Scanner; public class getSomething { public int sth; public int get () { Scanner S = new Scanner(System.in); if (S.hasNextInt()) sth = S.nextInt(); else { System.out.println("Ви ввели не число"); sth = get(); } return sth; } }

public class getNumber extends getSomething { public int get_(int n) { int number; System.out.printf("Введіть номер програми %d:", n); number = get(); return number; } }

public class getQuant extends getSomething { public int get_quant() { int quant; System.out.print("\nВведіть квант часу (мс):"); quant = get(); return quant; } }

public class getRequest extends getSomething { public int get_(int n) { int request; System.out.printf("Введіть частоту запитів на квант часу №%d:", n); request = get(); return request; } }

public class getTime extends getSomething { public int get_(int n) { int time; System.out.printf("Введіть час виконання сторінки %d:", n); time = get(); return time; } }

public class getPriority extends getSomething { public int get_ (int n) { int priority; System.out.printf("%d.Введіть пріорітет:", n); priority = get(); return priority; } }

public class getAge { private static final Random r = new Random(); public int get_(int n) { int age; age = r.nextInt(100) + 0; return age; } }

import java.util.Scanner; public class getData { private Scanner S = new Scanner(System.in); private getTime gT = new getTime(); private getQuant gQ = new getQuant(); private getRequest gR = new getRequest(); private getAge gA = new getAge(); private getPriority gP = new getPriority(); public DataBase[] array = new DataBase[3]; public int quant; public int length = array.length-1; public DataBase rez1; void get() { int count; int i = 0; while (i <= length) { array[i] = new DataBase(i+1, gT.get_(i+1), gR.get_(i+1), gA.get_(i+1), gP.get_(i+1)); i++; System.out.println(); } rez1 = new DataBase(i+1, gT.get_(i+1), gR.get_(i+1), gA.get_(i+1), gP.get_(i+1)); System.out.println(); quant = gQ.get_quant(); } }

public class printOrganizedAge { private int generalTime = 0; private int twp; public void print_result (DataBase[] array, int length, DataBase rez1, int quant) { System.out.println("AGE"); DataBase rez2; int j = 0; DataBase empty = new DataBase(0,0,0,0,0); int age; int index = 0; for (int i = 0; i <= length; i++) generalTime += array[i].time; generalTime += rez1.time; while (generalTime > 0) { age = array[0].age; index = 0; for (int i = 0; i < length; i++) { if (array[i].age > age) { age = array[i].age; index = i; } } if (array[j].time != 0) { calcul_twp(j, array[j].time, quant); pr_str(array[j].processNum, twp, array[j].request, array[j].age, array[j].priority); array[j].time = calculate(array[j].time, quant); } else if (rez1 != empty) { array[j] = rez1; rez1 = empty; } if (j == length) {

if (rez1 != empty) { rez2 = array[index]; array[index] = rez1; rez1 = rez2; rez2 = empty; } j = 0; } else j++; } pr_str(array[j].processNum, twp, array[j].request, array[j].age, array[j].priority); } private void pr_str(int n, int t, int r, int a, int pr) { System.out.printf("Процес №%2d|час виконання=%3d|к-сть запитів=%3d|вік=%3d|пріоритет=%2d\n",n,t,r,a,pr); } private void calcul_twp(int j, int time, int quant) { if (time > quant) twp = quant; else if (time == quant) twp = quant; else if (time < quant) twp = time; } private int calculate (int time, int quant) { if (time > quant) { time -= quant; generalTime -= quant; } else if (time == quant) { time = 0; generalTime -= quant; } else if (time < quant) { generalTime -= time; time = 0; } return time; } }

public class printOrganizedPriority { private int generalTime = 0; private int twp; public void print_result (DataBase[] array, int length, DataBase rez1, int quant) { System.out.println("PRIORITY"); DataBase rez2; DataBase empty = new DataBase(0,0,0,0,0); DataBase[] output = new DataBase[4]; int i; for (i = 0; i<=length; i++) { output[i] = array[i]; } i=length+1; output[i] = rez1; int min = output[0].priority; DataBase temp; for (int k = 0; k<=length+2; k++) for (i = 0; i <= length; i++) { if (output[i].priority > output[i+1].priority) { temp = output[i]; output[i] = output[i+1]; output[i+1] = temp; } } for (i = 0; i < output.length; i++) generalTime += output[i].time; int j = 0; while (generalTime > 0) { calcul_twp(j, output[j].time, quant); if (twp != 0) pr_str(j+1, twp, output[j].request, output[j].age, output[j].priority); output[j].time = calculate(output[j].time, quant); if (j == output.length-1) j = 0; else j++; } pr_str(array[j].processNum, twp, array[j].request, array[j].age, array[j].priority); }

public class printOrganizedRequest { private int generalTime = 0; private int twp; public void print_result (DataBase[] array, int length, DataBase rez1, int quant) { System.out.println("REQUEST"); DataBase rez2; int j = 0; DataBase empty = new DataBase(0,0,0,0,0); int min; int index = 0; for (int i = 0; i <= length; i++) generalTime += array[i].time; generalTime += rez1.time; while (generalTime > 0) { min = array[0].request; index = 0; for (int i = 0; i <= length; i++) if (array[i].request < min) { min = array[i].request; index = i; } if (array[j].time != 0) { calcul_twp(j, array[j].time, quant); pr_str(array[j].processNum, twp, array[j].request, array[j].age, array[j].priority); array[j].time = calculate(array[j].time, quant); } else if (rez1 != empty) { array[j] = rez1; rez1 = empty; } if (j == length) {

if (rez1 != empty) { rez2 = array[index]; array[index] = rez1; rez1 = rez2; rez2 = empty; } j = 0; System.out.println(); } else j++; } pr_str(array[j].processNum, twp, array[j].request, array[j].age, array[j].priority); }

public class UI { private Scanner S = new Scanner(System.in); private getData d = new getData(); private printOrganizedAge pA = new printOrganizedAge(); private printOrganizedPriority pP = new printOrganizedPriority(); private printOrganizedRequest pR = new printOrganizedRequest(); public void ui() { d.get(); int repeater; System.out.print("\nВведіть оператор вибору: від 1 до 3;\n1 - організація за запитом\n"); System.out.print("2 - організація за пріорітетом\n"); System.out.print("3 - організація за віком сторінки\n<<"); if (S.hasNextInt()) { repeater=S.nextInt(); switch (repeater) { case 1: pR.print_result(d.array, d.length, d.rez1, d.quant); break; case 2: pP.print_result(d.array, d.length, d.rez1, d.quant); break; case 3: pA.print_result(d.array, d.length, d.rez1, d.quant); break; } } else System.out.println("Ви ввели не число"); } }

Результат: