LinuxQuestions.org
Visit Jeremy's Blog.
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 12-16-2006, 04:40 AM   #1
Intimidator
Member
 
Registered: Mar 2005
Distribution: FC4
Posts: 83

Rep: Reputation: 15
fork and printf


#include<stdio.h>
main()
{
fork();
fork();
printf("Hello world\n");

}

The code prints Hello world 4 times..why is that?
 
Old 12-16-2006, 05:27 AM   #2
qwijibow
LQ Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
read man fork

your program forks into 2 threads, then those threads fork again.
And 2x2=4.
 
Old 12-16-2006, 08:00 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Your code should have generated a lot of complaints when you compiled it - unless this is just for show.

When you compile use gcc -Wall and fix all the complaints. Otherwise your life in programming will be hellish.

Also try man waitpid
 
Old 12-16-2006, 08:26 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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().
 
Old 12-16-2006, 10:04 AM   #5
Intimidator
Member
 
Registered: Mar 2005
Distribution: FC4
Posts: 83

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jim mcnamara
Your code should have generated a lot of complaints when you compiled it - unless this is just for show.

When you compile use gcc -Wall and fix all the complaints. Otherwise your life in programming will be hellish.

Also try man waitpid
My Code didn't generate any complaints

btw,It has nothing to do with waitpid
 
Old 12-16-2006, 10:24 AM   #6
Intimidator
Member
 
Registered: Mar 2005
Distribution: FC4
Posts: 83

Original Poster
Rep: Reputation: 15
Code:
main()
{
        fork();
        printf("Hello world");
        fork();
        fork();
        printf("Hello world 2\n");
}
Code:
main()
{
        fork();
        printf("Hello world\n");
        fork();
        fork();
        printf("Hello world 2\n");
}
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.
 
Old 12-16-2006, 02:52 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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)
 
Old 12-17-2006, 08:21 AM   #8
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
zombies have everything to do with waitpid.
 
  


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
is a printf wrapper possible? Thinking Programming 2 10-21-2005 07:44 AM
More arguments in printf() AMMullan Programming 3 02-23-2004 02:29 PM
Using 'printf' on a 'struct' skywalker27182 Programming 10 12-20-2003 12:06 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

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

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