Thread: Popen problem, program freeze

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    Popen problem, program freeze

    I am writing code in C and I use popen in read mode so as to call an external application, take its output and edit them.

    Here comes the problem. The output of this particular application are non-stop, that is to say it produces output continuously. The result is that by the time I call popen, the programme freeze. I can do neither pclose nor something else. Really, I am in serious problem since this output is of paramount importance for my application.

    I'm looking forward for your help, i'll really appreciate it.

    Thx in advance.
    Last edited by TuXaKoS; 04-28-2010 at 04:29 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure what you mean by "by the time I call popen, the programme freeze". popen is what starts the other application, right? I would be very surprised if you managed to lose information that way.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    popen is into the main(). when the program reaches this command, it freezes. if i run ps -x i see the other application running. If i kill the other application, my program continues and the gui appears.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would guess that just means that you're not dealing with the output correctly. pclose (for instance) is not going to force-quit the other program, merely wait for it to finish. If the other program is not going to finish, then that's a problem. If you've got the data you want, and you want the other program to stop, then you could send it a signal I suppose.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I Just want the other application run in the background. My program running and i geting the output of the background application. Is that possible? :/ And how?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TuXaKoS View Post
    I Just want the other application run in the background. My program running and i geting the output of the background application. Is that possible? :/ And how?
    If you want the other program to keep running and you to keep getting the output, then don't call pclose. You can continually call fgets/fscanf/whatever to read from the pipe, although those will block until output from the other program arrives.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    The problem is that when i call popen the program freeze, no other command after this execute :/ Any idea why? maybe because the output stream is non-stop?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Running some tests showed that, at least on this system that I am typing on right now, buffering could be standing in your way. (I.e., the output is fully buffered, not line-buffered, so if you are relying on seeing things as soon as a \n happens, well, no. I printed out 101 short lines (each ending with \n) and didn't receive any of them until the other process closed.) That means that, if for instance, the first thing after a popen is a fgets, you may be waiting a while to see anything. You can change the buffering of a FILE* with setvbuf().

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Thx my friend. Everything seems to work now setvbuf() was the solution, i changed the buffer from full to line buffer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM