Thread: printf() or write(1,...) when using fork?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    printf() or write(1,...) when using fork?

    I am not sure which print function to use after fork() system call. Should I use printf(), or sprintf with write to stdout?

    I was googling a lot about it. and some people say use one, some people say use another.

    Any help is appreciated, thanks.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Calling fork() has no effect on anything. The world is exactly the same as it was before, you're just in a new process. I'm not sure who's telling you that you need to "print differently" after calling fork(), but whoever it is they are really, really confused.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    So, if printf is used concurrently, both from child and parent, interleaving will not occur?

    Example of interleaving I am thinking of:
    parent: printf("Hello");
    child: printf("World\n");
    output to console: HeWollorld

    Thanks for your help

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by HaitiBoy View Post
    So, if printf is used concurrently, both from child and parent, interleaving will not occur?
    Nowhere in your question did you even hint that that was a requirement ;-)

    Yes, interleaving of the output may occur -- what other possibility is there? Two programs are writing to the same device at the same time.

    To prevent interleaving, the two instances of the program would have to coordinate with each other somehow (e.g. a semaphore or something like it). It's not going to happen automatically.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The main issue with printf() on either side of a fork() is that standard I/O is buffered. Something like:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
      printf("Hello");
    
      if(fork() == 0)
      {
        puts("child");
        _exit(0);
      }
    
      puts("parent");
    
      return 0;
    }
    Could easily produce output like:
    Code:
    Helloparent
    Hellochild
    This is the case if "Hello" is put in the buffer but not flushed (which it probably will be since there has been no newline printed); thus the child and parent both have a buffer with "Hello", and when the flush happens, they both print it. This wouldn't be the case with write(), but you'd also be OK if you just did fflush(NULL) (this flushes all output streams; you can choose just one if that's all you're using) before the fork().

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    I understand now.
    Thanks for your help guys, I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help me finish my Hangman program (C)
    By eddybro in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 08:52 AM
  2. Help again!!
    By ewic0190 in forum C Programming
    Replies: 1
    Last Post: 03-19-2010, 06:56 PM
  3. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM