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

  • Home
  • SEARCH
  • 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 6601395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:44:50+00:00 2026-05-25T18:44:50+00:00

Im new at process creation etc..so probably a basic question. Im creating a fixed

  • 0

Im new at process creation etc..so probably a basic question.
Im creating a fixed number of child processes, each doing nothing but printing their pid. The problem is in the output that i get. Have a look:

int main(){  
pid_t pid=0;  
int i=0,status=0;

    for(i=0;i<3;i++){
            pid=fork();
            switch(pid){
                    case 0:{        //Child
                            printf("\nChild pid: %d",getpid());
                            exit(0);
                            break;
                            }
                    case -1: {//Error
                            printf("Error occured in fork");
                            exit(1);
                            break;
                            }
                    default:{
                            printf("\nParent id: %d",getpid());
                            printf("\nIts child id: %d",pid);
                            wait(NULL);
                            }
            }

Output:
Child pid: 1450
Parent id: 1445
Its child id: 1450
Child pid: 1455Its child id: 1450
Parent id: 1445
Its child id: 1455
Child pid: 1460Its child id: 1455
Parent id: 1445
Its child id: 1460

The problem is I dont know why only the second print statement of the parent process is appearing and not the first, if any at all. I know im not waiting for my child processes to end( frankly i dont know how i would do that), but if the parent does execute before ending its child processes, why arent both its print statements appearing, and why is the \n being ignored in that line too.
Any help would be greatly appreciated.
Thx.

Update:If i replace wait(NULL) with printf("\n%d\n",wait(NULL)) It gives me a perfect output, without stray prints. Any idea what couldve fixed it? After all they both do the same thing.

  • 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-25T18:44:50+00:00Added an answer on May 25, 2026 at 6:44 pm

    The problem is that there are several processes writing to the same file (your console) at the same time without any concurrency control or any lock. That, and that the ttys are strange creatures, at best, make weird things happens. Over that, remember that printf is buffered.

    Your output should be read this way:

    Child pid: 1450    <- Child #1
    Parent id: 1445    <- Parent #1.1
    Its child id: 1450 <- Parent #1.2
    
    Child pid: 1455[Its child id: 1450] <- Child #2 with garbage at the end
    Parent id: 1445                     <- Parent #2.1
    Its child id: 1455                  <- Parent #2.2
    
    Child pid: 1460[Its child id: 1455] <- Child #3 with garbage at the end
    Parent id: 1445                     <- Parent #3.2
    Its child id: 1460                  <- Parent #3.2
    

    You could try redirecting the output to a file, and see if not being a tty makes any difference.

    Anyway, to do it the right wat, you should use any mechanism that guarantees multiprocess correctness.

    UPDATE

    Yes, now I see it. You have the ‘\n’ at the beginning of your printing lines, not at the end, as is usual. Stdout is normally line-buffered, that means that the buffer is flushed to the device when it sees a ‘\n’. And since you have them at the beginning there is always one line in the buffer waiting for output.

    Now, when you fork your process the output buffer gets duplicated and the last line from the parent is printed by the child (why it is printed after, and not before, is still a mystery to me).

    Anyway, the new printf you added has a ‘\n’ at the end, so it flushes the buffer, fork finds it empty and all goes fine. You could as well call fflush(stdout) but it is cumbersome.

    The morale of the story is: “When you printf for debugging purposes always put a \n at the end of every line or you can get partial, mixed contents.

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

Sidebar

Related Questions

I am creating a new build process for a DotNet project which is to
Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo(@C:/PsExec.exe); psi.UseShellExecute = false; psi.RedirectStandardOutput
Is it possible to create a new process on windows with a different user
I start a process in c# like this: Process p= new Process(); p.StartInfo.FileName =
I'm using VS2008 to debug an application that starts a new process. I believe
I have a PHP script where I create a new process using proc_open().It executes
Lets say that I'm trying to create a new process with the following code:
Is there a command-line argument that would force firefox.exe to launch a new process
In my Java code, I start a new process, then obtain its input stream
I have starter new System.Diagnostics.Process and it launchs FireFox. How can I obtain the

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.