Just a quick note about transference -- applying a concept from one field to another. I'm in the process of getting familiar with IPC::Run and in the process of writing a test script, I also built a C program that I could insert some sleeps into so as to try out the timer built into the module.
My original code was something like this:
#include <stdio.h> #include <unistd.h> /* For usleep */ /* Fake version of bc to test timeouts */ char *apszBanner[] = { "bc 0.6", "This is a fake version of bc.", "Don't trust any of the answers provided.", "Really, don't!", NULL }; int main ( int iArgC, char *apszArgV[] ) { char *pszLine, szLine[80]; int i; /* sleep ( 5 ); */ for ( i = 0; apszBanner[i]; i++ ) { puts ( apszBanner[i] ); } sleep ( 5 ); while ( pszLine = fgets ( szLine, sizeof(szLine), stdin ) ) { *(pszLine + strlen(pszLine) - 1) = '\0'; printf ( "Got %s, the answer is 42.\n", pszLine ); } return 0; /* Zero is success, right? */ }
To my consternation, the output didn't come out when I expected, but driving to chorus last night through horrendous traffic I was thinking about this and that, and on Yonge St, waiting for a light, I saw the light: I was suffering from buffering!
So, this morning, after dealing with the latest Production problem, I added fflush to my code:
#include <stdio.h> #include <unistd.h> /* For usleep */ /* Fake version of bc to test timeouts */ char *apszBanner[] = { "bc 0.6", "This is a fake version of bc.", "Don't trust any of the answers provided.", "Really, don't!", NULL }; int main ( int iArgC, char *apszArgV[] ) { char *pszLine, szLine[80]; int i; /* sleep ( 5 ); */ for ( i = 0; apszBanner[i]; i++ ) { puts ( apszBanner[i] ); } fflush ( stdout ); sleep ( 5 ); while ( pszLine = fgets ( szLine, sizeof(szLine), stdin ) ) { *(pszLine + strlen(pszLine) - 1) = '\0'; printf ( "Got %s, the answer is 42.\n", pszLine ); fflush ( stdout ); } return 0; /* Zero is success, right? */ }
And presto! Problem solved. Buffering and transference. So cool.
|
|---|