2

Is anyone familiar with os/161 and can answer a few questions for me?

How exactly does child pid, parent pid works.

I know that when you call thread_fork() you are creating another thread base on the current thread, the new thread should have a unique id for himself and a different file descriptor table. While sys_fork create a child from curthread, the child is the same as the parent besides the pid. But I am confused in how pid and parent pid works.

This is my interpretation of the process table. There is only one process table for the whole system. For now I have parent_pid and my_pid for every thread.
-A parent thread can have multiple child, (by keep calling sys_fork).
-A child can only have one parent.
-Whenever sys_fork is called, a child is created and the parent_pid for this child is set to the pid of the thread who created this child.
-pid of 1 is for the boot/menu thread.

Am I even on the right track in understanding how process table works?

One last question: For sys_waitpid(): Only parent can use waitpid? and they can only wait on their childs? Can a child use waitpid on a parent (or would this result in deadlock)?

I been searching around with Google, but I find so many contradictions, up till now I still can't find a clear answer to my questions.

sarnold
  • 102,305
  • 22
  • 181
  • 238
user308553
  • 1,238
  • 4
  • 18
  • 32

1 Answers1

4

I know nothing of OS/161 -- but your description sounds a lot like a standard POSIX system. So, here's how your questions work with POSIX and hopefully they'll make sense for OS/161 as well.

Only parents ever call waitpid(). Applications are designed around this. The POSIX specification of waitpid() requires returning an error with errno set to ECHILD if the pid is not a child of the calling process.

Children can determine if their parent has died by checking their own parent pid: getppid(3). If it is 1, then their parent has died and their parent has been set to init. (init is prepared to reap all orphaned children when they die, so the process state doesn't linger and fill the system process table with zombie processes.) (Modern systems don't have a "process table" any longer, but the pids must be recycled and some process control information must remain in the kernel until wait() of some sort has been called to reap the process. That memory is too important to leave idle for long.)

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks for the answer, but now I have another question. Pids must be re-used? why can't I assign a new Pid to threads? What I did for my Process table is whenever I create a new thread, I find the next available space on the table and add it in and then I loop thought all the thread in the process table to find the largest PID, add 1 to it, and put that as the pid for the newly created thread – user308553 Nov 11 '11 at 03:33
  • Pids are stored with a fixed-size data type: typically 32 bits, though older systems used 16 bits. Thus you _eventually_ have to re-use pids, once you've had more than 32768 processes run on the system. Linux, for example, limits the maximum pid value to 2^22: after four million-odd processes, the numbers wrap around to `1` again. Since the actual _value_ of a pid doesn't matter, there's usually no mechanism to change the pid of a running process. – sarnold Nov 11 '11 at 03:38
  • o ok that make sense. but just one more question. in os161, sys_exit(int exitcode) and sys_waitpid(pid_t pid, int *status) http://www.cs.hmc.edu/~mtauraso/man/syscall/waitpid.html http://www.cs.hmc.edu/~mtauraso/man/syscall/_exit.html What exactly is exitcode and status. Right now i made it so every thread have a exitcode, 0 for being active, 1 when it is using sys_exit. Me and my partner read the manual page over and over again, but we are still very confused as to how they work. Hope you can help us again. Thanks – user308553 Nov 11 '11 at 06:48
  • The exit code can be used to communicate general _success_ or _failure_ to a parent process. For a "simple" example, look to `grep(1)` -- it returns an exit status of `0` for "found a match" and an exit status of `1` for "no match found". `2` means there was an error. Other programs, like the shell, can use the exit status to determine future steps to take. Because `waitpid()` always sleeps until the child is dead, no parent can ever read the `0` "active" code you've designed. – sarnold Nov 11 '11 at 07:05