ive got function here blocks on fgets when print before fgets doesn't block.
int exec_command(char *command, char *output_buf, int buf_size) { file* pipe = null; char buffer[buffer_size]; char tmp[small_buffer_size]; unsigned total_read = 0; pipe = popen( command, "r"); if( !pipe ) { //error return -1; } memset(buffer, 0, sizeof(buffer)); while( !feof(pipe) ) { //printf("reading"); //if uncomment fgets doesnt block if( fgets(tmp, sizeof(tmp), pipe) != null ) { // check it'll fit: size_t len = strlen(tmp); if (total_read + len >= sizeof(buffer)) break; // , add big buffer if fits strcat(buffer, tmp); total_read += len; } } //is there copy if ( total_read ) strncpy (output_buf, buffer, buf_size); return pclose(pipe); } is there wrong on function above?
its because whatever writing pipe isn't flushing out buffer. when print, ends flushing (not garenteed happen though). when don't print, pipe isn't getting written because because stored in kernel buffer until fills, , kernel write data. call fsync or flush on pipe fd in process writing pipe make sure kernel buffer flushed.
Comments
Post a Comment