fork() is relatively simple. It returns twice. Once in the parent process and once in the child process. If it returns 0 you're in the child, >0 and you're in the parent.
if ((child_pid = fork()) > 0)
{
/* This is the parent process.
* child_pid contains the pid for the child. */
}
else
{
/* This is the child process in here. */
}
As for redirecting stdin, stdout, and stderr you need to look at the dup2() function. The file descriptors for the standard input, outpu and error are:
- stdin = 0
- stdout = 1
- stderr = 2
Just remember that you will have to make sure the program has some other way of receiving input, like sockets.
|