LinuxQuestions.org
Visit Jeremy's Blog.
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 09-16-2004, 12:29 AM   #1
feetyouwell
Member
 
Registered: Dec 2003
Location: NC, US
Distribution: Novell Linux Eval (2.6.5)
Posts: 240

Rep: Reputation: 30
about fork(), a few questions


I wrote a program as following in c

<all the include stuff>

for (i = 1; i<=3; i ++) {
fork();
printf("the fork number is %i", i);
}

return 0;

the results for that is:
The fork number is 1
The fork number is 2
The fork number is 3
The fork number is 3
The fork number is 2
The fork number is 1
The fork number is 2
The fork number is 2
The fork number is 3
The fork number is 3
[user@localhost c]$ The fork number is 3
The fork number is 3
The fork number is 3
The fork number is 3

why is it? I thought i was only expecting 3 results since I only fork() three times, only three child processes are created right? When i run fork() for the first time, does it create a parent process or child process? If all fork() creates is child process, when how can i find about which / where is the parent for the child processes? Is the parent process the program I am running? if so, then I should see 3 * 3 = 9 results, why there are 14 printouts? Please advise.
 
Old 09-16-2004, 12:41 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Try this:
Code:
pid_t pid;

for(i = 0;i < 3;++i)
{
  pid = fork();
  if(!pid)
  {
    printf("I'm in the child process! fork number %d\n", i);
    exit(0);
  }
  printf("I'm in the parent process! Child process created. fork number %d, pid %d\n", i, pid);
}
fork() returns twice, once in the parent and once in the child. It returns 0 to the child process and it returns the pid of the child process to the parent.

Last edited by itsme86; 09-16-2004 at 12:45 AM.
 
Old 09-16-2004, 12:46 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Maybe this helps:
Code:
itsme@dreams:~/C$ cat forktest.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
  int i;
  pid_t pid;

  for(i = 0;i < 3;++i)
  {
    pid = fork();
    if(!pid)
    {
      printf("In child process. fork number %d\n", i);
      exit(0);
    }
    printf("In parent process. child created is number %d, pid %d\n", i, pid);
  }

  return 0;
}
itsme@dreams:~/C$ ./forktest
In parent process. child created is number 0, pid 12432
In child process. fork number 0
In parent process. child created is number 1, pid 12433
In child process. fork number 2
In child process. fork number 1
In parent process. child created is number 2, pid 12434
itsme@dreams:~/C$
 
Old 09-16-2004, 08:18 PM   #4
feetyouwell
Member
 
Registered: Dec 2003
Location: NC, US
Distribution: Novell Linux Eval (2.6.5)
Posts: 240

Original Poster
Rep: Reputation: 30
yeah that's better, but in your program, now we have 3 parents and 3 children right? so there 6 processes in total, what if I only want to create children out of the same parent, how can i do that?
 
Old 09-16-2004, 09:40 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
There's only 1 parent and 3 children in my example.
 
Old 09-16-2004, 09:44 PM   #6
feetyouwell
Member
 
Registered: Dec 2003
Location: NC, US
Distribution: Novell Linux Eval (2.6.5)
Posts: 240

Original Poster
Rep: Reputation: 30
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main (void)
{
printf("Initial process \t PID %d", getpid());
printf("\t PPID %d", getppid());
printf("\t GID %d\n\n", getpgid(0));

// printf("%d\n", getpgid(pid_t(getppid())));

int i;
pid_t pid;

for (i = 0; i<3; ++i) {
pid = fork();
if (!pid) {
printf("New process \t\t PID %d", getpid());
printf("\t PPID %d", getppid());
printf("\t GID %d\n", getpgid(0));
}
}

return 0;

}

if you run the code above in your computer, the computer only create 3 children for the initial process (parent), but there are also child processes created from the child. So I am only expecting 4 processes here in total but I ended up with a lot more, why is that happening?
 
Old 09-17-2004, 12:25 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Because you're not using exit() at the end of your child process so each child loops creates children processes itself. Use exit() like I did in my example.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
fork beginner_84 Programming 2 08-20-2004 04:53 AM
about fork eshwar_ind Programming 5 02-11-2004 03:38 AM
over-fork luzi82 Linux - Newbie 2 01-02-2004 06:55 AM
fork() lowlifeish Programming 3 11-04-2002 10:50 AM
Fork Ztyx Linux - General 1 08-31-2002 11:25 AM

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

All times are GMT -5. The time now is 11:08 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