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 4614298
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:40:45+00:00 2026-05-22T01:40:45+00:00

I am trying the following C code: int main() { printf(text1\n); fork(); printf(text2\n); return

  • 0

I am trying the following C code:

int main()
{
    printf("text1\n");
    fork();
    printf("text2\n");
    return 0;
}

I was expecting to get the output where i get two “text1” and two “text2”, like:

text1
text1
text2
text2

But, i am, instead, getting:

text1
text2
text2

only one “text1”???
Ok, if child process executes from the fork(), then why do i get two “text1” for following:

int main()  
{  
    printf("text1");  
    fork();  
    printf("text2\n");  
    return 0;  
}  

the output now is:

text1text2  
text1text2 

If the child process starts after the fork, output should be:

text1  
text2  
text2  
  • 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-22T01:40:46+00:00Added an answer on May 22, 2026 at 1:40 am

    fork() creates a new process by copying everything in the current process into the new process. That typically includes everything in memory and the current values of the CPU registers with some minor adjustments. So in effect, the new process gets a copy of the process’s instruction pointer as well so it resumes at the same point where the original process would continue (the instruction following the fork()).


    To address your update, printf() is buffered. Normally the buffer is flushed when it encounters a newline character at the end, '\n'. However since you have omitted this, the contents of the buffer stays and is not flushed. In the end, both processes (the original and the child) will have the output buffer with "text1" in it. When it eventually gets flushed, you’ll see this in both processes.

    In practice, you should always flush files and all buffers (that includes stdout) before forking to ensure that this does not happen.

    printf("text1");
    fflush(stdout);
    fork();
    

    The output should look like this (in some order):

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

Sidebar

Related Questions

I was trying out the following C code: void main() { int i; for(i
I'm trying to mimic the following Java code: int[][] multi; // DIMENSIONS ARE UNKNOWN
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
Why is the following code resulting in Segmentation fault? (I'm trying to create two
I am trying the following code: <?php $link = mysql_connect('localhost', 'root', 'geheim'); if (!$link)
I'm trying the following code http://code.google.com/apis/ajax/playground/#change_the_playing_video It works well when runned from the playground
I'm trying the following code to execute a search and it's not working. On
I was trying out the following code which actually saves the pdf file to
When I am trying to execute following code to email the contact form details,
The following code is trying to remove any duplicate characters in a string. I'm

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.