Parent forking


 
Thread Tools Search this Thread
Top Forums Programming Parent forking
# 1  
Old 02-02-2011
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.
# 2  
Old 02-02-2011
Quote:
Originally Posted by TWhitt24
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.
That's what fork()'s return value is for. children will know they are children.
Code:
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");
}

# 3  
Old 02-02-2011
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.
# 4  
Old 02-02-2011
Quote:
Originally Posted by TWhitt24
I understand that. My question is how do I get the parent to fork and not the child.
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.
# 5  
Old 02-02-2011
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.
Code:
 #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);
  }

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 02-02-2011 at 07:07 PM..
# 6  
Old 02-02-2011
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
Code:
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();

Code:
#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);
        }
}

# 7  
Old 02-02-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

need help in forking

I have an input file with contents like: 5785690|68690|898809 7960789|89709|789789 7669900|87865|659708 7869098|65769|347658 so on.. I need to pass this file to 10 parallely running processes (forking)so that each line is processed by a process and no line is processed twice and write the... (1 Reply)
Discussion started by: rkrish
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Shell Programming and Scripting

Forking and Pinging

Keep in mind that I haven't done Perl scripting for a LONG time, so I'm quite rusty. This is what I would like to do: - using fork, create 3 or 4 processes to read 3 or 4 different text documents containing server names or IP addresses - in each of those processes, Perl will ping each of those... (7 Replies)
Discussion started by: kooshi
7 Replies

4. UNIX for Advanced & Expert Users

Forking a new process without parent dependance

hi, I want my program to fork a new process and then I want to kill the parent process. The parent program before dying will issue a SIGTERM to all its childs. Which eventually kills all Children. I cant handle the SIGTERM at the child level.:( What I was thinking of was the Parent... (3 Replies)
Discussion started by: tyler_durden
3 Replies

5. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

6. Programming

forking process.

#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pID; int i; for (i = 0; i < 3; i++) { pID = fork (); if (pID == 0) { printf ("Value of i --> %d... (2 Replies)
Discussion started by: kymthasneem
2 Replies

7. Programming

forking a new process

Hi I'm currently working with C on UNIX (HPUX) and need to be able to fork a seperate Java process from within a running C process. I can run the following code from the command line via a script but am having difficulty getting it to work from within the code. I am trying to use execl. Is... (4 Replies)
Discussion started by: themezzaman
4 Replies

8. UNIX for Dummies Questions & Answers

forking and killing parent processes

Hi everybody, I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls. I have to create a C program P0, which creates 9 other processes P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on. All the processes contain an infinite... (0 Replies)
Discussion started by: davewilliams20
0 Replies

9. UNIX for Advanced & Expert Users

Forking

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation with the schedular functionality it will help a lot. Because I am stuck with this. #include <stdio.h> main(){... (3 Replies)
Discussion started by: manjuWicky
3 Replies

10. Programming

Forking in a loop

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation. Because I am stuck with this. #include <stdio.h> main(){ int i = 0; printf("I am the... (1 Reply)
Discussion started by: manjuWicky
1 Replies
Login or Register to Ask a Question