LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-20-2009, 10:57 AM   #1
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
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
 
Old 02-20-2009, 11:26 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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>
#include <sys/utsname.h>

int main()
{
    struct utsname kernel_info;
    if (uname(&kernel_info) < 0) {
        perror("Error reading kernel info");
        return 1;
    }
    puts(kernel_info.machine);
    return 0;
}
 
Old 02-20-2009, 11:52 AM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
Quote:
Originally Posted by lali.p View Post
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).

Last edited by johnsfine; 02-20-2009 at 11:53 AM.
 
Old 02-20-2009, 03:36 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
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() */
 
Old 02-20-2009, 04:09 PM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
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
00400000-00405000 r-xp 00000000 08:05 22036532                           /bin/cat
00504000-00505000 rw-p 00004000 08:05 22036532                           /bin/cat
00505000-00526000 rwxp 00505000 00:00 0
2a95556000-2a95557000 rw-p 2a95556000 00:00 0
2a9557f000-2a95581000 rw-p 2a9557f000 00:00 0
2a95581000-2a983c4000 r--p 00000000 08:05 786549                         /usr/lib/locale/locale-archive
342c000000-342c015000 r-xp 00000000 08:05 33980418                       /lib64/ld-2.3.4.so
342c114000-342c115000 r--p 00014000 08:05 33980418                       /lib64/ld-2.3.4.so
342c115000-342c116000 rw-p 00015000 08:05 33980418                       /lib64/ld-2.3.4.so
342c200000-342c32f000 r-xp 00000000 08:05 33980421                       /lib64/tls/libc-2.3.4.so
342c32f000-342c42f000 ---p 0012f000 08:05 33980421                       /lib64/tls/libc-2.3.4.so
342c42f000-342c432000 r--p 0012f000 08:05 33980421                       /lib64/tls/libc-2.3.4.so
342c432000-342c434000 rw-p 00132000 08:05 33980421                       /lib64/tls/libc-2.3.4.so
342c434000-342c439000 rw-p 342c434000 00:00 0
7fbfffe000-7fc0000000 rw-p 7fbfffe000 00:00 0
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
Edit: But it's worse than that. I just copied cat from a 32bit linux to cat32 on a 64 bit linux and ran it:
Code:
 cat32 /proc/self/maps
0060b000-00621000 r-xp 00000000 08:05 9158669                            /lib/ld-2.3.4.so
00621000-00622000 r-xp 00015000 08:05 9158669                            /lib/ld-2.3.4.so
00622000-00623000 rwxp 00016000 08:05 9158669                            /lib/ld-2.3.4.so
00625000-0074e000 r-xp 00000000 08:05 9158683                            /lib/tls/libc-2.3.4.so
0074e000-00750000 r-xp 00128000 08:05 9158683                            /lib/tls/libc-2.3.4.so
00750000-00752000 rwxp 0012a000 08:05 9158683                            /lib/tls/libc-2.3.4.so
00752000-00754000 rwxp 00752000 00:00 0
08048000-0804c000 r-xp 00000000 00:26 240676                             /home/fine/cat32
0804c000-0804d000 rwxp 00003000 00:26 240676                             /home/fine/cat32
0804d000-0806e000 rwxp 0804d000 00:00 0
f7dd4000-f7fd4000 r-xp 00000000 08:05 786549                             /usr/lib/locale/locale-archive
f7fd4000-f7fd5000 rwxp f7fd4000 00:00 0
f7ffd000-f7ffe000 rwxp f7ffd000 00:00 0
ffffc000-ffffe000 rw-p ffffc000 00:00 0
ffffe000-fffff000 r-xp ffffe000 00:00 0
That is real hard to tell apart from using cat that way on a 32 bit system. Yes, I really did run that on a 64-bit system.

Last edited by johnsfine; 02-20-2009 at 04:18 PM.
 
Old 02-20-2009, 07:16 PM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
Well, it was worth a shot. Thanks for the info.
 
Old 02-23-2009, 12:19 PM   #7
gergely89
Member
 
Registered: Feb 2009
Posts: 100

Rep: Reputation: 21
A line like
Quote:
if (cmp1 == 0xFFFFFFE3 && cmp2 == 0xFFFFFF80 && cmp3 == 0xFFFFFF80)
will give different result on 32-bit and 64-bit architectures, the 64-bit version needs to look like
Quote:
if (cmp1 == 0xFFFFFFFFFFFFFFE3 && cmp2 == 0xFFFFFFFFFFFFFF80 && cmp3 == 0xFFFFFFFFFFFFFF80)
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.

linux

Last edited by gergely89; 02-27-2009 at 11:51 PM.
 
Old 02-23-2009, 12:26 PM   #8
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
Quote:
Originally Posted by gergely89 View Post
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.

Last edited by johnsfine; 02-23-2009 at 12:29 PM.
 
Old 05-20-2009, 12:28 PM   #9
smkururu
LQ Newbie
 
Registered: May 2009
Location: /dev/null
Distribution: Ubuntu Jaunty
Posts: 11

Rep: Reputation: 0
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.

Last edited by smkururu; 05-20-2009 at 01:08 PM.
 
Old 05-22-2009, 10:41 AM   #10
soleilarw
Member
 
Registered: Apr 2009
Posts: 107

Rep: Reputation: 19
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

Last edited by soleilarw; 06-18-2009 at 05:06 AM.
 
Old 05-23-2009, 01:15 PM   #11
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 05-23-2009, 04:40 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by lali.p View Post
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.

Last edited by dmail; 05-23-2009 at 04:43 PM.
 
Old 05-24-2009, 01:51 AM   #13
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,772

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
64 bit cpu-64 bit Ubuntu-are there 32 bit app issues? sofasurfer Ubuntu 7 04-09-2014 03:02 PM
32-bit to 64-bit migration for c++ program newbix Programming 3 01-15-2008 08:27 PM
How do i find out if the machine is 64-bit or 32-bit? hondo Linux - General 2 07-12-2007 06:26 PM
find out if a program is 32 or 64 bit galle Linux - Software 2 01-31-2007 02:59 PM
Assembling a 32 bit sample program in a 64 bit world debiant Programming 0 10-14-2006 03:52 PM


All times are GMT -5. The time now is 11:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration