Thread: Endless loop without program freeze

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

    Endless loop without program freeze

    Hello,

    Inside my program i have an endless loop. When the loop starts the program freeze. Is there any way for the endless loop to run without the program freeze?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The endless loop is not causing the program to freeze. Something inside of it is causing it to freeze, independent of the fact that it is an endless loop.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I use fgets to read from an end-less stream. Is that make program freeze? :/

    remember this? Popen problem, program freeze

    when i use fgets to read the stream the program freeze :/
    Last edited by TuXaKoS; 04-28-2010 at 04:32 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fgets will wait for input. Make sure the input is properly (un)buffered, and make sure that the input does happen (i.e., if the application is only putting something out there every thirty seconds, then fgets will wait for that thirty seconds until more input arrives).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    hm.. wait i use this

    fp2 = popen("application ", "r");
    setvbuf ( fp2, NULL, _IOLBF, 2048);

    the fp2 is an endless stream, how can i fgets it line-line without my program freeze? :/

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm writing this from memory because I'm at work and I did the test this morning at home, but try something like this:
    Code:
    /* helper.c -- the application that provides the data */
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void) {
        printf("Very first output!\n");
        while(1) {
            sleep(10);
            printf("More output.\n");
        }
        return 0;  /*won't happen but whatever*/
    }
    Code:
    /* pt.c -- reads the other application */
    #define _XOPEN_SOURCE
    #include <stdio.h>
    
    int main(void) {
        FILE *hey;
        int i = 0, j = 0;
        char data[80];
        hey = popen("./helper", "r");
        setvbuf(hey, 0, _IONBF, 0);
        while (1) {
            fgets(data, 80, hey);
            if (strcmp(data, "Very first output!\n") == 0) {
                printf("Received first output %d\n", ++i);
            } else if (strcmp(data, "More output.\n") == 0) {
                printf("Received data %d\n", ++j);
            } else {
                printf("Received unexpected data %s\n", data);
            }
        }
        return 0; /* won't happen but whatever */
    }
    You should see regular status updates.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    grrrr....well i made this

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    
    FILE		*fp2;
    char 		path[300];
    
    
    
    
    int main(void) {
    
    
    
    
    fp2 = popen("/bin/bash -c 'airodump-ng mon0 2>&1' ", "r"); 
    
    while (1){
    
    	
    sleep(10);
    printf("test \n");
    
    
    
    }
    return 0;
    and i name it helper.c

    and this


    Code:
    test5 = fopen("test5.txt", "w");
    	
    	fp2 = popen("/bin/bash -c ./helper ", "r"); 
    
    	
    	setvbuf ( fp2, NULL, _IOLBF,  1024);
    
    	while (1){
    
    	fgets(path, PATH_MAX, fp2);
    
    	fprintf(test5 , path);
    
    	}
    but the program freeze AGAIN wtf? :/

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you start testing your return values so you can see what's going on, and maybe throw in some things like printf("%d", x++ ); ... so you can in fact see that it's not freezing. What do you expect this to do:
    Code:
    while( 1 )
    You don't have anything to get out of the loop, so why shouldn't it go forever?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How can you tell that it's frozen? You are never ever ever printing anything to the screen. And printing to a file is block-buffered, so you won't see anything in your file for a long time.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I have a gui. I run the second code when a button clicked and the gui frozen :/ maybe i'm too idiot pff :/

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And now that I'm testing again: it's also important how the helper application's buffers are set -- if they're set to default (block buffers), then it doesn't matter what your application does, it won't get the data until the helper has filled up a block.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TuXaKoS View Post
    I have a gui. I run the second code when a button clicked and the gui frozen :/ maybe i'm too idiot pff :/
    Right, but the second code doesn't print anything to the screen (and the application being popen'ed can't print anything to the screen, since all that output is going to your driver application), so not seeing anything is Exactly What You Should Expect.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    could you give me an example? maybe using the code i paste it above or something like that? :/

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    Right, but the second code doesn't print anything to the screen (and the application being popen'ed can't print anything to the screen, since all that output is going to your driver application), so not seeing anything is Exactly What You Should Expect.
    Yes , but the gui becomes gray and i cant clicked anything. It freeze.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TuXaKoS View Post
    Yes , but the gui becomes gray and i cant clicked anything. It freeze.
    If everything was working perfectly, that's what you would see. You have an infinite loop that is writing to the file. You will see nothing on the screen, and the GUI won't continue (because your function will never return). That is what you have written, so you can't go complaining when that's what happens. (You may have other reasons for complaining, but this is not one of them.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop issue in tax program
    By mistymoon1966 in forum C Programming
    Replies: 4
    Last Post: 10-31-2009, 02:04 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  4. Loop Entire Program?
    By seanminator in forum C Programming
    Replies: 25
    Last Post: 07-22-2009, 01:23 PM
  5. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM