Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tarea2 #297

Open
wants to merge 6 commits into
base: riscv
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ CFLAGS += -MD
CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I.
CFLAGS += -Ikernel
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)

# Disable PIE when possible (for Ubuntu 16.10 toolchain)
Expand Down Expand Up @@ -132,6 +133,8 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_test_getancestor\
$U/_prioritytest\

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
Expand Down
26 changes: 24 additions & 2 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ allocproc(void)
found:
p->pid = allocpid();
p->state = USED;
p->priority = 0; // Prioridad inicial
p->boost = 1; // Boost inicial

// Allocate a trapframe page.
if((p->trapframe = (struct trapframe *)kalloc()) == 0){
Expand Down Expand Up @@ -455,17 +457,37 @@ scheduler(void)
for(p = proc; p < &proc[NPROC]; p++) {
acquire(&p->lock);
if(p->state == RUNNABLE) {
struct proc *q;
for (q = proc; q < &proc[NPROC]; q++) {
if (q->state != ZOMBIE) {
q->priority += q->boost;

if (q->priority >= 9) {
q->boost = -1;
} else if (q->priority <= 0) {
q->boost = 1;
}
}}
struct proc *highest = 0;
for (q = proc; q < &proc[NPROC]; q++) {
if (q->state == RUNNABLE && (!highest || q->priority < highest->priority)) {
highest = q;
}
}
// Switch to chosen process. It is the process's job
// to release its lock and then reacquire it
// before jumping back to us.
// {before jumping back to us.
if (highest) {
p = highest;
p->state = RUNNING;
c->proc = p;
printf("Ejecutando proceso %s %d\n", p->name, p->pid);
swtch(&c->context, &p->context);

// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
}
}}
release(&p->lock);
}
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state
struct proc {
struct spinlock lock;
int priority; // Prioridad del proceso
int boost;

// p->lock must be held when using these:
enum procstate state; // Process state
Expand Down
5 changes: 5 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ extern uint64 sys_unlink(void);
extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
extern uint64 sys_getppid(void);
extern uint64 sys_getancestor(void);

// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
Expand All @@ -126,6 +128,8 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_getpid] sys_getppid,
[SYS_getancestor] sys_getancestor
};

void
Expand All @@ -145,3 +149,4 @@ syscall(void)
p->trapframe->a0 = -1;
}
}

2 changes: 2 additions & 0 deletions kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_getppid 22
#define SYS_getancestor 23
34 changes: 32 additions & 2 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "memlayout.h"
#include "spinlock.h"
#include "proc.h"
#include "syscall.h"

uint64
sys_exit(void)
Expand All @@ -15,10 +16,39 @@ sys_exit(void)
return 0; // not reached
}

// Implementación de getppid
uint64
sys_getpid(void)
sys_getppid(void)
{
return myproc()->pid;
struct proc *curproc = myproc();
if (curproc->parent) // Si el proceso tiene un padre
return curproc->parent->pid;
return -1; // Si no tiene padre
}

// Implementación de getancestor
uint64
sys_getancestor(void)
{
int level;

// Obtener el argumento del nivel
argint(0, &level);

// Validar que el nivel no sea negativo
if (level < 0)
return -1;

struct proc *p = myproc(); // Proceso actual

// Recorre hacia arriba por los ancestros
for (int i = 0; i < level; i++) {
if (!p->parent) // Si no hay más ancestros válidos
return -1;
p = p->parent; // Avanzar al proceso padre
}

return p->pid; // Retornar el PID del ancestro
}

uint64
Expand Down
25 changes: 25 additions & 0 deletions user/prioritytest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

void busy_wait(int seconds) {
int start = uptime();
while (uptime() - start < seconds)
;
}

int main() {
for (int i = 0; i < 20; i++) {
if (fork() == 0) {
printf("Proceso hijo creado: PID = %d\n", getpid());
busy_wait(2);
exit(0);
}
}

for (int i = 0; i < 20; i++) {
wait(0);
}

exit(0);
}
20 changes: 20 additions & 0 deletions user/test_getancestor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../kernel/types.h"
#include "user.h"


int main() {
printf("PID actual: %d\n", getpid());
printf("PPID: %d\n", getppid());

for (int i = 0; i <= 3; i++) {
int ancestor = getancestor(i);
if (ancestor == -1) {
printf("Nivel %d: No hay ancestro\n", i);
} else {
printf("Nivel %d: PID %d\n", i, ancestor);
}
}

exit(0); // Terminar con éxito
return 0; // Solo por convención, aunque exit detiene la ejecución
}
1 change: 1 addition & 0 deletions user/ulib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
#include "kernel/syscall.h"

//
// wrapper so that it's OK if main() does not call exit().
Expand Down
4 changes: 4 additions & 0 deletions user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime(void);
int getppid(void);
int getancestor(int);
int syscall(int num);


// ulib.c
int stat(const char*, struct stat*);
Expand Down
2 changes: 2 additions & 0 deletions user/usys.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ sub entry {
entry("sbrk");
entry("sleep");
entry("uptime");
entry("getppid");
entry("getancestor");