I'm having issues with understanding fork()

I am programming on a Linux using the g++ compiler. I under stand that the syntax for fork goes at least something like this:
1
2
3
4
5
//proper includes go here
//main ect.
pid_t pId fork();
//error checking
//rest of code 


My issue is understanding how do I give it a specific process to fork. Is there some sample code of forking to a program such as emacs so that there is one or two in back ground and another in foreground. I am trying to make a basic bash and am currently stuck on understanding the concept of fork as well as how it correlates to the use of execvp().

Thank you.
Last edited on
After the fork you have two processes each running exactly the same program you started with!

They are not the same, however. One is the 'parent' and one is the 'child'

So the first thing you need to do after the fork is found out which one you are running in.
If the pid returned is zero, then you are in the child, a non zero pid indicates you are in the parent and the pid value returned is the process id of the child process.

It is unlikely that you want to continue just running two copies of the same process. So you change one (the child) to something
else. This is what the execvp command does - it replaces the entire process with another executable.

this site has an example that can be adapted to running copies of emacs as child processes

http://nixcraft.com/coding-general/13159-help-execvp-function-c-program.html


you cannot give it a process to fork, it can only fork the process who calls it.

you might need to check if system(), or exec() and its variants can do what you want.
Topic archived. No new replies allowed.