Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a program become independent of OS?

Tags:

c

opengl

What exactly do we mean when we say that a program is OS-independent? do we mean that it can run on any OS as long as the processor is same?

For example, OpenGL is a library which is OS independent. Functions it contain must be assuming a specific processor. But ain't codes/programs/applications OS-specific?

What I learned is that:

  1. OS is processor-specific.

  2. Applications (programs/codes/routines/functions/libraries) are OS specific.

  3. Source code is plain text.

  4. Compiler (a program) is OS specific, but it can compile source code for a
    different processor assuming the same OS.

  5. OpenGL is a library.

Therefore, OpenGL has to be OS/processor-specific. How can it be OS-independent?

What can be OS independent is the source code. Is this correct?

How does it help to know if a source code is OS-independent or not?

like image 871
KawaiKx Avatar asked Dec 19 '22 23:12

KawaiKx


1 Answers

What exactly do we mean when we say that a program is OS-independent? do we mean that it can run on any OS as long as the processor is same?

When a program uses only defined behaviour (no undefined, unspecified or implementation defined behaviours), then the program is guarenteed by the lanugage standard (in your case C language standard) to compile (using a standards compliant compiler) and run uniformly on all operating systems.

Basically you've to understand that a language standard like C or a library standard like OpenGL gives a set of minimum assumable guarentees that a programmer can make and build upon. These won't change as long as the compiler is compliant with the standard (in case of a library, the implementation is standards-compilant) and the program is not treading in undefined behaviour land.

openGL has to be OS/processor specific. How can it be OS-independent?

No. OpenGL is platform-independant. An OpenGL implementation (driver which implements the calls) is definitely platform and GPU-specific. Say C standard is implemented by GCC, MSVC++, etc. which are all different compiler implementations which can compile C code.

what can be OS independent is the source code. Is this correct?

Source code (if written for with portability in mind) is just one amongst many such platform-independant entities. Libraries (OpenGL, etc.), frameworks (.NET, etc.), etc. can be platform-independant too. For that matter even hardware can be spec'd by some one and implemented by someone else: ARM processors are standards/specifications charted out by ARM and implemented by OEMs like Qualcomm, TI, etc.

do we mean that it can run on any OS as long as the processor is same?

Both processor and the platform (OS) doesn't matter as long as you use only cross-platform components for building your program. Say you use C, a portable language; SDL, a cross-platform library for creating windows, handling events, framebuffers, etc.; OpenGL, a cross-platform graphics library. Now your program will run on multiple platforms, even then it depends on the weakest link. If SDL doesn't run on some J2ME-only phone then it'll not have a library distribution for that platform and thus you application won't run on that platform; so in a sense nothing is all independant. So it's wise to play around with the various libraries available for different architectures, platforms, compilers, etc. and then pick the required ones based on the platforms you're targetting.

like image 163
legends2k Avatar answered Dec 30 '22 04:12

legends2k