LINUX TIPS AND TRICKS --- September 22, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Notes on Portability
by Danny Kalev

Unlike many other operating systems (e.g., VMS and S/360) that run on 
proprietary hardware, Linux is available on a variety of hardware 
architectures, including Intel x86, SPARC, Alpha and PowerPC. This 
variety presents huge challenges to Linux software developers. This 
week, I will touch on two portability issues and give some guidelines 
for writing portable code.

Byte ordering, or endian-ness, is a well-known portability concern. For 
datatypes larger than a byte, each hardware architecture has a different 
memory layout. For example, little-endian ordering demands int be 
represented as a sequence of 4 bytes under modern Intel x86 hardware, 
where the least significant byte is the leftmost one. Processors that 
have big-endian ordering use the reverse order. These differences make 
binary data nonportable across incompatible Linux systems. To overcome 
this problem, a common technique detects the endian-ness of a given 
machine in advance and manipulates the data, before or after 
transmitting it, accordingly. However, sometimes this is impossible. The 
simplest solution, in this case, is to pass numeric data as text 
strings. 

32-bit vs. 64-bit architectures presents another portability issue. The 
size of fundamental data types, such as int and long, may vary between 
these hardware architectures. Long occupies 4 bytes on 32-bit systems 
whereas it usually occupies only 8 bytes on 64-bit systems. This 
difference also applies to the alignment of data aggregates and pointer 
sizes. For most purposes, these differences are transparent; however, 
these differences can be significant in low level programming.

I recommend performing a systematic code review and deciding which 
variables and data structures need to be changed before porting. For 
instance, if you write a time_t variable to a file and then read it from 
another machine, you might encounter undefined behavior if the 
underlying representation of time_t is incompatible. Storing the 
variable in its textual representation solves this problem. By contrast, 
it doesn't matter whether a loop that has 1000 iterations uses int as 
its counter. In fact, it's best to use plain int rather than a 
fixed-sized int32_t in this case. This way, the compiler can choose the 
most efficient underlying datatype, regardless of the actual platform.

Resources

The story of the Linux kernel 
Linus Torvalds explains what makes the Linux kernel great
http://www.itworld.com/jlw/lintpsnl/lw-1999-03/lw-03-opensources_p.html

Porting from Unix or Windows to Linux 
Geoffrey Noer, Cygnus Solutions; Gavriel State, Corel
http://www.itworld.com/jlw/lintpsnl/linuxworldtoday/34.html

Thinking ahead 
The promise of an Internet-based office suite
http://www.itworld.com/jlw/lintpsnl/lw-2000-03/lw-03-penguin_2_p.html

System interface design -- where do you start? 
Steps and considerations for building properly integrated system 
interfaces
http://www.itworld.com/jsw/lintpsnl/swol-12-1998/swol-12-itarchitect_p.html

************************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.

*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
