Finish scheduler implementation

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-10-24 18:30:09 -03:00
parent f5eda8639a
commit a2b410fca3
11 changed files with 137 additions and 89 deletions

View File

@ -11,38 +11,38 @@ static int currentY = 0;
int limitX[2] = {0, 80}; int limitX[2] = {0, 80};
int limitY[2] = {0, 25}; int limitY[2] = {0, 25};
char windowVideo = 1; // char windowVideo = 1;
//
void changeWindow() { // void changeWindow() {
windowVideo = 1 - windowVideo; // windowVideo = 1 - windowVideo;
moveToWindowVideo(windowVideo); // moveToWindowVideo(windowVideo);
} // }
//
void moveToWindowVideo(char window) { // void moveToWindowVideo(char window) {
if (window == -1) { // if (window == -1) {
windowVideo = -1; // windowVideo = -1;
limitX[0] = 0; // limitX[0] = 0;
limitX[1] = 80; // limitX[1] = 80;
limitY[0] = 0; // limitY[0] = 0;
limitY[1] = 25; // limitY[1] = 25;
} // }
if (window == 1) { // if (window == 1) {
windowVideo = 1; // windowVideo = 1;
limitX[0] = 40; // limitX[0] = 40;
limitX[1] = 80; // limitX[1] = 80;
limitY[0] = 0; // limitY[0] = 0;
limitY[1] = 25; // limitY[1] = 25;
} // }
if (window == 0) { // if (window == 0) {
windowVideo = 0; // windowVideo = 0;
limitX[0] = 0; // limitX[0] = 0;
limitX[1] = 40; // limitX[1] = 40;
limitY[0] = 0; // limitY[0] = 0;
limitY[1] = 25; // limitY[1] = 25;
} // }
currentX = limitX[0]; // currentX = limitX[0];
currentY = limitY[0]; // currentY = limitY[0];
} // }
void increment() { void increment() {
currentX++; currentX++;

View File

@ -11,7 +11,7 @@
void forceTimer(); void forceTimer();
void haltcpu(); void haltcpu();
#define STACK_SIZE 1024 #define STACK_SIZE 1024 * 512
#define MAX_PRIORITY 40 // Maximum number for a priority #define MAX_PRIORITY 40 // Maximum number for a priority
#define MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority) // me gusta #define MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority) // me gusta
#define DEF_PRIORITY 0 #define DEF_PRIORITY 0

View File

@ -84,12 +84,12 @@ static void startOver() {
clear(); clear();
// cleanProcesses(); // cleanProcesses();
moveToWindowVideo(1); // moveToWindowVideo(1);
//((fn)sampleCodeAddress)(); //((fn)sampleCodeAddress)();
} }
static void genericException(char * string, int len) { static void genericException(char * string, int len) {
moveToWindowVideo(-1); // moveToWindowVideo(-1);
clear(); clear();
printStringLen(15, string, len); printStringLen(15, string, len);
printRegs(); printRegs();

View File

@ -178,6 +178,7 @@ int main() {
// ((EntryPoint)sampleCodeModuleAddress)(); // ((EntryPoint)sampleCodeModuleAddress)();
char * argv[] = {"SampleCode"}; char * argv[] = {"SampleCode"};
enqueueProcess(sampleCodeModuleAddress, 1, 1, argv); enqueueProcess(sampleCodeModuleAddress, 1, 1, argv);
clear();
// haltcpu(); // haltcpu();
_sti(); _sti();
// forceTimer(); // forceTimer();

View File

@ -22,7 +22,7 @@ typedef unsigned long UBaseType_t;
#define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE 1024 * 1024 #define configTOTAL_HEAP_SIZE 1024 * 1024 * 100
#define configAPPLICATION_ALLOCATED_HEAP 0 #define configAPPLICATION_ALLOCATED_HEAP 0
#define portBYTE_ALIGNMENT 8 #define portBYTE_ALIGNMENT 8
#define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8 #define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8

View File

@ -1,4 +1,6 @@
#include "scheduler.h" #include "scheduler.h"
#define INIT_PID 1
void _initialize_stack_frame(void *, void *, int, char**); void _initialize_stack_frame(void *, void *, int, char**);
enum states {READY = 0, BLOCKED}; enum states {READY = 0, BLOCKED};
@ -25,33 +27,46 @@ int readyLen = 0;
int blockedLen = 0; int blockedLen = 0;
static processCDT * currentProcess = NULL; static processCDT * currentProcess = NULL;
int pids = 0; static int pids = INIT_PID;
static char update = 1;
#include "naiveConsole.h" #include "naiveConsole.h"
#include "time.h" #include "time.h"
uint64_t nextProcess() { uint64_t nextProcess() {
update = 1;
if (currentProcess == NULL) { if (currentProcess == NULL) {
ncClear(); // ncClear();
ncPrint("Una cubana para el socio biza"); // ncPrint("Una cubana para el socio biza");
ncPrint(firstReady->name); // ncPrint(firstReady->name);
// ncPrintDec(firstReady->pid); // // ncPrintDec(firstReady->pid);
ncPrintHex(firstReady->rsp); // ncPrintHex(firstReady->rsp);
ncPrintHex(firstReady->rbp); // ncPrintHex(firstReady->rbp);
// wait(4); // wait(4);
if (firstReady == NULL)
unblock(INIT_PID);
currentProcess = firstReady; currentProcess = firstReady;
return firstReady->rsp; return firstReady->rsp;
} }
if (currentProcess->executions < MAX_PRIORITY - currentProcess->priority + 1) { if (currentProcess->executions < MAX_PRIORITY - currentProcess->priority + 1) {
currentProcess->executions++; currentProcess->executions++;
ncClear(); // ncClear();
ncPrint("Hola"); // ncPrint("Hola");
// ncPrintDec(firstReady->pid); // ncPrintDec(firstReady->pid);
// wait(4); // wait(4);
return currentProcess->rsp; return currentProcess->rsp;
} }
ncPrint("Chau"); // ncPrint("Chau");
currentProcess->executions = 0; currentProcess->executions = 0;
currentProcess = currentProcess->next; if (currentProcess->next != NULL)
currentProcess = currentProcess->next;
else {
// ncPrint("Una venezolana para el socio biza");
// wait(4);
currentProcess = firstReady;
}
return currentProcess->rsp; return currentProcess->rsp;
} }
@ -67,8 +82,12 @@ void initScheduler() {
} }
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) { void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) {
if (firstReady != NULL && firstReady->pid == INIT_PID)
block(INIT_PID);
processADT process = pvPortMalloc(sizeof(processCDT)); processADT process = pvPortMalloc(sizeof(processCDT));
uint64_t * rbp = STACK_SIZE + pvPortMalloc(STACK_SIZE); uint64_t * auxi = pvPortMalloc(STACK_SIZE);
uint64_t * rbp = STACK_SIZE + auxi;
uint64_t * rsp = rbp - 20; //22 uint64_t * rsp = rbp - 20; //22
char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2; char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2;
@ -94,41 +113,40 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
_initialize_stack_frame(fn, rbp, argc, argv); _initialize_stack_frame(fn, rbp, argc, argv);
if (firstReady == NULL) { if (firstReady == NULL)
firstReady = process; firstReady = process;
lastReady = firstReady;
}
else else
lastReady->next = process; lastReady->next = process;
lastReady = process;
ncClear(); // ncClear();
ncPrint(argv[0]); // ncPrint(argv[0]);
// proc->name = argv[0]; // // proc->name = argv[0];
ncPrint(process->name); // ncPrint(process->name);
ncPrintDec(process->pid); // ncPrintDec(process->pid);
ncPrintHex(process->rsp); // ncPrintHex(process->rsp);
ncPrintHex(process->rbp); // ncPrintHex(process->rbp);
// wait(3); // wait(3);
return; return;
} }
void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) { void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) {
char aux[MAX_NAME_SIZE]; // char aux[MAX_NAME_SIZE];
int j; // int j;
for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) { // for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) {
aux[j] = name[j]; // aux[j] = name[j];
} // }
aux[j] = '\0'; // aux[j] = '\0';
process->name = aux; // process->name = aux;
process->pid = pids++; // process->pid = pids++;
process->ppid = currentProcess->pid; // process->ppid = currentProcess->pid;
process->priority = priority; // process->priority = priority;
process->rsp = rsp; // process->rsp = rsp;
process->rbp = rbp; // process->rbp = rbp;
process->executions = 0; // process->executions = 0;
process->foreground = foreground; // process->foreground = foreground;
process->state = READY; // process->state = READY;
} }
// void loader(int argc, char * argv[], void (*fn) (int, char **)) { // void loader(int argc, char * argv[], void (*fn) (int, char **)) {
@ -169,11 +187,20 @@ char block(int pid) {
firstReady = del->next; firstReady = del->next;
} }
lastBlocked->next = del; processCDT * next = del->next;
del->next = NULL; del->next = NULL;
if (lastBlocked != NULL)
lastBlocked->next = del;
else
firstBlocked = del;
lastBlocked = del; lastBlocked = del;
forceTimer(); if (pid == currentProcess->pid) {
update = 0;
currentProcess = next;
forceTimer();
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -188,11 +215,14 @@ char unblock(int pid) {
prev->next = del->next; prev->next = del->next;
else else
firstBlocked = del->next; firstBlocked = del->next;
del->next = NULL;
if (lastReady != NULL)
lastReady->next = del;
else
firstReady = del;
lastReady = del;
} }
if (pid == del->pid)
forceTimer();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -217,12 +247,17 @@ char kill(int pid) {
firstReady = del->next; firstReady = del->next;
} }
if (pid == del->pid) processCDT * next = del->next;
forceTimer();
vPortFree((void *) del->rsp); vPortFree((void *) del->rbp - STACK_SIZE);
vPortFree((void *) del); vPortFree((void *) del);
if (pid == currentProcess->pid) {
update = 0;
currentProcess = next;
forceTimer();
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -239,12 +274,13 @@ char nice(char offset) {
void updateRSP(uint64_t newRsp) { void updateRSP(uint64_t newRsp) {
if (currentProcess == NULL) { if (currentProcess == NULL) {
ncClear(); // ncClear();
ncPrint("ES NULL"); // ncPrint("ES NULL");
// wait(4); // wait(4);
return; return;
} }
currentProcess->rsp = newRsp; if (update)
currentProcess->rsp = newRsp;
} }
int getPid() { int getPid() {

View File

@ -31,11 +31,13 @@ int semPost(char semaphore) {}
// o pasando la estructura de sem. esta es la q hace posix // o pasando la estructura de sem. esta es la q hace posix
sem_t * semOpen(char * name, unsigned int value) { sem_t * semOpen(char * name, unsigned int value) {
enter_region(&semLock); enter_region(&semLock);
//
leave_region(&semLock); leave_region(&semLock);
} }
int semClose(sem_t * semaphore) { int semClose(sem_t * semaphore) {
enter_region(&semLock); enter_region(&semLock);
//
leave_region(&semLock); leave_region(&semLock);
} }
@ -60,5 +62,6 @@ int semWait(sem_t * semaphore) {
int semPost(sem_t * semaphore) { int semPost(sem_t * semaphore) {
enter_region(&semLock); enter_region(&semLock);
//
leave_region(&semLock); leave_region(&semLock);
} }

View File

@ -6,6 +6,7 @@
void winClear(); void winClear();
void printString(char * string); void printString(char * string);
void printStringError(char * string); void printStringError(char * string);
void printStringLen(char * string, int len);
void new_line(); void new_line();
char getChar(); char getChar();
int abs(); int abs();

View File

@ -5,6 +5,10 @@ void printString(char * string) {
sys_write(1, string, len); sys_write(1, string, len);
} }
void printStringLen(char * string, int len) {
sys_write(1, string, len);
}
void printStringError(char * string) { void printStringError(char * string) {
int len = strlen(string); int len = strlen(string);
sys_write(2, string, len); sys_write(2, string, len);

View File

@ -14,7 +14,7 @@
#define SIZE 100 #define SIZE 100
#define MAX_ARGS 5 #define MAX_ARGS 5
#define COLS 40 #define COLS 80
#define ROWS 25 #define ROWS 25
const int len = 8; const int len = 8;
@ -42,7 +42,7 @@ void addText(char * buffer, char * window, int * offset) {
} }
void printWindow(char * window) { void printWindow(char * window) {
printString(window); printStringLen(window, ROWS * COLS);
} }
void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) { void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) {
@ -80,6 +80,9 @@ void clearWindow(char * window, int * offset) {
printWindow(window); printWindow(window);
} }
// static char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0};
// static char buffer[SIZE] = {0};
// static char *tokens[SIZE] = {0};
void shell(int argc, char *argv[]) { void shell(int argc, char *argv[]) {
char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0};
int offset = (ROWS - 1) * COLS; int offset = (ROWS - 1) * COLS;