I have following two sample codes written in Linux. Both of them print the network interface names of the linux system. My question is which code is faster and reliable and why?
/sbin/ip -o link show up | awk -F : '{ print $2 }' without all the C code is definitely the way to go.
Otherwise, choose #2. Calling external commands from C programs is never the way to go. Paths where executables are kept change, you have no clue which version of awk is being called, the fork may fail due to resource issues. There is very little that can go wrong with the last one using the Unix C API..
#2 is orders of magnitude faster and infinitely more reliable since it is not dependent upon external programs. (and thus
infinitely more secure also).