CP264 A6 Assignment
CP264 A6 Assignment
A6: C Structs
General Instructions:
1- Create a C project in eclipse called A6. Download A6.zip file and extract the
contents to the A6 project.
2- Rename: "A6_template.txt" to "A6.c". Enter your credentials on top of the file.
3- Use the file "A6_test.c” to test your solution. Compare your results to those
shown in "A6_output.txt. Before making your submission, verify that all your
outputs match the desired output.
4- When you are done, you need to submit only your "A6.c" file. Do not export the
project or upload a .zip file.
5- In your solution, you can use any of the following libraries: stdout, stdlib,
ctype, string, math and stdbool.
6- You do not have to implement the suggested utility functions. You may also
define your own utility functions. However, this should be done locally, i.e.,
inside A6.c.
Overview
In this assignment, you will experiment with C structs. You will work on full
implementation of a C structure called OSProcess which is a modified version of the
Process struct built during R7.
The term OSProcess stands for Operating System Process. Each osprocess will
have a name, an ID called PID, a priority, a state, and a list of resources.
The name is a string (array of characters) that has a maximum length equal to the
constant MPNL (Maximum Process Name Length). The additional character is for the
'\0' character.
The priority is a single character that is set to either 'R' which means regular
priority, 'H' which means high priority, or 'L' which means low priority. By default, an
osprocess is initialized to 'R'.
Any osprocess can have several resources ranging from 0 to 6. Having zero
resources, automatically puts the process into the idle state. The struct member
num_resources keeps track of how many items are currently available at the
resources array.
The function receives a name string, which is copied to the osprocess name. If the
given string is longer than MPNL, then it will be sliced to contain only MPNL characters.
otherwise.
The function needs to ensure that the added resource is a valid one, i.e., it is one of
the six ones defined in the table. This can be done through the utility function:
The above function returns True, if the given resource is already present in the
resources array in p. Otherwise, it returns False.
In summary, the function should only add a resource if it is valid and currently not
listed in the array.
Both functions return True if the operation is successful, and False otherwise.
The start function simply sets the state to running. This can be done if the current
state is idle, and there are some resources. A process cannot be started if it has
no resources.
On the other hand, a process can be stopped if it is having a running state. This can
be done regardless of the number of resources.
The function serves a process by granting it service to one of its requested resources.
If the service was successful, it returns True. If it fails, it returns False.
To serve a process, the state should be "running". An idle process cannot be served.
This also means, there needs to be at least one resource (because if there were no
resources, the process is automatically set to idle).
In addition, the function removes the served resource from the resources list. To
achieve that, you can implement the following utility function:
The above function removes the first resource in the array and shift the other
resources to the left.
Be careful of the scenario when there is only one resource. In this case, after offering
service, and removing the resource, the resources list is empty and therefore, the
process should be stopped, i.e., set to the idle state.