Parent forking

My question is, how do you fork only the parent processes in unix? For example how would I use the fork function to fork the parent process more than once and leave the children processes alone. This way I do not have children of children. The way I have it set up now it the parent process forks 3 times with if statements. IE.. if Pid > 0 fork... When the first child process runs and kicks back the the first parent process, it finishes and then closes the program. I need it to run the other child processes then return to the parent. I have tried wait(&status) but that has not worked.

That's what fork()'s return value is for. children will know they are children.

pid_t pid=fork();

if(pid > 0)
{
        fprintf(stderr, "I am in the parent\n");
}
else if(pid == 0)
{
        fprintf(stderr, "I am the child so should not fork again\n");
}
else
{
        perror("could not fork");
}

I understand that. My question is how do I get the parent to fork and not the child. I need the parent to fork once and then fork again with out any of the children forking.

Don't call fork() in the child if you don't want the child to fork. Your process gets told whether it's in the child by the return value of fork(), and can use that to decide whether to fork or not via if-statements like above. If you don't know how to use an if statement, refer to the above example, or post your own code so I can modify it with the appropriate logic.

What I need to do is create 3 child processes, then output the parents PID, the first childs PID, the second childs PID, and then the third. Here is what I have been working with.

 #include <iostream>
  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <sys/wait.h>
  using namespace std;
  int main(void)
  {
    pid_t pId;
    int status;
    int child1;
    int child2;
    int child3;
   
    if(pId = fork() > 0)
      {
        cout<<"This is the parent process, My PID is: "<<getpid()<<endl;
        pId = fork();
        wait(&status);
        
      }
    if(pId > 0)
      {
        pId = fork();
        wait(&status);
        exit(1);
      }
    else if(pId == 0)
  {
        child1 = getpid();
        cout<<"This is the first child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ls", "ls", "-1", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child2 = getpid();
        cout<<"This is the second child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ps", "-1f", 0);
       exit(1);
      }
    else if(pId == 0)
      {
        child3 = getpid();
        cout<<"This is the third child process, My PID is: "<<getpid()<<endl;
        exit(1);
      }
    exit(1);
  }
   child1 = getpid();
        cout<<"This is the first child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ls", "ls", "-1", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child2 = getpid();
        cout<<"This is the second child process, My PID is: "<<getpid()<<endl;
        execl("/bin/ps", "-1f", 0);
        exit(1);
      }
    else if(pId == 0)
      {
        child3 = getpid();
        cout<<"This is the third child process, My PID is: "<<getpid()<<endl;
        exit(1);
      }
    exit(1);
  }

I usually find it convenient to give the child its own little section for it to run and then call exit() after so it doesn't run all the stuff below it. Much less clutter

if(in_the_child)
{
        // runs in the child
        command1();
        command2();
        command3();
        // child quits so it doesn't run parent_stuff();
        exit(0);
}

// Only the parent will run this stuff
parent_stuff();
parent_stuff();
parent_stuff();
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        pid_t pid[3];
        int n;

        for(n=0; n<3; n++)
        {
                pid[n]=fork();

                if(pid[n] == 0) // if we are the child, exit immediately
                {       exit(0);        }
        }

        printf("Parent pid: %d\n", (int)getpid());

        for(n=0; n<3; n++)
        {
                int status;
                printf("Child %d pid: %d\n", n, pid[n]);
                // We must wait for the child
                waitpid(pid[n], &status, 0);
        }
}

Thanks for the help, but how would I output something different for each child. for example. Im the first this is my PID ..., Im am the second this is my PID...., and so on.

I've already explained where I put everything and why, so your question just amounts to "how do I do different things depending on something". If you really have no idea how to use an "if" statement yet, you're in way over your head -- and if you do, you already have the answer, just apply it.

// in section that does child code
{
if(n==1) { print something } else if(n==2) { print something else } ...
exit(0);
}