问题
How can I read from the stdout of my program?
One of my threads needs to access the stdout to read what the other threads are logging.
I'm using the dear old plain C.
回答1:
Make stdout use a pipe by replacing the stdout fd with the write side fd of a pipe using dup.
回答2:
You shouldn't. stdout is supposed to be the output to your program and you're not therefore supposed to be reading it in as well. If you want to do this, do your logging elsewhere.
回答3:
If I understand you correctly, you want your threads to communicate with each other. Using stdout for inter-thread communication doesn't seem to be the right way to solve this problem.
Perhaps you should try a Web search for "communication between threads".
来源:https://stackoverflow.com/questions/2228219/read-the-stdout-from-the-same-c-program