int main(int argc, char *argv[]) {
  
  // Declare major variables
  // node_t is a link-list of type process
  node_t* dispatch_queue;
  node_t* realtime_queue;
  node_t* process_queue_1;
  node_t* process_queue_2;
  node_t* process_queue_3;

  dispatch_queue = (node_t *)malloc(sizeof(node_t));
  realtime_queue = (node_t *)malloc(sizeof(node_t));
  process_queue_1 = (node_t *)malloc(sizeof(node_t));
  process_queue_2 = (node_t *)malloc(sizeof(node_t));
  process_queue_3 = (node_t *)malloc(sizeof(node_t));

  dispatch_queue->proc.arrival_time = -1;
  dispatch_queue->proc.priority = -1;
  dispatch_queue->proc.processor_time = -1;
  dispatch_queue->proc.Mbytes = -1;
  dispatch_queue->proc.num_printers = -1;
  dispatch_queue->proc.num_scanners = -1;
  dispatch_queue->proc.num_modems = -1;
  dispatch_queue->proc.num_cds = -1;

  realtime_queue->proc.arrival_time = -1;
  realtime_queue->proc.priority = -1;
  realtime_queue->proc.processor_time = -1;
  realtime_queue->proc.Mbytes = -1;
  realtime_queue->proc.num_printers = -1;
  realtime_queue->proc.num_scanners = -1;
  realtime_queue->proc.num_modems = -1;
  realtime_queue->proc.num_cds = -1;

  process_queue_1->proc.arrival_time = -1;
  process_queue_1->proc.priority = -1;
  process_queue_1->proc.processor_time = -1;
  process_queue_1->proc.Mbytes = -1;
  process_queue_1->proc.num_printers = -1;
  process_queue_1->proc.num_scanners = -1;
  process_queue_1->proc.num_modems = -1;
  process_queue_1->proc.num_cds = -1;

  process_queue_2->proc.arrival_time = -1;
  process_queue_2->proc.priority = -1;
  process_queue_2->proc.processor_time = -1;
  process_queue_2->proc.Mbytes = -1;
  process_queue_2->proc.num_printers = -1;
  process_queue_2->proc.num_scanners = -1;
  process_queue_2->proc.num_modems = -1;
  process_queue_2->proc.num_cds = -1;

  process_queue_3->proc.arrival_time = -1;
  process_queue_3->proc.priority = -1;
  process_queue_3->proc.processor_time = -1;
  process_queue_3->proc.Mbytes = -1;
  process_queue_3->proc.num_printers = -1;
  process_queue_3->proc.num_scanners = -1;
  process_queue_3->proc.num_modems = -1;
  process_queue_3->proc.num_cds = -1;


  resources computer_resources;
  computer_resources.max_printers = 2;
  computer_resources.max_scanner = 1;
  computer_resources.max_modems  = 1;
  computer_resources.max_cd = 3;
  memset(computer_resources.max_memory, 0, MEMORY);

  // Load the dispatch list file and create the dispatch queue.
  load_dispatch("dispatchlist", dispatch_queue);

  // Iterate through each item in the job dispatch list, add each process
  // to the appropriate queues

  while(1) {
    process handle = pop(&dispatch_queue);
    if (handle.arrival_time == NULL) {
      break;
    }
    switch(handle.priority) {
      case 0: 
        push(handle, realtime_queue);
      case 1: 
        push(handle, process_queue_1);
      case 2: 
        push(handle, process_queue_2);
      case 3: 
        push(handle, process_queue_3);
    }
  }
  // pop(&dispatch_queue);
  // pop(&realtime_queue);
  // pop(&process_queue_1);
  // pop(&process_queue_2);
  // pop(&process_queue_3);

  // Allocate the resources for each process before it's executed
    
  while (realtime_queue != NULL) {
    _runprocess(computer_resources, realtime_queue);
  }
  while(process_queue_1 != NULL) {
    _runprocess(computer_resources, process_queue_1);
  }
  while(process_queue_2 != NULL) {
    _runprocess(computer_resources, process_queue_2);
  }
  while(process_queue_3 != NULL) {
    _runprocess(computer_resources, process_queue_3);
  }
    
  free(dispatch_queue);
  free(realtime_queue);
  free(process_queue_1);
  free(process_queue_2);
  free(process_queue_3);

  return EXIT_SUCCESS;
}
Exemple #2
0
int main(int argc, char *argv[])
{
    // ==================== YOUR CODE HERE ==================== //

    // Load the dispatchlist
    char *dpFile = NULL;

    if(argc > 1)
        dpFile = argv[1];

    // Read the dispatch file 
    load_dispatch(dpFile);

    // Iterate through each item in the job dispatch list, add each process
    // to the appropriate queues
    dispTime = 0;

    // Execute the block once
    do {
        // Iinitialize available resources
        initializeAvaiableResources();

        // queue data structure
        structNode *poppedStruct;

        // continually loop until dispatches aren't null
        while ( listOfDispatches != NULL  && seek(listOfDispatches)->process.arrival_time <= dispTime)  {
        	// Check if the next list of dispatch is empty
            if (listOfDispatches->next == NULL)  {
            	// pop the first element from the list of dispatches
              poppedStruct = listOfDispatches;
              listOfDispatches = NULL;
            }
            else {
              poppedStruct = pop(listOfDispatches);
            }

            // check if the popped dispatcher has arrival time that is less than the dispatched time
            if (poppedStruct->process.arrival_time <= dispTime)  {
            	// get the status of the realtime job
              bool process_1 = poppedStruct->process.res.num_printers > 0 || poppedStruct->process.res.num_scanners > 0 || poppedStruct->process.res.num_modems > 0 || poppedStruct->process.res.num_CDs > 0;
              bool process_2 = poppedStruct->process.res.num_printers > NUM_PRINTERS || poppedStruct->process.res.num_scanners > NUM_SCANNERS || poppedStruct->process.res.num_modems > NUM_MODEMS || poppedStruct->process.res.num_CDs > NUM_CDS;

              // check if the process priority is 0
              if (poppedStruct->process.priority == 0)  {
            	  	  // check if process 1 is true
                    if (process_1) {
                        printf("ERROR WITH THE REAL TIME JOB\n");
                    }
                    else {
                    	// push the process to the queue
                        rtData = push(rtData, poppedStruct->process);
                    }
                }
                else {
                	// check if the second process is true
                    if (process_2) {
                        printf("ERROR WITH THE RESOURCE\n");
                    }
                    else {
                    	// push the process onto the queue
                        userJobs = push(userJobs, poppedStruct->process);
                    }
                }
            }
        }

        // Allocate the resource necessary for the process
        // check if the user jobs aren't null
        // Check the amount of free space and return true if there is enough space for allocation
        // check for the available resource
        // continually loop until these above criteria meets
        while(userJobs != NULL && checkForMemory(userJobs->process.res, userJobs->process.MBytes) && checkForResources(userJobs->process.res))  {
        		// set the memory index to the allocated memory
                userJobs->process.memory_index = alloc_mem(userJobs->process.res, userJobs->process.MBytes);

                // create temporary queue structure
                structNode *temp;

                // continually loop through the jobs
                while( userJobs != NULL) {
                	// Check if the next element is null
                    if (userJobs->next == NULL){
                    	// assign the job to the temporary queue
                      temp = userJobs;

                      userJobs = NULL;
                    }
                    else {
                    	// pop the first element and add it to temp queue
                      temp = pop(userJobs);
                }

                // Allocate the resources for the temp process
                allocateResources(temp->process);

                // push the process to the queue
                pushToQueue(temp->process);
            }
        }

        // check if the active doesn't contain NULL
       if (activeProcess != NULL) {
    	   // decrement the active processes
            activeProcess->process.processor_time--;

            // check if the processor time is 0
            if (activeProcess->process.processor_time == 0)  {
            	// kill the active process
                kill(activeProcess->process.pid, SIGINT);

                // system call suspends execution of the calling process until a child specified by the pid
                waitpid(activeProcess->process.pid, &status, WUNTRACED);

                // free the memory
                free_mem(activeProcess->process.res, activeProcess->process.memory_index, activeProcess->process.MBytes);

                // Free the resources
                freeResources(activeProcess->process.res);

                // free the variable
                free(activeProcess);

                activeProcess = NULL;
            } else if (activeProcess->process.priority > 0 && !(rtData == NULL && firstPriority == NULL && secondPriority == NULL && thirdPriority == NULL)) {
                // kill the process
            	kill(activeProcess->process.pid , SIGTSTP);

                // system call suspends execution of the calling process until a child specified by the pid
                waitpid(activeProcess->process.pid + 1, &status, WUNTRACED);

                // set the suspended state to true
                activeProcess->process.suspended = true;

                // check if the priority is less than 3
                if(activeProcess->process.priority < 3)  {
                	// increment the priority
                    activeProcess->process.priority++;
                }

                // push the process to queue
                pushToQueue(activeProcess->process); // add back to the queue
                activeProcess = NULL;

            }
       }

       // Pop the first process from the queue
       if (activeProcess == NULL && !(rtData == NULL && firstPriority == NULL && secondPriority == NULL && thirdPriority == NULL))  {
           // check if the real time data is not null
    	   if (rtData != NULL)  {
    		   // check if the next element is not null
                if (rtData->next != NULL) {
                	// pop the first element and assign it to active
                    activeProcess = pop(rtData);
                } else {
                	// set the active process
                    activeProcess = rtData;
                    rtData = NULL;
                }
            } else if (firstPriority != NULL) {
            	// check if the first priority is not null
                if (firstPriority->next != NULL) {
                	// pop the first element
                    activeProcess = pop(firstPriority);
                } else {
                	// set the first priority to an active process
                    activeProcess = firstPriority;
                    firstPriority = NULL;
                }
            } else if (secondPriority != NULL) {
            	// check if the second priority has next element
                if (secondPriority->next != NULL) {
                	// pop the first element and assign it to the active process
                    activeProcess = pop(secondPriority);
                } else {
                	// assign the second priority task to the process
                    activeProcess = secondPriority;
                    secondPriority = NULL;
                }
            } else if (thirdPriority != NULL) {
            	// check if the next element in third priority is not null
                if (thirdPriority->next != NULL) {
                	// pop the first element and add it to active process
                    activeProcess = pop(thirdPriority);
                } else {
                	// assign the third priority to the active process
                    activeProcess = thirdPriority;
                    thirdPriority = NULL;
                }
            }

    	    // check if the process is suspended
            if(activeProcess->process.suspended == true)  {
            	// kill the suspended process
                kill(activeProcess->process.pid, SIGCONT);

                // set the state of suspended to false
                activeProcess->process.suspended = false;
            } else {
            	// create process id
                pid_t pid;

                // fork the process
                pid = fork();

                // check if the pid is less than 0, then throw error
                if (pid < 0) {
                    printf("ERROR WITH FORK\n");
                }
                else if (pid == 0)  {
                	// get the child process
                    execvp(procArgv[0], procArgv);
                }
                // set the active process id
                activeProcess->process.pid = pid;

                // print the process to the console
                print_process(activeProcess->process);

            }
        }

    // sleep for 1 second
    sleep(1);

    // increment the dispatcher time
    dispTime ++;
    //printf("Time: %d\n", dispTime);
   } while (activeProcess != NULL || !(rtData == NULL && firstPriority == NULL && secondPriority == NULL && thirdPriority == NULL) || listOfDispatches != NULL);

    return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
    // Load the dispatchlist
    // Add each process structure instance to the job dispatch list queue
    node_t *tmp_head = NULL;
    printf("Dispatch list: \n");
    load_dispatch("dispatchlist", &tmp_head);
    print_list(tmp_head);

    // Iterate through each item in the job dispatch list, add each process
    // to the appropriate queues
    node_t *test = NULL;
    while(tmp_head != NULL) {
        proc current = pop(&tmp_head);

        // get a segfault, so this is currently commented out
        push(&head_dispatch_queues[current.priority], current);
    }

    // go through the queues from highest to lowest priority
    for(int i = 0; i < 4; i++) {
        // while this queue isn't empty
        // it will keep pushing resources to the end of the queue until this on is completely empty
        while(head_dispatch_queues[i] != NULL) {
            proc current = pop(head_dispatch_queues[i]);
            if(res_available(current, resources)) {
                // Allocate the resources for each process before it's executed
                int printer_start = alloc_res(&resources.printers,  current.required_printers);
                int scanner_start = alloc_res(&resources.scanners,  current.required_scanners);
                int modem_start   = alloc_res(&resources.modems,    current.required_modems);
                int cd_start      = alloc_res(&resources.cd_drives, current.required_cds);
                int memory_start  = alloc_res(&resources.memory,    current.required_memory);

                // Execute the process binary using fork and exec
                // Perform the appropriate signal handling
                // decrease the current.processor_time by the amount of time this process ran
                run_for_time(&current);

                // Deallocate the resources
                free_res(&resources.printers,  printer_start, current.required_printers);
                free_res(&resources.scanners,  scanner_start, current.required_scanners);
                free_res(&resources.modems,    modem_start,   current.required_modems);
                free_res(&resources.cd_drives, cd_start,      current.required_cds);
                free_res(&resources.memory,    memory_start,  current.required_memory);
            }

            // if the process needs more time to run, add it back to the queue
            if(current.processor_time > 0) {
                push(head_dispatch_queues[i], current);
            }
        }
    }


    // make sure the queues are clear to avoid memory leaks
    for(int i = 0; i < 4; i++)
        while(head_dispatch_queues[i] != NULL)
            pop(head_dispatch_queues[i]);

    return EXIT_SUCCESS;
}
int main(int argc, char *argv[]){
  //Init queues
  q_dispatch = (queue*)malloc(sizeof(queue*));
  q_dispatch->head=NULL;
  q_dispatch->tail=NULL;

  q_real = (queue*)malloc(sizeof(queue*));
  q_real->head=NULL;
  q_real->tail=NULL;

  q_1 = (queue*)malloc(sizeof(queue*));
  q_1->head=NULL;
  q_1->tail=NULL;

  q_2 = (queue*)malloc(sizeof(queue*));
  q_2->head=NULL;
  q_2->tail=NULL;

  q_3 = (queue*)malloc(sizeof(queue*));
  q_3->head=NULL;
  q_3->tail=NULL;


  //Init resources
  for(int i=0;i<PRINTERS;i++){res_avail.printers[i]=0;}
  for(int i=0;i<SCANNERS;i++){res_avail.scanners[i]=0;}
  for(int i=0;i<MODEMS;i++){res_avail.modems[i]=0;}
  for(int i=0;i<DRIVES;i++){res_avail.drives[i]=0;}
  for(int i=0;i<MEMORY;i++){res_avail.memory[i]=0;}

  // Load the dispatchlist adds to queue as well
  load_dispatch(argv[1]);

  int tick =0;
  //Start dispatching
  while(true){
    //check dispatch list for processes that have arrived
    node* dispatch_node = q_dispatch->head;
    while(dispatch_node){
      if(dispatch_node->process.arrival_time==tick){
        //Push process to appropriate queue
        switch(dispatch_node->process.priority){
          case 0:
          push(q_real,dispatch_node->process);
          break;
          case 1:
          push(q_1,dispatch_node->process);
          break;
          case 2:
          push(q_2,dispatch_node->process);
          break;
          case 3:
          push(q_3,dispatch_node->process);
          break;
        }
      }
      dispatch_node=dispatch_node->next;
    }

    //Run processes
    proc* get_proc;
    bool find_proc = true;
    int count;

    //Real Time Queue
    count = q_size(q_real);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>0){
      get_proc = pop(q_real);
      //check if process can be allocated
      if(alloc_resources(&res_avail,get_proc)){
        //run
        run_proc(get_proc);
        find_proc=false;
      }else{
        push(q_real,*get_proc);
        count--;
      }
    }

    //1st Queue
    count = q_size(q_1);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>1){
      get_proc = pop(q_1);
      //Check if process can be allocated
      if(alloc_resources(&res_avail,get_proc)){
        //run if not completed during run push to next queue
        if(run_proc(get_proc))
        push(q_2,*get_proc);
        find_proc=false;
      }else{
        //if resources not available push back onto queue
        push(q_1,*get_proc);
        count--;
      }
    }

    //2nd Queue
    count = q_size(q_2);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>0){
      get_proc = pop(q_2);
      //if process is a new process
      if(!get_proc->suspended){
        //check if it can be allocated
        if(alloc_resources(&res_avail,get_proc)){
          //run if not completed during run push to next queue
          if(run_proc(get_proc))
          push(q_3,*get_proc);
          find_proc=false;
        }else{
          //if resources not available push back onto queue
          push(q_2,*get_proc);
          count--;
        }
      }else{
        //if processes has already been allocated just run it some more
        if(run_proc(get_proc))
        push(q_3,*get_proc);
        find_proc=false;
      }
    }

    //3rd Qeue
    count = q_size(q_3);
    while(find_proc&&count>0){
      get_proc = pop(q_3);
      if(!get_proc->suspended){
        if(alloc_resources(&res_avail,get_proc)){
          if(run_proc(get_proc))
          push(q_3,*get_proc);
          find_proc=false;
        }else{
          push(q_3,*get_proc);
          count--;
        }
      }else{
        if(run_proc(get_proc))
        push(q_3,*get_proc);
        find_proc=false;
      }
    }

    //if no process was able to be run either system is hung or there are no jobs left
    if(find_proc){
      printf("TERMINATING: Could not find anymore processes\n");
      break;
    }

    //increase tick count on dispatcher
    tick++;
  }

  return EXIT_SUCCESS;
}