├── .gitignore ├── Project.exe ├── Report.docx ├── ReportPDF.pdf ├── Screenshots ├── 1.Display.PNG ├── 3.Output.PNG └── 2.Enter Input.PNG ├── .github └── workflows │ └── compiler.yml ├── README.md ├── TestCases.md ├── LICENSE └── Project.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /Project.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/Project.exe -------------------------------------------------------------------------------- /Report.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/Report.docx -------------------------------------------------------------------------------- /ReportPDF.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/ReportPDF.pdf -------------------------------------------------------------------------------- /Screenshots/1.Display.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/Screenshots/1.Display.PNG -------------------------------------------------------------------------------- /Screenshots/3.Output.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/Screenshots/3.Output.PNG -------------------------------------------------------------------------------- /Screenshots/2.Enter Input.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garvit-joshi/OS_Scheduling/HEAD/Screenshots/2.Enter Input.PNG -------------------------------------------------------------------------------- /.github/workflows/compiler.yml: -------------------------------------------------------------------------------- 1 | name: Windows 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: windows-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | 12 | - name: Setup MSVC 13 | uses: ilammy/msvc-dev-cmd@v1 14 | 15 | - name: build 16 | run: | 17 | cl 18 | cl /EHsc Project.cpp 19 | - name: Upload Executables(x64) 20 | uses: actions/upload-artifact@v2 21 | with: 22 | name: Application 23 | path: | 24 | D:/a/Main.exe 25 | - name: Commit change & Push 26 | run: | 27 | git config user.name 'github-actions[bot]' 28 | git config user.email '41898282+github-actions[bot]@users.noreply.github.com' 29 | git commit -am "bot: Updated Binaries" 30 | git push 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OS_Scheduling 2 | My GitHub Repository for Operating System Assignment-Lovely Professional University(LPU,Phagwara,Punjab) 3 | 4 | ## Question 7: 5 | Design a scheduling program to implements a Queue with two levels.
6 | Level 1 : Fixed priority preemptive Scheduling,
7 | Level 2 : Round Robin Scheduling
8 | For a Fixed priority preemptive Scheduling (Queue 1), the Priority 0 is highest priority. If one process P1 is scheduled and running , another process P2 with higher priority comes. The New process (high priority) process P2 preempts currently running process P1 and process P1 will go to second level queue. Time for which process will strictly execute must be considered in the multiples of 2. 9 | All the processes in second level queue will complete their execution according to round robin scheduling. 10 | ### Consider: 11 | 1. Queue 2 will be processed after Queue 1 becomes empty.
12 | 2. Priority of Queue 2 has lower priority than in Queue 1.
13 | 14 | ### Notes: 15 | Project.exe is compiled using MSVC, and automatically updates after every push. 16 | -------------------------------------------------------------------------------- /TestCases.md: -------------------------------------------------------------------------------- 1 | # Test Case #1: 2 | 3 | ## Input: 4 | | PID | Priority | Arrival Time | Burst Time | 5 | |----- |---------- |-------------- |------------ | 6 | | 1 | 1 | 2 | 3 | 7 | | 2 | 4 | 5 | 6 | 8 | | 3 | 0 | 8 | 9 | 9 | | 4 | 1 | 2 | 3 | 10 | | 5 | 4 | 2 | 6 | 11 | 12 | Enter Time Quantum(Multiples Of two):2
13 | Input Processes 14 | 15 | ## Output: 16 | 17 | | PID | Priority | Arrival Time | Burst Time | Completion Time | TurnAround Time | Waiting Time | Response Time | 18 | |----- |---------- |-------------- |------------ |----------------- |----------------- |-------------- |--------------- | 19 | | 1 | 1 | 2 | 3 | 5 | 3 | 0 | 2 | 20 | | 2 | 4 | 5 | 6 | 29 | 24 | 18 | 19 | 21 | | 3 | 0 | 8 | 9 | 17 | 9 | 0 | 8 | 22 | | 4 | 1 | 2 | 3 | 8 | 6 | 3 | 5 | 23 | | 5 | 4 | 2 | 6 | 27 | 25 | 19 | 17 | 24 | 25 | All Processes Completed In 29 unit time.
26 | Output 27 | 28 | ## Status: 29 | ### Passed 30 | ### You Are Free To find bugs 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Project.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************************************** 2 | *Design a scheduling program to implements a Queue with two levels: * 3 | *Level 1 : Fixed priority preemptive Scheduling * 4 | *Level 2 : Round Robin Scheduling * 5 | *For a Fixed priority preemptive Scheduling (Queue 1), the Priority 0 is highest priority. * 6 | *If one process P1 is scheduled and running , another process P2 with higher priority comes. * 7 | *The New process (high priority) process P2 preempts currently running process P1 and process * 8 | *P1 will go to second level queue. Time for which process will strictly execute must be * 9 | *considered in the multiples of 2. All the processes in second level queue will complete their * 10 | *execution according to round robin scheduling. * 11 | *Consider: * 12 | *1. Queue 2 will be processed after Queue 1 becomes empty. * 13 | *2. Priority of Queue 2 has lower priority than in Queue 1. * 14 | *****************************************************************************************************/ 15 | #include 16 | 17 | #include 18 | 19 | #include 20 | 21 | #include //Sleep() function 22 | 23 | using namespace std; 24 | 25 | long max_arrival = -1, min_arrival = LONG_MAX, warning = 1; //Global Variable 26 | 27 | /* 28 | max_arrival will store the maximim arrival time of a process 29 | min_arrival will store the minimul arrival time of a process 30 | warning will store the no. of warning that came in during 31 | program execution(due to user input and constraints) 32 | */ 33 | struct Process 34 | { 35 | long pid = 0; //Process ID 36 | long priority = 0; //the Priority 0 is highest priority 37 | long arrival_time = 0; //Time At Which Process Came 38 | long burst_time = 0; //The Total Time for which process should run 39 | long completion_time = 0; //Time at which CPU completed the whole process 40 | long turnaround_time = 0; //Turn_Around_Time=Completetion_Time-Arrival_Time 41 | long waiting_time = 0; //Waiting_Time=Turn_Around_Time-Burst_Time 42 | long response_time = 0; //RT=CPU got Process first time-Arrival Time 43 | long remaining_time = 0; //Time For Which Process Is Remaining to be Executed 44 | long CPUtime = -1; //Stores When Process got CPU for first time 45 | }; 46 | 47 | vector ready_queue; //for round robin(stores all the process Index no. for which remaining time is left) 48 | 49 | bool comparison_Priority(Process a, Process b) //Driver Function-Sorting According to Priority 50 | { 51 | return (a.priority < b.priority); 52 | } 53 | 54 | bool comparison_ArrivalTime(Process a, Process b) //Driver Function-Sorting According to Arrival Time(Acending Order) 55 | { 56 | return (a.arrival_time < b.arrival_time); 57 | } 58 | 59 | bool comparison_PID(Process a, Process b) //Driver Function-Sorting According to PID(Acending Order) 60 | { 61 | return (a.pid < b.pid); 62 | } 63 | 64 | bool comparison_RemainingTime(Process a, Process b) //Driver Function-Sorting According to Remaining Time(Acending Order) 65 | { 66 | return (a.remaining_time < b.remaining_time); 67 | } 68 | /* 69 | The Above Four Functions Are Used As A Parameter In sort() functions. 70 | They act as helping functions to sort the process according to our need 71 | */ 72 | 73 | long display(bool prompt = false) 74 | { 75 | /* 76 | Display Function Used for displaying the question at the starting of program 77 | */ 78 | time_t now = time(0); 79 | char *dt = ctime(&now); 80 | cout << dt; //Displays Date And Time Of System 81 | cout << "\n\n\n"; 82 | cout << "\t\t || ||\n"; 83 | cout << "\t\t=================================================================================================\n"; 84 | cout << "\t\t || Operating System Scheduling ||\n"; 85 | cout << "\t\t || --Garvit Joshi ||\n"; 86 | cout << "\t\t || ||\n"; 87 | cout << "\t\t ||/*Design a scheduling program to implements a Queue with two levels. Level 1 : Fixed ||\n"; 88 | cout << "\t\t || priority preemptive Scheduling. Level 2 : Round Robin Scheduling For a Fixed priority ||\n"; 89 | cout << "\t\t || the Priority 0 is highest priority. If one process P1 is scheduled and running, another||\n"; 90 | cout << "\t\t || process P2 with higher priority comes. The New process (high priority) process P2 ||\n"; 91 | cout << "\t\t || preempts currently running process P1 and process P1 will go to second level queue. ||\n"; 92 | cout << "\t\t || Time for which process will strictly execute must be considered in the multiples of 2. ||\n"; 93 | cout << "\t\t || All the processes in second level queue will complete their execution according to ||\n"; 94 | cout << "\t\t || round robin scheduling. */ ||\n"; 95 | cout << "\t\t || ||\n"; 96 | cout << "\t\t=================================================================================================\n"; 97 | cout << "\t\t || /*CONSIDER*/ ||\n"; 98 | cout << "\t\t || 1.Queue 2 will be processed after Queue 1 becomes empty. ||\n"; 99 | cout << "\t\t || 2.Priority of Queue 2 has lower priority than in Queue 1. ||\n"; 100 | cout << "\t\t || ||\n"; 101 | cout << "\t\t=================================================================================================\n"; 102 | cout << "\t\t || ||\n"; 103 | if (prompt == false) 104 | { 105 | cout << "Please Wait While Program Loads . . . "; 106 | Sleep(5000); 107 | system("CLS"); 108 | display(true); //Recursion 109 | return 0; 110 | } 111 | cout << "\n"; 112 | cout << "Program Successfully Loaded\n"; 113 | system("pause"); 114 | system("CLS"); 115 | return 0; 116 | } 117 | 118 | long Enter_Process(long &temp, vector &p, long i) 119 | { 120 | /* 121 | Function To Enter All Processes. This Function will be called as much as 122 | time the number of Proccess. 123 | */ 124 | cout << "Process:" << i + 1; 125 | temp++; //Variable Gives Unique PID(Process ID) To each Process 126 | p[i].pid = temp; 127 | cout << "\nEnter Priority:"; 128 | cin >> p[i].priority; 129 | while (p[i].priority < 0) 130 | { 131 | cout << "\t\t\t\tWarning " << warning << ": A Process Cannot Have Priority In Negative.\n"; 132 | cout << "Please Enter Priority Again:"; 133 | cin >> p[i].priority; 134 | warning++; 135 | } 136 | cout << "Enter Arrival Time:"; 137 | cin >> p[i].arrival_time; 138 | while (p[i].arrival_time < 0) 139 | { 140 | cout << "\t\t\t\tWarning " << warning << ": A Process Cannot Have Arrival Time In Negative.\n"; 141 | cout << "Please Enter Arrival Time Again:"; 142 | cin >> p[i].arrival_time; 143 | warning++; 144 | } 145 | if (p[i].arrival_time < min_arrival) 146 | { 147 | /* 148 | Calculating Minimum Arrival time 149 | */ 150 | min_arrival = p[i].arrival_time; 151 | } 152 | if (p[i].arrival_time > max_arrival) 153 | { 154 | /* 155 | Calculating Maximum Arrival Time 156 | */ 157 | max_arrival = p[i].arrival_time; 158 | } 159 | cout << "Enter Burst Time:"; 160 | cin >> p[i].burst_time; 161 | while (p[i].burst_time < 0) 162 | { 163 | cout << "\t\t\t\tWarning " << warning << ": A Process Cannot Have Burst Time In Negative.\n"; 164 | cout << "Please Enter Burst Time Again:"; 165 | cin >> p[i].burst_time; 166 | warning++; 167 | } 168 | p[i].remaining_time = p[i].burst_time; 169 | cout << "====================================================\n"; 170 | return 0; 171 | } 172 | 173 | long Show_Process(vector &p, long n, bool b = false) 174 | { 175 | if (b == false) 176 | { 177 | /* 178 | By Default This Conditional Statement Will Work, 179 | It Will Only Show PID,Priority,Arrival Time,Burst Time 180 | */ 181 | cout << "\nPID || Priority || Arrival Time || Burst Time\n"; 182 | for (long i = 0; i < n; i++) 183 | { 184 | cout << p[i].pid << "\t" << p[i].priority << "\t\t" << p[i].arrival_time << "\t\t" << p[i].burst_time << "\n"; 185 | } 186 | } 187 | else if (b == true) 188 | { 189 | /* 190 | This Works when the function call is called with a third 191 | parameter which must be true 192 | */ 193 | cout << "\nPID || Priority || Arrival Time || Burst Time || Completion Time || TurnAround Time || Waiting Time || Response Time\n"; 194 | for (long i = 0; i < n; i++) 195 | { 196 | cout << p[i].pid << "\t" << p[i].priority << "\t\t" << p[i].arrival_time << "\t\t" << p[i].burst_time << "\t\t" << p[i].completion_time << "\t\t" << p[i].turnaround_time << "\t\t" << p[i].waiting_time << "\t\t" << p[i].CPUtime << "\n"; 197 | } 198 | } 199 | return 0; 200 | } 201 | 202 | long calculation(vector &p, long n) 203 | { 204 | /* 205 | Function Calculates TurnAround Time,Waiting Time, 206 | Response Time. 207 | */ 208 | for (long i = 0; i < n; i++) 209 | { 210 | if (p[i].burst_time == 0) 211 | { 212 | p[i].turnaround_time = 0; 213 | p[i].waiting_time = 0; 214 | p[i].response_time = 0; 215 | } 216 | else 217 | { 218 | p[i].turnaround_time = p[i].completion_time - p[i].arrival_time; 219 | p[i].waiting_time = p[i].turnaround_time - p[i].burst_time; 220 | p[i].response_time = p[i].CPUtime - p[i].arrival_time; 221 | } 222 | } 223 | return 0; 224 | } 225 | 226 | long FPPS(vector &p, long n, long &time) 227 | { 228 | /* 229 | Fixed Priority Preemtive Scheduling:Processes are 230 | Executed in the oreder of there priority 231 | Less Priority Number=More Priority For That Process 232 | */ 233 | system("CLS"); 234 | if (n == 1) 235 | { 236 | /* 237 | If No Of Processes is One OS we have o just execute 238 | it in FPPS. 239 | */ 240 | time = p[0].arrival_time + p[0].burst_time; 241 | p[0].completion_time = time; 242 | p[0].CPUtime = p[0].arrival_time; 243 | return 0; 244 | } 245 | time = min_arrival; 246 | sort(p.begin(), p.end(), comparison_Priority); 247 | sort(p.begin(), p.end(), comparison_ArrivalTime); 248 | long min_priority, k, current, small_priority_index; 249 | while (time <= max_arrival) 250 | { 251 | long small_priority = LONG_MAX; 252 | for (long i = 0; i < n; i++) 253 | { 254 | /* 255 | loop to find how many processes are in ready queue. 256 | */ 257 | if (p[i].arrival_time <= time) 258 | { 259 | current = i; 260 | continue; 261 | } 262 | else 263 | { 264 | /* 265 | Value of current signifies the processes index 266 | which can be executed in the CPU. 267 | */ 268 | break; 269 | } 270 | } 271 | long s = 0; 272 | while (s <= current) 273 | { 274 | /* 275 | Loop Finds Out The Smallest Priority Of The Current 276 | Ready Processes 277 | */ 278 | if (p[s].priority < small_priority && p[s].remaining_time != 0) 279 | { 280 | small_priority = p[s].priority; 281 | small_priority_index = s; 282 | } 283 | s++; 284 | } 285 | /* 286 | Executes The Process for 1 unit time 287 | */ 288 | p[small_priority_index].remaining_time--; 289 | if (p[small_priority_index].CPUtime == -1) 290 | { 291 | /* 292 | This Conditional Statement tells 293 | what was the time when the process 294 | was first time executed. 295 | */ 296 | p[small_priority_index].CPUtime = time; 297 | } 298 | time++; 299 | if (p[small_priority_index].remaining_time == 0) 300 | { 301 | /* 302 | Saves the time when a process was fully executed 303 | */ 304 | p[small_priority_index].completion_time = time; 305 | } 306 | } 307 | /* 308 | The Statement Below This Comment Executes a last partially 309 | Running Process and then exits the function 310 | */ 311 | long remaining_time = p[small_priority_index].remaining_time; 312 | if (p[small_priority_index].remaining_time == 0) 313 | { 314 | /* 315 | As Remaining Time is 0,So No Advantage of 316 | going further 317 | */ 318 | return 0; 319 | } 320 | p[small_priority_index].remaining_time = 0; 321 | if (p[small_priority_index].CPUtime == -1) 322 | { 323 | /* 324 | This Conditional Statement Gives 325 | tells what was the time when the process 326 | was first time executed. 327 | */ 328 | p[small_priority_index].CPUtime = time; 329 | } 330 | time += remaining_time; 331 | if (p[small_priority_index].remaining_time == 0) 332 | { 333 | /* 334 | Gives the time when a process was fully executed 335 | */ 336 | p[small_priority_index].completion_time = time; 337 | } 338 | return 0; 339 | } 340 | 341 | long Round_Robin(vector &p, long n, long tq, long &time) //Round Robin Scheduling 342 | { 343 | if (n == 1) 344 | { 345 | /* 346 | If there is only one process, the Process has been executed in FPPS 347 | */ 348 | return 0; 349 | } 350 | /*Round Robin Scheduling*/ 351 | long start = -1, remaining_time = -1, cur = -1; 352 | sort(p.begin(), p.end(), comparison_RemainingTime); //sort according to Remaining_time 353 | for (long i = 0; i < n; i++) 354 | { 355 | /* 356 | Finds the index of Process which does 357 | not have remaining time as 0. 358 | */ 359 | if (p[i].remaining_time == 0) 360 | { 361 | continue; 362 | } 363 | else 364 | { 365 | start = i; 366 | break; 367 | } 368 | } 369 | sort(p.begin() + start, p.begin() + n, comparison_ArrivalTime); //sort according to Remaining_time 370 | for (long i = 0; i < n; i++) 371 | { 372 | if (p[i].remaining_time == 0) 373 | { 374 | /* 375 | If A Process Has Remaining time as zero 376 | We take a partially running process from 377 | ready_queue and execute it 378 | */ 379 | if (!ready_queue.empty()) 380 | { 381 | cur = ready_queue[0]; 382 | ready_queue.erase(ready_queue.begin()); 383 | if (p[cur].remaining_time <= tq) 384 | { 385 | /* 386 | If remaining time is less then or equal to 387 | time quantum, then execute the whole process 388 | */ 389 | remaining_time = p[cur].remaining_time; 390 | p[cur].remaining_time = 0; 391 | time += remaining_time; 392 | p[cur].completion_time = time; 393 | } 394 | else 395 | { 396 | /* 397 | If remaining time is more then time quantum, 398 | then execute the process for time quantum 399 | and then store it in ready_queue 400 | */ 401 | p[cur].remaining_time -= tq; 402 | time += tq; 403 | ready_queue.push_back(cur); 404 | } 405 | } 406 | continue; 407 | } 408 | else 409 | { 410 | if (p[i].arrival_time <= time) 411 | { 412 | if (p[i].remaining_time <= tq) 413 | { 414 | /* 415 | If remaining time is less then or equal to 416 | time quantum, then execute the whole process 417 | */ 418 | remaining_time = p[i].remaining_time; 419 | p[i].remaining_time = 0; 420 | if (p[i].CPUtime == -1) 421 | { 422 | /* 423 | This Conditional Statement Gives 424 | tells what was the time when the process 425 | was first time executed. 426 | */ 427 | p[i].CPUtime = time; 428 | } 429 | time += remaining_time; 430 | } 431 | else 432 | { 433 | /* 434 | If remaining time is more then time quantum, 435 | then execute the process for time quantum 436 | and then store it in ready_queue 437 | */ 438 | p[i].remaining_time -= tq; 439 | if (p[i].CPUtime == -1) 440 | { 441 | /* 442 | This Conditional Statement Gives 443 | tells what was the time when the process 444 | was first time executed. 445 | */ 446 | p[i].CPUtime = time; 447 | } 448 | time += tq; 449 | ready_queue.push_back(i); 450 | } 451 | } 452 | else 453 | { 454 | if (!ready_queue.empty()) 455 | { 456 | cur = ready_queue[0]; 457 | ready_queue.erase(ready_queue.begin()); 458 | if (p[cur].remaining_time <= tq) 459 | { 460 | /* 461 | If remaining time is less then or equal to 462 | time quantum, then execute the whole process 463 | */ 464 | remaining_time = p[cur].remaining_time; 465 | p[cur].remaining_time = 0; 466 | time += remaining_time; 467 | p[cur].completion_time = time; 468 | } 469 | else 470 | { 471 | /* 472 | If remaining time is more then time quantum, 473 | then execute the process for time quantum 474 | and then again store it in ready_queue 475 | */ 476 | p[cur].remaining_time -= tq; 477 | time += tq; 478 | ready_queue.push_back(cur); 479 | } 480 | } 481 | } 482 | } 483 | } 484 | while (!ready_queue.empty()) 485 | { 486 | /* 487 | Executes all the processes in ready queue 488 | */ 489 | cur = ready_queue[0]; 490 | ready_queue.erase(ready_queue.begin()); 491 | if (p[cur].remaining_time <= tq) 492 | { 493 | /* 494 | If remaining time is less then or equal to 495 | time quantum, then execute the whole process 496 | */ 497 | remaining_time = p[cur].remaining_time; 498 | p[cur].remaining_time = 0; 499 | time += remaining_time; 500 | p[cur].completion_time = time; 501 | } 502 | else 503 | { 504 | /* 505 | If remaining time is more then time quantum, 506 | then execute the process for time quantum 507 | and then again store it in ready_queue 508 | */ 509 | p[cur].remaining_time -= tq; 510 | time += tq; 511 | ready_queue.push_back(cur); 512 | } 513 | } 514 | return 0; 515 | } 516 | 517 | int main() 518 | { 519 | display(); 520 | /* 521 | Just Comment the above function call if you are testing the code: 522 | this function may take upto more then 5 seconds. 523 | */ 524 | long n = 0, temp = 0, time_q = 0, time = 0; 525 | cout << "\t\t\tOperating System Scheduling\n\t\t\t\t\t\t-Garvit Joshi\n"; 526 | cout << "Enter No. Of Processes:"; 527 | cin >> n; 528 | while (n <= 0) 529 | { 530 | cout << "\t\t\t\tWarning " << warning << ": Number Of Processes Cannot Be less Then or Equal to 0.\n"; 531 | cout << "Please Enter No. Of Processes Again:"; 532 | cin >> n; 533 | warning++; 534 | } 535 | vector p(n); 536 | cout << "====================================================\n"; 537 | for (long i = 0; i < n; i++) 538 | { 539 | Enter_Process(temp, p, i); 540 | } 541 | cout << "Successfully Added The Process:"; 542 | Show_Process(p, n); 543 | cout << "Enter Time Quantum(Multiples Of Two):"; 544 | cin >> time_q; 545 | while (time_q % 2 != 0 || time_q < 2) 546 | { 547 | /* 548 | Time Quantum Should Be In Multiples Of Two 549 | */ 550 | cout << "\t\t\t\tWarning " << warning << ": Time Quantum Should Be In Multiples Of Two (Positive):\n"; 551 | cout << "Enter Time In Multiples Of 2:"; 552 | cin >> time_q; 553 | warning++; 554 | } 555 | FPPS(p, n, time); //Fixed Priority Preemtive Scheduling 556 | Round_Robin(p, n, time_q, time); //Round Robin Scheduling 557 | calculation(p, n); 558 | sort(p.begin(), p.end(), comparison_PID); 559 | Show_Process(p, n, true); 560 | cout << "\n"; 561 | cout << "All Process Completed In " << time << " unit time.\n\n"; 562 | system("pause"); 563 | return 0; 564 | } --------------------------------------------------------------------------------