How to know the OS architecture (32 or 64 bits)?

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 64
bits, however it doesn't send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

···

--
Iñaki Baz Castillo <ibc@aliax.net>

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 64
bits, however it doesn't send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

--
Iñaki Baz Castillo <ibc@aliax.net>

You can use Fixnum#size to get number of bytes for a Fixnum and multiply by 8 bits/byte:

irb(main):001:0> 1.size * 8
=> 64
irb(main):002:0> RUBY_PLATFORM
=> "x86_64-linux"

1.size * 8

=> 32

RUBY_PLATFORM

=> "universal-darwin9.0"

I think it's reliable. (Although I guess there could be a 32-bit ruby running on a 64-bit platform.)

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Jan 14, 2010, at 6:12 PM, Iñaki Baz Castillo wrote:

Probably not.

Could you explain what you're trying to do? Without knowing why you think
you need to know this, it's hard to give you good advice. I couldn't tell
you off the top of my head whether the machine I'm working on right now is
32-bit or 64-bit. I've been doing software development on it for two years
and I've never needed to know.

-s

···

On 2010-01-14, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

I can't vouch for how accurate it is, but an OS gem was recently announced on this list.
gem install os

irb(main):001:0> require 'os'
=> true
irb(main):002:0> OS.bits
=> 64
irb(main):004:0> OS.posix?
=> true
irb(main):005:0>

···

On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 64
bits, however it doesn't send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

The application I'm developing uses Posix Queue Messages thanks to posix_mq
gem:
  posix_mq - POSIX message queues for Ruby

When the app runs it tries to create a posix mqueue with maxmsg=5000 and
msgsize=1024. The user running the application could have not permissions to
create such posix mqueue due to system limits ("ulimit -q").

In that case the creation of the posix mqueue raises a Errno::ENOMEM and I
want to tell the user the exact amount of bytes required.

The algorimth to know such amount of required bytes is:

  queue.attr.mq_maxmsg * sizeof(struct msg_msg *) +
  queue.attr.mq_maxmsg * queue.attr.mq_msgsize

In 32 bits sizeof(struct msg_msg *) is 4 bytes while in 64 it's 8 bytes, so
the total ammount of bytes changes. This means that "ulimit -q" must be
different depending on the system architecture (32/64 bits).

···

El Viernes, 15 de Enero de 2010, Seebs escribió:

On 2010-01-14, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> Hi, is there a reliable way under Ruby to know the OS architecture (32 or
> 64 bits)?

Probably not.

Could you explain what you're trying to do? Without knowing why you think
you need to know this, it's hard to give you good advice. I couldn't tell
you off the top of my head whether the machine I'm working on right now is
32-bit or 64-bit. I've been doing software development on it for two years
and I've never needed to know.

--
Iñaki Baz Castillo <ibc@aliax.net>

It's really good!
Thanks a lot.

···

El Viernes, 15 de Enero de 2010, Rob Biedenharn escribió:

On Jan 14, 2010, at 6:12 PM, Iñaki Baz Castillo wrote:
> Hi, is there a reliable way under Ruby to know the OS architecture
> (32 or 64
> bits)?
>
> I've just found RUBY_PLATFORM constant which returns "x86_64-linux"
> under 64
> bits, however it doesn't send very reliable for me.
>
> I need a way working under Linux and BSD. Thanks for any suggestion.

You can use Fixnum#size to get number of bytes for a Fixnum and
multiply by 8 bits/byte:

irb(main):001:0> 1.size * 8
=> 64
irb(main):002:0> RUBY_PLATFORM
=> "x86_64-linux"

> 1.size * 8
=> 32
> RUBY_PLATFORM
=> "universal-darwin9.0"

--
Iñaki Baz Castillo <ibc@aliax.net>

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

···

On 1/15/2010 9:36 AM, Walton Hoops wrote:

On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 64
bits, however it doesn't send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

I can't vouch for how accurate it is, but an OS gem was recently announced on this list.
gem install os

irb(main):001:0> require 'os'
=> true
irb(main):002:0> OS.bits
=> 64
irb(main):004:0> OS.posix?
=> true
irb(main):005:0>

Correct me if I'm wrong, but don't 32bit apps running in a 64bit architecture run in a special space which mimics 32 bits? If that's the case, then I'd think the behaviour was as expected.

Matt

···

On Sat, 16 Jan 2010, Walton Hoops wrote:

On 1/15/2010 9:36 AM, Walton Hoops wrote:

On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 64
bits, however it doesn't send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

I can't vouch for how accurate it is, but an OS gem was recently announced on this list.
gem install os

irb(main):001:0> require 'os'
=> true
irb(main):002:0> OS.bits
=> 64
irb(main):004:0> OS.posix?
=> true
irb(main):005:0>

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

Interesting, I'll take a look to its implementation. However in my case I will
never run 32 bits Ruby over a 64 bits SO.

···

El Viernes, 15 de Enero de 2010, Walton Hoops escribió:

On 1/15/2010 9:36 AM, Walton Hoops wrote:
> On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:
>> Hi, is there a reliable way under Ruby to know the OS architecture
>> (32 or 64
>> bits)?
>>
>> I've just found RUBY_PLATFORM constant which returns "x86_64-linux"
>> under 64
>> bits, however it doesn't send very reliable for me.
>>
>> I need a way working under Linux and BSD. Thanks for any suggestion.
>
> I can't vouch for how accurate it is, but an OS gem was recently
> announced on this list.
> gem install os
>
> irb(main):001:0> require 'os'
> => true
> irb(main):002:0> OS.bits
> => 64
> irb(main):004:0> OS.posix?
> => true
> irb(main):005:0>

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

--
Iñaki Baz Castillo <ibc@aliax.net>

Note that to know the bits it uses "rbconfig" gem, and them:

def self.bits
    @bits ||= begin
      require 'rbconfig'
      host_os = RbConfig::CONFIG['host_os']
      if host_os =~ /32/
        32
      else
        if host_os =~ /64/
          64
        else # cygwin...
          if (1<<32).class == Fixnum
            64
          else
            32
          end
        end
      end
    end
  end

In my server RbConfig::CONFIG['host_os'] = "linux-gnu" so finally it ends
doing:

          if (1<<32).class == Fixnum
            64
          else
            32
          end

Which is basically the same as doing

  if 1.size == 8
    64
  else
    32
  end

···

El Viernes, 15 de Enero de 2010, Walton Hoops escribió:

On 1/15/2010 9:36 AM, Walton Hoops wrote:
> On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:
>> Hi, is there a reliable way under Ruby to know the OS architecture
>> (32 or 64
>> bits)?
>>
>> I've just found RUBY_PLATFORM constant which returns "x86_64-linux"
>> under 64
>> bits, however it doesn't send very reliable for me.
>>
>> I need a way working under Linux and BSD. Thanks for any suggestion.
>
> I can't vouch for how accurate it is, but an OS gem was recently
> announced on this list.
> gem install os
>
> irb(main):001:0> require 'os'
> => true
> irb(main):002:0> OS.bits
> => 64
> irb(main):004:0> OS.posix?
> => true
> irb(main):005:0>

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

--
Iñaki Baz Castillo <ibc@aliax.net>

When the app runs it tries to create a posix mqueue with maxmsg=5000 and
msgsize=1024. The user running the application could have not permissions to
create such posix mqueue due to system limits ("ulimit -q").

Ahh.

The algorimth to know such amount of required bytes is:

  queue.attr.mq_maxmsg * sizeof(struct msg_msg *) +
  queue.attr.mq_maxmsg * queue.attr.mq_msgsize

In 32 bits sizeof(struct msg_msg *) is 4 bytes while in 64 it's 8 bytes, so
the total ammount of bytes changes. This means that "ulimit -q" must be
different depending on the system architecture (32/64 bits).

Ahh, yes. Although to be picky, you're now into one of the areas where, if
you ever end up on stranger hardware, the answer may be ill-defined; it
appears you care specifically about pointer sizes. There are some systems
where "pointer size" is not the same as "integer size", and so on... So
you could get some surprises.

I suspect Ruby tries to get a 64-bit fixnum if it can, so you're probably
set.

-s

···

On 2010-01-15, Iñaki Baz Castillo <ibc@aliax.net> wrote:
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

No matter how many bits the OS has, as long the compiled interpreter
is 32 bits, the returned values is going to be 32 bits.

Windows can run 32bits applications along with 64bits ones, but that
doesn't mean you can access 64bits address space or tools from 32bits
applications.

···

On Jan 15, 1:40 pm, Walton Hoops <wal...@vyper.hopto.org> wrote:

On 1/15/2010 9:36 AM, Walton Hoops wrote:

> On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:
>> Hi, is there a reliable way under Ruby to know the OS architecture
>> (32 or 64
>> bits)?

>> I've just found RUBY_PLATFORM constant which returns "x86_64-linux"
>> under 64
>> bits, however it doesn't send very reliable for me.

>> I need a way working under Linux and BSD. Thanks for any suggestion.

> I can't vouch for how accurate it is, but an OS gem was recently
> announced on this list.
> gem install os

> irb(main):001:0> require 'os'
> => true
> irb(main):002:0> OS.bits
> => 64
> irb(main):004:0> OS.posix?
> => true
> irb(main):005:0>

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

--
Luis Lavena

Note that to know the bits it uses "rbconfig" gem, and them:

Well, "rbconfig" is not a gem but a Ruby built in library.

In my server RbConfig::CONFIG['host_os'] = "linux-gnu" so finally it ends
doing:

          if (1<<32).class == Fixnum
            64
          else
            32
          end

Which is basically the same as doing

  if 1.size == 8
    64
  else
    32
  end

Definitively I don't like "os" gem at all. It could use
RbConfig::CONFIG['host_cpu'] rather than the not reliable
RbConfig::CONFIG['host_os']:

a) 32 bits host:

  RbConfig::CONFIG['host_os'] => "linux-gnu"
  RbConfig::CONFIG['host_cpu'] => "i486"

b) 64 bits host:

  RbConfig::CONFIG['host_os'] => "linux-gnu"
  RbConfig::CONFIG['host_cpu'] => "x86_64"

···

El Viernes, 15 de Enero de 2010, Iñaki Baz Castillo escribió:
  
--
Iñaki Baz Castillo <ibc@aliax.net>

Yes, for now I use 1.size to determine if I'm under 32 or 64 bits but it's
just a hack. Most probably I will code a small C extension method with returns
the exact value of sizeof(struct msg_msg *). Then the calculated valued would
be exact for any kinf of strange architecture (I hope).

Regards.

···

El Viernes, 15 de Enero de 2010, Seebs escribió:

> The algorimth to know such amount of required bytes is:
>
> queue.attr.mq_maxmsg * sizeof(struct msg_msg *) +
> queue.attr.mq_maxmsg * queue.attr.mq_msgsize
>
> In 32 bits sizeof(struct msg_msg *) is 4 bytes while in 64 it's 8 bytes,
> so the total ammount of bytes changes. This means that "ulimit -q" must
> be different depending on the system architecture (32/64 bits).

Ahh, yes. Although to be picky, you're now into one of the areas where, if
you ever end up on stranger hardware, the answer may be ill-defined; it
appears you care specifically about pointer sizes. There are some systems
where "pointer size" is not the same as "integer size", and so on... So
you could get some surprises.

--
Iñaki Baz Castillo <ibc@aliax.net>

Ah! But this tool doesn't claim to tell me about my address space or runtime enviornment, but rather my _OS_.

Just because I'm in a 32-bit app, doesn't mean I may not care that I'm on a 64-bit os.

···

On 1/15/2010 1:10 PM, Luis Lavena wrote:

On Jan 15, 1:40 pm, Walton Hoops<wal...@vyper.hopto.org> wrote:
   

On 1/15/2010 9:36 AM, Walton Hoops wrote:

On 1/14/2010 4:12 PM, Iñaki Baz Castillo wrote:
       

Hi, is there a reliable way under Ruby to know the OS architecture
(32 or 64
bits)?
         
I've just found RUBY_PLATFORM constant which returns "x86_64-linux"
under 64
bits, however it doesn't send very reliable for me.
         
I need a way working under Linux and BSD. Thanks for any suggestion.
         
I can't vouch for how accurate it is, but an OS gem was recently
announced on this list.
gem install os
       
irb(main):001:0> require 'os'
=> true
irb(main):002:0> OS.bits
=> 64
irb(main):004:0> OS.posix?
=> true
irb(main):005:0>
       

Hmm.. it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>
     

No matter how many bits the OS has, as long the compiled interpreter
is 32 bits, the returned values is going to be 32 bits.

Windows can run 32bits applications along with 64bits ones, but that
doesn't mean you can access 64bits address space or tools from 32bits
applications.

--
Luis Lavena

That seems like it should work.

-s

···

On 2010-01-15, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Most probably I will code a small C extension method with returns
the exact value of sizeof(struct msg_msg *). Then the calculated valued would
be exact for any kinf of strange architecture (I hope).

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

submit a bug! GitHub - rdp/os: The OS gem allows for some easy telling if you’re on windows or not. OS.windows? as well as some other helper utilities

My main concern with that though:
Would RbConfig::CONFIG['host_cpu'] return "x86_64" if I'm running 32-bit Linux on a 64-bit CPU?

···

On 1/15/2010 9:59 AM, Iñaki Baz Castillo wrote:

El Viernes, 15 de Enero de 2010, Iñaki Baz Castillo escribió:

Note that to know the bits it uses "rbconfig" gem, and them:
     

Well, "rbconfig" is not a gem but a Ruby built in library.

In my server RbConfig::CONFIG['host_os'] = "linux-gnu" so finally it ends
doing:

           if (1<<32).class == Fixnum
             64
           else
             32
           end

Which is basically the same as doing

   if 1.size == 8
     64
   else
     32
   end

Definitively I don't like "os" gem at all. It could use
RbConfig::CONFIG['host_cpu'] rather than the not reliable
RbConfig::CONFIG['host_os']:

a) 32 bits host:

   RbConfig::CONFIG['host_os'] => "linux-gnu"
   RbConfig::CONFIG['host_cpu'] => "i486"

b) 64 bits host:

   RbConfig::CONFIG['host_os'] => "linux-gnu"
   RbConfig::CONFIG['host_cpu'] => "x86_64"

No. If the OS isn't 64 bits itself, it'll run on an x86_64 architecture, but the CPU is in 32 bit mode. The only thing you can really test, is the bit-ness of the OS, not the CPU.

After all, how should a 32 bit OS deal with a 64 bit memory address?

···

On 15.01.2010 20:56, Walton Hoops wrote:

My main concern with that though:
Would RbConfig::CONFIG['host_cpu'] return "x86_64" if I'm running 32-bit
Linux on a 64-bit CPU?

--
Phillip Gawlowski

Walton Hoops wrote:

In my server RbConfig::CONFIG['host_os'] = "linux-gnu" so finally it ends
   if 1.size == 8

submit a bug! GitHub - rdp/os: The OS gem allows for some easy telling if you’re on windows or not. OS.windows? as well as some other helper utilities

My main concern with that though:
Would RbConfig::CONFIG['host_cpu'] return "x86_64" if I'm running 32-bit
Linux on a 64-bit CPU?

Not on this machine
cat /proc/cpuinfo | grep lm

flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc
arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16
xtpr pdcm lahf_lm tpr_shadow
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc
arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16
xtpr pdcm lahf_lm tpr_shadow

uname -a

Linux xxxxxxx 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC
2009 i686 GNU/Linux

ruby -e "require 'rbconfig'; p(Config::CONFIG['host_cpu'])"

"i486"

Mac

···

On 1/15/2010 9:59 AM, Iñaki Baz Castillo wrote:

--
Posted via http://www.ruby-forum.com/\.

My main concern with that though:
Would RbConfig::CONFIG['host_cpu'] return "x86_64" if I'm running 32-bit
Linux on a 64-bit CPU?

At least within a 32-bit OS (in a VM) it appears to be i686-linux so I
think we're safe there.

Also, thanks for the hint on 1.size I didn't know that one--it's
integrated now [v 0.6.1]. That wouldn't work for jruby (which always
returns 8), but should work fine for MRI, and I think we handle jruby
ok.

I also added a .mac? method--if anybody on a mac could try it out
[and/or tell me what the RUBY_PLATFORM is for OS X and OS X 64 bit] then
I could actually test it.

Re: OS.bits on a 32 within a 64...anybody know how you can tell that
you're on 64 bit running a 32 bit ruby, on windows [or linux]?

Thanks.
-r

···

--
Posted via http://www.ruby-forum.com/\.