LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2008, 06:15 AM   #1
embesil
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Rep: Reputation: 0
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..
 
Old 03-22-2008, 10:27 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-22-2008, 09:01 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 03-23-2008, 07:45 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
.. 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.

Last edited by Hko; 03-23-2008 at 07:47 AM.
 
Old 03-23-2008, 01:21 PM   #5
embesil
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 03-24-2008, 08:03 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating sequential child processes using fork() BrokenFighter Programming 1 03-06-2007 10:11 PM
change child name (c fork) alaios Programming 15 09-11-2005 04:51 PM
why there're lots of child processes when fork? iclinux Programming 3 01-18-2005 07:09 AM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration