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.
After a fork() you have two processes - the original and a child process - which are both about to execute the instruction after the fork(). You should test the return value of form fork() to work out which one you are in.
So, after the first fork() in your process, you have two processes which both execute the second fork(). After that you have four processes which then execute the printf().
The above two pieces of code produces different output.
The only difference is the '\n' in the first printf function.
The output of the first code is
Code:
Hello world
Hello world 2
Hello world 2
Hello world 2
Hello world 2
Hello world
Hello world 2
Hello world 2
Hello world 2
Hello world 2
for second piece of code..
Code:
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
Hello worldHello world 2
This has something to-do with the buffer property of printf function..
Last edited by Intimidator; 12-17-2006 at 02:38 AM.
There's no guarantee what order the forked processes will execute in. Context switches are not guaranteed in a particular order or within any specific time.
BTW, put code in [CODE] tags to format it properly in the forum port.
Have a look at this:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
char* level_desc[25] = {
"Original process",
"Child of original",
"Grandchild of original",
};
int main(int argc, char** argv)
{
pid_t my_pid;
pid_t moms_pid;
int level = 0;
my_pid = getpid();
moms_pid = getppid();
printf("at start: process id = %5d, parent = %5d (%s)\n",
my_pid, moms_pid, level_desc[level]);
my_pid = fork();
if ( my_pid == 0 ) {
// if 0, this is the child process, so lets set the PID using getpid()
// and the PPID as well.
my_pid = getpid();
moms_pid = getppid();
level++;
}
else {
// this is the parent process, PID is 0, and the PPID hasn't changed
// we'll get the PID again.
my_pid = getpid();
}
printf("after first fork(): process id = %5d, parent = %5d (%s)\n",
my_pid, moms_pid, level_desc[level]);
my_pid = fork();
if ( my_pid == 0 ) {
my_pid = getpid();
moms_pid = getppid();
level++;
}
else {
my_pid = getpid();
}
printf("after second fork(): process id = %5d, parent = %5d (%s)\n",
my_pid, moms_pid, level_desc[level]);
return 0;
}
The output is clearer than your original program:
Code:
at start: process id = 9524, parent = 5270 (Original process)
after first fork(): process id = 9525, parent = 9524 (Child of original)
after first fork(): process id = 9524, parent = 5270 (Original process)
after second fork(): process id = 9527, parent = 9524 (Child of original)
after second fork(): process id = 9524, parent = 5270 (Original process)
after second fork(): process id = 9526, parent = 9525 (Grandchild of original)
after second fork(): process id = 9525, parent = 9524 (Child of original)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.