ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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$
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?
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.