User Tools

Site Tools


Sidebar


Start


Teaching:

Feedback


os_cp:2025:prog_assignment

Your assignment is to implement a programming library for managing resources. Imagine a storage room where people can arrive with a list of items to take, and they are served first come-first served. If some items form their lists were taken by others, they simply wait until the items are returned. New categories (kinds) of items can always be added to the storage room. One can take a new set of items only after all previous items were returned.

Below, you are provided a header file with declarations of functions and the description of how they have to work. You must not modify the header file. For synchronization you must use the POSIX API.

Your code will be run against automated tests verifying example scenarios. Code that does not compile, or does not implement the functions from the header file, or uses API for synchronization other than the POSIX API will be graded negatively.

The solutions will be graded basing on the correctness of the code, use of fine-grained locking and correctness of memory management. Moreover, severe code illegibility, spaghetti code, code bloat and redundancy will lower the grade.

The deadline for the assignment is 01.06.2025 (AoE). Please send the solutions to me as a single attachment to an e-mail message with the subject beginning with [OSCP].

Please reach me by e-mail if anything there is not clear for you.

resources.h
#pragma once
 
/**
 * This function must be called before any other 'resources_*' function.
 * 
 * It may initialize data, but may as well be empty. It's up to you.
 */
void resources_init();
 
/**
 * Registers new kind of resource, and sets the number of items of this kind.
 * 
 * The function returns:
 *   • 0 on success
 *   • 1 if 'kind' is negative or 'availableCount' is nonpositive
 *   • 2 if resource of the provided 'kind' has already been created
 */
int resources_create(int kind, int availableCount);
 
/**
 * Takes resources according to specification. The 'what' argument is an array
 * of 'resources_request' structures, of length 'arrayLength'. Each element of
 * the array specifies a resource kind and the number of items to take.
 * 
 * This function waits until the resources become available.
 * 
 * Assigning resources must be done in first come - first served (first in -
 * first out) manner. That is, when a thread t₂ asked for the resources later
 * than t₁, then t₁ must not wait longer because of t₂ getting the resources
 * first. Notice that if it is possible to give resources to t₂ without causing
 * t₁ to wait longer, this has to be done.
 * 
 * A thread must not request resources it it still has some resources it did
 * not return.
 * 
 * The function returns:
 *   • 0 on success
 *   • 1 if array length is negative
 *   • 2 if the array has the same kind of resources on two indexes
 *   • 3 if the thread did not return some resourced that it took earlier
 *   • 4 if the thread requested a resource kind that was not yet created
 *   • 5 if the thread requested more resources of some kind than there exist
 */ 
struct resources_request {
    int kind;
    int count;
};
int resources_take(int arrayLength, struct resources_request *what);
 
/**
 * Returns a resource that was taken by the thread earlier on.
 * 
 * The resources can be returned in arbitrary order.
 * 
 * If multiple resources of the the same kind were taken, they may be returned
 * in portions.
 * 
 * The function returns:
 *   • 0 on success
 *   • 1 if the kind of resource does not exist
 *   • 2 if the thread did not take the resource it attempts to return
 *   • 3 if the thread attempts to return more resources than it took
 */
int resources_return(int kind, int count);

An example code that uses the library is provided here:

os_cp/2025/prog_assignment.txt · Last modified: 2025/05/08 00:57 by jkonczak