Find the answer to your Linux question:
Results 1 to 4 of 4
I would like to create a thread and pipe all "printf" to a new terminal (kconsole or xterm) mean time the main program stays in different kconsole for more user ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    6

    printf to different console

    I would like to create a thread and pipe all "printf" to a new terminal (kconsole or xterm) mean time the main program stays in different kconsole for more user inputs. Can you please help where I should start??

    Thanks in advance.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Is it necessary that this be a thread, or can it be a different process?

    And which programming language are you using?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    6
    I am using C code. I would like to do it w/ threads.
    If not, then I will try process.
    Any answer will be appreciated!!

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I presume you want to use printf() calls as they are now, without modifying them.

    Try this:
    1. In the main program, before firing off the subsidiary process, create a file with a particular name, and make sure it's empty. I'll call this file fred.
    2. After the file has been created, the main program should do something like this:
      Code:
      fork_result=fork();
      
      if(fork_result==-1)
      {
        /* error handling goes here */
      }
      
      if(fork_result==0)
      {
        system("xterm -e tail -f fred");
      
        exit(0);
      }
    3. In the subsidiary process, do something like this:
      Code:
      if(freopen(stdout,"fred","w")==NULL)
      {
        /* error handling goes here */
      }

    The reason this can't be done with threads is that the freopen() will affect standard output for all threads, not just that one.

    Fair warning: I have not tested this concept.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...