CS3413/Assignment4/lib/process.c

37 lines
1.2 KiB
C
Raw Permalink Normal View History

2023-10-24 13:50:05 -03:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "process.h"
//Assume that the string passed in is null terminated
Process *createProcess(char *username, char job, int arrival_time, int duration) {
// 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;
return node;
}
void destroyProcess(Process *node) {
// Free the data first, then free the process
free(node->username);
free(node);
}