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?
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?
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.
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.
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).
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? :/
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*/ }You should see regular status updates.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 */ }
grrrr....well i made this
and i name it helper.cCode:#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 this
but the program freeze AGAIN wtf? :/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); }
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:You don't have anything to get out of the loop, so why shouldn't it go forever?Code:while( 1 )
Quzah.
Hope is the first step on the road to disappointment.
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.
I have a gui. I run the second code when a button clicked and the gui frozen :/ maybe i'm too idiot pff :/
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.
could you give me an example? maybe using the code i paste it above or something like that? :/
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.)