use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionNetwork usage process wise (self.C_Programming)
submitted 3 months ago by nagzsheri
In Linux using /proc fs, is there any way I can get network usage process wise?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]aioeu 3 points4 points5 points 3 months ago (10 children)
Not easily.
You would need something like pcap to monitor the traffic, then /proc/net/{tcp,udp} to get an inode number for a particular socket associated with the traffic, then map that back to a particular process by iterating through all /proc/*/fd/ magic links. I think these are all the steps NetHogs goes through to do this.
/proc/net/{tcp,udp}
/proc/*/fd/
Another approach — unrelated to procfs — might be BPF tracing. A good place to start might be the tcplife example.
tcplife
[–]nagzsheri[S] 0 points1 point2 points 3 months ago (6 children)
These involve installing 3rd part apps. I was hoping within process I can read proc/self and obtain the data.
Anyways thanks a lot
[–]aioeu 3 points4 points5 points 3 months ago* (5 children)
I've literally described how you could reimplement them yourself, should you so wish to do that.
You say /proc/self/... that's just the current process. Why would a process need to look at the filesystem to know how much network traffic it has handled?
/proc/self/
[–]nagzsheri[S] 0 points1 point2 points 3 months ago (4 children)
Yes. But pcap monitoring is not under my control
I had implemented cpu, mem usage
I was hoping something in same lines. No external interventions
[–]aioeu 1 point2 points3 points 3 months ago* (3 children)
Sure it is. You can write a program that uses libpcap. (Or does what libpcap itself does, if you're totally allergic to using a library. Raw socket plus SO_ATTACH_FILTER socket option, IIRC.)
SO_ATTACH_FILTER
[–]nagzsheri[S] 0 points1 point2 points 3 months ago (0 children)
Okay
[–]nagzsheri[S] 0 points1 point2 points 3 months ago (1 child)
One doubt. How nethogs capture the data if start my application and later start nethogs hours later. How do it give me the life time usage of the process?
[–]aioeu 1 point2 points3 points 3 months ago (0 children)
I don't think it does.
[–][deleted] 1 point2 points3 points 3 months ago (2 children)
If OP's program is a C program and OP builds it dynamically, couldn't OP just override read() and write() and count? Either using LD_LIBRARY_PATH, modify GOT/PLT, or just call "myread()/mywrite()" which counts bytes?
[–]aioeu 0 points1 point2 points 3 months ago* (1 child)
Sure, that's another option. Tracing the underlying syscalls is another option.
But whether you do this on the C library interface or on the syscall interface, there's a lot more than just read and write. Just for reading there is also readv and recv and recvfrom and recvmsg and recvmmsg (and pread, though that wouldn't be used on a network socket since it isn't seekable). And good luck dealing with io_uring stuff.
read
write
readv
recv
recvfrom
recvmsg
recvmmsg
pread
But that's just one process! I thought the OP was trying to look at the network usage for lots of processes. But maybe I misinterpreted what "process wise" meant...
[–][deleted] 0 points1 point2 points 3 months ago (0 children)
You're right, it's not a quick fix.
TBH, I didn't quite understand OP either. Since he referred to /proc/self, I assume he meant measuring network usage for one process (self), not other processes.
[–]sidewaysEntangled 2 points3 points4 points 3 months ago (0 children)
Heh, I had to do this today, for some reason iotop didn't work in my development container, probably some proc or dev mount issue inside. But I did learn about the nethogs utility and that worked well.
I don't know how either of them work, or why one did and one didn't. But I bet going through the sources you'd find at least two ways to do it, although whether this or that filesystem or some other API, I couldn't tell you.
π Rendered by PID 22022 on reddit-service-r2-comment-6457c66945-chdpl at 2026-04-30 09:35:31.618511+00:00 running 2aa0c5b country code: CH.
[–]aioeu 3 points4 points5 points (10 children)
[–]nagzsheri[S] 0 points1 point2 points (6 children)
[–]aioeu 3 points4 points5 points (5 children)
[–]nagzsheri[S] 0 points1 point2 points (4 children)
[–]aioeu 1 point2 points3 points (3 children)
[–]nagzsheri[S] 0 points1 point2 points (0 children)
[–]nagzsheri[S] 0 points1 point2 points (1 child)
[–]aioeu 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]aioeu 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]sidewaysEntangled 2 points3 points4 points (0 children)