Code:
#include <stdio.h>
#include <malloc.h>
//////////////////////////////////////////////////////////////////////////
//
// Process Structure, Global Variables, and Functions
//
//////////////////////////////////////////////////////////////////////////
const unsigned int TRUE = 1;
const unsigned int FALSE = 0;
struct process {
char name[5];
char type[3];
unsigned int arrival;
unsigned int length;
unsigned int timeLeft;
unsigned int deadline;
unsigned int startTime;
unsigned int running; // Boolean: Is the process currently running?
unsigned int expired; // Boolean: Is the process past its deadline?
unsigned int hasArrived; // Boolean: Has the process arrived yet?
};
struct process SP[35]; // an array of processes that have not arrived yet
unsigned int time = 90000;
const unsigned int QUANTUM_LENGTH = 100;
unsigned int quantum;
void tick()
// represents a single millisecond tick of the system time
{
quantum--;
time++;
}
/////////////////////////////////////////////////////////////////////////
//
// Doubly Linked List
//
/////////////////////////////////////////////////////////////////////////
typedef struct process ITEMTYPE;
struct node {
ITEMTYPE item; // Member with the ITEMTYPE type
struct node *next; // Pointer to next node
struct node *prev; // Pointer to previous node
};
struct node * AddElement( struct node *h , ITEMTYPE p)
{
struct node *tmp;
if(h == NULL)
// if head is NULL, initialize the list
{
h = (struct node *)malloc(sizeof(struct node));
h->item = p;
h->next = NULL;
h->prev = NULL;
return h;
}
else if(h->next == NULL && h->prev == NULL) // List is empty
{
h->next = (struct node *)malloc(sizeof(struct node));
h->next->item = p;
h->next->next = h;
h->prev = h->next;
h->next->prev = h;
return h;
}
else
{
tmp = (struct node *)malloc(sizeof(struct node));
tmp->item = p;
tmp->next = h;
tmp->prev = h->prev;
h->prev->next = tmp;
h->prev = tmp;
return h;
}
return 0;
}
void PrintQueue( struct node *h , int format)
// format == 1 prints the info for the snapshot of jobs that have been implemented
// format == 2 prints the info for the snapshot of jobs that missed their deadline
{
struct node *current = h;
struct node *tmp = h;
while(current!=NULL)
{
if(format == 1)
printf("%s,%u,%u,%u,%u,%s\n", current->item.name, current->item.length, current->item.arrival, current->item.startTime, current->item.timeLeft, current->item.type);
else if(format == 2)
printf("%s,%u,%u,%u,%u\n", current->item.name, current->item.length, current->item.arrival, current->item.startTime, current->item.deadline);
current=current->next;
if(current == h)
return;
}
printf("\n");
return;
}
//////////////////////////////////////////////////////////////////////////
//
// MAIN()
//
//////////////////////////////////////////////////////////////////////////
int main()
{
// Read All Processes in from "input_2011.txt"
FILE *fr; // file pointer
fr = fopen ("input_2011.txt", "rt"); // open the file for reading
if (fr == NULL) perror("Error Opening File.");
else {
int i = 0;
for (i = 0; i < 35; i++)
{
fscanf(fr,"%s\t%u\t%u\t%u\t%s\t", SP[i].name, &SP[i].arrival, &SP[i].length, &SP[i].deadline, SP[i].type);
printf("%s,%u,%u,%u,%s\n", SP[i].name, SP[i].arrival, SP[i].length, SP[i].deadline, SP[i].type);
}
// Initialize the rest of the processes:
for (i = 0; i< 35; i++)
{
SP[i].timeLeft = SP[i].length;
SP[i].startTime = 0;
SP[i].running = FALSE;
SP[i].expired = FALSE;
SP[i].hasArrived= FALSE;
}
}
fclose(fr);
// Create queues for finished jobs:
struct node *complete_head = NULL;
struct node *pastDeadline_head = NULL;
// Create queue for each job type:
struct node *MM_head = NULL;
struct node *TXT_head = NULL;
struct node *FP_head = NULL;
quantum = QUANTUM_LENGTH; // set time quantum
while(time < 100000)
{
tick();
}
return 0;
}