wow-pravin wrote:
1) why can't the programs compiled in one os work under some other os - i know their executable format is different - but can't we convert it to other os's format. for e.g. from .out to .exe for windows.
the system calls would be also same - the 'c' functions like printf() work by calling the assemly instrn. right - then atleast the programs can work on same architecture.
if we have unix and win on Intel archi. then we just have to change the executable file format - is this correct - please tell me ???
Excellent question, but unfortunately it really does not work that way. This is for a number of reasons, mostly to do with the system calls and the system's assumptions. I'm going to start by assuming myself that you are developing for the x86 platform (i.e., the PC, which Intel now calls the IA-32), but the issues would come up any time you have two OSes on the same system.
First, you have to realize that the standard library functions are not themselves system calls; they act as a wrapper around the system calls, so that they can be used the same way in different environments. The lowest-level functions, like fputc(), use the actual system calls to perform their operations. The system calls themselves can be radically different between operating systems. On x86 systems, they are usually implemented as either soft interrupts or as call gates, but even if the same mechanism is used by two OSes, the way each uses it is unique.
One result of this is that, while you can use a compiler like gcc to generate code for any x86 OS, you have to reimplement those parts of the standard library which use system calls, and recompile the whole library and the programs which call it, in order to use them on a new OS.
Given this, it becomes clear why, even an OS can use a given file format such as ELF (which has increasingly become the standard in hobby OSes, as most open-source assemblers and compilers can output ELF object files), it cannot use a program which uses system calls for a different OS.
Even if this were not the case, it is not always possible to either run certain formats on some OSes, or convert between one format and another. This is because not every format supports all of the same data fields and operations; in the case of Windows PE format, there are some system-dependent fields which Windows need to load the files, and which cannot be sensibly defaulted.
Last, and perhaps more crucial of all, is that different OSes have different memory models, register allocation policies, argument passing styles, user interfaces, etc., all of which define a framework which a program has to work within. Programs from different frameworks will make invalid assumptions (or the OS will make such assumptions about the program), with potentially catastrophic results.
This isn't to say it is absolutely impossible, but the effort of trying to do so would be extrordinary, and to very little gain. It is far easier to port a program from source code than from binaries.
Oh, for additional information about executable formats, see the book
Linkers and Loaders by John Levine. HTH. CCW.