How to find if an OS is 32 bit or 64 bit using a program ?
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 ?
So checking the sizeof(void*) should be good enough. But is there any other way ? Sorry for the question, but i just wanted to know if there was any other way to find the answer to this question programmatically. Regards lali |
It is possible to get a string describing the processor type. I suppose it is possible to deduct 32-bit or 64-bit from there.
This code prints "i686" on my PC with a Intel 32-bits CPU. Code:
#include <stdio.h> |
Quote:
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:
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> |
I'm too lazy to try it, but why should it work? Even for a 64-bit application the length of addresses in /proc/self/maps isn't consistent
Code:
cat /proc/self/maps Code:
cat32 /proc/self/maps |
Well, it was worth a shot. Thanks for the info.
|
A line like
Quote:
Quote:
The point is, with some bit-testing on unsigned long long and unsigned long variables you can find out the CPU bit-width. linux |
Quote:
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. |
I believe this is what you want:
uname -m on 64 bit computer it'll show x86_64 Give it a try. Edit: Sorry, I just read that you want to check it programmatically. |
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!
Linux Archive |
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) Regards lali |
Quote:
|
Regarding Linux, you'll have to define 'OS'. You could well have a 64-bit-enabled kernel running on a system with 32-bits and binaries.
|
All times are GMT -5. The time now is 01:24 PM. |