QuestionQuestion

Please see Question.pdf

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "clock.h"
#include "structs.h"
#include "constants.h"
#include "scheduler.h"

/* Called when the program terminates.
* You may use this to clean up memory or for any other purpose.
*/
void scheduler_exit(Schedule *schedule) {
scheduler_free(schedule);
return;
}

/* Initialize the Schedule Struct
* Follow the specification for this function.
* Returns a pointer to the new Schedule or NULL on any error.
*/
Schedule *scheduler_init() {

Schedule *schedule = (Schedule *) malloc(sizeof(Schedule));

if (schedule == NULL) {
    return NULL;
}

List *ready_list = (List *) malloc(sizeof(List));

if (ready_list == NULL) {
    return NULL;
}
ready_list->head = NULL;
ready_list->count = 0;

List *stopped_list = (List *) malloc(sizeof(List));
if (stopped_list == NULL) {
    return NULL;
}
stopped_list->head = NULL;
stopped_list->count = 0;

List *defunct_list = (List *) malloc(sizeof(List));
if (defunct_list == NULL) {
    return NULL;
}
defunct_list->head = NULL;
defunct_list->count = 0;

schedule->ready_list = ready_list;
schedule->stopped_list = stopped_list;
schedule->defunct_list = defunct_list;

return schedule;
}

int insert_into_list(List *lst, Process *process) {
    if (lst != NULL) {      
       Process *tmp = lst->head;
       Process *prev = NULL;   
       int done = 0;
       // Adding the process in ascending order of pid
       while (tmp != NULL) {
          if(tmp->pid > process->pid) {
            if(prev == NULL) {
             process->next = tmp;
             lst->head = process;
             done = 1;
             break;
            } else {
             prev->next = process;
             process->next = tmp;
             done = 1;
             break;
            }
          }
          prev = tmp;
          tmp = tmp->next;
       }

       if(done == 0) {
          if (prev != NULL) { // Reached at the end
            prev->next = process;
          } else { // List is empty
            lst->head = process;
          }
       }
       lst->count += 1;
       return 0;
    } else {
      return -1;
    }
}

/* Add the process into the appropriate linked list.
* Follow the specification for this function.
* Returns a 0 on success or a -1 on any error.
*/
int scheduler_add(Schedule *schedule, Process *process) {
if (process == NULL || schedule == NULL) {
    return -1;
}
if ((process->flags & STATE_CREATED) == STATE_CREATED) {
    int mask = 1 << 0;
    process->flags = (process->flags & ~mask) | ((0 << 0) & mask);
    process->flags = process->flags | STATE_READY;

    List *ready_list = schedule->ready_list;
    return insert_into_list(ready_list, process);

} else if ((process->flags & STATE_READY) == STATE_READY) {
    if (process->time_remaining > 0) {
      List *ready_list = schedule->ready_list;
      return insert_into_list(ready_list, process);
    } else if (process->time_remaining == 0){
      int mask = 1 << 1;
      process->flags = (process->flags & ~mask) | ((0 << 1) & mask);
      process->flags = process->flags | STATE_DEFUNCT;
      List *defunct_list = schedule->defunct_list;
      return insert_into_list(defunct_list, process);
    } else if ((process->flags & STATE_DEFUNCT) == 1) {
      List *defunct_list = schedule->defunct_list;
      return insert_into_list(defunct_list, process);
    }
}

return -1;
}

/* Move the process with matching pid from Ready to Stopped.
* Change its State to Stopped.
* Follow the specification for this function.
* Returns a 0 on success or a -1 on any error.
*/
int scheduler_stop(Schedule *schedule, int pid) {

if (schedule != NULL...
$67.50 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats