diff --git a/apps/src/init.c b/apps/src/init.c index 6ceb235e..a1c86ff7 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -64,7 +64,7 @@ int main() printf("Welcome to Luna!\n"); - printf("Running as PID %ld\n\n", getpid()); + printf("Running as PID %ld, PPID %ld\n\n", getpid(), getppid()); sleep(1); @@ -163,7 +163,7 @@ int main() if (child == 0) { msleep(500); - printf("I am the child, who is my parent?\n"); + printf("I am the child (PID %ld), my parent is PID %ld!!\n", getpid(), getppid()); execv("/bin/sym", NULL); perror("execv"); return 1; diff --git a/libs/libc/include/bits/getprocid.h b/libs/libc/include/bits/getprocid.h new file mode 100644 index 00000000..0336f904 --- /dev/null +++ b/libs/libc/include/bits/getprocid.h @@ -0,0 +1,7 @@ +#ifndef _BITS_GETPROCID_H +#define _BITS_GETPROCID_H + +#define ID_PID 0 +#define ID_PPID 1 + +#endif \ No newline at end of file diff --git a/libs/libc/include/luna.h b/libs/libc/include/luna.h index 468b0e05..7ed32c00 100644 --- a/libs/libc/include/luna.h +++ b/libs/libc/include/luna.h @@ -1,6 +1,7 @@ #ifndef _LUNA_H #define _LUNA_H +#include #include #include @@ -9,6 +10,9 @@ extern "C" { #endif + /* Returns a numeric identifier associated with the current process, depending on field. */ + long getprocid(int field); + /* Sleeps for ms milliseconds. */ unsigned int msleep(unsigned int ms); diff --git a/libs/libc/include/luna/syscall.h b/libs/libc/include/luna/syscall.h index 9f6147fe..7b1b387a 100644 --- a/libs/libc/include/luna/syscall.h +++ b/libs/libc/include/luna/syscall.h @@ -6,7 +6,7 @@ #define SYS_sleep 2 #define SYS_write 3 #define SYS_paint 4 -#define SYS_getpid 5 +#define SYS_getprocid 5 #define SYS_mmap 6 #define SYS_munmap 7 #define SYS_open 8 diff --git a/libs/libc/include/unistd.h b/libs/libc/include/unistd.h index 286d3e3c..1f3613eb 100644 --- a/libs/libc/include/unistd.h +++ b/libs/libc/include/unistd.h @@ -27,9 +27,12 @@ extern "C" * the parent. */ pid_t fork(void); - /* Returns the current process's process ID. */ + /* Returns the current process' process ID. */ pid_t getpid(void); + /* Returns the current process' parent's process ID. */ + pid_t getppid(void); + /* Terminates the program with the status code status. */ __lc_noreturn void _exit(int status); diff --git a/libs/libc/src/luna.cpp b/libs/libc/src/luna.cpp index e5c66f5a..9fef0d0f 100644 --- a/libs/libc/src/luna.cpp +++ b/libs/libc/src/luna.cpp @@ -7,6 +7,11 @@ extern "C" { + long getprocid(int field) + { + return syscall(SYS_getprocid, field); + } + unsigned int msleep(unsigned int ms) { return (unsigned int)syscall(SYS_sleep, ms); diff --git a/libs/libc/src/unistd.cpp b/libs/libc/src/unistd.cpp index 1f51c213..0a088413 100644 --- a/libs/libc/src/unistd.cpp +++ b/libs/libc/src/unistd.cpp @@ -27,7 +27,12 @@ extern "C" pid_t getpid(void) { - return syscall(SYS_getpid); + return getprocid(ID_PID); + } + + pid_t getppid(void) + { + return getprocid(ID_PPID); } long syscall(long number, ...) @@ -40,9 +45,9 @@ extern "C" { case SYS_clock: case SYS_yield: - case SYS_fork: - case SYS_getpid: result = __luna_syscall0(number); break; + case SYS_fork: result = __luna_syscall0(number); break; case SYS_exit: + case SYS_getprocid: case SYS_close: case SYS_exec: case SYS_mkdir: