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().
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