Re: fc5 C system call always returning -1

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

 



On Thu, 2006-09-21 at 17:21 -0700, Greg Frinchaboy wrote:
> This may be an old issue but I could not find info in the archives.
> 
> u_int num_cpus = system("cat /proc/cpuinfo | grep -c processor > 
> /dev/null");
> 
> Has yielded the number of cpus on the system since I used it in RH7.3 
> and fc3.
> Now in fc5, all uses of system("cmd");  return -1
> 
> The command executes OK, just the return value is always -1.
> 
> u_int num_cpus = system("cat /proc/cpuinfo");
> dumps the cpuinfo file to the system console
> 
> u_int num_cpus = system("cat /proc/cpuinfo | grep -c processor");
> dumps a 2 to the system console, etc.
> 
> Any help would be appreciated...
> Greg

your line of code suggests you expect system() to return you an integer
parsed from the console output of your command.  This isn't what system
does:

man system
"The value returned is -1 on error (e.g.  fork() failed), and the return
status of the command otherwise."

"grep -c" puts the count of the matches found on stdout, and returns a
status of zero if something is found and 1 otherwise (and 2 on error).
So I'd expect your "num_cpus" to be zero, typically, and 1 if no
'processor' string was found in /proc/cpuinfo.  Indeed, that's exactly
what happened when I tried it just now.

By the way, at the command line you can use "echo $?" to show the exit
status of the last executed command.

Hope that helps.

Matt Davey		I went to a Grateful Dead Concert and they played 
mcdavey@xxxxxxxxxxxxxx 	     for SEVEN hours.  Great song.


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux