3

I am using tcpstat in a linux environment. I want to capture its output in a C program even though it has not finished. I tried using the popen() function, but it can only process the output after the program has finished. I want to process the output of tcpstat on the fly as and when it prints it on standard output. How do i do so?

For example,

$ tcpstat -i wlan0 1
Time:1297790227 n=2 avg=102.50  stddev=42.50    bps=1640.00
Time:1297790228 n=11    avg=86.36   stddev=19.05    bps=7600.00
Time:1297790229 n=32    avg=607.97  stddev=635.89   bps=155640.00
Time:1297790230 n=13    avg=582.92  stddev=585.55   bps=60624.00

The above output keeps going on till infinity. So I want to process the output in a C program as and when tcpstat outputs something onto stdout.

Thanks and Regards,

Hrishikesh Murali

2 Answers 2

6

Run tcpstat -i wlan0 -a 1 | your_program and read from the standard input in your program. This way the shell will take care of the piping.

The popen library function and the pipe system call can be used to achieve the same result at a lower level. You may want to take a look at named pipes too - they appear like files in userspace and can be manipulated in the same way.

Sign up to request clarification or add additional context in comments.

2 Comments

But when I do popen() and then fread(), fread doesn't execute until the command executed by popen finishes. Since the command runs forever, there is a problem. Your suggestion of using a pipe is fine, but I want to make my program independent of a shell. Also, the program just does not execute this one command. There are multiple threads executing similar commands in one single program and each thread has to read the output of tcpstat independently. So piping is not enough I guess. Also, how do you say I can achieve the functionality of the pipe using the popen library function?
I guess you should be fine with popen, only make sure that the other process flushes its output regularly, as I pointed out in my comment to Hasturkun's answer.
5

Run tcpstat with the -F option, this will cause it to flush its output on every interval. (instead of using the default block buffering for stdout)

In addition, you may want to explicitly disable the buffering on your popen FILE handle using setbuf, eg.

setbuf(popen_fd, NULL);

Alternately, you can set it to be line buffered, using setlinebuf

setlinebuf(popen_fd);

5 Comments

+1: the problem really could be that the tcpstat process doesn't flush its output regularly.
Thanks, I'll keep this in mind :-) Is there a way using popen? I'm using multiple threads in the same program, which use tcpstat independently, and have to process the output independently. So piping wouldn't work there, would it?
@Hrishikesh: you should be able to do this using popen. as an aside, I'm not entirely sure that the -F flag will work, the tcpstat code appears to output using write rather than fwrite, so shouldn't be buffered in the first place.
@Hasturkun: in addition to the C lib buffering, the OS also buffers.
@wnoise: That may be so, but the fflush(NULL) the code employs probably wouldn't flush anything in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.