LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create N child processes - fork() (https://www.linuxquestions.org/questions/programming-9/create-n-child-processes-fork-629832/)

embesil 03-22-2008 06:15 AM

Create N child processes - fork()
 
Hello there, I'm pretty new in OS concept, I can not understand forking process..

Creating one child is pretty simple:
Code:

int pid = fork();
  if (pid < 0) return -1;
    if (pid != 0) {
    cout<< “I am the parent\n”;
  } else {
    cout << “I am the child\n”;
  }


What if I want to create N child processes in a same parent? Should I execute fork() in a for loop N times? Can anyone pls explain me..

Tinkster 03-22-2008 10:27 AM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.

chrism01 03-22-2008 09:01 PM

In short; yes.
Try it, adding a loop ctr in the parent section and get each child to print the ctr value when it's created.

Hko 03-23-2008 07:45 AM

.. just make sure that the child processes don't continue the same loop. Otherwise the childs continue forking off childs which in turn continue the same loop, forking sub-childs and so on. That will cause an "explosion" of processes, which may hang your computer (depending on the number of times the loops runs and whether or not you have a limit set on the number of processes). See also this recent thread.

embesil 03-23-2008 01:21 PM

Code:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int foo(const char *whoami)
{
        printf("I am a %s.  My pid is:%d  my ppid is %d\n",
                        whoami, getpid(), getppid() );
        return 1;
}

int main(void)
{
        int n= 10;
        int i=0;
        int status=0;

        int x = 5;

        printf("Creating %d children\n", n);
        foo("parent");
        for(i=0;i<n;i++)
        {
                pid_t pid=fork();

                if (pid==0) /* only execute this if child */
                {
                        foo("child");
                        for(i=0; i< 5; i++);
                        {
                                x++;
                                printf("x value in for: %d ", x);
                        }

                        exit(0);

                }
                wait(&status);  /* only the parent waits */
        }

        x = 100 * x;
        printf("x value in main: %d ", x);


        return 0;
}


The output is

Creating 10 children
I am a parent. My pid is:7208 my ppid is 6223
I am a child. My pid is:7209 my ppid is 7208
x value in for: 6 I am a child. My pid is:7210 my ppid is 7208
x value in for: 6 I am a child. My pid is:7211 my ppid is 7208
x value in for: 6 I am a child. My pid is:7212 my ppid is 7208
x value in for: 6 I am a child. My pid is:7213 my ppid is 7208
x value in for: 6 I am a child. My pid is:7214 my ppid is 7208
x value in for: 6 I am a child. My pid is:7215 my ppid is 7208
x value in for: 6 I am a child. My pid is:7216 my ppid is 7208
x value in for: 6 I am a child. My pid is:7217 my ppid is 7208
x value in for: 6 I am a child. My pid is:7218 my ppid is 7208
x value in for: 6 x value in main: 500



Since I'm very pretty new forking I try to understand the logic..The point that I didnt understand quite yet is why the for loop in children process turns 1 time.. How do I make it 5 times?

As far as I understand that parent and children code parts executes parallel, but what if I want to do a recursion in children processes, is that possible? Thanks for all your attention..

Best


edit: typo

chrism01 03-24-2008 08:03 PM

You don't need a semi-colon here:

for(i=0; i< 5; i++);

Also, be careful recursing in child process, as Hko mentions, or you'll flood your machine.


All times are GMT -5. The time now is 05:51 AM.