Add last changes

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-11-01 22:42:46 -03:00
parent ab402462c5
commit 3c7957ce17
44 changed files with 861 additions and 859 deletions

View File

@ -19,8 +19,6 @@ GLOBAL _exception6Handler
EXTERN irqDispatcher
EXTERN exceptionDispatcher
EXTERN systemCallsDispatcher
; EXTERN preserveStack
; EXTERN newStack
EXTERN changeWindow
EXTERN updateRSP
EXTERN nextProcess
@ -28,7 +26,6 @@ EXTERN nextProcess
GLOBAL switchContext
GLOBAL loadProcess
GLOBAL _initialize_stack_frame
;GLOBAL _switchContext, forceTimerAux
EXTERN getFPUaddress, getSSEaddress
EXTERN checkSleeping
@ -76,10 +73,9 @@ SECTION .text
fsave [bytesForFPU]
fxsave [bytesForSSEAligned]
mov rdi, %1 ; pasaje de parametro
mov rdi, %1
call irqDispatcher
; signal pic EOI (End of Interrupt)
mov al, 20h
out 20h, al
@ -99,7 +95,7 @@ SECTION .text
pushState
mov rdi, %1 ; pasaje de parametro
mov rdi, %1
mov rsi, [insPointer]
mov rdx, [rspPointer]
mov rcx, rsp
@ -168,7 +164,7 @@ picMasterMask:
picSlaveMask:
push rbp
mov rbp, rsp
mov ax, di ; ax = mascara de 16 bits
mov ax, di
out 0A1h,al
pop rbp
retn

View File

@ -94,4 +94,4 @@ getRegs:
ret
section .bss
regs resb 120 ; 8 bytes * 16 regs
regs resb 120

View File

@ -13,5 +13,6 @@ char * itoa(int value, char * buffer, int base, int length);
void strlen(const char *str, int *len);
int strcpy(char * strDest, const char * strSrc);
char addSpaces(char * str, char qty);
int strcmp(const char *s1, const char *s2);
#endif

View File

@ -0,0 +1,12 @@
#ifndef IRQDISP_H
#define IRQDISP_H
#include <stdint.h>
void keyboard_handler();
void timer_handler();
static void int_20();
static void int_21();
#endif

View File

@ -0,0 +1,11 @@
#ifndef SYSCALLSDISP_H
#define SYSCALLSDISP_H
#include <stdint.h>
#include "systemCalls.h"
#include "memManager.h"
#include "pipeLib.h"
#include "semLib.h"
#include "schedulerLib.h"
#endif

View File

@ -1,9 +1,4 @@
#include <stdint.h>
static void int_20();
static void int_21();
void keyboard_handler();
void timer_handler();
#include "irqDispatcher.h"
void irqDispatcher(uint64_t irq) {
switch (irq) {

View File

@ -1,9 +1,4 @@
#include <stdint.h>
#include "systemCalls.h"
#include "memManager.h"
#include "pipeLib.h"
#include "semLib.h"
#include "schedulerLib.h"
#include "systemCallsDispatcher.h"
uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9) {
switch (rdi) {
@ -59,6 +54,8 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
return wait();
case 22:
return pipes();
case 23:
return quitCPU();
default:
return -1;
}

View File

@ -1,5 +1,4 @@
#include <stdint.h>
// #include <string.h>
#include <lib.h>
#include <moduleLoader.h>
#include <naiveConsole.h>
@ -29,11 +28,7 @@ void clearBSS(void *bssAddress, uint64_t bssSize) {
}
void *getStackBase() {
return (void *) (
(uint64_t) & endOfKernel
+ PageSize * 8 //The size of the stack itself, 32KiB
- sizeof(uint64_t) //Begin at the top of the stack
);
return (void *) ((uint64_t) &endOfKernel + PageSize * 8 - sizeof(uint64_t));
}
void *initializeKernelBinary() {
@ -49,140 +44,24 @@ void *initializeKernelBinary() {
}
void load_idt();
uint64_t getRSP();
void printBottlerAndWait();
#include "test_util.h"
#ifdef BUDDY
#define MAX_BLOCKS 128
#define MAX_MEMORY 800
#else
#define MAX_BLOCKS 42//1024/24 //128
#define MAX_MEMORY 352//1024-42x16 //Should be around 80% of memory managed by the MM
#endif
typedef struct MM_rq {
void *address;
uint32_t size;
} mm_rq;
void test_mm() {
mm_rq mm_rqs[MAX_BLOCKS];
uint8_t rq;
uint32_t total;
int i = 1000;
while (i-- != 0) {
// while (1){
rq = 0;
total = 0;
// Request as many blocks as we can
while (rq < MAX_BLOCKS && total < MAX_MEMORY) {
mm_rqs[rq].size = GetUniform(MAX_MEMORY - total - 1) + 1;
mm_rqs[rq].address = pvPortMalloc(mm_rqs[rq].size);
// mm_rqs[rq].address = memMalloc(mm_rqs[rq].size);
if (mm_rqs[rq].address == NULL) {
for (int rqAux = 0; rqAux < rq; rqAux++) {
ncPrintDec(mm_rqs[rqAux].size);
ncPrint(" - ");
}
// printStringLen(13, "malloc() -- ERROR", 20);
// new_line();
ncPrint("Malloc -- null");
ncNewline();
}
total += mm_rqs[rq].size;
rq++;
}
// ncPrint("libre dps de malloc: ");
// ncPrintDec(xPortGetFreeHeapSize());
// ncNewline();
// ncPrint("cumpli maximo");
// ncNewline();
// Set
uint32_t i;
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
memset(mm_rqs[i].address, i, mm_rqs[i].size);
// Check
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
if (!memcheck(mm_rqs[i].address, i, mm_rqs[i].size)) {
// printStringLen(13, "memCheck() -- ERROR", 20);
// new_line();
ncPrint("memCheck -- null");
ncNewline();
}
// Free
for (i = 0; i < rq; i++) {
if (mm_rqs[i].address != NULL)
vPortFree(mm_rqs[i].address);
}
// memFree(mm_rqs[i].address);
// ncPrint("libre dps de free: ");
// ncPrintDec(xPortGetFreeHeapSize());
// ncNewline();
ncPrint("termine un loop regio");
ncNewline();
// if (i == 5)
// wait(3);
// ncClear();
// printStringLen(13, "ALL GOOD", 9);
// new_line();
}
}
// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
void initScheduler();
void _cli();
void _sti();
void haltcpu();
void forceTimer();
int main() {
// _cli();
load_idt();
// if (initMemoryManager(memoryModuleAddress, memoryModuleAddress + sizeof(void *)) == -1) {
// printStringLen(13, "createMemoryManager() -- ERROR", 31);
// new_line();
// return EXIT_FAILURE;
// }
initMemoryManager(memoryModuleAddress);
initScheduler();
#ifndef BUDDY
// SACAR DESPUÉS! ES SOLO PARA TESTEO... CORRER DE A UNO!
// if (testOne() == EXIT_FAILURE)
// return EXIT_FAILURE;
// if (testTwo() == EXIT_FAILURE)
// return EXIT_FAILURE;
#endif
// test_mm();
saveSampleRSP(getRSP());
char *argv[] = {"SampleCode"};
enqueueProcess(sampleCodeModuleAddress, 1, 1, argv, NULL);
clear();
// haltcpu();
_sti();
forceTimer();

View File

@ -95,4 +95,12 @@ int strcpy(char *strDest, const char *strSrc) {
i++;
}
return i;
}
int strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *s1 - *s2;
}

View File

@ -1,54 +0,0 @@
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include "test_util.h"
// #define MAX_BLOCKS 128
// #define MAX_MEMORY 1024 //Should be around 80% of memory managed by the MM
// typedef struct MM_rq{
// void *address;
// uint32_t size;
// }mm_rq;
// void test_mm(){
// mm_rq mm_rqs[MAX_BLOCKS];
// uint8_t rq;
// uint32_t total;
// while (1){
// rq = 0;
// total = 0;
// // Request as many blocks as we can
// while(rq < MAX_BLOCKS && total < MAX_MEMORY){
// mm_rqs[rq].size = GetUniform(MAX_MEMORY - total - 1) + 1;
// mm_rqs[rq].address = malloc(mm_rqs[rq].size); // TODO: Port this call as required
// //TODO: check if NULL
// total += mm_rqs[rq].size;
// rq++;
// }
// // Set
// uint32_t i;
// for (i = 0; i < rq; i++)
// if (mm_rqs[i].address != NULL)
// memset(mm_rqs[i].address, i, mm_rqs[i].size); // TODO: Port this call as required
// // Check
// for (i = 0; i < rq; i++)
// if (mm_rqs[i].address != NULL)
// if(!memcheck(mm_rqs[i].address, i, mm_rqs[i].size))
// printf("ERROR!\n"); // TODO: Port this call as required
// // Free
// for (i = 0; i < rq; i++)
// if (mm_rqs[i].address != NULL)
// free(mm_rqs[i].address); // TODO: Port this call as required
// }
// }
// int main(){
// test_mm();
// return 0;
// }

View File

@ -1,107 +0,0 @@
// #include <stdint.h>
// #include <stdio.h>
// #define MINOR_WAIT 1000000 // TODO: To prevent a process from flooding the screen
// #define WAIT 10000000 // TODO: Long enough to see theese processes beeing run at least twice
// uint64_t my_getpid(){
// return 0;
// }
// uint64_t my_create_process(char * name){
// return 0;
// }
// uint64_t my_nice(uint64_t pid, uint64_t newPrio){
// return 0;
// }
// uint64_t my_kill(uint64_t pid){
// return 0;
// }
// uint64_t my_block(uint64_t pid){
// return 0;
// }
// uint64_t my_unblock(uint64_t pid){
// return 0;
// }
// void bussy_wait(uint64_t n){
// uint64_t i;
// for (i = 0; i < n; i++);
// }
// void endless_loop(){
// uint64_t pid = my_getpid();
// while(1){
// printf("%d ",pid);
// bussy_wait(MINOR_WAIT);
// }
// }
// #define TOTAL_PROCESSES 3
// void test_prio(){
// uint64_t pids[TOTAL_PROCESSES];
// uint64_t i;
// for(i = 0; i < TOTAL_PROCESSES; i++)
// pids[i] = my_create_process("endless_loop");
// bussy_wait(WAIT);
// printf("\nCHANGING PRIORITIES...\n");
// for(i = 0; i < TOTAL_PROCESSES; i++){
// switch (i % 3){
// case 0:
// my_nice(pids[i], 0); //lowest priority
// break;
// case 1:
// my_nice(pids[i], 1); //medium priority
// break;
// case 2:
// my_nice(pids[i], 2); //highest priority
// break;
// }
// }
// bussy_wait(WAIT);
// printf("\nBLOCKING...\n");
// for(i = 0; i < TOTAL_PROCESSES; i++)
// my_block(pids[i]);
// printf("CHANGING PRIORITIES WHILE BLOCKED...\n");
// for(i = 0; i < TOTAL_PROCESSES; i++){
// switch (i % 3){
// case 0:
// my_nice(pids[i], 1); //medium priority
// break;
// case 1:
// my_nice(pids[i], 1); //medium priority
// break;
// case 2:
// my_nice(pids[i], 1); //medium priority
// break;
// }
// }
// printf("UNBLOCKING...\n");
// for(i = 0; i < TOTAL_PROCESSES; i++)
// my_unblock(pids[i]);
// bussy_wait(WAIT);
// printf("\nKILLING...\n");
// for(i = 0; i < TOTAL_PROCESSES; i++)
// my_kill(pids[i]);
// }
// int main(){
// test_prio();
// return 0;
// }

View File

@ -1,101 +0,0 @@
// #include <stdio.h>
// #include "test_util.h"
// //TO BE INCLUDED
// void endless_loop(){
// while(1);
// }
// uint32_t my_create_process(char * name){
// return 0;
// }
// uint32_t my_kill(uint32_t pid){
// return 0;
// }
// uint32_t my_block(uint32_t pid){
// return 0;
// }
// uint32_t my_unblock(uint32_t pid){
// return 0;
// }
// #define MAX_PROCESSES 10 //Should be around 80% of the the processes handled by the kernel
// enum State {ERROR, RUNNING, BLOCKED, KILLED};
// typedef struct P_rq{
// uint32_t pid;
// enum State state;
// }p_rq;
// void test_processes(){
// p_rq p_rqs[MAX_PROCESSES];
// uint8_t rq;
// uint8_t alive = 0;
// uint8_t action;
// while (1){
// // Create MAX_PROCESSES processes
// for(rq = 0; rq < MAX_PROCESSES; rq++){
// p_rqs[rq].pid = my_create_process("endless_loop"); // TODO: Port this call as required
// if (p_rqs[rq].pid == -1){ // TODO: Port this as required
// printf("Error creating process\n"); // TODO: Port this as required
// return;
// }else{
// p_rqs[rq].state = RUNNING;
// alive++;
// }
// }
// // Randomly kills, blocks or unblocks processes until every one has been killed
// while (alive > 0){
// for(rq = 0; rq < MAX_PROCESSES; rq++){
// action = GetUniform(2) % 2;
// switch(action){
// case 0:
// if (p_rqs[rq].state == RUNNING || p_rqs[rq].state == BLOCKED){
// if (my_kill(p_rqs[rq].pid) == -1){ // TODO: Port this as required
// printf("Error killing process\n"); // TODO: Port this as required
// return;
// }
// p_rqs[rq].state = KILLED;
// alive--;
// }
// break;
// case 1:
// if (p_rqs[rq].state == RUNNING){
// if(my_block(p_rqs[rq].pid) == -1){ // TODO: Port this as required
// printf("Error blocking process\n"); // TODO: Port this as required
// return;
// }
// p_rqs[rq].state = BLOCKED;
// }
// break;
// }
// }
// // Randomly unblocks processes
// for(rq = 0; rq < MAX_PROCESSES; rq++)
// if (p_rqs[rq].state == BLOCKED && GetUniform(2) % 2){
// if(my_unblock(p_rqs[rq].pid) == -1){ // TODO: Port this as required
// printf("Error unblocking process\n"); // TODO: Port this as required
// return;
// }
// p_rqs[rq].state = RUNNING;
// }
// }
// }
// }
// int main(){
// test_processes();
// return 0;
// }

View File

@ -1,84 +0,0 @@
// #include <stdint.h>
// #include <stdio.h>
// uint64_t my_create_process(char * name){
// return 0;
// }
// uint64_t my_sem_open(char *sem_id, uint64_t initialValue){
// return 0;
// }
// uint64_t my_sem_wait(char *sem_id){
// return 0;
// }
// uint64_t my_sem_post(char *sem_id){
// return 0;
// }
// uint64_t my_sem_close(char *sem_id){
// return 0;
// }
// #define TOTAL_PAIR_PROCESSES 2
// #define SEM_ID "sem"
// int64_t global; //shared memory
// void slowInc(int64_t *p, int64_t inc){
// int64_t aux = *p;
// aux += inc;
// yield();
// *p = aux;
// }
// void inc(uint64_t sem, int64_t value, uint64_t N){
// uint64_t i;
// if (sem && !my_sem_open(SEM_ID, 1)){
// printf("ERROR OPENING SEM\n");
// return;
// }
// for (i = 0; i < N; i++){
// if (sem) my_sem_wait(SEM_ID);
// slowInc(&global, value);
// if (sem) my_sem_post(SEM_ID);
// }
// if (sem) my_sem_close(SEM_ID);
// printf("Final value: %d\n", global);
// }
// void test_sync(){
// uint64_t i;
// global = 0;
// printf("CREATING PROCESSES...(WITH SEM)\n");
// for(i = 0; i < TOTAL_PAIR_PROCESSES; i++){
// my_create_process("inc", 1, 1, 1000000);
// my_create_process("inc", 1, -1, 1000000);
// }
// }
// void test_no_sync(){
// uint64_t i;
// global = 0;
// printf("CREATING PROCESSES...(WITHOUT SEM)\n");
// for(i = 0; i < TOTAL_PAIR_PROCESSES; i++){
// my_create_process("inc", 0, 1, 1000000);
// my_create_process("inc", 0, -1, 1000000);
// }
// }
// int main(){
// test_sync();
// return 0;
// }

View File

@ -1,27 +0,0 @@
#include <stdint.h>
#include "test_util.h"
static uint32_t m_z = 362436069;
static uint32_t m_w = 521288629;
uint32_t GetUint(){
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
}
uint32_t GetUniform(uint32_t max){
uint32_t u = GetUint();
return (u + 1.0) * 2.328306435454494e-10 * max;
}
uint8_t memcheck(void *start, uint8_t value, uint32_t size){
uint8_t *p = (uint8_t *) start;
uint32_t i;
for (i = 0; i < size; i++, p++)
if (*p != value)
return 0;
return 1;
}

View File

@ -20,5 +20,6 @@ char unblockFirst(int pid);
void sleep(int secs);
char blockIO();
void unblockIO();
char getState(int pid);
#endif

View File

@ -1,268 +1,231 @@
#ifdef BUDDY
// #include <stdio.h>
// Basado en: https://github.com/cloudwu/buddy/blob/master/buddy.c
#include <stdlib.h>
// #include <time.h>
// #include <math.h>
#include <string.h>
#define MANAGED_MEMORY_SIZE 1024
#include <stddef.h>
#include <stdint.h>
// https://github.com/sdpetrides/BuddyAllocator
#define NODE_UNUSED 0
#define NODE_USED 1
#define NODE_SPLIT 2
#define NODE_FULL 3
// char initMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) {
// return 1;
// }
#define LEVEL 20
// void * memMalloc(const size_t memoryToAllocate) {
// return NULL;
// }
#define SIZE (1<<LEVEL)
typedef struct meta {
unsigned char allo : 1; // 0000000_ - allocated
unsigned char left : 1; // 000000_0 - first or second
unsigned char size : 6; // ______00 - n where (2^n)-1 is the block size
} Meta;
struct buddy{
int level;
uint8_t tree[SIZE*2-1];
};
// static char myblock[MANAGED_MEMORY_SIZE];
static char *myblock;
void initMemoryManager(void * managedMemory) {
myblock = managedMemory;
static struct buddy self;
void * buddy_alloc(int);
int buddy_free(void *);
void * pvPortMalloc(int size){
return buddy_alloc(size);
}
void unpack(Meta * m, int pos);
/* Fills myblock with zeros and creates first metadata */
void init_block() {
memset(&myblock, '\0', MANAGED_MEMORY_SIZE);
memset(&myblock, 54, 1);
int vPortFree(void * pointer){
return buddy_free(pointer);
}
int ceil(float num) {
int inum = (int)num;
if (num == (float)inum) {
return inum;
}
return inum + 1;
static int heapAddress = 0;
void initMemoryManager(void *managedMemSize) {
heapAddress = (int) managedMemSize;
self.level = LEVEL;
memset(self.tree , NODE_UNUSED , SIZE*2-1);
}
unsigned int log2(unsigned int n)
{
return (n > 1) ? 1 + log2(n / 2) : 0;
int is_pow_of_2(uint32_t x) {
return !(x & (x-1));
}
// /* Returns log base 2 of a double d */
// double log2(double d) {
// return log(d) / log(2);
// }
/* Returns the level a reqSize will fit in */
int size_to_n(size_t reqSize) {
reqSize+=1;
double d = log2((double)reqSize);
return (int)ceil(d);
uint32_t next_pow_of_2(uint32_t x) {
if ( is_pow_of_2(x) )
return x;
x |= x>>1;
x |= x>>2;
x |= x>>4;
x |= x>>8;
x |= x>>16;
return x+1;
}
/* Returns the position of the next block of the correct size */
int jump_next(int n, int pos) {
int bits = pos>>(n);
bits+=1;
int ret = bits<<(n);
if (ret == MANAGED_MEMORY_SIZE) {
return ret;
} else {
return ret;
}
#define PAGE_SIZE 0x1000
int _index_offset(int index, int level, int max_level) {
return (int*)((((index + 1) - (1 << level)) << (max_level - level)) * PAGE_SIZE + heapAddress);
}
/* Returns the position of the left half of a pair */
int jump_back(int n, int pos) {
int bits = pos>>(n);
bits-=1;
return bits<<(n);
void _mark_parent(int index) {
while(1) {
int buddy = index - 1 + (index & 1) * 2;
if (buddy > 0 && (self.tree[buddy] == NODE_USED || self.tree[buddy] == NODE_FULL)) {
index = (index + 1) / 2 - 1;
self.tree[index] = NODE_FULL;
} else {
return;
}
}
}
/* Fills a Meta struct with metadata at pos */
void unpack(Meta * m, int pos) {
memset(m, myblock[pos], 1);
void * buddy_alloc(int s) {
if(s%PAGE_SIZE != 0){
s=(s/PAGE_SIZE)+1;
}else{
s/=PAGE_SIZE;
}
int size;
if (s==0) {
size = 1;
} else {
size = (int)next_pow_of_2(s);
}
int length = 1 << self.level;
if (size > length)
return NULL;
int index = 0;
int level = 0;
int nextStep = 1;
while (index >= 0) {
nextStep = 1;
if (size == length) {
if (self.tree[index] == NODE_UNUSED) {
self.tree[index] = NODE_USED;
_mark_parent(index);
return _index_offset(index, level, self.level);
}
} else {
switch (self.tree[index]) {
case NODE_USED: break;
case NODE_FULL: break;
case NODE_UNUSED:
self.tree[index] = NODE_SPLIT;
self.tree[index*2+1] = NODE_UNUSED;
self.tree[index*2+2] = NODE_UNUSED;
default:
index = index * 2 + 1;
length /= 2;
level++;
nextStep = 0;
}
}
if( nextStep ){
if (index & 1) {
++index;
} else {
int cont = 1;
while(cont) {
level--;
length *= 2;
index = (index+1)/2 -1;
if (index < 0)
return NULL;
if (index & 1) {
++index;
cont = 0;
}
}
}
}
}
return NULL;
}
/* Returns whether position at level n is left or right partner */
int is_left(int n, int pos) {
// Manipulate bits to set nth bit on
int k = 1;
k<<=(n);
// Manipulate bits to zero bits above n
unsigned int p = (unsigned int)pos;
p<<=(31-n);
p>>=(31-n);
if (k == p) {
return 0; // Right
} else {
return 1; // Left
}
void _combine(int index) {
while(1) {
int buddy = index - 1 + (index & 1) * 2;
if (buddy < 0 || self.tree[buddy] != NODE_UNUSED) {
self.tree[index] = NODE_UNUSED;
while (((index = (index + 1) / 2 - 1) >= 0) && self.tree[index] == NODE_FULL){
self.tree[index] = NODE_SPLIT;
}
return;
}
index = (index + 1) / 2 - 1;
}
}
/* Mergee two unallocated blocks with same size */
void merge(int pos, int pos2, int n) {
int buddy_free(void * pointer) {
// Create new meta and set size field
char newMeta = (n+1)<<2;
int offset=(int)pointer;
// Set left field
if (is_left(n+1, pos)) {
newMeta+=2;
}
offset=(offset- heapAddress)/PAGE_SIZE;
// Add new meta
myblock[pos] = newMeta;
// Delete meta on right partner
myblock[pos2] = 0;
if (offset >= (1<< self.level)){
return -1;
}
int left = 0;
int length = 1 << self.level;
int index = 0;
while(1) {
switch (self.tree[index]) {
case NODE_USED:
if (offset != left){
return -1;
}
_combine(index);
return 0;
case NODE_UNUSED:
return -1;
default:
length /= 2;
if (offset < left + length) {
index = index * 2 + 1;
} else {
left += length;
index = index * 2 + 2;
}
break;
}
}
}
/* MYmymalloc */
void * pvPortMalloc(size_t reqSize) {
// Check if too big
if (reqSize > MANAGED_MEMORY_SIZE - 1) {
// fprintf(stderr, "Error: Requested size too large\n");
return NULL;
}
// Traverse heap to find block of correct size - algo(n)
int n = size_to_n(reqSize);
int pos = 0;
unsigned char c = 0;
Meta * m = memset(&c, 0, 1);
while (pos < MANAGED_MEMORY_SIZE) {
// Read metadata
unpack(m, pos);
// // Debugging
// if (m->size == 0) {
// exit(0);
// }
if (n <= m->size) {
if (m->allo == 1) {
// Jump
pos = jump_next(n, pos);
continue;
} else if (m->size == n) {
// Allocate
myblock[pos]+=1;
pos+=1;
return (void*)((long int)&myblock+pos);
} else {
// Partition
// Get partner position
int partner = jump_next((m->size)-1, pos);
// Set Left
char meta_1 = 2;
char meta_2 = 0;
// Set Size
char s = ((m->size)-1)<<2;
meta_1 = (meta_1 | s);
meta_2 = (meta_2 | s);
// Fill in metadata
myblock[pos] = meta_1;
myblock[partner] = meta_2;
// Continue on same position with new size of block
continue;
}
} else {
// Jump
pos = jump_next(n, pos);
continue;
}
}
//error
return 0;
static void dumpMM(struct buddy * self, int index , int level) {
char buffer[100];
switch (self->tree[index]) {
case NODE_UNUSED:
printStringLen("(%d:%d)", _index_offset(index, level, self->level) , 1 << (self->level - level));
break;
case NODE_USED:
printStringLen("(", 1);
printStringLen(itoa(_index_offset(index, level, self->level), buffer, 10));
printStringLen(":", 1);
printStringLen(itoa(1 << (self->level - level), buffer, 10));
printStringLen(")", 1);
break;
case NODE_FULL:
printStringLen("{", 1);
_dump(self, index * 2 + 1, level+1);
_dump(self, index * 2 + 2, level+1);
printStringLen("}", 1);
break;
default:
printStringLen("(", 1);
_dump(self, index * 2 + 1, level+1);
_dump(self, index * 2 + 2, level+1);
printStringLen(")", 1);
break;
}
}
/* MYmyfree */
void vPortFree(void * ptr) {
// Error Checking
if (ptr <= (void *)&myblock || ptr > (void *)(&myblock + MANAGED_MEMORY_SIZE)) {
//error
return;
}
// Get position
// int pos = (int)(ptr-(void *)&myblock-1);
int pos = (int)((char *)ptr-(char *)&myblock-1);
// Check if valid metadata location
if (pos%2 == 1 || myblock[pos] == 0) {
//error
return;
}
// Initialize variables for merge
unsigned char c1 = 0;
unsigned char c2 = 0;
Meta * m1 = memset(&c1, 0, 1);
Meta * m2 = memset(&c2, 0, 1);
unpack(m1,pos);
// Change allocated field
myblock[pos] = myblock[pos] - 1;
while (pos >= 0 && pos <= 8196){
// Read metadata
unpack(m1,pos);
if (m1->left) { // Left Partner
// Get position of other partner and read metadata
int pos2 = jump_next(m1->size, pos);
if (pos2 >= 0 && pos2 <= MANAGED_MEMORY_SIZE - 2) {
unpack(m2,pos2);
} else {
break;
}
// Merge or break
if (m2->allo || m2->size != m1->size) {
break;
} else {
merge(pos, pos2, m1->size);
}
} else { // Right Partner
// Get position of other partner and read metadata
int pos2 = jump_back(m2->size,pos);
if (pos2 >= 0 && pos2 <= MANAGED_MEMORY_SIZE -2) {
unpack(m2,pos2);
} else {
break;
}
// Merge or break
if (m2->allo || m2->size != m1->size) {
break;
} else {
merge(pos2, pos, m1->size);
}
}
}
void buddy_dumpMM(struct buddy * self) {
dumpMM(self, 0 , 0);
printStringLen("\n", 1);
}
#endif

View File

@ -188,14 +188,6 @@ void *pvPortMalloc(size_t xWantedSize) {
}
} else {
// ncNewline();
// ncPrint("MALLOC: ");
// ncPrintDec(xFreeBytesRemaining);
// ncPrint(" ");
// ncPrintDec(xWantedSize);
// ncPrint(" ");
// ncPrintDec(configADJUSTED_HEAP_SIZE);
// ncNewline();
}
}
@ -227,12 +219,12 @@ void vPortFree(void *pv) {
allocated. */
pxLink->xBlockSize &= ~xBlockAllocatedBit;
{
// {
/* Add this block to the list of free blocks. */
xFreeBytesRemaining += pxLink->xBlockSize;
prvInsertBlockIntoFreeList(((BlockLink_t *) pxLink));
xNumberOfSuccessfulFrees++;
}
// }
}
}
}

View File

@ -58,7 +58,7 @@ uint64_t nextProcess(uint64_t currentRSP) {
currentProcess->rsp = currentRSP;
processCDT *prev = currentProcess;
if (currentProcess != NULL && currentProcess->state == READY &&
currentProcess->executions == MAX_PRIORITY - currentProcess->priority + 1) {
currentProcess->executions == MAX_PRIORITY - currentProcess->priority) {
currentProcess->executions = 0;
currentProcess = currentProcess->next;
}
@ -115,8 +115,7 @@ int enqueueProcess(void (*fn)(int, char **), char foreground, int argc, char *ar
block(IDLE_PID);
if (currentProcess != NULL) {
if (foreground)
currentProcess->children++;
currentProcess->children++;
if (currentProcess->foreground && foreground) {
currentProcess->foreground = 0;
currentProcess->backWait = 1;
@ -230,18 +229,9 @@ void unblockIO() {
char blockIO() {
if (currentProcess == NULL || currentProcess->state == DEAD)
return EXIT_FAILURE;
else {
currentProcess->state = BLOCKEDIO;
}
// processCDT *prev = NULL;
// processADT parent = searchProcess(&prev, currentProcess->ppid, firstProcess);
// if (currentProcess->foreground) {
// if (parent->backWait) {
// parent->backWait = 0;
// parent->foreground = 1;
// }
// }
currentProcess->state = BLOCKEDIO;
currentProcess->executions = 0;
forceTimer();
return EXIT_SUCCESS;
@ -264,6 +254,7 @@ char block(int pid) {
}
}
if (pid == currentProcess->pid) {
currentProcess->executions = 0;
forceTimer();
}
@ -275,9 +266,9 @@ char unblock(int pid) {
processADT del = searchProcess(&prev, pid, firstProcess);
if (del == NULL || del->state == DEAD) {
return EXIT_FAILURE;
} else {
del->state = READY;
}
del->state = READY;
processADT parent = searchProcess(&prev, del->ppid, firstProcess);
if (del->foreground) {
@ -289,6 +280,46 @@ char unblock(int pid) {
return EXIT_SUCCESS;
}
char unblockFirst(int pid) {
processADT prev = NULL;
processADT del = searchProcess(&prev, pid, firstProcess);
if (del == NULL || del->state == DEAD) {
return EXIT_FAILURE;
}
removeProcess(del, &firstProcess, &lastProcess);
if (currentProcess != NULL) {
currentProcess->executions = MAX_PRIORITY - currentProcess->priority;
del->next = currentProcess->next;
currentProcess->next = del;
if (currentProcess == lastProcess)
lastProcess = del;
}
else {
if (firstProcess == NULL) {
del->next = NULL;
firstProcess = del;
lastProcess = del;
}
else {
del->next = firstProcess->next;
firstProcess = del;
}
}
del->state = READY;
processADT parent = searchProcess(&prev, del->ppid, firstProcess);
if (del->foreground) {
if (parent->foreground) {
parent->backWait = 1;
parent->foreground = 0;
}
}
forceTimer();
return EXIT_SUCCESS;
}
char kill(int pid) {
processADT prev = NULL;
processADT del = searchProcess(&prev, pid, firstProcess);
@ -297,19 +328,21 @@ char kill(int pid) {
}
processADT parent = searchProcess(&prev, del->ppid, firstProcess);
if (parent != NULL && del->foreground) {
if (parent != NULL) {
parent->children--;
if (!parent->children && parent->state == WAITING) {
parent->state = READY;
}
if (parent->backWait) {
if (parent->backWait && del->foreground) {
parent->backWait = 0;
parent->foreground = 1;
}
}
vPortFree(del->fd);
vPortFree((void *) del->rbp - STACK_SIZE);
vPortFree(del->name);
void *auxi = del->rbp - STACK_SIZE;
vPortFree((void *) auxi);
del->state = DEAD;
if (pid == currentProcess->pid) {
@ -367,15 +400,27 @@ char updateRSP(uint64_t newRsp) {
int getPid() {
if (currentProcess == NULL)
return EXIT_FAILURE;
return -1;
return currentProcess->pid;
}
char getState(int pid){
processCDT * aux = firstProcess;
while (aux != NULL) {
if (aux->pid == pid){
break;
}
aux = aux -> next;
}
return aux == NULL ? -1 : aux->state;
}
char quitCPU() {
int pid = getPid();
if (pid == EXIT_FAILURE)
if (pid == -1)
return EXIT_FAILURE;
// return block(pid);
currentProcess->executions = MAX_PRIORITY - currentProcess->priority;
forceTimer();
return EXIT_SUCCESS;
}

View File

@ -10,9 +10,27 @@ typedef struct node_t {
node_t *firstSem = NULL;
static char counter = 0;
node_t * searchSem(char * name, node_t ** prev) {
*prev = NULL;
node_t * aux = firstSem;
while (aux != NULL) {
if (!strcmp(name, aux->sem->name))
break;
}
return aux;
}
sem_t *semOpen(char *name, unsigned int value) {
enter_region(&semLock);
node_t * prev = NULL;
node_t * aux = searchSem(name, &prev);
if (aux != NULL) {
leave_region(&semLock);
return aux->sem;
}
sem_t *sem = pvPortMalloc(sizeof(sem_t));
node_t *node = pvPortMalloc(sizeof(node_t));
node->sem = sem;
@ -74,6 +92,8 @@ char semClose(sem_t *sem) {
return EXIT_SUCCESS;
}
#include "video.h"
void semWait(sem_t *sem) {
enter_region(&semLock);
@ -109,8 +129,10 @@ void semPost(sem_t *sem) {
sem->entering = sem->entering->next;
if (sem->entering == NULL)
sem->last = NULL;
unblock(aux->pid);
leave_region(&semLock);
unblockFirst(aux->pid);
vPortFree(aux);
enter_region(&semLock);
}
leave_region(&semLock);

View File

@ -5,7 +5,7 @@ GLOBAL _getRegs, sys_switchContext
GLOBAL cpu_id, cpu_id_support
GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem, sys_openPipe, sys_semClose
GLOBAL sys_nice, sys_semWait, sys_semPost, sys_semOpen, sys_sleep, sys_kill, sys_getPid,
GLOBAL sys_block, sys_unblock, sys_wait, sys_pipes
GLOBAL sys_block, sys_unblock, sys_wait, sys_pipes, sys_quitCPU
section .text
@ -227,6 +227,21 @@ sys_getPid:
pop rbp
ret
sys_quitCPU:
push rbp
mov rbp, rsp
push rdi
mov rdi, 23
int 80h
pop rdi
mov rsp, rbp
pop rbp
ret
sys_wait:
push rbp
mov rbp, rsp

View File

@ -2,63 +2,63 @@
void bottler(int argc, char **argv) {
printStringLen(" ", 80);
new_line();
newline();
printStringLen(" (%( ", 80);
new_line();
newline();
printStringLen(" Welcome to", 17);
printStringLen(" %%%%% ", 80);
new_line();
newline();
printStringLen(" BottlerOS", 18);
printStringLen(" %%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%% %%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%% %%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%% %%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%% ", 27);
printStringLen(" %%%% %%%% ", 22);
printStringLen(" %%%%% ", 30);
new_line();
newline();
printStringLen(" %%%%% ", 28);
printStringLen(" (% %( ", 21);
printStringLen(" %%%%% ", 30);
new_line();
newline();
printStringLen(" %%%%%%% %%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%% ", 41);
printStringLen(" % %* ", 8);
printStringLen(" %%%%%%%%% ", 30);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%**%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/%% ", 80);
new_line();
newline();
printStringLen(" %%* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /%% ", 80);
new_line();
newline();
printStringLen(" %%* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /%% ", 80);
new_line();
newline();
printStringLen(" %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% ", 80);
new_line();
newline();
printStringLen(" ,%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%. ", 80);
new_line();
newline();
printStringLen(" %%. %%%%%%%%%%%%%%%%% .%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%%%%%%%% ", 80);
new_line();
newline();
printStringLen(" %%%%%%% ", 80);
new_line();
newline();
printStringLen(" ", 80);
new_line();
newline();
sys_sleep(3);
winClear();

View File

@ -30,8 +30,10 @@ void sys_semPost(sem_t *);
sem_t * sys_semOpen(char *, unsigned int);
int sys_getPid();
char sys_semClose(void *);
void sys_kill(int pid);
void sys_block(int pid);
void sys_unblock(int pid);
char sys_kill(int pid);
char sys_block(int pid);
char sys_unblock(int pid);
void sys_quitCPU();
void sys_wait();
#endif

View File

@ -42,7 +42,7 @@ void putChar(char c) {
sys_write(1, &buffer, 0);
}
void new_line() {
void newline() {
putChar('\n');
}
@ -50,6 +50,10 @@ void backspace() {
putChar('\b');
}
void addEOF() {
putChar(-1);
}
void winClear() {
sys_write(1, "\e\f", 2);
}

View File

@ -8,7 +8,7 @@ int main(int argc, char *argv[]) {
winClear();
char *argv1[] = {"bottler"};
sys_loadProcess(bottler, 1, 1, argv1, NULL);
sys_loadProcess(bottler, 0, 1, argv1, NULL);
sys_wait();
char *argv2[] = {"shell"};

View File

@ -1,4 +1,4 @@
#include "phylo.h"
#include "phyloLib.h"
philosopher_t *firstPhil;
sem_t *mutex;
@ -154,7 +154,6 @@ void phylo(int argc, char ** argv) {
} while (phil != firstPhil);
char c;
// while (1) {
while ((c = getChar()) != 0 && c != -1) {
if (c == 'a') {
addPhilo();
@ -165,8 +164,8 @@ void phylo(int argc, char ** argv) {
else if (c == 'q') {
end();
}
// sys_sleep(1);
}
// }
sys_exit();
}
@ -195,6 +194,6 @@ void end() {
}
/*
* Taken from "Operating Systems - Design and Implementation" by Tanenbaum
* Section 2.3, page 91, Figure 2-10
*/
* Inspirados en "Operating Systems - Design and Implementation" de Tanenbaum
* Sección 2.3, pág. 91
*/

View File

@ -1,7 +1,6 @@
#include "libc.h"
#include "shell.h"
#include "pipes.h"
void pipes(int argc, char *argv[]) {
void pipe(int argc, char *argv[]) {
char * output = sys_pipes();
printString(output);
newline();

View File

@ -0,0 +1,69 @@
#include "test_mm.h"
typedef struct MM_rq{
void *address;
uint32_t size;
} mm_rq;
static uint32_t m_z = 362436069;
static uint32_t m_w = 521288629;
uint32_t GetUint(){
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w;
}
uint32_t GetUniform(uint32_t max){
uint32_t u = GetUint();
return (u + 1.0) * 2.328306435454494e-10 * max;
}
uint8_t memcheck(void *start, uint8_t value, uint32_t size){
uint8_t *p = (uint8_t *) start;
uint32_t i;
for (i = 0; i < size; i++, p++)
if (*p != value)
return 0;
return 1;
}
void test_mm(int argc, char ** argv){
mm_rq mm_rqs[MAX_BLOCKS];
uint8_t rq;
uint32_t total;
while (1){
rq = 0;
total = 0;
while(rq < MAX_BLOCKS && total < MAX_MEMORY){
mm_rqs[rq].size = GetUniform(MAX_MEMORY - total - 1) + 1;
mm_rqs[rq].address = sys_malloc(mm_rqs[rq].size);
//TODO: check if NULL
total += mm_rqs[rq].size;
rq++;
}
// Set
uint32_t i;
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
memset(mm_rqs[i].address, i, mm_rqs[i].size);
// Check
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
if(!memcheck(mm_rqs[i].address, i, mm_rqs[i].size))
printStringLen("ERROR!\n", 8);
// Free
for (i = 0; i < rq; i++)
if (mm_rqs[i].address != NULL)
sys_free(mm_rqs[i].address);
}
sys_exit();
}

View File

@ -0,0 +1,88 @@
#include "test_prio.h"
#define MINOR_WAIT 10000000 // TODO: To prevent a process from flooding the screen
#define WAIT 1000000000 // TODO: Long enough to see theese processes beeing run at least twice
void bussy_wait(uint64_t n){
uint64_t i;
for (i = 0; i < n; i++);
}
void endless_loop2(){
uint64_t pid = sys_getPid();
while(1){
char buffer[10];
printStringLen(itoa(pid, buffer, 10), 10);
// sys_sleep(1);
bussy_wait(MINOR_WAIT);
}
sys_exit();
}
#define TOTAL_PROCESSES 3
void test_prio(int argc, char ** argv){
uint64_t pids[TOTAL_PROCESSES];
uint64_t i;
for(i = 0; i < TOTAL_PROCESSES; i++) {
char *argv[] = {"test_prio"};
pids[i] = sys_loadProcess(endless_loop2, 0, 1, argv, NULL);
}
bussy_wait(WAIT);
// sys_sleep(5);
printStringLen("\nCHANGING PRIORITIES...\n", 25);
for(i = 0; i < TOTAL_PROCESSES; i++){
switch (i % 3){
case 0:
sys_nice(pids[i], 19); //lowest priority
break;
case 1:
sys_nice(pids[i], 0); //medium priority
break;
case 2:
sys_nice(pids[i], -20); //highest priority
break;
}
}
bussy_wait(WAIT);
// sys_sleep(5);
printStringLen("\nBLOCKING...\n", 14);
for(i = 0; i < TOTAL_PROCESSES; i++)
sys_block(pids[i]);
printStringLen("CHANGING PRIORITIES WHILE BLOCKED...\n", 38);
for(i = 0; i < TOTAL_PROCESSES; i++){
switch (i % 3){
case 0:
sys_nice(pids[i], 0); //medium priority
break;
case 1:
sys_nice(pids[i], 0); //medium priority
break;
case 2:
sys_nice(pids[i], 0); //medium priority
break;
}
}
printStringLen("UNBLOCKING...\n", 15);
for(i = 0; i < TOTAL_PROCESSES; i++)
sys_unblock(pids[i]);
bussy_wait(WAIT);
// sys_sleep(5);
printStringLen("\nKILLING...\n", 13);
for(i = 0; i < TOTAL_PROCESSES; i++)
sys_kill(pids[i]);
sys_exit();
}

View File

@ -0,0 +1,103 @@
#include "test_processes.h"
#include "ps.h"
#define MAX_PROCESSES 10 //Should be around 80% of the the processes handled by the kernel
enum State {ERROR, RUNNING, BLOCKED, KILLED};
typedef struct P_rq{
uint32_t pid;
enum State state;
}p_rq;
void endless_loop(){
while(1);
sys_exit();
}
void debugPSA() {
return;
}
void test_processes(int argc, char ** argv){
p_rq p_rqs[MAX_PROCESSES];
uint8_t rq;
uint8_t alive = 0;
uint8_t action;
int i = 0;
while (1) {
debugPSA();
// Create MAX_PROCESSES processes
for(rq = 0; rq < MAX_PROCESSES; rq++){
char *argv[] = {"endless"};
p_rqs[rq].pid = sys_loadProcess(endless_loop, 0, 1, argv, NULL); // TODO: Port this call as required
sys_nice(p_rqs[rq].pid, 19);
i++;
if (p_rqs[rq].pid == -1){ // TODO: Port this as required
printStringLen("Error creating process\n", 24); // TODO: Port this as required
return;
}else{
p_rqs[rq].state = RUNNING;
alive++;
}
}
// Randomly kills, blocks or unblocks processes until every one has been killed
while (alive > 0){
for(rq = 0; rq < MAX_PROCESSES; rq++){
action = GetUniform(2) % 2;
switch(action){
case 0:
if (p_rqs[rq].state == RUNNING || p_rqs[rq].state == BLOCKED){
sys_kill(p_rqs[rq].pid);
p_rqs[rq].state = KILLED;
alive--;
}
break;
case 1:
if (p_rqs[rq].state == RUNNING){
if(sys_block(p_rqs[rq].pid) == -1){ // TODO: Port this as required
printStringLen("Error blocking process\n", 24); // TODO: Port this as required
return;
}
p_rqs[rq].state = BLOCKED;
}
break;
}
}
// Randomly unblocks processes
for(rq = 0; rq < MAX_PROCESSES; rq++)
if (p_rqs[rq].state == BLOCKED && GetUniform(2) % 2){
if(sys_unblock(p_rqs[rq].pid) == -1){ // TODO: Port this as required
printStringLen("Error unblocking process\n", 26); // TODO: Port this as required
return;
}
p_rqs[rq].state = RUNNING;
}
}
if (i % 10000 == 0) {
printStringLen("Procesos: ", 11);
char buffer[4];
printString(itoa(i, buffer, 10));
newline();
}
// char buffer[6] = {0};
// winClear();
// char buffer[4];
// printString(itoa(i, buffer, 10));
// printStringLen(" procesos creados\n", 19);
// char * output = sys_ps();
// printString(output);
// newline();
}
sys_exit();
}

View File

@ -0,0 +1,81 @@
#include "test_sync.h"
#define TOTAL_PAIR_PROCESSES 2
#define SEM_ID "sem"
#define ITERS "2000"
static int global;
void slowInc(int *p, int inc){
int aux = *p;
aux += inc;
sys_quitCPU();
*p = aux;
}
void inc(int argc, char ** argv) {
if (argc != 4) {
printStringLen("inc recibe 4 argumentos\n", 25);
sys_exit();
return;
}
uint64_t semDir = (uint64_t) atoi(argv[1], 14);
int64_t value = atoi(argv[2], 10);
uint64_t N = atoi(argv[3], 10);
uint64_t i;
char buff[10] = {0};
for (i = 0; i < N; i++){
if (semDir) sys_semWait(semDir);
slowInc(&global, value);
if (semDir) sys_semPost(semDir);
}
char buffer[10] = {0};
printStringLen("Final value: ", 14);
printStringLen(itoa(global, buffer, 10), 10);
newline();
sys_exit();
}
void test_sync(int argc, char ** argv){
uint64_t i;
global = 0;
printStringLen("CREATING PROCESSES...(WITH SEM)\n", 33);
sem_t * sem = sys_semOpen(SEM_ID, 1);
char semDir[10] = {0};
itoa((int) sem, semDir, 10);
for(i = 0; i < TOTAL_PAIR_PROCESSES; i++){
char * argv1[] = {"inc", semDir, "1", ITERS};
char * argv2[] = {"inc", semDir, "-1", ITERS};
sys_loadProcess(inc, 0, 4, argv1, NULL);
sys_loadProcess(inc, 0, 4, argv2, NULL);
}
sys_wait();
sys_semClose(sem);
sys_exit();
}
void test_no_sync(int argc, char ** argv){
uint64_t i;
global = 0;
printStringLen("CREATING PROCESSES...(WITHOUT SEM)\n", 36);
for(i = 0; i < TOTAL_PAIR_PROCESSES; i++){
char * argv1[] = {"inc", "0", "1", ITERS};
char * argv2[] = {"inc", "0", "-1", ITERS};
sys_loadProcess(inc, 0, 4, argv1, NULL);
sys_loadProcess(inc, 0, 4, argv2, NULL);
}
sys_exit();
}

View File

@ -0,0 +1,27 @@
#include <stdint.h>
#include "test_util.h"
// static uint32_t m_z = 362436069;
// static uint32_t m_w = 521288629;
// uint32_t GetUint(){
// m_z = 36969 * (m_z & 65535) + (m_z >> 16);
// m_w = 18000 * (m_w & 65535) + (m_w >> 16);
// return (m_z << 16) + m_w;
// }
// uint32_t GetUniform(uint32_t max){
// uint32_t u = GetUint();
// return (u + 1.0) * 2.328306435454494e-10 * max;
// }
// uint8_t memcheck(void *start, uint8_t value, uint32_t size){
// uint8_t *p = (uint8_t *) start;
// uint32_t i;
// for (i = 0; i < size; i++, p++)
// if (*p != value)
// return 0;
// return 1;
// }

View File

@ -1,33 +1,6 @@
#ifndef PHYLO_H
#define PHYLO_H
#include <stddef.h>
#include <stdint.h>
#include "libc.h"
#include "system.h"
#include "phyloLib.h"
#define ARGV_SIZE 2
#define BUFF_SIZE 20
#define MAX_PHILO_SIZE 3
#define MAX_NAME_SIZE 10
#define STARTING 5
int * state;
typedef enum states {EATING = 0, HUNGRY, THINKING} states;
typedef struct philosopher_t {
int debug;
char ** argv;
char * buffer;
sem_t * sem;
int pid;
states state;
struct philosopher_t * left;
struct philosopher_t * right;
} philosopher_t;
void philosopher(int argc, char ** argv);
void phylo(int argc, char *argv[]);
#endif

View File

@ -1,6 +1,33 @@
#ifndef PHYLOLIB_H
#define PHYLOLIB_H
void phylo(int argc, char *argv[]);
#include <stddef.h>
#include <stdint.h>
#include "libc.h"
#include "system.h"
#include "phylo.h"
#define ARGV_SIZE 2
#define BUFF_SIZE 20
#define MAX_PHILO_SIZE 3
#define MAX_NAME_SIZE 10
#define STARTING 5
int * state;
typedef enum states {EATING = 0, HUNGRY, THINKING} states;
typedef struct philosopher_t {
int debug;
char ** argv;
char * buffer;
sem_t * sem;
int pid;
states state;
struct philosopher_t * left;
struct philosopher_t * right;
} philosopher_t;
void philosopher(int argc, char ** argv);
#endif

View File

@ -1,6 +1,9 @@
#ifndef PIPES_LIB
#define PIPES_LIB
void pipes(int argc, char *argv[]);
#include "libc.h"
#include "shell.h"
void pipe(int argc, char *argv[]);
#endif

View File

@ -1,5 +1,5 @@
#ifndef SHELL
#define SHELL
#ifndef SHELL_H
#define SHELL_H
#include "system.h"
#include "libc.h"
@ -18,14 +18,18 @@
#include "wc.h"
#include "filter.h"
#include "cat.h"
#include "semCom.h"
#include "sems.h"
#include "stddef.h"
#include "nice.h"
#include "phyloLib.h"
#include "phylo.h"
#include "kill.h"
#include "block.h"
#include "unblock.h"
#include "loop.h"
#include "test_prio.h"
#include "test_sync.h"
#include "test_processes.h"
#include "test_mm.h"
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0

View File

@ -0,0 +1,14 @@
#ifndef TESTMM_H
#define TESTMM_H
#include "test_util.h"
#include "system.h"
#include <stddef.h>
#include "libc.h"
void test_mm(int argc, char *argv[]);
#define MAX_BLOCKS 128
#define MAX_MEMORY 0.8 * 1024 * 1024 * 512 //Should be around 80% of memory managed by the MM
#endif

View File

@ -0,0 +1,12 @@
#ifndef TESTPRIO_H
#define TESTPRIO_H
#include <stdint.h>
#include <stddef.h>
#include "libc.h"
#include "system.h"
#include "libc.h"
void test_prio(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,13 @@
#ifndef TESTPROC_H
#define TESTPROC_H
#include <stdint.h>
#include <stddef.h>
#include "libc.h"
#include "system.h"
#include "test_util.h"
#include "libc.h"
void test_processes(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,12 @@
#ifndef TESTSYNC_H
#define TESTSYNC_H
#include <stdint.h>
#include <stddef.h>
#include "system.h"
#include "libc.h"
void test_sync(int argc, char *argv[]);
void test_no_sync(int argc, char ** argv);
#endif

View File

@ -14,15 +14,19 @@ cmd_t commands[] = {
{"cpufeatures", cpufeatures, 0, 1},
{"nice", nice, 0, 1},
{"ps", ps, 0, 1},
{"pipes", pipes, 0, 1},
{"pipe", pipe, 0, 1},
{"kill", kill, 1, 1},
{"sem", sem, 0, 1},
{"quadratic", quadratic, 0, 1},
{"printmem", printmem, 0, 1},
{"phylo", phylo, 0, 1},
{"wc", wc, 0, 1},
{"loop", loop, 0, 0},
{"loopcaca", loop, 0, 1},
{"loop", loop, 0, 1},
{"test_mm", test_mm, 0, 1},
{"test_prio", test_prio, 0, 1},
{"test_processes", test_processes, 0, 1},
{"test_sync", test_sync, 0, 1},
{"test_no_sync", test_no_sync, 0, 1},
{NULL, NULL, 0, 0}
};
@ -112,6 +116,10 @@ void processInput(char *input) {
if (comm_flag0 && pipe != -1) {
for (int i = 0; commands[i].name != NULL; i++) {
if (!strcmp(tokens[pipe + 1], commands[i].name)) {
if (commands->isBuiltIn && ampersand) {
printStringLen("built-in commands are not able to run in background\n", 53);
return;
}
comm_flag1 = 1;
comm1 = i;
break;
@ -151,7 +159,7 @@ void shell(int argc, char *argv[]) {
char buffer[SIZE] = {0};
while (scanfNoPrint(buffer) != 0) {
new_line();
newline();
processInput(buffer);
printStringLen("$> ", 3);
}
@ -165,5 +173,5 @@ void incorrect_comm(char *buffer) {
void incorrect_arg(char *command) {
printStringLen("Incorrect arguments for command ", 33);
printString(command);
new_line();
newline();
}

View File

@ -1 +1 @@
ke
be