Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

why does not fork() return pid of parent to child and zero to parent nd how can you find pid of parent when in child?

OpenStudy (nick67):

have you already seen this ? http://www.unix.com/unix-dummies-questions-answers/12815-fork-function.html

OpenStudy (anonymous):

no

OpenStudy (anonymous):

and i really dont understand it -.-

OpenStudy (nick67):

it's a discussion about pid's after fork() use

OpenStudy (anonymous):

sir um can u please answer my question?

OpenStudy (nick67):

according to this: http://beej.us/guide/bgipc/output/html/multipage/fork.html you should use this: So, I call fork() and save the return value in the pid variable. fork() is easy, since it can only return three things: 0 If it returns 0, you are the child process. You can get the parent's PID by calling getppid(). Of course, you can get your own PID by calling getpid(). -1: If it returns -1, something went wrong, and no child was created. Use perror() to see what happened. You've probably filled the process table—if you turn around you'll see your sysadmin coming at you with a fireaxe. else: Any other value returned by fork() means that you're the parent and the value returned is the PID of your child. This is the only way to get the PID of your child, since there is no getcpid() call (obviously due to the one-to-many relationship between parents and children.)

OpenStudy (nick67):

an example: http://www.cs.cityu.edu.hk/~lwang/fork

OpenStudy (anonymous):

ok got it thanks getppid()

OpenStudy (nick67):

you're welcome

OpenStudy (anonymous):

:D

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!