confused of using usleep & sleep
Dear all member,
I'm trying to create new C program in linux which uses usleep function. The simplified code looks like below. Code:
#include <stdio.h> But, the program do not work like what exactly i want. The program waits 2 seconds to print two sentences. I created similar program using sleep(ms) function for windows. It successfully compiled and worked properly like it should. Please help. I have run out of time just for this. Thank you. ag2uki |
Try it like this
Code:
#include <stdio.h> Code:
#include <stdio.h> Strictly speaking, your code should look like this: Code:
#include <stdio.h> |
Thanks for your reply uncle theodore, it works.
So we have to flush output buffer or add new line character ("\n") at the end of the argument of printf, is not it? Humm... It's really annoying. I have not met with things like this before. If we use another programming language, we do not have to do it. For example, I wrote a similar program using Java as programming language. Then, it works properly. Code:
public class TestSleep { |
Quote:
Code:
cat > 1.c <<EOD; gcc 1.c -o 1; ./1 Code:
beginning of main line; end of main line Code:
cat > 2.c <<EOD; gcc 2.c -o 2; ./2 Code:
beginning of main line; Code:
cat > 3.c <<EOD; gcc 3.c -o 3; ./3 Code:
beginning of main line; ahhhhhh end of main line Code:
cat > 3.c <<EOD; gcc 3.c -o 3; ./3 Code:
ahhhhhh beginning of main line; end of main line Code:
cat > 5.c <<EOD; gcc 5.c -o 5; ./5 Code:
beginning of main line; ahhhhhh end of main line |
Use
Code:
fprintf(stderr, ...) |
Quote:
|
Quote:
Quote:
Code:
prog1 2> redirected_stderr.txt |
No, that's not what I meant. If you know that your program prints to stderr when it's not supposed to, because standard output should be printed to stdout, where it's expected, then, of course, you can redirect or do the usual stuff with piping, etc. But if someone is using your program and does NOT know that, s/he is in for a lot of trouble.
Things should be done in standard way. That's why we have standards... :) |
Quote:
Buffered output allows the system to wait till it has some time before it's written. So it can spend time on things that are more important at that moment. |
Quote:
If the author needs a diagnostic message that helps him/her to debug the program it is the UNIX way. For example, in Perl I print all my diagnostic messages through 'warn' which outputs to STDERR. My favorite example is Code:
cat f1.txt f2.txt f3.txt where, say, f2.txt doesn't exist. The error message (diagnostics) goes to stderr, normal output - to stdout |
All times are GMT -5. The time now is 05:58 AM. |