CS3413/Assignment5/lib/process.c

40 lines
1.3 KiB
C
Raw Normal View History

2023-10-27 10:42:23 -03:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "process.h"
//Assume that the string passed in is null terminated
2023-10-27 11:24:40 -03:00
Process *createProcess(char *username, char job, int arrival_time, int duration, int affinity) {
2023-10-27 10:42:23 -03:00
// Allocate memory for the process, and check if it was successful
Process *node = calloc(1, sizeof(Process));
if (node == NULL) {
printf("Error allocating memory for process\n");
exit(EXIT_FAILURE);
}
// Free the data first, then allocate memory for the new data
node->username = calloc(strlen(username) + 1, sizeof(char));
if (node->username == NULL) {
printf("Error allocating memory for username data\n");
exit(EXIT_FAILURE);
}
// Copy data from the string passed in to the process's data
// Makes sure if the string passed in is changed, the process's data is not changed
// Also makes sure that if the data is on the stack, it is not freed at some other point
strcpy(node->username, username);
node->job = job;
node->arrival_time = arrival_time;
node->duration = duration;
node->next_elem = NULL;
2023-10-27 11:24:40 -03:00
node->prev_elem = NULL;
node->finish_time = 0;
node->affinity = affinity;
2023-10-27 10:42:23 -03:00
return node;
}
void destroyProcess(Process *node) {
// Free the data first, then free the process
free(node->username);
free(node);
}