Can I execute any c made prog without any os platform?

后端 未结 9 652
失恋的感觉
失恋的感觉 2021-01-30 23:46

I googled about it and somewhere I read ....

Yes, you can. That is happening in the case of embedded systems

I think NO, it\'s not poss

相关标签:
9条回答
  • 2021-01-31 00:23

    every piece of hardware has to have a piece of software that operates it, be it embedded firmware (smaller and relatively fixed, like vxworks) or an operating system software that can run complex arbitrary code on top of it (like windows, linux, or mac).

    think of it as a stack. at the bottom, you have the hardware. on top of that, a piece of software that can control that hardware. on top of that, you can have all sorts of stuff. in the case of a voip phone, you'll have vxworks controlling the hardware, and a layer on top of that that handles all the phone applications.

    so going back to your question, yes, you CAN run any c program on anything, BUT it depends what kind of c program it is. if it's a low level c program that can talk to hardware, then you dont need anything other than your program and the hardware. if it's a higher level c program (like a chat program), then you need a whole bunch of stuff between your program and the hardware.

    make sense?

    0 讨论(0)
  • 2021-01-31 00:26

    Embedded systems are legitimate OS's in their own right, they're just not general purpose OS's. Any userland program (i.e. a program that is not itself an operating system) needs an operating system to run on top of.

    0 讨论(0)
  • 2021-01-31 00:30

    You definitely don't need an OS to run your C code on any system. What you will need is two pieces of initialization code - one to initialize the hardware needed (processor, clock, memory) and another to set up your stack and C runtime (i.e. intialization of data and BSS sections). This, of course, means that you cannot take advantage of the multithreading, messaging and synchronization services that an OS would provide. I'll try and break it down into some steps to give you an idea:

    1. Write a "reset_routine" that run when the board starts. This will initialize the clock and any external memory needed. (This routine will have to execute from a memory that is either internal or one that can be initialized and programmed externally).
    2. The reset_routine, after the hardware initializations, transfers control to a "sw_runtime_init" routine that will set up the stack and the globals definied by you application. (Do a jump from reset_routine to sw_runtime_init instead of a call to avoid stack usage).
    3. Compile and link this to you application, whilst ensuring that the "reset_routine" is linked to the location where the reset vector points to.
    4. Load this onto your target and pray.
    0 讨论(0)
  • 2021-01-31 00:31

    You can run a program in a system without an Operating System ... and that program need not be an Operating System itself.

    Think about all the computers (or processors if you prefer) inside a car: engine management, air conditioning, ABS, ..., ...
    All of those system have a program (possibly written in C) running. None of the processors have an OS.

    The Standard specifically differentiates between hosted implementations and freestanding implementations:

        5.1.2.1 Freestanding environment
    1   In a freestanding environment (in which C program execution may take place
        without any benefit of an operating system), the name and type of the
        function called at program startup are implementation-defined. Any library
        facilities available to a freestanding program, other than the minimal set
        required by clause 4, are implementation-defined.
    2   The effect of program termination in a freestanding environment is
        implementation-defined.
    
        5.1.2.2 Hosted environment
    1   A hosted environment need not be provided, but shall conform to the
        following specifications if present.
        ...
    
    
    0 讨论(0)
  • 2021-01-31 00:35

    Of course you can. All a (typical) CPU needs is power and access to a memory, then it will execute its hard-coded boot sequence.

    Typically this will involve reading some pre-defined address, interpreting the contents there as instructions, and starting to run them.

    These instructions could of course come from a C program, although at this level it's more common to write the very early stages (called bootstrapping) in assembly.

    This of course doesn't mean, if I were to read your question title literally, that any C program be run this way. If the program assumes there is an OS, but there isn't, it won't work. This should be pretty obvious.

    0 讨论(0)
  • 2021-01-31 00:38

    Usually, any C program will have a variety of system calls which depend on the operating system. For example, printf makes a system call to write to the screen buffer. Opening files and things like that are also system calls.

    So basically, you can run the C code which just gets compiled and assembled in to machine code on a processor, but if the code makes any system calls, it would just freeze up the processor when it tries to jump to a memory location that it thinks is the operating system. This of course would depend on your being able to get the program running in the first place, which is not easy without the operating system as well.

    0 讨论(0)
提交回复
热议问题