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

Tarea1 #296

Open
wants to merge 5 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
2 changes: 2 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,7 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_test_getancestor\

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
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
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");