vanush
kdslkf
OMG
I finally finished it
Now to beautify
I finally finished it
Now to beautify
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "pcb.h"
#include "res.h"
void execute(PcbPtr n);
void printRes(int * systemRes);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Please specifiy a job list.\n");
} else {
int dispatcherTimer = 0;
PcbPtr runningProcess = NULL;
pid_t prev;
int status;
pcbQueue jobQueue = (pcbQueue) initQueue((char *)argv[1]);
PcbPtr jobHead = jobQueue->head;
MabPtr pending;
mabList memList = createMemList();
MabPtr rt = memSplit(memList->head, 64, memList);
//rt->locked = 1;
int * systemRes = createRes();
printf("Resources available : %d %d %d %d\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
while (jobHead) {
printf("%d %d %d %d (%d %d %d %d)\n", jobHead->arrivaltime, jobHead->priority, jobHead->remainingcputime, jobHead->mbytes, jobHead->res[0], jobHead->res[1], jobHead->res[2], jobHead->res[3]);
jobHead = jobHead->next;
}
pcbQueue userJobQueue = (pcbQueue) createQueue();
pcbQueue * feedbackQueue = (pcbQueue *) malloc(4 * sizeof(pcbQueue));
feedbackQueue[0] = createQueue();
feedbackQueue[1] = createQueue();
feedbackQueue[2] = createQueue();
feedbackQueue[3] = createQueue();
//pcbQueue freeList = createQueue();
while (queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3]) ||
queueSize(jobQueue) || queueSize(userJobQueue) || runningProcess)
{
while (getNodeArrivalTime(jobQueue->head) <= dispatcherTimer) {
fprintf(stderr, "Dispatcher TImer is %d\n", dispatcherTimer);
PcbPtr job = copyNode(deQueue(jobQueue));
fprintf(stderr, "[init que to userjob queue] Getting job [%d] [%d] from the init queue\n", job->priority, job->mbytes);
assert(job != NULL);
if (getNodePriority(job) == 0) {
fprintf(stderr, "[init que to userjob queue] Adding priority job [%d] to real time queue\n", job->mbytes);
job->mem = rt;
enQueue(feedbackQueue[0], job);
} else {
fprintf(stderr, "[init que to userjob queue] Adding job [%d] to User Job Queue\n", job->mbytes);
enQueue(userJobQueue, job);
}
}
while (queueSize(userJobQueue) && ((pending = memChk(memList->head, userJobQueue->head->mbytes)) != NULL) && resChk(systemRes, userJobQueue->head->res)) {
fprintf(stderr, "Size of user job queue is: %d\n", queueSize(userJobQueue));
PcbPtr userjob = deQueue(userJobQueue);
assert(userjob != NULL);
fprintf(stderr, "Getting job [%d] [%d] from the user job queue\n", userjob->priority, userjob->mbytes);
MabPtr usermem = memAlloc(pending, userjob->mbytes, memList);
assert(usermem != NULL);
userjob->mem = usermem;
resAlloc(systemRes, userjob->res);
enQueue(feedbackQueue[getNodePriority(userjob)], userjob);
fprintf(stderr, "Adding job [%d] [%d] to feedback quue %d \n", userjob->priority, userjob->mbytes, getNodePriority(userjob));
}
if (runningProcess != NULL)
{
prev = getNodePid(runningProcess);
runningProcess->remainingcputime--;
fprintf(stderr,"Process CPU TIME : %d\n", runningProcess->remainingcputime);
if (runningProcess->remainingcputime == 0) {
kill(getNodePid(runningProcess), SIGINT);
fprintf(stderr, "Killing [%d] [%d] job\n", runningProcess->priority, runningProcess->mbytes);
memFree(runningProcess->mem, memList);
if (runningProcess->priority != 0) {
resFree(systemRes, runningProcess->res);
}
printMemory(memList);
free(runningProcess);
runningProcess = NULL;
assert(!runningProcess);
} else if ((getNodePriority(runningProcess) != 0) && (queueSize(feedbackQueue[0]) || queueSize(userJobQueue) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3])))
{
kill(getNodePid(runningProcess), SIGTSTP);
runningProcess->suspend = 1;
if (getNodePriority(runningProcess) != 3) {
runningProcess->priority++;
enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
fprintf(stderr, "(lower) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
} else {
enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
fprintf(stderr, "(own) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
}
runningProcess = NULL;
assert(runningProcess == NULL);
}
}
if (!runningProcess && ((queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3])))) {
waitpid(prev, &status, WUNTRACED);
int doneDeq = 0;
int i = 0;
while (i < (4) && !doneDeq) {
if (queueSize(feedbackQueue[i])) {
runningProcess = deQueue(feedbackQueue[i]);
fprintf(stderr, "This process is from Queue %d with priority %d and pid %d\n", i, runningProcess->priority, runningProcess->pid);
assert(runningProcess->remainingcputime != NULL);
assert(runningProcess != NULL);
doneDeq = 1;
}
i++;
}
if (runningProcess->suspend == 1) {
kill(getNodePid(runningProcess), SIGCONT);
runningProcess->suspend = 0;
} else {
execute(runningProcess);
}
}
sleep(1);
fprintf(stdout, "========= DISPATCHER TIMER %d ==========\n", dispatcherTimer);
dispatcherTimer++;
}
}
return 0;
}
void execute(PcbPtr n) {
pid_t pid;
if ((pid = fork()) < 0) {
printf("Error");
exit(1);
}
else if (pid == 0) {
execvp(n->args[0], n->args);
}
fprintf(stderr, "Executing process %d with priority %d\n", pid, n->priority);
n->pid = pid;
}
void printRes(int * systemRes) {
fprintf(stderr, "[%d %d %d %d]\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
}