[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: FC5 system() call

On Friday, Sep 22nd 2006 at 07:02 -0700, quoth Brian D. McGrew:

=>Morning all,
=>Using FC3 life is good.  I compile up my code on FC5 with gcc-4.1.1 and
=>all of a sudden the system() call is broken.
=>The code:
=>int num_proc = system("cat /proc/cpuinfo | grep -c processor >>
=>/dev/null 2>&!");
=>On every FC3 machine we've run on, this works fine and returns 1, 2 or
=>Likewise on every FC5 machine we've ever run on, it returns -1, nothing
=>else.  The man page for system() hasn't changed and I can't find any
=>other information about it.
=>Any ideas?

First of all, I suspect you intended to say 2>&1 instead of 2>&!

Next, why say 

cat /proc/cpuinfo | grep -c processor

instead of just saying

grep -c processor /proc/cpuinfo


Third, What happens if you issue the command directly from the 

Fourth, why say >> instead of >? Not a big deal but I'm just curious about 
the intended semantics of appending to /dev/null.

Fifth, why redirect stderr to stdout in the first place? Is there some 
sort of suspicion that /proc/cpuinfo might not be there?

Sixth, whenever you get a system error, you need to find out why you're 
getting it and the proper way to get started is to call perror(3) which 
looks at the value of errno.

And last, I have no idea what your application is doing, so your error is 
more likely the result of a process problem and not related to 
permissions. Use this as a suggestion to apply the information from errno 
and perror to possible fork/exec problems instead of problems with 
open/read/close sequences.

Be aware that system is very handy but has a higher overhead than what 
people might expect. There's nothing wrong with using it, but you should 
know that when you use it you are forking your process which creates a 
full duplicate. Then that new child execs a shell and runs the specified 
command. The child then exits and the system call then returns the exit 
status of the child. A simple open, read loop, close is going to be a lot 
more efficient.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]