2013年9月15日星期日

Encounter difficult problems , please help answer !

 This post last edited by the lljl2009 on 2013-09-13 14:58:04
Function Description:
The Process A stdout, stderr process B as standard input stdin, process B reads stdin and read out information to stdout.


running steps:
1. run.sh by exec. / A 2> & 1 | B Start A and B;
2. A few can be triggered printf information ( for example, insert U disk detected will print a lot of information ) ;
3. B in the treatment:
char buffer [1024];
while (1)
{
memset (buffer, 0x0, sizeof (buffer));
if (fgets (buffer, sizeof (buffer), stdin) && buffer [0] ;! = '\ n')
bytes_read = strlen (buffer);
else {
printf ("----- fgets stdin <= 0 - --- \ n ");
break;
}
/ / fflush (stdin) ;/ / no usable
setvbuf (stdout, NULL, _IONBF, 0);
/ / fwrite (buffer, bytes_read, 1, stdout);
ret = fputs (buffer, stdout);
if (ret == EOF)
printf ("=== fputs to stdout error === \ n");
else
fflush (stdout);
}

questions:
insert U disk trigger A print information , but from the stdout to see the last part of A printed information is not output to stdout and then have to wait until the time of data ( such as re- plug U disk again trigger A print information ) to the last remaining print message " punch out" . This is why in the end ?


attempts have been made : the fgets replaced read nor ; fcntl function called before the fgets set O_NONBLOCK approach does not work ; getline has no effect ; ......

Please help us to see ! Thank you !
------ Solution ---------------------------------------- ----

Thank you to answer ! which means that the B call did not work. . . Do you mean to call it inside the A ? A own output to stdout no problem oh. . . That 's what the A call it ? So much printed information  

is in A calls. A separate operation is no problem, because the output of printf when the terminal and redirection , pipes, etc. There are different buffering strategy .
Method One : A called once before output setbuf (stdout, NULL); redirect stdout is set to no buffering, you can immediately after each printf output .
Method Two : A call before output setvbuf, will be set to stdout line buffered , each printf followed by a newline.
Method three : A After each call to printf call fflush (stdout); flush the output buffer .
------ For reference only -------------------------------------- -
stdout a buffer , you can use other functions setbuf be set to unbuffered , or use fflush flushes the output buffer .
That is, to call in the A
setbuf (stdout, NULL);
or
fflush (stdout);
You said fflush unusable What does it mean ?
------ For reference only -------------------------------------- -
print swipe when flush buffer .
------ For reference only -------------------------------------- -

Thank you to answer ! which means that the B call did not work. . . Do you mean to call it inside the A ? A own output to stdout no problem oh. . . That 's what the A call it ? So much printed information
------ For reference only ---------------------------------- -----

Thank you for answering ! Do you mean also in A refresh ? printf before each refresh ? A own output to stdout no problem oh. . .
------ For reference only -------------------------------------- -

Thank you to answer ! which means that the B call did not work. . . Do you mean to call it inside the A ? A own output to stdout no problem oh. . . That 's what the A call it ? So much printed information          
  
is in A calls. A separate operation is no problem, because the output of printf when the terminal and redirection , pipes, etc. There are different buffering strategy .   
Method One : A called once before output setbuf (stdout, NULL); redirect stdout is set to no buffering, you can immediately after each printf output .   
Method Two : A call before output setvbuf, will be set to stdout line buffered , each printf followed by a newline.   
Method three : A After each call to printf call fflush (stdout); flush the output buffer .  

Thank instructor , I verified it and see .
------ For reference only -------------------------------------- -

Thank you to answer ! which means that the B call did not work. . . Do you mean to call it inside the A ? A own output to stdout no problem oh. . . That 's what the A call it ? So much printed information                
    
is in A calls. A separate operation is no problem, because the output of printf when the terminal and redirection , pipes, etc. There are different buffering strategy .     
Method One : A called once before output setbuf (stdout, NULL); redirect stdout is set to no buffering, you can immediately after each printf output .     
Method Two : A call before output setvbuf, will be set to stdout line buffered , each printf followed by a newline.     
Method three : A After each call to printf call fflush (stdout); flush the output buffer .          
  
Thank instructor , I verified it and see .  

I have verified the three methods are feasible ! ! ! Thanks !
A, but given a lot of threads and file, select the method one better.
Thanks again shucheng36 master ! ! !
------ For reference only -------------------------------------- -

MTK_ frog fish that is also true, but forgot to give points , sorry ! Thank you ! !

没有评论:

发表评论