Finish semaphore 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-25 11:47:02 -03:00
parent a2b410fca3
commit d29d6ced63
10 changed files with 215 additions and 49 deletions

View File

@ -12,7 +12,7 @@ int ticks_elapsed() {
int seconds_elapsed() { int seconds_elapsed() {
// return ticks / 18; // return ticks / 18;
return ticks; return ticks / 18;
} }
int getTime(char option) { int getTime(char option) {
@ -33,6 +33,11 @@ int getTime(char option) {
} }
} }
// TODO
void wait(long seconds) { void wait(long seconds) {
while (seconds_elapsed() < seconds); // int initialSeconds = getTime(SECONDS);
// while (initialSeconds - seconds != initialSeconds) {
// }
// while (seconds_elapsed() < seconds);
} }

View File

@ -74,15 +74,21 @@ void printRegs() {
*rspValueA = (uint64_t) getSampleRSP(); *rspValueA = (uint64_t) getSampleRSP();
} }
void exitProcess();
static void startOver() { static void startOver() {
unsigned char key = 0;
while (key != '\n') {
_sti();
haltcpu();
key = getKeyFromBuffer();
}
clear(); clear();
exitProcess();
// TODO : hacer wait con getTimegen
// unsigned char key = 0;
// while (key != '\n') {
// _sti();
// haltcpu();
// key = getKeyFromBuffer();
// }
// clear();
// cleanProcesses(); // cleanProcesses();
// moveToWindowVideo(1); // moveToWindowVideo(1);
//((fn)sampleCodeAddress)(); //((fn)sampleCodeAddress)();

View File

@ -2,6 +2,9 @@
#include "systemCalls.h" #include "systemCalls.h"
void exitProcess(); void exitProcess();
// void setFn(uint64_t, uint64_t, uint64_t);
char * processes();
void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t);
uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8) { uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8) {
switch (rdi) { switch (rdi) {
@ -18,6 +21,11 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
case 4: case 4:
exitProcess(); exitProcess();
break; break;
case 5:
// setFn(rsi, rdx, rcx);
break;
case 6:
return (uint64_t) processes();
default: default:
return -1; return -1;
} }

View File

@ -63,7 +63,7 @@ uint64_t nextProcess() {
if (currentProcess->next != NULL) if (currentProcess->next != NULL)
currentProcess = currentProcess->next; currentProcess = currentProcess->next;
else { else {
// ncPrint("Una venezolana para el socio biza"); // ncPrint("Una colombiana para el socio biza");
// wait(4); // wait(4);
currentProcess = firstReady; currentProcess = firstReady;
} }
@ -81,6 +81,11 @@ void initScheduler() {
enqueueProcess(idle, 0, 1, argv); enqueueProcess(idle, 0, 1, argv);
} }
// void setFn(void (*fn) (int, char **), int argc, char *argv[]) {
// currentProcess->rsp = (currentProcess->rbp) - 20;
// _initialize_stack_frame(fn, currentProcess->rbp, argc, argv);
// }
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) if (firstReady != NULL && firstReady->pid == INIT_PID)
block(INIT_PID); block(INIT_PID);
@ -313,50 +318,72 @@ char getProcessData(char * out, processCDT * proc) {
char written = 0; char written = 0;
char flag = 0; char flag = 0;
for (int j = 0; j < MAX_ATTR_SIZE; j++) { for (int j = 0; j < MAX_NAME_SIZE; j++) {
if (proc->name[j] != 0) if (!flag && proc->name[j] == 0)
flag = 1; flag = 1;
if (flag){ else if (flag)
out += addSpaces(out, 1); out += addSpaces(out, 1);
} else else
*out++ = proc->name[j]; *out++ = proc->name[j];
} }
written += MAX_ATTR_SIZE; written += MAX_NAME_SIZE;
out += addSpaces(out, 2); out += addSpaces(out, 2);
written += 2; written += 2;
char buffer[10]; char buffer[10];
written += strcpy(out, itoa(proc->pid, buffer, 10, 10)); char copied = strcpy(out, itoa(proc->pid, buffer, 10, 10));
out += copied;
out += addSpaces(out, MAX_ATTR_SIZE - copied);
written += MAX_ATTR_SIZE - copied;
out += addSpaces(out, 2); out += addSpaces(out, 2);
written += copied + 2;
buffer[0] = 0; // buffer = itoa(proc->priority, buffer, 10, 2);
written += strcpy(out, itoa(proc->priority, buffer, 10, 2)); copied = strcpy(out, itoa(proc->priority, buffer, 10, 2));
out += copied;
out += addSpaces(out, MAX_ATTR_SIZE - copied);
written += MAX_ATTR_SIZE - copied;
out += addSpaces(out, 2); out += addSpaces(out, 2);
written += copied + 2;
buffer[0] = 0; copied = strcpy(out, itoa(proc->rsp, buffer, 16, 10));
written += strcpy(out, itoa(proc->rsp, buffer, 16, 10)); out += copied;
out += addSpaces(out, MAX_NAME_SIZE - copied);
written += MAX_NAME_SIZE - copied;
out += addSpaces(out, 2); out += addSpaces(out, 2);
written += copied + 2;
buffer[0] = 0; copied = strcpy(out, itoa(proc->rbp, buffer, 16, 10));
written += strcpy(out, itoa(proc->rbp, buffer, 16, 10)); out += copied;
out += addSpaces(out, MAX_NAME_SIZE - copied);
written += MAX_NAME_SIZE - copied;
out += addSpaces(out, 2); out += addSpaces(out, 2);
written += copied + 2;
buffer[0] = 0; copied = strcpy(out, (proc->foreground == 1) ? "F" : "B");
written += strcpy(out, (proc->foreground == 1) ? "F" : "B"); out += copied;
out += addSpaces(out, MAX_ATTR_SIZE - copied);
written += MAX_ATTR_SIZE - copied;
out += addSpaces(out, 2);
written += copied + 2;
// out += addSpaces(out, 2); // out += addSpaces(out, 2);
// *out++;
// *out = '\0';
return written; return written;
} }
char * processes(){ char * processes(){
char * ans = pvPortMalloc(pids * PROCESS_DATA_MAX_SIZE); char * ans = pvPortMalloc(pids * PROCESS_DATA_MAX_SIZE);
char * ret = ans;
int lastPid = getPid(); char * info = "name pid prio rsp rbp fore\n";
if (lastPid == EXIT_FAILURE) strcpy(ans, info);
return NULL; ans += 56;
processCDT * aux = firstReady; processCDT * aux = firstReady;
while (aux != NULL){ while (aux != NULL) {
char writtenChars = getProcessData(ans, aux); char writtenChars = getProcessData(ans, aux);
if (writtenChars == EXIT_FAILURE) if (writtenChars == EXIT_FAILURE)
return NULL; return NULL;
@ -371,7 +398,8 @@ char * processes(){
aux = aux->next; aux = aux->next;
} }
*ans = 0; *ans = 0;
return ans;
return ret;
} }
/* /*

View File

@ -1,4 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "libc.h"
#include "scheduler.h"
#define MAX_SEM 100 #define MAX_SEM 100
#define MAX_NAME 100 #define MAX_NAME 100
@ -11,8 +15,15 @@ typedef struct sem {
unsigned int value; unsigned int value;
char name[MAX_NAME]; char name[MAX_NAME];
// cola FIFO // cola FIFO
pid_t * entering;
pid_t * last;
} sem_t; } sem_t;
typedef struct pid_t {
int pid;
struct pid_t * next;
} pid_t;
// Podemos manejarnos con indices. // Podemos manejarnos con indices.
static sem_t semaphores[MAX_SEM]; static sem_t semaphores[MAX_SEM];
/* /*
@ -31,37 +42,63 @@ 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);
//
sem_t * sem = pvPortMalloc(sizeof(sem_t));
strcpy(sem->name, name);
sem->value = value;
leave_region(&semLock); leave_region(&semLock);
} }
int semClose(sem_t * semaphore) { int semClose(sem_t * sem) {
if (sem == NULL)
return EXIT_FAILURE;
enter_region(&semLock); enter_region(&semLock);
//
pid_t * pid = sem->entering;
while (pid != NULL) {
pid_t * aux = pid;
pid = pid->next;
vPortFree(aux);
}
vPortFree(sem);
leave_region(&semLock); leave_region(&semLock);
} }
int semWait(sem_t * semaphore) { int semWait(sem_t * sem) {
enter_region(&semLock); enter_region(&semLock);
if (semaphore->value > 0) { if (sem->value > 0) {
semaphore->value--; sem->value--;
} }
else { else {
// leave_region(&semLock); leave_region(&semLock);
// enter_region(&semLock);
// semaphore->value--;
// mandarlo a noni. es decir bloquearlo y solo despertarlo cuando alguien hace post pid_t * curr = pvPortMalloc(sizeof(pid_t));
// block(con el pid) curr->pid = getPid();
// curr->next = NULL;
sem->last->next = curr;
sem->last = curr;
block(curr->pid);
enter_region(&semLock);
sem->value--;
} }
leave_region(&semLock); leave_region(&semLock);
} }
int semPost(sem_t * semaphore) { int semPost(sem_t * sem) {
enter_region(&semLock); enter_region(&semLock);
//
sem->value++;
pid_t * aux = sem->entering;
sem->entering = sem->entering->next;
unblock(sem->entering->pid);
pvPortFree(aux);
leave_region(&semLock); leave_region(&semLock);
} }

View File

@ -3,7 +3,7 @@ GLOBAL _getMem, sys_loadProcess
GLOBAL raiseOpcodeExc GLOBAL raiseOpcodeExc
GLOBAL _getRegs, sys_switchContext GLOBAL _getRegs, sys_switchContext
GLOBAL cpu_id, cpu_id_support GLOBAL cpu_id, cpu_id_support
GLOBAL sys_exit GLOBAL sys_exit, sys_exec, sys_fork, sys_ps
section .text section .text
@ -70,6 +70,66 @@ sys_exit:
pop rbp pop rbp
ret ret
sys_fork:
push rbp
mov rbp, rsp
push rdi
push rsi
push rdx
mov rdx, rbp
mov rsi, rsp
mov rdi, 3
int 80h
pop rdx
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_exec:
push rbp
mov rbp, rsp
push rdi
push rsi
push rdx
push rcx
mov rcx, rdx
mov rdx, rsi
mov rsi, rdi
mov rdi, 5
int 80h
pop rcx
pop rdx
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_ps:
push rbp
mov rbp, rsp
push rdi
mov rdi, 6
int 80h
pop rdi
mov rsp, rbp
pop rbp
ret
_getMem: _getMem:
push rbp push rbp
mov rbp, rsp mov rbp, rsp

View File

@ -16,12 +16,12 @@ void inforeg(char * window, int * offset) {
char bufferAux[20]; char bufferAux[20];
for (int i = 0; i < 17; i++) { for (int i = 0; i < 17; i++) {
if (i % 3 == 0 && i != 0) if (i % 5 == 0 && i != 0)
substractLine(window, offset); substractLine(window, offset);
addText(regsNames[i], window, offset); addText(regsNames[i], window, offset);
addText("0x", window, offset); addText("0x", window, offset);
addText(gtoa(regs[i], bufferAux, 16, 20), window, offset); addText(gtoa(regs[i], bufferAux, 16, 20), window, offset);
if (i != 14 && i % 3 != 2) if (i != 14 && i % 5 != 4)
addText(" - ", window, offset); addText(" - ", window, offset);
} }
printWindow(window); printWindow(window);

View File

@ -0,0 +1,11 @@
#include "libc.h"
#include "shell.h"
char * sys_ps();
void ps(char * window, int * offset) {
char * output = sys_ps();
addText(output, window, offset);
printWindow(window);
substractLine(window, offset);
}

View File

@ -0,0 +1,6 @@
#ifndef PS_LIB
#define PS_LIB
void ps(char * window, int * offset);
#endif

View File

@ -10,6 +10,7 @@
#include "quadratic.h" #include "quadratic.h"
#include "cpu_id.h" #include "cpu_id.h"
#include "change.h" #include "change.h"
#include "ps.h"
#define SIZE 100 #define SIZE 100
#define MAX_ARGS 5 #define MAX_ARGS 5
@ -18,8 +19,8 @@
#define ROWS 25 #define ROWS 25
const int len = 8; const int len = 8;
char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "change"}; char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps"};
void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, change}; void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps};
void substractLine(char * window, int * offset) { void substractLine(char * window, int * offset) {
for (int i = 0; i < ROWS - 1; i++) { for (int i = 0; i < ROWS - 1; i++) {
@ -36,7 +37,11 @@ void substractLine(char * window, int * offset) {
void addText(char * buffer, char * window, int * offset) { void addText(char * buffer, char * window, int * offset) {
while (*buffer != 0) { while (*buffer != 0) {
if (*offset == ROWS * COLS - 1) substractLine(window, offset); if (*offset == ROWS * COLS - 1) substractLine(window, offset);
if (*buffer == '\n') {
buffer++;
substractLine(window, offset);
}
window[(*offset)++] = *buffer++; window[(*offset)++] = *buffer++;
} }
} }