Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:46:39+00:00 2026-05-14T02:46:39+00:00

OS: Linux, Language: pure C I’m moving forward in learning C programming in general,

  • 0

OS: Linux, Language: pure C

I’m moving forward in learning C programming in general, and C programming under UNIX in a special case.

I detected a strange (for me) behaviour of the printf() function after using a fork() call.

Code

#include <stdio.h>
#include <system.h>

int main()
{
    int pid;
    printf( "Hello, my pid is %d", getpid() );

    pid = fork();
    if( pid == 0 )
    {
            printf( "\nI was forked! :D" );
            sleep( 3 );
    }
    else
    {
            waitpid( pid, NULL, 0 );
            printf( "\n%d was forked!", pid );
    }
    return 0;
}

Output

Hello, my pid is 1111
I was forked! :DHello, my pid is 1111
2222 was forked!

Why did the second “Hello” string occur in the child’s output?

Yes, it is exactly what the parent printed when it started, with the parent’s pid.

But! If we place a \n character at the end of each string we get the expected output:

#include <stdio.h>
#include <system.h>

int main()
{
    int pid;
    printf( "Hello, my pid is %d\n", getpid() ); // SIC!!

    pid = fork();
    if( pid == 0 )
    {
            printf( "I was forked! :D" ); // removed the '\n', no matter
            sleep( 3 );
    }
    else
    {
            waitpid( pid, NULL, 0 );
            printf( "\n%d was forked!", pid );
    }
    return 0;
}

Output:

Hello, my pid is 1111
I was forked! :D
2222 was forked!

Why does it happen? Is it correct behaviour, or is it a bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:46:40+00:00Added an answer on May 14, 2026 at 2:46 am

    I note that <system.h> is a non-standard header; I replaced it with <unistd.h> and the code compiled cleanly.

    When the output of your program is going to a terminal (screen), it is line buffered. When the output of your program goes to a pipe, it is fully buffered. You can control the buffering mode by the Standard C function setvbuf() and the _IOFBF (full buffering), _IOLBF (line buffering) and _IONBF (no buffering) modes.

    You could demonstrate this in your revised program by piping the output of your program to, say, cat. Even with the newlines at the end of the printf() strings, you would see the double information. If you send it direct to the terminal, then you will see just the one lot of information.

    The moral of the story is to be careful to call fflush(0); to empty all I/O buffers before forking.


    Line-by-line analysis, as requested (braces etc removed – and leading spaces removed by markup editor):

    1. printf( "Hello, my pid is %d", getpid() );
    2. pid = fork();
    3. if( pid == 0 )
    4. printf( "\nI was forked! :D" );
    5. sleep( 3 );
    6. else
    7. waitpid( pid, NULL, 0 );
    8. printf( "\n%d was forked!", pid );

    The analysis:

    1. Copies “Hello, my pid is 1234” into the buffer for standard output. Because there is no newline at the end and the output is running in line-buffered mode (or full-buffered mode), nothing appears on the terminal.
    2. Gives us two separate processes, with exactly the same material in the stdout buffer.
    3. The child has pid == 0 and executes lines 4 and 5; the parent has a non-zero value for pid (one of the few differences between the two processes – return values from getpid() and getppid() are two more).
    4. Adds a newline and “I was forked! :D” to the output buffer of the child. The first line of output appears on the terminal; the rest is held in the buffer since the output is line buffered.
    5. Everything halts for 3 seconds. After this, the child exits normally through the return at the end of main. At that point, the residual data in the stdout buffer is flushed. This leaves the output position at the end of a line since there is no newline.
    6. The parent comes here.
    7. The parent waits for the child to finish dying.
    8. The parent adds a newline and “1345 was forked!” to the output buffer. The newline flushes the ‘Hello’ message to the output, after the incomplete line generated by the child.

    The parent now exits normally through the return at the end of main, and the residual data is flushed; since there still isn’t a newline at the end, the cursor position is after the exclamation mark, and the shell prompt appears on the same line.

    What I see is:

    Osiris-2 JL: ./xx
    Hello, my pid is 37290
    I was forked! :DHello, my pid is 37290
    37291 was forked!Osiris-2 JL: 
    Osiris-2 JL: 
    

    The PID numbers are different – but the overall appearance is clear. Adding newlines to the end of the printf() statements (which becomes standard practice very quickly) alters the output a lot:

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
        int pid;
        printf( "Hello, my pid is %d\n", getpid() );
    
        pid = fork();
        if( pid == 0 )
            printf( "I was forked! :D %d\n", getpid() );
        else
        {
            waitpid( pid, NULL, 0 );
            printf( "%d was forked!\n", pid );
        }
        return 0;
    }
    

    I now get:

    Osiris-2 JL: ./xx
    Hello, my pid is 37589
    I was forked! :D 37590
    37590 was forked!
    Osiris-2 JL: ./xx | cat
    Hello, my pid is 37594
    I was forked! :D 37596
    Hello, my pid is 37594
    37596 was forked!
    Osiris-2 JL:
    

    Notice that when the output goes to the terminal, it is line-buffered, so the ‘Hello’ line appears before the fork() and there was just the one copy. When the output is piped to cat, it is fully-buffered, so nothing appears before the fork() and both processes have the ‘Hello’ line in the buffer to be flushed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using C language and Linux OS as my programming platform. And I
I am using C language and Linux as my programming platform in embedded device.
I am using C language and Linux as my programming platform. In my user-space
I am using C language and Linux as my programming platform. I am developing
I am using C language and Linux as my programming platform. In my app,
I am using C language and Linux as my programming platform. Right now I
OS: Linux(RedHat) Programming Language: C++ I need to create a daemon(process) for Linux using
I am about build a server on linux (I get to pick programming language)
Whilst learning the assembler language (in linux on a x86 architecture using the GNU
Using any combination of Linux tools (without going into any full featured programming language)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.