Skip to content

Commit

Permalink
Pipes implemented (not tested yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuArce committed May 23, 2021
1 parent 8aa3fa3 commit 9d5041e
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Kernel/include/pipes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PIPES_H
#define PIPES_H

int pipeOpen(int fd[2]);

int pipeClose(int fd);

int pipeRead(int fd, int length, char * buffer);

int pipeWrite(int fd, int length, char * buffer);

#endif //PIPES_H
1 change: 1 addition & 0 deletions Kernel/include/syscallDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memoryManagement.h>
#include <semaphore.h>
#include <defs.h>
#include <pipes.h>

#define MEM_BYTES 32

Expand Down
21 changes: 6 additions & 15 deletions Kernel/interruptions/syscallDispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,17 @@ uint64_t syscallDispatcher(Params* p) {
}

uint64_t readHandler(uint64_t fd, uint64_t length, uint64_t toRead, uint64_t fourthP, uint64_t fifthP, uint64_t sixthP, uint64_t seventhP) {
if(fd == STDIN){
if(getFdIn() == STDIN){ //read from keyboard
return readBuffer((int) length, (char *) toRead);
} else {

}
if(fd == STDIN && getFdIn() == STDIN){ //read from keyboard
return readBuffer(length, (char *) toRead);
}
return readBuffer((int) length, (char *) toRead);
return pipeRead(fd, length, (char *) toRead);
}

uint64_t writeHandler(uint64_t fd, uint64_t length, uint64_t toWrite, uint64_t row, uint64_t col, uint64_t color, uint64_t seventhP) {
if(fd == STDOUT){
if(getFdOut() == STDOUT){ //write to screen
return printStringFrom((char *) toWrite, (int) length, (int) row, (int) col, (int) color);
} else {
println("//TODO");
//TODO
}
if(fd == STDOUT && getFdOut() == STDOUT){ //write to screen
return printStringFrom((char *) toWrite, (int) length, (int) row, (int) col, (int) color);
}
return printStringFrom((char *) toWrite, (int) length, (int) row, (int) col, (int) color);
return pipeWrite(fd, length, (char *) toWrite);
}

uint64_t drawHandler(uint64_t matrix, uint64_t row, uint64_t col, uint64_t rows, uint64_t columns, uint64_t sixthP, uint64_t seventhP) {
Expand Down
137 changes: 137 additions & 0 deletions Kernel/utilities/pipes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include <pipes.h>
#include <memoryManagement.h>
#include <queue.h>
#include <scheduler.h>

#define TOTAL_PIPES 10
#define MAX_BUFFER 256

typedef struct Pipe {
unsigned int fdIn;
unsigned int fdOut;
char buffer[MAX_BUFFER];
unsigned int readPos;
unsigned int writePos;
QueueADT blockedProcesses;
uint64_t attached;
uint64_t isActive;
uint64_t inClosed;
uint64_t outClosed;

} Pipe;

static Pipe pipes[TOTAL_PIPES];

static int openedPipes = 0;

static int nextFd = 3;

static int searchPipe(int fd);

/*
* Convención: fd[0]: lectura | fd[1]: escritura
* */
int pipeOpen(int fd[2]) {
if (openedPipes == TOTAL_PIPES - 1) {
return -1;
}

int i;
for (i = 0; i < TOTAL_PIPES; i++) {
if (!pipes[i].isActive) {
break;
}
}
pipes[i].fdIn = nextFd++;
pipes[i].fdOut = nextFd++;

pipes[i].inClosed = 0;
pipes[i].outClosed = 0;

pipes[i].readPos = 0;
pipes[i].writePos = 0;

pipes[i].blockedProcesses = newQueue();

pipes[i].isActive = 1;
openedPipes++;
return 0;
}

int pipeClose(int fd) {
int index = searchPipe(fd);
if(index != -1){
if (pipes[index].fdIn == fd) {
pipes[index].inClosed = 1;
}
if (pipes[index].fdOut == fd) {
pipes[index].outClosed = 1;
}

if(pipes[index].inClosed){
freeQueue(pipes[index].blockedProcesses);
openedPipes--;
pipes[index].isActive = 0;
}
return 0;
}
return -1;
}

int pipeRead(int fd, int length, char * buffer) {
int index = searchPipe(fd);
if(index == -1 || pipes[index].fdOut == fd || pipes[index].inClosed) {
return 0;
}

Pipe* aux = &pipes[index];
if(aux->readPos == aux->writePos){
if(aux->outClosed){
return -1; //EOF
}
int pid = getPid();
enqueue(aux->blockedProcesses ,pid);
sleep(pid);
}

int i;
for (i = 0; aux->readPos != aux->writePos && i < length; i++) {
buffer[i] = aux->buffer[aux->readPos];
aux->buffer[aux->readPos] = 0;
aux->readPos = (aux->readPos + 1) % MAX_BUFFER;
}

buffer[i] = 0;
return i;
}

int pipeWrite(int fd, int length, char * buffer) {
int index = searchPipe(fd);
if(index == -1 || pipes[index].fdIn == fd || pipes[index].outClosed) {
return 0;
}

Pipe* aux = &pipes[index];

int i;
for(i = 0; i < length; i++) {
aux->buffer[aux->writePos] = buffer[i];
aux->writePos = (aux->writePos + 1) % MAX_BUFFER;
}

//TODO preguntar caso donde una lee, quedan cosas para leer que quiere leer otro. Los dos quedaron en la cola al hacer su read
if(!isEmpty(aux->blockedProcesses)) {
wakeup(dequeue(aux->blockedProcesses));
}
return i;
}


static int searchPipe(int fd) {
for (int i = 0; i < TOTAL_PIPES; i++) {
if (pipes[i].isActive && (pipes[i].fdIn == fd || pipes[i].fdOut == fd)) {
return i;
}
}
return -1;
}

0 comments on commit 9d5041e

Please sign in to comment.