Modestus Moon OS  R4
CS 450 project
pcb.c
Go to the documentation of this file.
1 #include <core/pcb.h>
2 
7 
8 
10 
11 
12 
14 
16 {
17 #ifndef LL_IS_ARRAY_BACKED
19 
20  pcb_t* temp = (pcb_t*)sys_alloc_mem(sizeof(pcb_t));
21  temp->representingNode = makeNewNode(0, (void*)temp);
22 
23  if(!temp || !temp->representingNode) { prevPCBError = ERROR_ALLOCATING_NODE; return 0; }
24 
25  return temp;
26 #endif
27 }
28 
33 {
35  if(pcbToFree)
36  {
37  prevPCBError = removePCB(pcbToFree);
38  if(prevPCBError != SUCCESS)
39  {
41  }
42  if(pcbToFree->representingNode)
43  {
44 
45  //prevPCBError = sys_free_mem(pcbToFree->representingNode);
46  if(prevPCBError != SUCCESS)
47  {
49  }
50  }
51  if(pcbToFree->stackBase)
52  {
53  //prevPCBError = sys_free_mem(pcbToFree->stackBottom);
54  if(prevPCBError != SUCCESS)
55  {
57  }
58  }
59 
60  //prevPCBError = sys_free_mem(pcbToFree);
61  }
62 
63  if(prevPCBError != SUCCESS)
64  {
66  }
67 
68  return prevPCBError = SUCCESS;
69 }
70 
71 pcb_t* setupPCB(const char* processName, e_PROCESS_CLASS_t processClass, processPriority_t processPriority)
72 {
74  int lengthOfName = strlen(processName);
75  if(lengthOfName >= PROCESS_MAX_NAME_LENGTH || lengthOfName <= 0 ) { prevPCBError = ERROR_NAME_INVALID; return 0; }
76  if(processPriority > 9) { prevPCBError = ERROR_PRIORITY_INVALID; return 0; }
77  if((processClass != SYSTEM) && (processClass != USER_APP)) { prevPCBError = ERROR_CLASS_INVALID; return 0; }
78 
79  pcb_t* newPCB = findPCB(processName);
80  if(newPCB){ prevPCBError = ERROR_NAME_COLLISION; return 0; }
81 
82  newPCB = allocatePCB();
83 
84  if(!newPCB) { prevPCBError = ERROR_ALLOCATING_NODE; return 0; }
85 
86  strcpy(newPCB->name, processName);
87  newPCB->processClass = processClass;
88  newPCB->priority = processPriority;
89  newPCB->processState = READY;
90  newPCB->processSuspensionState = SUSPENDED;
91  newPCB->stackBase= &newPCB->stack[0];
92  newPCB->stackTop = newPCB->stackBase+PROCESS_STACK_SIZE-sizeof(processContext_t);
93  newPCB->context = (processContext_t*)newPCB->stackTop;
94 
95  insertPCB(newPCB);
96 
97  return newPCB;
98 }
99 
100 pcb_t* findPCB(const char* processName)
101 {
103  node_t* temp;
104  temp = searchList(&readyQueue,(void*) processName);
105  if(temp)
106  {
107  return (pcb_t*)(temp->data);
108  }
109 
110  temp = searchList(&blockedQueue,(void*) processName);
111  if(temp)
112  {
113  return (pcb_t*)(temp->data);
114  }
115 
116  temp = searchList(&suspendedReadyQueue,(void*) processName);
117  if(temp)
118  {
119  return (pcb_t*)(temp->data);
120  }
121 
122  temp = searchList(&suspendedBlockedQueue,(void*) processName);
123  if(temp)
124  {
125  return (pcb_t*)(temp->data);
126  }
127 
129  return 0;
130 }
131 
132 void insertPCB(pcb_t* pcbToInsert)
133 {
135  if((pcbToInsert->processState == READY) &&
136  (pcbToInsert->processSuspensionState == NOT_SUSPENDED))
137  {
138  (void)insertNode(&readyQueue,pcbToInsert->representingNode);
139  return;
140  }
141  if((pcbToInsert->processState == BLOCKED) &&
142  (pcbToInsert->processSuspensionState == NOT_SUSPENDED))
143  {
144  (void)insertNode(&blockedQueue,pcbToInsert->representingNode);
145  return;
146  }
147  if((pcbToInsert->processState == READY) &&
148  (pcbToInsert->processSuspensionState == SUSPENDED))
149  {
150  (void)insertNode(&suspendedReadyQueue,pcbToInsert->representingNode);
151  return;
152  }
153  if((pcbToInsert->processState == BLOCKED) &&
154  (pcbToInsert->processSuspensionState == SUSPENDED))
155  {
156  (void)insertNode(&suspendedBlockedQueue,pcbToInsert->representingNode);
157  return;
158  }
160 }
161 
163 {
164  (void)removeNode(pcbToRemove->representingNode);
165  return prevPCBError=SUCCESS;
166 }
167 
168 e_PCB_ERROR_CODE_t changeProcessState(const char* processName, e_PROCESS_STATE_t newState)
169 {
171 
172  if (newState != INITIAL && newState != READY && newState != RUNNING &&
173  newState != BLOCKED && newState != TERMINAL)
174  {
176  return ERROR_STATE_INVALID;
177  }
178 
179  pcb_t* process = findPCB(processName);
180  if (!process) return ERROR_PCB_NOT_FOUND;
181 
182  process->processState = newState;
183 
184  removePCB(process);
185  insertPCB(process);
186 
187  return prevPCBError;
188 }
189 
191 {
193  if((suspensionState != SUSPENDED) &&
194  (suspensionState != NOT_SUSPENDED))
195  {
197  }
198 
199  pcb_t* process = findPCB(processName);
200  if(!process){ return prevPCBError = ERROR_PCB_NOT_FOUND; }
201 
202  process->processSuspensionState = suspensionState;
203 
204  removePCB(process);
205  insertPCB(process);
206 
207  return prevPCBError;
208 }
209 
210 e_PROCESS_STATE_t getState(const char* name)
211 {
213  pcb_t* process = findPCB(name);
214  return process->processState;
215 }
216 
217 int fifoInsertFunc(void* pcb1, void* pcb2)
218 {
219  (void)pcb1; (void)pcb2;
220  return -1;
221 }
222 
223 int priorityInsertFunc(void* pcb1, void* pcb2)
224 {
225  return ((pcb_t*)pcb2)->priority - ((pcb_t*)pcb1)->priority;
226 }
227 
228 int pcbSearchFunc(void* pcb, void* nameToFind)
229 {
230  return strcmp(((pcb_t*)pcb)->name, (char*)nameToFind);
231 }
232 
233 void initPCBQueues(void)
234 {
235  initLinkedList(&readyQueue);
236  initLinkedList(&blockedQueue);
237  initLinkedList(&suspendedReadyQueue);
238  initLinkedList(&suspendedBlockedQueue);
239 
242  setSearchComparisonFunction(&suspendedReadyQueue, &pcbSearchFunc);
243  setSearchComparisonFunction(&suspendedBlockedQueue, &pcbSearchFunc);
244 
245  setPrintFunction(&readyQueue, &printPCBFunc);
246  setPrintFunction(&blockedQueue, &printPCBFunc);
247  setPrintFunction(&suspendedReadyQueue, &printPCBFunc);
248  setPrintFunction(&suspendedBlockedQueue, &printPCBFunc);
249 
250 // setInsertComparisonFunction(&readyQueue, &priorityInsertFunc);
251 }
252 
253 void pcbTest(void)
254 {
255  printf("%s","PCB Queue Test\r\n\r\n");
256  int i;
257  for( i = 0; i < 30; i++)
258  {
259  char name[20];
260  sprintf(name, 20, "Proc %d", i);
261  setupPCB(name, SYSTEM, i/3);
262  }
263  printf("Ready Queue Contents%s","\r\n");
264  printList(&readyQueue);
265 }
266 
268 {
269  switch(error)
270  {
271  case SUCCESS: return "Success";
272  case ERROR: return "Error";
273  case ERROR_PCB_NOT_FOUND: return "Error: PCB not found";
274  case ERROR_NAME_INVALID: return "Error: name invalid";
275  case ERROR_PRIORITY_INVALID: return "Error: priority invalid";
276  case ERROR_FREEING_NODE: return "Error freeing node";
277  case ERROR_FREEING_STACK: return "Error freeing stack";
278  case ERROR_FREEING_PCB: return "Error freeing PCB";
279  case ERROR_REMOVING_PCB: return "Error removing PCB";
280  case ERROR_STATE_INVALID: return "Error: State Invalid";
281  case ERROR_CLASS_INVALID: return "Error: Class Invalid";
282  case ERROR_ALLOCATING_NODE: return "Error Allocating Node";
283  case ERROR_NAME_COLLISION: return "Error: PCB Name Already in Use";
284  case ERROR_INSERTING_PCB: return "Error: PCB States have resulted in invalid transition";
285  default: return "Error parsing error code";
286  }
287 }
288 
289 const char* classToString(e_PROCESS_CLASS_t processClass)
290 {
291  switch(processClass)
292  {
293  case SYSTEM: return "System";
294  case USER_APP: return "User-App";
295  case CLASS_UNKNOWN: return "Class Unknown";
296  default: return "Error parsing class code";
297  }
298  return "Error parsing class code";
299 }
300 
302 {
303  switch(state)
304  {
305  case INITIAL: return "Initial";
306  case READY: return "Ready";
307  case RUNNING: return "Running";
308  case BLOCKED: return "Blocked";
309  //case SUSPENDED_READY: return "Suspended-ready";
310  //case SUSPENDED_BLOCKED: return "Suspened-blocked";
311  case TERMINAL: return "Terminal";
312  case STATE_UNKNOWN: return "State unknown";
313  default: return "Error parsing state code";
314  }
315 }
316 
318 {
319  switch(state)
320  {
321  case SUSPENDED: return "Suspended";
322  case NOT_SUSPENDED: return "Not-Suspended";
323  default:
324  case SUSPENSION_UNKNOWN: return "Suspension-State-Unknown";
325  }
326 }
327 
328 e_PROCESS_CLASS_t stringToClass(const char * stringifiedClass)
329 {
330  if(!strcmp("System", stringifiedClass)){ return SYSTEM; }
331  if(!strcmp("User-App", stringifiedClass)){ return USER_APP; }
332  return CLASS_UNKNOWN;
333 }
334 
335 void printPCBFunc(void* pcb)
336 {
337  printf("PCB: %s | Priority: %d | Class: %s | State: %s | Suspension Status: %s\r\n",
338  ((pcb_t*)pcb)->name, ((pcb_t*)pcb)->priority, classToString(((pcb_t*)pcb)->processClass),
339  stateToString(((pcb_t*)pcb)->processState), suspensionToString(((pcb_t*)pcb)->processSuspensionState));
340 }
341 
343 {
345  if(newPriority > PROCESS_HIGHEST_PRIORITY)
346  {
347  return ERROR_PRIORITY_INVALID;
348  }
349  pcb_t* foundProcess = findPCB(procName);
350  if(!foundProcess)
351  {
352  return ERROR_PCB_NOT_FOUND;
353  }
354 
355  foundProcess->priority = newPriority;
356  return SUCCESS;
357 }
358 
int pcbSearchFunc(void *pcb, void *nameToFind)
pcbSearchFunc pcb search function for linked list. All pcb linkedLists use this function for searchin...
Definition: pcb.c:228
void * sys_alloc_mem(u32int size)
Definition: mpx_supt.c:33
Definition: pcb.h:54
e_PROCESS_SUSPENSION_STATE_t
Definition: pcb.h:49
Definition: pcb.h:47
Definition: pcb.h:47
typedef of linked list node (see s_ll_node).
void printPCBFunc(void *pcb)
printPCBFunc prints the status of a process
Definition: pcb.c:335
const char * errorToString(e_PCB_ERROR_CODE_t error)
errorToString creates a string form of the error passed in
Definition: pcb.c:267
void setPrintFunction(linkedList_t *list, void(*newPrintFunc)(void *))
sets the function whose job it is to print the list to the screen.
Definition: linked_list.c:207
#define PROCESS_MAX_NAME_LENGTH
Definition: pcb.h:24
Definition: pcb.h:49
int priorityInsertFunc(void *pcb1, void *pcb2)
priorityInsertFunc function that inserts pcbs into new pcb queues ordered by the pcbs&#39; priority...
Definition: pcb.c:223
const char * procName
Definition: mcb.h:43
void setSearchComparisonFunction(linkedList_t *list, int(*newSearchFunc)(void *, void *))
sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function.
Definition: linked_list.c:73
e_PCB_ERROR_CODE_t freePCB(pcb_t *pcbToFree)
freePCB free all associated memory with the PCB, including the stack and other pointers ...
Definition: pcb.c:32
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
void initLinkedList(linkedList_t *list)
initilize list and the optional array that backs the list
Definition: linked_list.c:3
pcb_t * setupPCB(const char *processName, e_PROCESS_CLASS_t processClass, processPriority_t processPriority)
setupPCB calls allocatePCB, initializes the PCB with the arguments and sets it state to ready ...
Definition: pcb.c:71
The s_processContext struct defines the context that each process stores .
Definition: system.h:71
e_PCB_ERROR_CODE_t changeProcessPriority(const char *procName, processPriority_t newPriority)
changeProcessPriority
Definition: pcb.c:342
const char * classToString(e_PROCESS_CLASS_t processClass)
classToString creates a string form of the class passed in
Definition: pcb.c:289
linkedList_t blockedQueue
Definition: pcb.c:4
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
typedef of linked list (see s_ll).
int fifoInsertFunc(void *pcb1, void *pcb2)
priorityInsertFunc function that inserts pcbs into new pcb queues ordered by the sequence of arrival...
Definition: pcb.c:217
linkedList_t suspendedReadyQueue
Definition: pcb.c:5
#define PROCESS_HIGHEST_PRIORITY
Definition: pcb.h:27
Definition: pcb.h:38
char * strcpy(char *s1, const char *s2)
strcpy copies one string to another string
Definition: string.c:26
#define PROCESS_STACK_SIZE
Definition: pcb.h:22
typedef for pcb_t struct
Definition: pcb.h:54
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
linkedList_t readyQueue
Definition: pcb.c:3
void initPCBQueues(void)
initPCBQueues initlize the queues for each process queue. This function calls the list init function ...
Definition: pcb.c:233
struct s_processContext processContext_t
Definition: system.h:65
node_t * makeNewNode(linkedList_t *list, void *data)
get new node, allocate a new node or find an unused node from the list&#39;s pool
Definition: linked_list.c:37
const char * stateToString(e_PROCESS_STATE_t state)
stateToString creates a string form of the state passed in
Definition: pcb.c:301
void insertPCB(pcb_t *pcbToInsert)
insertPCB insert the PCB into the queue represented by the process state, following each queue&#39;s rule...
Definition: pcb.c:132
Definition: pcb.h:47
e_PROCESS_STATE_t
The PROCESS_STATE_t enum defines the possible operating states of a process.
Definition: pcb.h:47
int strlen(const char *s)
strlen returns the length of a string
Definition: string.c:10
e_PROCESS_CLASS_t stringToClass(const char *stringifiedClass)
stringToClass returns the enum representation of a string
Definition: pcb.c:328
Definition: pcb.h:47
e_PCB_ERROR_CODE_t changeProcessSuspensionState(const char *processName, e_PROCESS_SUSPENSION_STATE_t suspensionState)
changeProcessSuspensionState
Definition: pcb.c:190
Definition: pcb.h:38
pcb_t * findPCB(const char *processName)
findPCB will search all queues for the PCB with the input name
Definition: pcb.c:100
e_PCB_ERROR_CODE_t
The e_PCB_ERROR_CODE_t enum defines the return status of functions working with PCBs.
Definition: pcb.h:38
e_PCB_ERROR_CODE_t prevPCBError
Definition: pcb.c:13
e_PROCESS_STATE_t getState(const char *name)
getState gets the current state of the process
Definition: pcb.c:210
e_PCB_ERROR_CODE_t changeProcessState(const char *processName, e_PROCESS_STATE_t newState)
changeProcessState changes the state of the process being called
Definition: pcb.c:168
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
const char * suspensionToString(e_PROCESS_SUSPENSION_STATE_t state)
Definition: pcb.c:317
int sprintf(char *str, int bufLength, const char *format,...) __attribute__((format(printf
sprintf print with format to specified string buffer
void pcbTest(void)
pcbTest a simple test case function to show pcb queue functionality
Definition: pcb.c:253
int strcmp(const char *s1, const char *s2)
strcmp compares two strings.
Definition: string.c:77
Definition: pcb.h:47
node_t * searchList(linkedList_t *listToSearch, void *data)
goes through the list using the list&#39;s comparison function to find the node which matches the data...
Definition: linked_list.c:154
e_PCB_ERROR_CODE_t removePCB(pcb_t *pcbToRemove)
removePCB remove the input PCB from its associated queue
Definition: pcb.c:162
e_PROCESS_CLASS_t
The PROCESS_CLASS_t enum defines the operating class of the process.
Definition: pcb.h:54
pcb_t * allocatePCB(void)
allocatePCB allocate new memory for a pcb_t
Definition: pcb.c:15
linkedList_t suspendedBlockedQueue
Definition: pcb.c:6
pcb_t * currentOperatingProcess
Definition: pcb.c:9
size_t processPriority_t
processPriority_t defines a process priority to the system
Definition: pcb.h:87