You can use this for mostly all kind of linux distro
For C code
num_cpus = sysconf( _SC_NPROCESSORS_ONLN );
(In QNX systems , you can use num_cpus = sysinfo_numcpu())
For shell scripting, you can use cat /proc/cpuinfo
or use lscpu or nproc commands in linux