├── FCFS ├── README.md ├── generatepid.c ├── graphics.c ├── main.c └── struct.h ├── License ├── PBS ├── README.md ├── generatepid.c ├── graphics.c ├── main.c └── struct.h ├── README.md ├── RR ├── README.md ├── generatepid.c ├── graphics.c ├── main.c └── struct.h ├── SJF ├── README.md ├── generatepid.c ├── graphics.c ├── main.c └── struct.h └── scripts └── pre-commit.sample /FCFS/README.md: -------------------------------------------------------------------------------- 1 | ## First Come First Server CPU scheduling algorithm 2 | 3 | ## Output 4 | ![Image] (http://i.gyazo.com/57762883b8da4a91f9505165b8be7aef.png) 5 | -------------------------------------------------------------------------------- /FCFS/generatepid.c: -------------------------------------------------------------------------------- 1 | /* This randomly genrates the Process IDS */ 2 | 3 | #include "struct.h" 4 | #include 5 | #include 6 | #include 7 | 8 | extern struct process pr[20]; 9 | extern struct process pr2[20]; 10 | void sorting(int, struct process *, int); 11 | void displaygraphics(int, struct process *); 12 | void generatepid(int n) { 13 | int myRand(int r) { 14 | int a = 1; 15 | int b = 100; 16 | return (r % (a - b) + a); 17 | } 18 | int i; 19 | unsigned int inputseed; 20 | inputseed = (unsigned int)time(NULL); 21 | srand(inputseed); 22 | for (i = 0; i < n; ++i) { 23 | pr[i].at = myRand(rand()); 24 | pr[i].bt = myRand(rand()); 25 | } 26 | for (i = 0; i < n; i++) { 27 | pr[i].pid = i + 1; 28 | } 29 | for (i = 0; i < n; i++) { 30 | pr2[i].bt = pr[i].bt; 31 | pr2[i].at = pr[i].at; 32 | pr2[i].pid = pr[i].pid; 33 | } 34 | displaygraphics(n, pr); 35 | } 36 | -------------------------------------------------------------------------------- /FCFS/graphics.c: -------------------------------------------------------------------------------- 1 | /* This is responsible for the grpahics of the FCFS algorithm */ 2 | 3 | #include "struct.h" 4 | #include 5 | #include 6 | 7 | extern struct process pr2[20]; 8 | extern struct process pr[20]; 9 | void sorting(int, struct process *, int); 10 | 11 | void displaygraphics(int n, struct process *pr1) { 12 | int gd = DETECT, gm = 100800; 13 | int x, y, i, j; 14 | struct process *base = pr1; 15 | 16 | char m[] = "FCFS"; 17 | char p[100]; 18 | char id[50]; 19 | initgraph(&gd, &gm, 0); 20 | moveto(0, 0); 21 | x = getmaxx(); 22 | y = getmaxy(); 23 | setcolor(BLUE); 24 | rectangle(0, 0, x, y); 25 | line(0, y / 3, x, y / 3); 26 | setfontcolor(RED); 27 | 28 | for (i = 0; i < n; ++i, ++base) { 29 | sprintf(p, "Process %d", i + 1); 30 | outtextxy(x / n * i, 20, p); 31 | sprintf(p, "ID %5u", base->pid); 32 | outtextxy(x / n * i, 50, p); 33 | sprintf(p, "AT%5u", base->at); 34 | outtextxy(x / n * i, 60, p); 35 | sprintf(p, "BT %5u", base->bt); 36 | outtextxy(x / n * i, 70, p); 37 | line(x / n * i, 0, x / n * i, y / 3); 38 | } 39 | outtextxy(x / 3, 5 + y / 3, m); 40 | setcolor(BLUE); 41 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 42 | sorting(n, pr1, 20); 43 | for (i = 0; i < n; i++) { 44 | for (j = 0; j < n; j++) { 45 | if ((pr1[i].at) == (pr2[j].at)) { 46 | setcolor(BLUE); 47 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 48 | sprintf(id, "pid%d", pr2[j].pid); 49 | outtextxy(x / 3 + 30, y / 3 + 55, id); 50 | delay(2000); 51 | setfontcolor(BLUE); 52 | } 53 | } 54 | setfontcolor(RED); 55 | } 56 | 57 | setcolor(BLUE); 58 | line(0, 2 * y / 3, x, 2 * y / 3); 59 | for (i = 0; i < n; i++) { 60 | for (j = 0; j < n; j++) { 61 | if ((pr1[i].at) == (pr2[j].at)) { 62 | sprintf(id, "pid%d", pr2[j].pid); 63 | outtextxy(x / n * i, 2 * (y / 3) + 50, id); 64 | delay(2000); 65 | } 66 | } 67 | line(x / n * i, 2 * y / 3, x / n * i, y); 68 | } 69 | delay(2000); 70 | outtextxy(x - 150, y - 20, "Tapasweni Pathak"); 71 | while (!kbhit()) 72 | ; 73 | closegraph(); 74 | } 75 | 76 | // sorting 77 | void sorting(int n, struct process pr1[], int a) { 78 | int temp, i, j; 79 | for (i = 1; i < n; i++) { 80 | temp = pr1[i].at; 81 | j = i - 1; 82 | while (temp < pr1[j].at && j >= 0) { 83 | pr1[j + 1].at = pr1[j].at; 84 | j = j - 1; 85 | } 86 | pr1[j + 1].at = temp; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /FCFS/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This contains the main function. 3 | * This calls the generatepid function, which generates random process ids. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | int n; 10 | extern void generatepid(int a); 11 | extern void generateBT(int b); 12 | extern void generateAT(int c); 13 | extern void generatepriority(int c); 14 | int main() { 15 | printf("Enter the no. of processes: "); 16 | scanf("%d", &n); 17 | generatepid(n); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /FCFS/struct.h: -------------------------------------------------------------------------------- 1 | /* This contains the declaration of structure, having 2 | * the details of the processes. 3 | */ 4 | 5 | struct process 6 | { 7 | int pid; //Process ID 8 | int at; //Process Arival Time 9 | int bt; //Process Burst Time 10 | }pr[20], pr2[20]; 11 | 12 | -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Tapasweni Pathak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /PBS/README.md: -------------------------------------------------------------------------------- 1 | Priority Based CPU Scheduling Algorithm 2 | ====== 3 | 4 | ## Output 5 | ![Image] (http://i.gyazo.com/78fa658941f881270eb21ac471457246.png) 6 | -------------------------------------------------------------------------------- /PBS/generatepid.c: -------------------------------------------------------------------------------- 1 | #include "struct.h" 2 | #include 3 | #include 4 | #include 5 | extern struct process pr[20]; 6 | extern struct process pr2[20]; 7 | void sorting(int, struct process *, int); 8 | void displaygraphics(int, struct process *); 9 | 10 | // Function generating values 11 | void generatepid(int n) { 12 | int myRand(int r) { 13 | int a = 1; 14 | int b = 100; 15 | return (r % (a - b) + a); 16 | } 17 | int i; 18 | unsigned int inputseed; 19 | inputseed = (unsigned int)time(NULL); 20 | srand(inputseed); 21 | for (i = 0; i < n; ++i) { 22 | pr[i].at = myRand(rand()); 23 | pr[i].bt = myRand(rand()); 24 | pr[i].Priority = myRand(rand()); 25 | } 26 | for (i = 0; i < n; i++) { 27 | pr[i].pid = i + 1; 28 | } 29 | for (i = 0; i < n; i++) { 30 | pr2[i].bt = pr[i].bt; 31 | pr2[i].at = pr[i].at; 32 | pr2[i].pid = pr[i].pid; 33 | pr2[i].Priority = pr[i].Priority; 34 | } 35 | displaygraphics(n, pr); 36 | } 37 | -------------------------------------------------------------------------------- /PBS/graphics.c: -------------------------------------------------------------------------------- 1 | #include "struct.h" 2 | #include 3 | #include 4 | extern struct process pr2[20]; 5 | extern struct process pr[20]; 6 | void sorting(int, struct process *, int); 7 | 8 | void displaygraphics(int n, struct process *pr1) { 9 | int gd = DETECT, gm = VGAMAX; 10 | int x, y, i, j; 11 | struct process *base = pr1; 12 | char m[] = "PBS"; 13 | char p[100]; 14 | char id[50]; 15 | initgraph(&gd, &gm, 0); 16 | moveto(0, 0); 17 | x = getmaxx(); 18 | y = getmaxy(); 19 | setcolor(BLUE); 20 | rectangle(0, 0, x, y); 21 | line(0, y / 3, x, y / 3); 22 | setfontcolor(RED); 23 | for (i = 0; i < n; ++i, ++base) { 24 | sprintf(p, "Process %d", i + 1); 25 | outtextxy(x / n * i, 20, p); 26 | sprintf(p, "ID %5u", base->pid); 27 | outtextxy(x / n * i, 50, p); 28 | sprintf(p, "AT%5u", base->at); 29 | outtextxy(x / n * i, 60, p); 30 | sprintf(p, "BT %5u", base->bt); 31 | outtextxy(x / n * i, 70, p); 32 | sprintf(p, "Priority %5u", base->Priority); 33 | outtextxy(x / n * i, 90, p); 34 | line(x / n * i, 0, x / n * i, y / 3); 35 | } 36 | // Second Block of Display 37 | outtextxy(x / 3, 5 + y / 3, m); 38 | setcolor(BLUE); 39 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 40 | sorting(n, pr1, 20); 41 | for (i = 0; i < n; i++) { 42 | for (j = 0; j < n; j++) { 43 | if ((pr1[i].Priority) == (pr2[j].Priority)) { 44 | setcolor(BLUE); 45 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 46 | sprintf(id, "pid%d", pr2[j].pid); 47 | outtextxy(x / 3 + 30, y / 3 + 55, id); 48 | delay(2000); 49 | setfontcolor(BLUE); 50 | } 51 | } 52 | setfontcolor(RED); 53 | } 54 | 55 | setcolor(BLUE); 56 | line(0, 2 * y / 3, x, 2 * y / 3); 57 | // Third Block of Graphics 58 | for (i = 0; i < n; i++) { 59 | for (j = 0; j < n; j++) { 60 | if ((pr1[i].Priority) == (pr2[j].Priority)) { 61 | sprintf(id, "pid%d", pr2[j].pid); 62 | outtextxy(x / n * i, 2 * (y / 3) + 50, id); 63 | delay(2000); 64 | } 65 | } 66 | line(x / n * i, 2 * y / 3, x / n * i, y); 67 | } 68 | delay(2000); 69 | outtextxy(x - 150, y - 20, "Tapasweni Pathak"); 70 | while (!kbhit()) 71 | ; 72 | closegraph(); 73 | } 74 | 75 | // Sort 76 | void sorting(int n, struct process pr1[], int a) { 77 | int temp, i, j; 78 | for (i = 1; i < n; i++) { 79 | temp = pr1[i].Priority; 80 | j = i - 1; 81 | while (temp < pr1[j].Priority && j >= 0) { 82 | pr1[j + 1].Priority = pr1[j].Priority; 83 | j = j - 1; 84 | } 85 | pr1[j + 1].Priority = temp; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /PBS/main.c: -------------------------------------------------------------------------------- 1 | /* File having main function */ 2 | 3 | #include 4 | #include 5 | #include 6 | int n; 7 | extern void generatepid(int a); // Generates Process ids randomly 8 | extern void generateBT(int b); // Generates Burst Time randomly 9 | extern void generateAT(int c); // Generates Arrival Time randomly 10 | extern void generatepriority(int c); // Generates Priorities randomly 11 | int main() { 12 | printf("Enter the no. of processes : "); 13 | scanf("%d", &n); 14 | generatepid(n); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /PBS/struct.h: -------------------------------------------------------------------------------- 1 | struct process{ 2 | int pid; //Process IDs 3 | int at; //Arival Time 4 | int bt; //Burst Time 5 | int Priority; //Priority 6 | }pr[20],pr2[20]; 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://img.shields.io/badge/-Visualization%20of%20CPU%20Scheduling%20Algorithms-blueviolet.svg) 2 | 3 | Visualization of CPU Scheduling Algorithms 4 | ====== 5 | [![GitHub issues](https://img.shields.io/github/issues/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms.svg)](https://github.com/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms/issues) 6 | [![GitHub forks](https://img.shields.io/github/forks/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms.svg)](https://github.com/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms/network) 7 | [![GitHub stars](https://img.shields.io/github/stars/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms.svg)](https://github.com/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms/stargazers) 8 | [![GitHub license](https://img.shields.io/github/license/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms.svg)](https://github.com/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms/blob/master/License) 9 | [![Twitter](https://img.shields.io/twitter/url/https/github.com/tapaswenipathak/Visualization-of-CPU-Scheduling-Algorithms.svg?label=Visualization-of-CPU-Scheduling-Algorithms&style=social)](https://twitter.com/intent/tweet?text=Visualization%20of%20CPUScheduling%20Algorithm:&url=https%3A%2F%2Fgithub.com%2Ftapaswenipathak%2FVisualization-of-CPU-Scheduling-Algorithms) 10 | 11 | 12 | ## Code structure 13 | 14 | 1. `struct.h`: This has structure of the process which has process ID, arrival time, burst time and struct object. Create struct object in this file if you need more process objects. 15 | 16 | 2. `generatepid.c`: This generates and assigns process ids, arrival time, burst time. 17 | 18 | 3. `main.c`: This file is used to let the user provide the number of processes which 19 | is later used in other files. 20 | 21 | 4. `graphics.c`: This has basic implementation of visualization using C graphics library [graphics.h](http://www.programmingsimplified.com/c/graphics.h). 22 | To add implementation of any new CPU scheduling algorithm: 23 | * Change the name of the algorithm 24 | * Manipulate the sorting algorithm 25 | 26 | ## Instructions 27 | To use this code you need to install some packages and libraries which will enable you to compile **graphics.h** program in Linux. 28 | 29 | 0. `sudo apt-get install build-essential` 30 | 31 | 1. For Ubuntu 16.04: `sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 guile-2.0-dev libsdl1.2debian-all libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev` 32 | 33 | 2. Download [libgraph](http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz). 34 | 35 | ```bash 36 | 37 | tar -xvzf libgraph-1.0.2 38 | 39 | cd libgraph-1.0.2 40 | 41 | ./configure 42 | 43 | sudo make 44 | 45 | sudo make install 46 | 47 | sudo cp /usr/local/lib/libgraph.* /usr/lib 48 | 49 | ``` 50 | 51 | ## Compile and Run 52 | 0. Compile and run the source code files. 53 | 54 | ```bash 55 | 56 | gcc generatepid.c struct.h graphics.c main.c -lgraph 57 | 58 | ./a.out 59 | 60 | ``` 61 | 62 | 1. Enter the number of process and view how these CPU scheduling algorithm works. Everything will be genrated randomly. 63 | 64 | -------------------------------------------------------------------------------- /RR/README.md: -------------------------------------------------------------------------------- 1 | ## Round Robin CPU scheduling algorithm. 2 | 3 | ## Output 4 | -------------------------------------------------------------------------------- /RR/generatepid.c: -------------------------------------------------------------------------------- 1 | #include "struct.h" 2 | #include 3 | #include 4 | #include 5 | 6 | extern struct process pr[20]; 7 | extern struct process pr2[20]; 8 | void sorting(int, struct process *, int); 9 | void displaygraphics(int, struct process *); 10 | void generatepid(int n) { 11 | int myRand(int r) { 12 | int a = 1; 13 | int b = 100; 14 | return (r % (a - b) + a); 15 | } 16 | int i; 17 | unsigned int inputseed; 18 | inputseed = (unsigned int)time(NULL); 19 | srand(inputseed); 20 | for (i = 0; i < n; ++i) { 21 | pr[i].at = myRand(rand()); 22 | pr[i].bt = myRand(rand()); 23 | } 24 | for (i = 0; i < n; i++) { 25 | pr[i].pid = i + 1; 26 | } 27 | for (i = 0; i < n; i++) { 28 | pr2[i].bt = pr[i].bt; 29 | pr2[i].at = pr[i].at; 30 | pr2[i].pid = pr[i].pid; 31 | } 32 | displaygraphics(n, pr); 33 | } 34 | -------------------------------------------------------------------------------- /RR/graphics.c: -------------------------------------------------------------------------------- 1 | /* This is responsible for the graphics of the SJF algorithm */ 2 | 3 | #include "struct.h" 4 | #include 5 | #include 6 | 7 | extern struct process pr2[20]; 8 | extern struct process pr[20]; 9 | void sorting(int, struct process *, int); 10 | 11 | void displaygraphics(int n, struct process *pr1) { 12 | int gd = DETECT, gm = 100800; 13 | int x, y, i, j; 14 | struct process *base = pr1; 15 | 16 | char m[] = "FCFS"; 17 | char p[100]; 18 | char id[50]; 19 | initgraph(&gd, &gm, 0); 20 | moveto(0, 0); 21 | x = getmaxx(); 22 | y = getmaxy(); 23 | setcolor(BLUE); 24 | rectangle(0, 0, x, y); 25 | line(0, y / 3, x, y / 3); 26 | setfontcolor(RED); 27 | 28 | for (i = 0; i < n; ++i, ++base) { 29 | sprintf(p, "Process %d", i + 1); 30 | outtextxy(x / n * i, 20, p); 31 | sprintf(p, "ID %5u", base->pid); 32 | outtextxy(x / n * i, 50, p); 33 | sprintf(p, "AT%5u", base->at); 34 | outtextxy(x / n * i, 60, p); 35 | sprintf(p, "BT %5u", base->bt); 36 | outtextxy(x / n * i, 70, p); 37 | line(x / n * i, 0, x / n * i, y / 3); 38 | } 39 | outtextxy(x / 3, 5 + y / 3, m); 40 | setcolor(BLUE); 41 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 42 | sorting(n, pr1, 20); 43 | for (i = 0; i < n; i++) { 44 | for (j = 0; j < n; j++) { 45 | if ((pr1[i].at) == (pr2[j].at)) { 46 | setcolor(BLUE); 47 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 48 | sprintf(id, "pid%d", pr2[j].pid); 49 | outtextxy(x / 3 + 30, y / 3 + 55, id); 50 | delay(2000); 51 | setfontcolor(BLUE); 52 | } 53 | } 54 | setfontcolor(RED); 55 | } 56 | 57 | setcolor(BLUE); 58 | line(0, 2 * y / 3, x, 2 * y / 3); 59 | for (i = 0; i < n; i++) { 60 | for (j = 0; j < n; j++) { 61 | if ((pr1[i].at) == (pr2[j].at)) { 62 | sprintf(id, "pid%d", pr2[j].pid); 63 | outtextxy(x / n * i, 2 * (y / 3) + 50, id); 64 | delay(2000); 65 | } 66 | } 67 | line(x / n * i, 2 * y / 3, x / n * i, y); 68 | } 69 | delay(2000); 70 | outtextxy(x - 150, y - 20, "Tapasweni Pathak"); 71 | while (!kbhit()) 72 | ; 73 | closegraph(); 74 | } 75 | 76 | // sorting 77 | void sorting(int n, struct process pr1[], int a) { 78 | int quantum = 79 | 2; // is there a paper on how to calculate better quantum dynamically? 80 | 81 | int res_seq[n]; 82 | for (int i = 0; i < n; ++i) { 83 | res_seq[i] = pr1[i].bt; 84 | } 85 | int cur_time = 0; 86 | 87 | while (true) { 88 | bool complete = true; 89 | for (int i = 1; i < n; ++i) { 90 | if (temp_bt[i] > 0) { 91 | complete = false; 92 | if (res_seq[i] > quantum) { 93 | cur_time += quantum; 94 | res_seq[i] -= quantum; 95 | } else { 96 | cur_time += res_sq[i]; 97 | res_seq[i] = 0; 98 | } 99 | } 100 | } 101 | if (complete) { 102 | break; 103 | } 104 | } 105 | 106 | for (int i = 1; i < n; ++i) { 107 | pr1[i].bt = res_seq[res[seq[i]]]; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /RR/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This contains the main function. 3 | * This calls the generatepid function, which generates random process ids. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | int n; 10 | extern void generatepid(int a); 11 | extern void generateBT(int b); 12 | extern void generateAT(int c); 13 | extern void generatepriority(int c); 14 | int main() { 15 | printf("Enter the no. of processes: "); 16 | scanf("%d", &n); 17 | generatepid(n); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /RR/struct.h: -------------------------------------------------------------------------------- 1 | /* This contains the declaration of structure, having 2 | * the details of the processes. 3 | */ 4 | 5 | struct process 6 | { 7 | int pid; //Process ID 8 | int at; //Process Arival Time 9 | int bt; //Process Burst Time 10 | }pr[20], pr2[20]; 11 | 12 | -------------------------------------------------------------------------------- /SJF/README.md: -------------------------------------------------------------------------------- 1 | ## Shortes Job First CPU scheduling algorithm. 2 | 3 | ## Output 4 | -------------------------------------------------------------------------------- /SJF/generatepid.c: -------------------------------------------------------------------------------- 1 | #include "struct.h" 2 | #include 3 | #include 4 | #include 5 | 6 | extern struct process pr[20]; 7 | extern struct process pr2[20]; 8 | void sorting(int, struct process *, int); 9 | void displaygraphics(int, struct process *); 10 | void generatepid(int n) { 11 | int myRand(int r) { 12 | int a = 1; 13 | int b = 100; 14 | return (r % (a - b) + a); 15 | } 16 | int i; 17 | unsigned int inputseed; 18 | inputseed = (unsigned int)time(NULL); 19 | srand(inputseed); 20 | for (i = 0; i < n; ++i) { 21 | pr[i].at = myRand(rand()); 22 | pr[i].bt = myRand(rand()); 23 | } 24 | for (i = 0; i < n; i++) { 25 | pr[i].pid = i + 1; 26 | } 27 | for (i = 0; i < n; i++) { 28 | pr2[i].bt = pr[i].bt; 29 | pr2[i].at = pr[i].at; 30 | pr2[i].pid = pr[i].pid; 31 | } 32 | displaygraphics(n, pr); 33 | } 34 | -------------------------------------------------------------------------------- /SJF/graphics.c: -------------------------------------------------------------------------------- 1 | /* This is responsible for the graphics of the SJF algorithm */ 2 | 3 | #include "struct.h" 4 | #include 5 | #include 6 | 7 | extern struct process pr2[20]; 8 | extern struct process pr[20]; 9 | void sorting(int, struct process *, int); 10 | 11 | void displaygraphics(int n, struct process *pr1) { 12 | int gd = DETECT, gm = 100800; 13 | int x, y, i, j; 14 | struct process *base = pr1; 15 | 16 | char m[] = "FCFS"; 17 | char p[100]; 18 | char id[50]; 19 | initgraph(&gd, &gm, 0); 20 | moveto(0, 0); 21 | x = getmaxx(); 22 | y = getmaxy(); 23 | setcolor(BLUE); 24 | rectangle(0, 0, x, y); 25 | line(0, y / 3, x, y / 3); 26 | setfontcolor(RED); 27 | 28 | for (i = 0; i < n; ++i, ++base) { 29 | sprintf(p, "Process %d", i + 1); 30 | outtextxy(x / n * i, 20, p); 31 | sprintf(p, "ID %5u", base->pid); 32 | outtextxy(x / n * i, 50, p); 33 | sprintf(p, "AT%5u", base->at); 34 | outtextxy(x / n * i, 60, p); 35 | sprintf(p, "BT %5u", base->bt); 36 | outtextxy(x / n * i, 70, p); 37 | line(x / n * i, 0, x / n * i, y / 3); 38 | } 39 | outtextxy(x / 3, 5 + y / 3, m); 40 | setcolor(BLUE); 41 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 42 | sorting(n, pr1, 20); 43 | for (i = 0; i < n; i++) { 44 | for (j = 0; j < n; j++) { 45 | if ((pr1[i].at) == (pr2[j].at)) { 46 | setcolor(BLUE); 47 | bar(x / 3 + 20, y / 3 + 30, x / 3 + 100, y / 3 + 100); 48 | sprintf(id, "pid%d", pr2[j].pid); 49 | outtextxy(x / 3 + 30, y / 3 + 55, id); 50 | delay(2000); 51 | setfontcolor(BLUE); 52 | } 53 | } 54 | setfontcolor(RED); 55 | } 56 | 57 | setcolor(BLUE); 58 | line(0, 2 * y / 3, x, 2 * y / 3); 59 | for (i = 0; i < n; i++) { 60 | for (j = 0; j < n; j++) { 61 | if ((pr1[i].at) == (pr2[j].at)) { 62 | sprintf(id, "pid%d", pr2[j].pid); 63 | outtextxy(x / n * i, 2 * (y / 3) + 50, id); 64 | delay(2000); 65 | } 66 | } 67 | line(x / n * i, 2 * y / 3, x / n * i, y); 68 | } 69 | delay(2000); 70 | outtextxy(x - 150, y - 20, "Tapasweni Pathak"); 71 | while (!kbhit()) 72 | ; 73 | closegraph(); 74 | } 75 | 76 | // sorting 77 | void sorting(int n, struct process pr1[], int a) { 78 | int temp, i, j; 79 | for (i = 1; i < n; i++) { 80 | temp = pr1[i].bt; 81 | j = i - 1; 82 | while (temp < pr1[j].bt && j >= 0) { 83 | pr1[j + 1].bt = pr1[j].bt; 84 | j = j - 1; 85 | } 86 | pr1[j + 1].bt = temp; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /SJF/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This contains the main function. 3 | * This calls the generatepid function, which generates random process ids. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | int n; 10 | extern void generatepid(int a); 11 | extern void generateBT(int b); 12 | extern void generateAT(int c); 13 | extern void generatepriority(int c); 14 | int main() { 15 | printf("Enter the no. of processes: "); 16 | scanf("%d", &n); 17 | generatepid(n); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /SJF/struct.h: -------------------------------------------------------------------------------- 1 | /* This contains the declaration of structure, having 2 | * the details of the processes. 3 | */ 4 | 5 | struct process 6 | { 7 | int pid; //Process ID 8 | int at; //Process Arival Time 9 | int bt; //Process Burst Time 10 | }pr[20], pr2[20]; 11 | 12 | -------------------------------------------------------------------------------- /scripts/pre-commit.sample: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export PATH=`git rev-parse --git-dir`/../tools/:$PATH 4 | 5 | git clang-format --extensions c,cpp,hpp --style file -v 6 | 7 | exec < /dev/tty 8 | --------------------------------------------------------------------------------