diff --git a/Makefile b/Makefile index 39a99d7a8f..b1a523e775 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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) diff --git a/kernel/syscall.c b/kernel/syscall.c index ed654094cb..f2bce8037c 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -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. @@ -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 @@ -145,3 +149,4 @@ syscall(void) p->trapframe->a0 = -1; } } + diff --git a/kernel/syscall.h b/kernel/syscall.h index bc5f35651c..697498d9ba 100644 --- a/kernel/syscall.h +++ b/kernel/syscall.h @@ -20,3 +20,5 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_getppid 22 +#define SYS_getancestor 23 \ No newline at end of file diff --git a/kernel/sysproc.c b/kernel/sysproc.c index 1de184e0ab..4746145ad7 100644 --- a/kernel/sysproc.c +++ b/kernel/sysproc.c @@ -5,6 +5,7 @@ #include "memlayout.h" #include "spinlock.h" #include "proc.h" +#include "syscall.h" uint64 sys_exit(void) @@ -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 diff --git a/user/test_getancestor.c b/user/test_getancestor.c new file mode 100644 index 0000000000..93593faf94 --- /dev/null +++ b/user/test_getancestor.c @@ -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 +} diff --git a/user/ulib.c b/user/ulib.c index c7b66c4756..16255f8fdb 100644 --- a/user/ulib.c +++ b/user/ulib.c @@ -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(). diff --git a/user/user.h b/user/user.h index 4d398d5069..9977cea400 100644 --- a/user/user.h +++ b/user/user.h @@ -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*); diff --git a/user/usys.pl b/user/usys.pl index 01e426e6b3..4bc7749aa1 100755 --- a/user/usys.pl +++ b/user/usys.pl @@ -36,3 +36,5 @@ sub entry { entry("sbrk"); entry("sleep"); entry("uptime"); +entry("getppid"); +entry("getancestor"); \ No newline at end of file