Assignment#02 Dsa
Assignment#02 Dsa
Instructions
Your assignment should represent your own effort. However, you are not expected to work alone. It is
fine to discuss the exercises and try to find solutions together, but each student shall write down and
submit his/her solutions separately. It is a good academic standard to acknowledge collaborators, so if
you worked together with other students, please list their names.
For a programming task, your solution must contain (i) an explanation of your solution to the problem,
(ii) the C++ code, in a form that we can run it, (iii) instructions on how to run it.
For all programming tasks, it is not allowed to use any external libraries if not stated otherwise.
Please, include your name, student ID, and email address in your submission.
Plagiarism will lead to -100% marks (any type of code sharing is not allowed).
Submissions after Dec 18 (11:30 pm) will get zero. Any discussion/excuses regarding deadlines
will lead to -50% marks.
Your program will take all the jobs as input and store them in a Queue (insertion at the end/tail).
The program will get the first job (extract from the start/head), and send it to the CPU. CPU executes it
for 2 seconds and then returns the job to the controller function (main function). The execution time of
the job is reduced by 2. For example, if the job execution time is 7 before execution, then the job
execution time will be updated to 5 after execution (as the CPU executes the job for 2 seconds).
When the job completed its execution time (2s), the controller function checks the updated execution
time. If the execution time is greater than 0, it will be inserted at the end of the queue. Otherwise, no
need to insert it into the queue (it is complete now).
Example:
Time before start 3 (A) 8 (B) 1 (C) The queue is filled in the start with job ids
(A, B, and C) and execution time (3, 8, and
1)
Job-A returns from 8 (B) 1 (C) 1 (A) The job-A execution time is updated to 1.
the CPU The controller function checks the time. It is
greater than 0, so the controller function
added it again into the queue.
Job-B returns from 1 (C) 1 (A) 6 (B) The job-A execution time is updated to 6.
the CPU The controller function checks the time. It is
greater than 0, so the controller function
added it again into the queue.
Job-C returns from 1 (A) 6 (B) The job-C execution time is updated to 0 (or
the CPU - 1). The controller function checks the time.
It is not greater than 0, so the controller
function will not add it to the queue. You
can make an extra queue that stores the
completed jobs for tracking purposes.
Job-A returns from 6 (B) The job-A execution time is updated to 0 (or
the CPU - 1). The controller function checks the time.
It is not greater than 0, so the controller
function will not add it to the queue. You
can make an extra queue that stores the
completed jobs for tracking purposes.
Q no 2
Now change your implementation for Priority Queue. The queue contains the processes/jobs/tasks that
have ID and priority.
Job Details: ID and Priority
Your program will take all the jobs as input and store them in a Queue.
The implementation of the priority queue will be changed as you are not inserting the tasks at the end.
The tasks will be inserted according to their priorities. For example, we have a queue that has three
tasks with priorities equal to 1,1,5. The new task has been entered with priority 3. The queue will be
1,1,3,5 now. 3 will be inserted between 1 and 5.
All the tasks that have the same priorities will be scheduled according to the First-Come-First-Serve
base technique. So, for this, you can add one more parameter “arrival time” as job details.
When any task is sent to the CPU, it will be executed completely. It will never go back to the queue. It
means that your program is just doing the scheduling while adding the tasks to a queue. If the order of
the insertion of the tasks is correct, you will be able to execute the tasks in the correct way.
Q no 3
Think about the problem with Q no 2. If any task with high priority required too much time, the
priority queue allows it for running till its completion. The other tasks have to wait for a long time.
Any solution?
Think before reading the solution below.
The solution to avoid long waiting:
Combine the above two strategies (round-robin, priority).
Job Details: ID, Priority, Execution-Time, Arrival-Time
Hybrid Strategy:
The tasks will be inserted according to their priorities (priority case).
The execution will be changed now. Every task will be executed on time according to its priority. The
highest priority task will get CPU for more time as compared to the task that has low priority. For
example, if 3 tasks are in the queue. The execution times of these tasks are 15, 7, and 4 and the
priorities are 4, 3, 1. The task with priority 4 will get more time as compared to the task that has
priority equal to 3. You can use some formula to assign the CPU on the basis of priority.
AIM: Make sure that the highest-priority tasks have more CPU time than low-priority tasks. The
wait for low-priority tasks should be as minimum as possible.
The tasks that have high priorities will be partly/completely executed first and then the low priorities
tasks will be executed.
Note: The system you developed must be able to print the queue status with details (ID, Time) after
every time the controller gets the control from the CPU (after each execution, the print function will
be called that prints the updated execution time, and other details).
Submission Instruction:
Your submission should contain separate files (.cpp) for all parts. All the functionalities of stacks
should be added to their corresponding file (applies same on Queue as well). Do not make separate
files for different functionalities of any part. The folder should be in the following format (Roll-No,
First-Name, for example, 201111_XXXX, do not insert special characters with roll number or name).
The file name of part-a should be 201111_XXXX_a.cpp.
Combine the solutions of all parts (part-i, and part-ii) into a single folder named Assignment_02.
Assignment_02 folder will contain 2 sub-folders, one for each part. The first folder will contain the
solutions of part-i (stacks implementation, 3 files), while the second folder will contain the solutions of
algebraic expression (1 file) and Queue parts (3 files). Naming conventions should be followed for
each folder.
Please follow the instruction strictly to avoid any inconvenience
Best of Luck and Happy Coding 😊