PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] What 's problem of the output in multiple process in c?

 August 16, 2022     c, fork, output, process     No comments   

Issue

I have the code below:

int main()
{
    for (int i = 0; i < 3; i++) {
        int t = fork();
        if (t == 0) {
            printf("child %d id: %ld from pid: %ld\n", i, (long)getpid(), (long)getppid());
            exit(0);
        }
        else if (t > 0) {
            wait(NULL);
            printf("father %d id: %ld from pid: %ld\n", i, (long)getpid(), (long)getppid());
        }
    }
    return(0);
}

When I run the output is: output1

And when I delete the "\n" in the printf when t>0, the output is: output2

How does the second output print more than the first one? Please!!

Sorry, I realized that this is problem about buffer in C and I just come accross this in my multithread assignment while I'm newbie in C and don't know about this yet. I just think it comes from multithread's problem so I ask here.


Solution

You need to remember that printf does not necessarily cause any data to be written. Instead, printf just writes data into an internal buffer that is flushed as needed.

Let's simplify the code a bit and unroll the loop to examine what's happening

int
main(void)
{
        int t, i = 0;
        t = fork();
        if( t == 0 ) {
                printf("child %d id: %ld from pid: %ld\n",
                        i, (long)getpid(), (long)getppid());
                exit(0);
        } else if( t > 0 ) {
                wait(NULL);
                printf("parent %d id: %ld from pid: %ld",
                        i, (long)getpid(), (long)getppid());
        }
        /* The output buffer now contains "parent N id ..."
           but the parent has not yet written any data.  */
        i += 1;
        t = fork();
        if( t == 0 ) {
                /* Here, the output buffer still contains "parent N id ..." */
                printf("child %d id: %ld from pid: %ld\n",
                        i, (long)getpid(), (long)getppid());
                /* The newline above causes the buffer to be flushed, 
so now this child writes "parent N id ...child M id: ..." */
                exit(0);
      ... 

If you include the \n in the parent's printf, that causes (in some situations) the output buffer to be flushed so that it is empty when you call fork and the child does not write the additional data. Notice that if you redirect the output to a file, you may get different behavior since the \n may not cause a flush in that scenario. If you want to ensure that the buffers are flushed at any given point, call fflush.



Answered By - William Pursell
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing