How to find if an OS is 32 bit or 64 bit using a program ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well my answer is that for a 64 bit OS, the pointer size should be 8 byte and for a 32 bit OS pointer size should be 4 byte ?
No. That is testing whether the program itself was compiled 32 bit or 64 bit. That doesn't tell you whether the OS is 32 bit or 64 bit.
In the x86 world, you can compile and run 32 bit programs under a 64 bit OS. Also, nothing in Linux itself says you can't compile and run 64 bit programs under a 32 bit OS. The x86_64 architecture won't let you do that and I don't know of any Linux ports to architectures that let you do that. But if the architecture let you, Linux probably would let you.
Quote:
So checking the sizeof(void*) should be good enough. But is there any other way ?
There are ways for a human to do it that don't seem to require a human level of intelligence. For the basic mechanics (as opposed to the intelligence) of doing almost anything in Linux, if a human can do it then a program could as well.
Hko already gave you a more concrete version of that answer (a human might run uname and look at the result).
Would someone with a 64-bit system please compile this program for 32 bit pointers, run it on that 64-bit system, and post the output? If it says 64 bits, then there's your magic bullet.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char buffer[1024];
FILE *phyle;
phyle=fopen("/proc/self/maps","r");
if(!fgets(buffer,sizeof(buffer),phyle))
{
abort();
}
printf("bits per pointer is %d\n",2*(strchr(buffer,' ')-buffer-1));
return 0;
} /* main() */
to yield the same result (and it also uses unsigned long long instead of unsigned long). This is a real code sample for dealing with 2-byte japanese chars.
The point is, with some bit-testing on unsigned long long and unsigned long variables you can find out the CPU bit-width.
with some bit-testing on unsigned long long and unsigned long variables you can find out the CPU bit-width.
That is an indirect and unsound way of testing the thing sizeof(void*) tests much more directly and reliably.
If you want to know the size of a pointer in the architecture/mode in which your program is running, just test that. Don't test something indirect.
"CPU bit-width" is a very vague phrase subject to many possible meanings. But I don't think any of those meanings can be reliably tested by the method you propose.
The OP apparently wanted to know the size of a pointer in the architecture/mode in which the Linux kernel is running. That might not be the same as the size of a pointer in the program doing the test.
There are more sound ways to find out the bit-size of the system, but gergely89 gives a quite interesting view on dealing with some value comparing tasks that will certainly give different results on 32-bit and 64-bit systems. He didn't mark his sample as the way to test for 64-bit systems, but as a hint on coding differences. At least that's how I interpreted it. Thanks anyway!
Hi,
Firstly thanks for your replies. What i wanted to know was how to "programmatically" find if an OS is 32 or 64 bit without assuming that *nix specific system calls would work on it.
Say, i don't know if the OS in question is windows or *nix.
So is there a way, i can write a program and conclude that the OS in use is 32 bit or 64 bit.
I agree that you can run a 32 bit compiled program on a 64 bit OS(but not the vice versa)
Hi,
Say, i don't know if the OS in question is windows or *nix.
So is there a way, i can write a program and conclude that the OS in use is 32 bit or 64 bit.
Regards
lali
Yes but it is going to be platform specific so would have to hide it under a layer of abstraction. For example in windows you could call GetSystemInfo and check the processor type via "wProcessorArchitecture" in the returned SYSTEM_INFO. On Linux you maybe use the same method as wine and parse "/proc/cpuinfo" checking for lm (long mode) in the flags in cpuinfo, if present it is 64bit system.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.