0% found this document useful (0 votes)
34 views6 pages

CP264 A6 Assignment

The document describes the tasks for an assignment on C structs representing operating system processes (OSProcesses). Students are to: 1. Create a function that allocates and initializes an OSProcess struct with default values. 2. Create functions to convert an OSProcess to a string and print the string. 3. Add a resource to an OSProcess if valid and not already present. 4. Start and stop an OSProcess by changing its state field. 5. Serve a running OSProcess by removing a resource. 6. Destroy an OSProcess by freeing its dynamic memory.

Uploaded by

Kimara Desanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

CP264 A6 Assignment

The document describes the tasks for an assignment on C structs representing operating system processes (OSProcesses). Students are to: 1. Create a function that allocates and initializes an OSProcess struct with default values. 2. Create functions to convert an OSProcess to a string and print the string. 3. Add a resource to an OSProcess if valid and not already present. 4. Start and stop an OSProcess by changing its state field. 5. Serve a running OSProcess by removing a resource. 6. Destroy an OSProcess by freeing its dynamic memory.

Uploaded by

Kimara Desanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CP264 Spring 2022 A6

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 OSProcess is defined in A6.h as the following:


typedef struct{
char name[MPNL+1]; //process name
char* state; //{"idle", "running"}
char priority; //'H'= high, 'R'= regular, 'L'= low
short num_resources; //number of requested resources
char resources[6][4]; //array of requested resources
unsigned long PID; //8 digits
}OSProcess;

Qutaiba Albluwi © 2022


CP264 Spring 2022 A6

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 state is a string that is either set to "idle" or "running". By default, an


osprocess is set to idle. The functions start_osprocess and
stop_osprocess will control the change of states.

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'.

The resources is an array of strings representing requested resources by the


osprocess. The following table defines the valid resources:

Resource Name Interpretation


CP CPU
NK Network
IN Input Resource (e.g., keyboard)
OU Output Resource (e.g., screen)
HD Hard disk
RM RAM

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.

Qutaiba Albluwi © 2022


CP264 Spring 2022 A6

Task 1: Create OSProcess [2 pts]

Implement the function:


OSProcess* create_osprocess(char* name){
The function creates and returns a pointer to an osprocess struct. The pointer is
created using dynamic memory allocation.

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.

The function also performs the following:

1- Creates and initializes the state to idle


2- Sets the priority to 'R'
3- Initializes all 6 resources to an empty string
4- Initializes num_resources to 0
5- Calls a private utility function called _get_UPID() to set the PID.

The _get_UPID() function generates an 8 digit PID and return it as an unsigned


long number. The lower4 and higher4 digits in the PID are controlled by two
separate static counters, both initialized to 1000. The lower4 counter is
incremented with every function call, while the second counter is incremented with
every 4 calls of the function.

Task 2: Printing an OSProcess [1.25 pts]

Implement the two functions:

char* osprocess_to_str(OSProcess *p)


void print_osprocess(OSProcess *p)

The first function generates a string representation of an OSProcess variable. The


following format is used:
Qutaiba Albluwi © 2022
CP264 Spring 2022 A6
<name>:(<priority>[<PID>:state]{<resources>}

The resources are separated by commas.

The second function prints the above string to the console.

Task 3: Adding a Resource [2 pts]

Implement the following function:

int add_resource_osprocess(OSProcess *p, char* resource)


The function adds the given resource to the list of resources in the given
OSProcess. The function returns True if the addition was successful, and False

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:

int _is_valid_resource(char* res)


The function should only add a resource if it does not currently exist. This means, a
resource cannot appear twice in the resources array. To achieve that, you may
implement a utility function called:

int _has_resource(OSProcess *p, char* resource)

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.

Task 4: Starting and Stopping an OSProcess [1.25 pts]

Implement the following two functions:


int start_osprocess(OSProcess *p)
int stop_osprocess(OSProcess *p)
Qutaiba Albluwi © 2022
CP264 Spring 2022 A6

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.

Task 5: Serving a Process [2 pts]

Implement the function:

int serve_osprocess(OSProcess *p)

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).

When serving, the first item in the array is always selected.

The function prints the following statement:

serving osprocess [<PID>] for <resource>\n

In addition, the function removes the served resource from the resources list. To
achieve that, you can implement the following utility function:

void _remove_osprocess_resource(OSProcess *p)

The above function removes the first resource in the array and shift the other
resources to the left.

Qutaiba Albluwi © 2022


CP264 Spring 2022 A6

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.

Task 6: Destroying an OSProcess [1.5 pts]

Implement the following function:


void destroy_osprocess(OSProcess ** p)
The function should destroy an osprocess by:
1- Setting all numbers to 0
2- Setting all strings to an empty string
3- Setting all pointers to NULL
4- Freeing any dynamically created variables

Qutaiba Albluwi © 2022

You might also like