1

Based on this SO post, and this example, I expect that, when I use fork(), will allow me executing system/execvp in non-blocking manner. However, when I try to issue a long-running child process in a fork block in the above mentioned example code, the control does not return to parent block, till the child has finished.

Can you tell a method, how I should design the code to allow non-blocking calls to system, in C/C++ code. Also, I plan to write a program, where more than one chidren are forked from a same parent. How can I get the pid of the children?

Thanks for your kind help.

1
  • 1
    Please add some code to illustrate what you are doing. Commented Apr 8, 2012 at 21:19

2 Answers 2

3

fork will immediately return to both the child and parent. However, that example (test3.c) calls wait4, which like it sounds, waits for another process to do something (in this case exit).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. That solves the issue. Can you also tell, if I have to issue many processes, whether same method of fork will work. Also, when I get pid of a child-process using fork, it gives 0. How can I get the right pid?
That's how fork works. It returns the PID of the child process to the parent and zero to the child. If this didn't happen, there's no (easy) way to tell which process is which. Basically, if you need your own PID, you'll have to get it the usual way, which I don't remember right now, but I'm sure you can look it up.
@user984260 fork returns the PID of the new process in the parent. In the child it returns 0. To get the PID of the current process you can call getpid. The manual pages are your friend!
Think of fork as a system call that makes a clone. (In fact, in a bunch of systems now, there is a syscall named clone, and fork is just a special case of it.) The clone is an almost-exact-duplicate of the original. The original just called fork, so the clone also just called fork. How will the original and clone know which one is which? Answer: fork returns the clone's ID in the original, and 0 in the clone. How will the clone find his new pid? Call the "get me my pid" syscall, getpid.
1

Mentioned sample code waits for child to return after spawning - that's why it blocks.

To get the pid of child process, use return value of fork(). fork() is single system code which returns two different values - pid of child to parent process and 0 to child process. This is why you can distinguish code blocks in your program which should be executed by parent and children.

Refer to man fork(2).

Another thing you probably should pay attention to concerning fork() and wait() is that after child process exits kernel still holds some information about it (e.g. exit status) which should be consumed somehow. Otherwise such process will become 'zombie' (Z in ps output). This is work done with wait*() calls. Besides, after child exits its parent is notified by kernel with SIGCHLD. If you don't want to process children return values, you can notify system that you're going to ignore SIGCHLD with signal(), sigaction(), etc. In this case that additional data is automatically reaped off. Such behavior may be default on your system but it is still adviseable that you state such behavior explicitly to improve portability of your program.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.