Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- struct QNode {
- int arrivalTime;
- int processId;
- int runTime;
- int blockTime;
- int exchangeTimeSize;
- int actualRunTime;
- int totalReadyTime;
- int hddTimeRemaining;
- int *exchangeTime;
- struct QNode* next;
- };
- struct queue {
- int count;
- struct QNode *front;
- struct QNode *back;
- };
- struct cpu {
- int timer;
- int processId;
- };
- struct hdd {
- int timer;
- int processId;
- };
- void initCpu(struct cpu *c) {
- c->timer = 0;
- c->processId = 0;
- }
- void initHdd(struct hdd *h) {
- h->timer = 0;
- h->processId = 0;
- }
- struct QNode* newNode(int arrivalTime, int processId, int runTime, int blockTime, int actualRunTime, int totalReadyTime, int *exchangeTime, int exchangeTimeSize) {
- struct QNode * tempNode = (struct QNode*) malloc(sizeof(struct QNode));
- tempNode->arrivalTime = arrivalTime;
- tempNode->processId = processId;
- tempNode->runTime = runTime;
- tempNode->blockTime = blockTime;
- tempNode->actualRunTime = actualRunTime;
- tempNode->totalReadyTime = totalReadyTime;
- tempNode->hddTimeRemaining = 0;
- tempNode->exchangeTime = exchangeTime;
- tempNode->exchangeTimeSize = exchangeTimeSize;
- return tempNode;
- }
- struct queue* createQueue() {
- struct queue* q = (struct queue*) malloc(sizeof(struct queue));
- q->front = q->back = NULL;
- q->count = 0;
- return q;
- }
- void enqueue(struct queue *q, int arrivalTime, int processId, int runTime, int blockTime, int actualRunTime, int totalReadyTime, int *exchangeTime, int exchangeTimeSize) {
- struct QNode* tempNode = newNode(arrivalTime, processId, runTime, blockTime, actualRunTime, totalReadyTime, exchangeTime, exchangeTimeSize);
- // printf("Queue Count: %d\n", q->count);
- if (q->count == 0) {
- q->front = q->back = tempNode;
- q->count++;
- return;
- }
- q->back->next = tempNode;
- q->back = tempNode;
- q->count++;
- }
- void dequeue(struct queue *q) {
- if (q->count == 0) {
- return;
- }
- struct QNode* previousFront = q->front;
- q->front = q->front->next;
- if (q->front == NULL) {
- q->back = NULL;
- }
- q->count--;
- //free(previousFront);
- }
- int checkIfArrayContains(int * arr, int actualRunTime, int size, int timer) {
- int *p = NULL;
- for (p = arr; p < arr + size; ++p) {
- if (*p == actualRunTime) {
- *p = -1;
- return 1;
- }
- }
- return 0;
- }
- int main() {
- int quantum = 500;
- int hddTime = 800;
- struct cpu *c;
- c = (struct cpu*)malloc(sizeof(struct cpu));
- // Set quantum
- c->timer = quantum;
- struct hdd *h;
- h = (struct hdd*)malloc(sizeof(struct hdd));
- h->timer = hddTime;
- struct QNode *process0 = newNode(0, 0, 10000, 0, 0, 0, NULL, 0);
- struct queue *nq = createQueue();
- //Test case 1
- int numArray1[1] = {500};
- int numArray2[2] = {500};
- enqueue(nq, 100, 12, 800, 0, 0, 0, numArray1, 1);
- enqueue(nq, 300, 11, 1000, 0, 0, 0, numArray2, 1);
- enqueue(nq, 700, 13, 800, 0, 0, 0, NULL, 0);
- //Test case 2
- // int numArray1[2] = {100, 700};
- // int numArray2[3] = {100, 200, 300};
- // enqueue(nq, 100, 43, 1000, 0, 0, 0, numArray1, 2);
- // enqueue(nq, 200, 42, 300, 0, 0, 0, numArray2, 3);
- struct queue *rq = createQueue();
- struct queue *bq = createQueue();
- //display(nq->front);
- //dequeue(nq);
- struct queue *fq = createQueue();
- int timer = 0;
- while ((nq->count != 0) || (rq->count != 0) || (bq->count != 0)) {
- //Move items from new queue into ready queue
- if (nq->count > 0) {
- if (nq->front->arrivalTime == timer) {
- enqueue(rq, nq->front->arrivalTime, nq->front->processId, nq->front->runTime, nq->front->blockTime,
- nq->front->actualRunTime, nq->front->totalReadyTime, nq->front->exchangeTime, nq->front->exchangeTimeSize);
- // printf("Add from nq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
- // printf("Dequeue from nq: Time: %d, process: %d\n", timer, nq->front->processId);
- dequeue(nq);
- }
- }
- if (rq->count == 0) {
- process0->actualRunTime++;
- }
- // if (rq->count > 1) {
- // int i;
- // for (i = 0; i < rq->count; i++) {
- // if (i != 0) {
- // rq->front->totalReadyTime++;
- // }
- // enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- // dequeue(rq);
- // }
- // }
- if (rq->count > 0) {
- if (rq->front->exchangeTimeSize != 0) {
- int useHdd = checkIfArrayContains(rq->front->exchangeTime, rq->front->actualRunTime, rq->front->exchangeTimeSize, timer);
- // printf("Timer: %d, Process:%d, ActualRunTime:%d, UseHDD: %d\n", timer, rq->front->processId, rq->front->actualRunTime, useHdd);
- if (useHdd == 1) {
- rq->front->hddTimeRemaining = hddTime;
- enqueue(bq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- // printf("Add from rq to bq: Time: %d, Process: %d\n", timer, bq->back->processId);
- // printf("Timer: %d, arrivalTime: %d, blockTime: %d, SUM: %d\n", timer, rq->front->arrivalTime, rq->front->blockTime, timer + rq->front->arrivalTime, rq->front->blockTime);
- c->timer = quantum;
- // printf("1Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
- dequeue(rq);
- }
- }
- if (rq->count > 0) {
- if (rq->front->actualRunTime == rq->front->runTime) {
- //Final remove from queue. Free memory here
- // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
- // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
- // printf("timer: %d, PID: %d, runTime: %d, blockTime: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime);
- enqueue(fq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- dequeue(rq);
- }
- else if (rq->front->actualRunTime > rq->front->runTime) {
- rq->front->actualRunTime--;
- //Final remove from queue. Free memory here
- // printf("2Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
- // printf("timer: %d, PID: %d, runTime: %d, blockTime: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime);
- enqueue(fq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- dequeue(rq);
- }
- }
- }
- if (c->timer == 0) {
- c->timer = quantum;
- //addToQueue(rq, rq->front->data);
- enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- // printf("Add from rq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
- // printf("3Dequeue from rq: Time: %d, process: %d\n", timer, rq->front->processId);
- dequeue(rq);
- }
- if (h->timer == 0) {
- h->timer = hddTime;
- enqueue(rq, bq->front->arrivalTime, bq->front->processId, bq->front->runTime, bq->front->blockTime, bq->front->actualRunTime, bq->front->totalReadyTime, bq->front->exchangeTime, bq->front->exchangeTimeSize);
- // printf("Add from bq to rq: Time: %d, Process: %d\n", timer, rq->back->processId);
- // printf("Dequeue from bq: Time: %d, process: %d\n", timer, bq->front->processId);
- dequeue(bq);
- // printf("nqC: %d, rqC: %d, bqC: %d\n", nq->count, rq->count, bq->count);
- }
- if (rq->count > 0) {
- c->timer--;
- }
- if (bq->count > 0) {
- // printf("BQ: Time:%d, PID:%d, HDDTime:%d\n", timer, bq->front->processId, h->timer);
- h->timer--;
- int i;
- // printf("Count of BQ: %d\n", bq->count);
- for (i = 0; i < bq->count; i++) {
- bq->front->blockTime++;
- enqueue(bq, bq->front->arrivalTime, bq->front->processId, bq->front->runTime, bq->front->blockTime, bq->front->actualRunTime, bq->front->totalReadyTime, bq->front->exchangeTime, bq->front->exchangeTimeSize);
- dequeue(bq);
- }
- //bq->front->blockTime++;
- }
- if (rq->count > 0) {
- rq->front->actualRunTime++;
- int i;
- for (i = 0; i < rq->count; i++) {
- if (i != 0) {
- rq->front->totalReadyTime++;
- }
- enqueue(rq, rq->front->arrivalTime, rq->front->processId, rq->front->runTime, rq->front->blockTime, rq->front->actualRunTime, rq->front->totalReadyTime, rq->front->exchangeTime, rq->front->exchangeTimeSize);
- dequeue(rq);
- }
- }
- // if (rq->count > 0) {
- // printf("Timer: %d, Process: %d, actualRunTime: %d, blockTime: %d, cTimer: %d\n", timer, rq->front->processId, rq->front->actualRunTime, rq->front->blockTime, c->timer);
- // }
- timer++;
- }
- printf("%d %d\n", process0->processId, process0->actualRunTime);
- int i;
- int end = fq->count;
- for (i = 0; i < end; i++) {
- printf("%d %d %d %d\n", fq->front->processId, fq->front->actualRunTime, fq->front->totalReadyTime, fq->front->blockTime);
- dequeue(fq);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement