Skip to content

Instantly share code, notes, and snippets.

@aeonblue3
Created February 14, 2012 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeonblue3/1821977 to your computer and use it in GitHub Desktop.
Save aeonblue3/1821977 to your computer and use it in GitHub Desktop.
Scheduler
//
// scheduler.c
//
//
// Created by Chris Neitzer on 2/9/12.
// Copyright (c) 2012 All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESSES 3
// Structure for my processes
typedef struct {
int id;
int priority;
int burstTime;
int waitTime;
int runningTime;
char *myProcessName;
} myProcess;
// Declare three processes
myProcess *p;
// Global variables
int processIndex = 0;
int mySignal = 0;
int currentProcess;
int totalRunningTime = 0;
// Declare functions
void addProcess(int index, int priority, int burstTime, char *name);
int getCurrentProcess(myProcess *theProcess);
void processProcess(myProcess *theProcess);
// Iterators
int i = 0;
int j = 0;
// Main Program
int main() {
// Allocate memory for process structs
p = malloc(sizeof(myProcess) * MAX_PROCESSES);
/*
* mySignal indicates that a new process has been added to the queue
*/
do {
if ( i == 0 )
{
addProcess(processIndex, 1, 30, "Process One\0");
printf("%s added!\n", p[0].myProcessName);
mySignal = 1;
} else if ( i == 5 )
{
addProcess(processIndex, 2, 40, "Process Two\0");
printf("%s added!\n", p[1].myProcessName);
mySignal = 1;
} else if ( i == 10 )
{
addProcess(processIndex, 3, 20, "Process Three\0");
printf("%s added!\n", p[2].myProcessName);
mySignal = 1;
} else {
;
}
if (mySignal == 1)
{
currentProcess = getCurrentProcess(p);
}
printf("Current Process: %s\n", p[currentProcess].myProcessName);
processProcess(p[currentProcess]);
i++;
} while (i < totalRunningTime);
return 0;
}
void addProcess(int index, int priority, int burstTime, char *name)
{
p[index].id = index;
p[index].priority = priority;
p[index].burstTime = burstTime;
p[index].waitTime = 0;
p[index].runningTime = 0;
p[index].myProcessName = name;
totalRunningTime += p[index].burstTime; /* Everytime a process is added, the total running time is increased by the burst time. */
processIndex++;
}
int getCurrentProcess(myProcess *theProcess)
{
int highestPriorityProcess = 0;
int highestPriorityProcessId = 0;
int k;
for (k = 0; k < MAX_PROCESSES; ++k)
{
if (theProcess[k].priority > highestPriorityProcess)
{
highestPriorityProcess = theProcess[k].priority;
highestPriorityProcessId = theProcess[k].id;
}
}
mySignal = 0;
return highestPriorityProcessId;
}
void processProcess(myProcess *theProcess)
{
/*struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 1000000;
if (&theProcess.burstTime != &theProcess.runningTime)
{
theProcess.runningTime++;
nanosleep(&tim, &tim2);*/
printf("%s is running: %d\n",theProcess.myProcessName, theProcess.runningTime);
/*} else {
printf("%s has ended.\n", theProcess.myProcessName);
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment