Sleep function under Linux, fflush function and buffer problem

  
 

//---------------------- sleep() function

Function name: sleep Function: Execute suspend for a period of time Usage: Unsigned int sleep(unsigned int seconds); Example:

#include <unistd.h>#include <stdio.h>

int main(void) { int i; Br>

for (i=1; i<5; i++) { printf("Sleeping for %d seconds\ ", i); sleep(i); } return 0; }

Another:

The VC function prototype in VC++ is:

void Sleep(DWORD dwMilliseconds);

The sleep function prototype under linux is:

unsigned Int sleep(unsigned int seconds);

MFC is microseconds, and linux is seconds. The microsecond thread sleep function under Linux is:

void usleep(unsigned long usec);int usleep(unsigned long usec); /* SUSv2 */

or use the select function +timeval The structure can also be (up to the microseconds),

or use the pselect function +timespec (can be accurate to nanoseconds, accurate enough!)

//------- ---------fflush() function

The following is C99's definition of the fflush function:

int fflush(FILE *stream);

if stream Pointing to the output stream or update stream, and the most recent operation of this update stream is not input, the fflush function will transfer any data to be written in the stream to the host environment to write to the file. Otherwise, its behavior is undefined.

The original text is as follows:

int fflush(FILE *stream);


If stream points to an output stream or an update stream in whichthe most most recent Operation was not input, the fflush function causesany unwritten data for that stream to be delivered to the host environmentto be written to the file; otherwise, the behavior is undefined.

where, the host environment can be understood as Operating system
or kernel, etc.

Thus, if stream points to an input stream (such as stdin), the behavior of the fflush function is undefined. Therefore, using fflush(stdin) is not correct, at least it is not portable.

The function of the fflush library function is to write out all unwritten data in the file stream immediately. For example, you can use this function to ensure that an interactive prompt is sent to the terminal before attempting to read in a user response. Using this function also ensures that important data has been written to disk before the program continues to execute. Sometimes when debugging a program, you can use it to determine if the program is writing data instead of being suspended. Note that calling the fclose function implicitly performs a flush operation, so you don't have to call fflush before fclose.

This function should be used sparingly. The book says fflush is only used for exceptions. Normally, fclose and EXIT should be used to refresh the output buffer. It can't be used like this under LINUX.

*





*

//---- -------------- Buffer problems

*

Details can be seen: a summary of the understanding of streams and buffers and the solution to general standard input problems

*

*



The purpose of standard I/O caching is to minimize the use of read and write calls. Quantity. It also automatically caches each I/O stream, avoiding the hassle of having to consider this. Unfortunately, the most confusing thing about the standard I/O library is its cache. Different types of caches often make people feel overwhelmed when doing I/O operations. Standard I/O provides three types of caching: full cache, row cache, and no cache.

The most clear thing to say is Stevens' "Advanced Programming for UNIX Environments". The following is taken from Stevens' "Advanced Programming for UNIX Environments" Chapter 5:


//******************

Standard I/O provides three types of caching: (1) full caching. In this case, the actual I/O operation is performed when the standard I/O buffer is filled. Files that reside on disk are usually fully cached by standard I/O libraries. When performing the first I/O operation on a stream, the associated standard I/O function usually calls m a l l o c (see Section 7.8) to get the cache to use. The term refresh (f l u s h) describes the write operation of the standard I/O cache. The cache can be automatically refreshed by standard I/O routines (for example when filling up a cache), or the function ff l u s h can be called to refresh a stream. It is worth noting that in the U N I X environment, refreshing has two meanings. In the case of standard I/O libraries, refreshing means writing the contents of the cache to disk (the cache can only be partially filled). In terms of terminal drivers (such as the t c f l u s h function described in Chapter 11), refreshing means discarding data in the existing cache. (2) Row cache. In this case, the standard I/O library performs I/O operations when new line characters are encountered in the input and output. This allows us to output one character at a time (using the standard I/O fputc function), but the actual I/O operation is only done after a line has been written. Row buffering is typically used when the stream involves a terminal (such as standard input and standard output). There are two restrictions on row caching. The first one is because the length of the cache used by the standard I/O library to collect each row is fixed, so as long as the cache is filled, I/O operations are performed even if a new line character has not yet been written. The second is: whenever you want to get input data from (a) a stream without a cache, or (b) a stream with a row cache (which requires data from the kernel in advance), then you will get input data from the standard input and output library. Causes all row cache output streams to be flushed. The reason for having a description in parentheses in (b) is that the required data may already be in the cache, and it does not require the kernel to do this when the data is needed. Obviously, inputting from a stream without a cache ((a)) requires that data be obtained from the kernel at the time. (3) Without cache. Standard I/O libraries do not cache characters. If you write a number of characters to a stream without a buffer using a standard I/O function, it is equivalent to writing these characters to the associated open file using the w r i t e system call function. The standard error stream s t d e r r is usually uncached, which allows error messages to be displayed as soon as possible, regardless of whether they contain a new line character. ANSI C requires the following caching features: (1) They are fully cached if and only if the standard input and standard output do not involve an interaction device. (2) Standard errors are never fully cached. However, this does not tell us if standard input and output involve uninterrupted devices, whether they are cached or line cached, and whether the standard output is uncached or line cached. The S V R 4 and 4. 3 + B S D systems use the following types of cache by default: • Standard errors are not cached. • If other streams are involved in the terminal device, they are line cached; otherwise they are fully cached.

//****************************

*

We Standard input and output are often used, and ANSI C does not impose provisions on the cache characteristics of stdin, stdout, and stderr, so that different systems may have different cache characteristics for stdin, stdout, and stderr. The main cache features are: stdin and stdout are line buffers; stderr is uncached.



Copyright © Windows knowledge All Rights Reserved