Program a custom OS?

Discussion in 'Mac Programming' started by pchipchip, Apr 26, 2012.

  1. macrumors regular

    Sep 7, 2011
    Is it possible to program my own OS from scratch? Don't tell me that it is a stupid idea or anything. I am just wondering if it is possible and if so, where should I start?
  2. macrumors 68040

    Feb 13, 2012
    Perth, Western Australia
    Yes you can.

    You'll need to start off by learning assembly language for your chosen CPU architecture, so that you are able to program low level device drivers to get something to boot.

    You'll also need to learn the low level details of PCI, SATA, etc.

    It's not a small task - learning assembly language alone will take you quite some time.

    I'd certainly recommend doing this using something like VMware (until you know what you're doing) to contain your efforts to a protected, separate environment from your day to day computer.

    Low level device programming gives you the power to cause all sorts of data corruption and even hardware problems, if run on physical hardware and you don't know what you're doing.
  3. macrumors Core


    Jul 24, 2006
    The Ivory Tower (I'm not coming down)
    Lots of things are "possible." Whether they're likely is competely different.
  4. macrumors 68030


    Jan 19, 2006
    Depending on why you want to build your own, you might be better to start like NeXT did by farming Open Source and University Research Projects by build out the bones of the OS.

    Apple still does this in order to get Low level parts of the OS, generally employing key project staff to ensure they keep moving forward.
  5. macrumors 68030

    Oct 19, 2011
    The answer is obviously yes. Someone had to do it in the past, otherwise there would be no operating systems. So it can be done again.
  6. macrumors 68020


    Feb 17, 2011
    Spokane, WA
    I wonder if you could use what apple already has for boot code and drivers, and then build from that? I know you couldn't legally, but is it possible?
  7. throAU, Apr 27, 2012
    Last edited: Apr 27, 2012

    macrumors 68040

    Feb 13, 2012
    Perth, Western Australia
    Well, given that the core of OS X is darwin, and Darwin is open source, well yes you could, it is even legal. A few people did just that with the PureDarwin and OpenDarwin projects.

    I agree it would certainly be a lot easier to start off with something like Darwin or FreeBSD in a virtual machine to play with and modify/break, than trying to start out from scratch without any experience in operating system design.

    Back in the 80s when an OS wasn't expected to do much (like DOS), writing one from scratch would be a lot easier.

    I think its an example of just how much work and complexity is involved that it took apple several failed attempts (and they ended up buying NEXT back off Jobs to turn it into OS X) to replace the original Mac OS.

    IMHO the only reason to write your own quick and dirty "OS" these days is if you want to program the machine to do a single task as quickly as possible. Essentially you're BYPASSING any operating system and doing everything yourself. E.g., many games in the 80s and 90s for stuff like the Amiga, Atari ST, consoles, etc used to do this. Positive: You have no operating system sucking CPU power you could use for your app. Negative: YOU are responsible for all low level device access - no drivers, no graphics libraries, no UI, etc are available. You're on your own.

    Otherwise (if you want to run other people's software in a multi-tasking environment) writing an efficient scheduler and memory manager is not a trivial task to undertake. Even Windows, Linux, OS X and FreeBSD are having regular changes and tweaks made to improve their schedulers and memory managers, after several decades of real world use and refinement. And that's before you even get to the user interface stuff.

    Not to poo-poo the idea, it's how Linus started out with Linux, and many others created operating systems before him - but to make anything even remotely comparable to OS X (or even Linux) will take many people a long time, even if they're good, experienced programmers.
  8. macrumors G5


    Nov 25, 2005
    Linus Torvalds did. But then he didn't ask anyone where to start.
  9. macrumors 6502

    Jan 16, 2008
    Nottingham, England
    I think you're wrong about that. Linus was a CS student when he started Linux, and he asked plenty of advice (as any smart person would).

    As it happens, you can still read some of his conversations with Andrew Tanenbaum on usenet. Tanenbaum is a professor who specialises in teaching OS design. He even built a teaching OS called Minix. The name Linux is a bastardisation of the Minix name, I guess because Linus was studying Minix at the time.

    So here's my advice to OP: If you want to write an operating system, get Andrew Tanenbaum's books on the subject. They're not easy to read, but if you want to write an OS they're going to be a serious help. If you can't make it to the end of those books, you're probably biting off more than you can chew.
  10. macrumors 6502

    Aug 10, 2008
    Yes, it's easier than you think

    Yes! You don't have to know Assembly either. You can write an OS in any high level language including Java.

    Probably the best place to start would be to take a course on Operating Systems, and if that's not possible, reading a book on Operating Systems. That will give you an idea of what an OS is, and which parts of an OS you want to write.

    Then you simply write the different parts of the OS. Compile it for your architecture, and away you go.

    It is easier than you think, and it is a fantastic idea. It gives you a great understanding of computers.
  11. macrumors 68030


    Sep 13, 2001
    Portland, OR

    I'm curious, how does one write an interrupt handler in Java? Or, for that matter, bootstrap the JVM on their OS if their OS is written in Java?
  12. macrumors 6502

    Feb 7, 2008
    The OS/X kernel (Darwin) is open source and the source code is available from Apple. It would be a good exercise to build it and get it running.

    Here is an Apple support document describing how to obtain and build Darwin.
  13. dmz
    macrumors regular


    Jan 29, 2007
    OS in Java? Ha-ha-ha-ha-ha - that's rich!

    You cannot write an OS in a high-level language, sorry mrbash, no-can-do.

    You can write a GUI that way, on top of Darwin or any of the free UNIX-like kernels. Is that perhaps what you meant?

    Writing an OS from scratch is a gargantuan task - try picking a small target if you wish to pursue this idea seriously - an 8-bit processor with a small assembler and few device drivers - VDT, keyboard, floppy disk and serial port are a good start. I wrote a DOS for the ZX80 way back in the early 80's, and for one person, it was still a huge task, but very, very educational.

    Good luck, and keep learning!

  14. macrumors 601

    Anonymous Freak

    Dec 12, 2002
    Um, Darwin is written in Objective-C. That is a high-level language. Very little of modern OSes are written in assembly. NONE of the Linux kernel is written in assembly. As long as it is 32-bit, and there is a GNU C compiler for it, Linux will run on it.
  15. macrumors member

    Dec 6, 2010
    try taking a look at BareMetalOS, or a small, very small distro of unix... it will require some good knowledge of C, assembly helps too
  16. macrumors 65816

    Sep 5, 2011
    Lusaka, Zambia(If you know where it is)
    okay, you can do it after learning assembly language.A good book is called "The art of assembly language programming" by Randall Hyde. I wouldn't recommend a high-level language as the abstractions would just cause more occupation of size and memory, and less performance.
  17. macrumors 6502

    Aug 10, 2008
    I suspect most of the people claiming that you cannot write an OS in a high level language do not have a formal education in computer Science. And for those who do, shame on you, you should know better.

    A first course in OS will shown that the level of the language does not matter, it is only the logic that you implement that matters.

    I should also add, that assembly is considered a symbolic language, like object code. The operating system converts Assembly code into machine code, just like the operating system converts object code from a high-level language into machine code. That is to say you can write an OS in assembly, like you can write an OS in object code.

    I doubt the OP intends to write a commercial OS. Most likely they want to learn some of the core OS concepts like Process, Scheduling and Memory management.

    Also to answer some of the critics of my suggestion that it is possible to write an OS in Java: Java handles signals (the general case of interrupts) quite well.
  18. subsonix, Apr 28, 2012
    Last edited: Apr 28, 2012

    macrumors 68040

    Feb 2, 2008
    The OS will need to interface directly with the hardware, it must not require a runtime or interpreter as there are none. Certain parts of the OS as the bootloader requires assembly, but you will find it in other parts as well.

    Wrong. The assembler converts assembly instructions to machine code, the cpu can run the machine code natively. Java byte code requires a Java runtime, which in turn requires an operating system where it can be executed, there is your chicken and egg problem as you are writing the operating system there are no Java runtime.
  19. macrumors 604

    Aug 9, 2009
    All operating system software was written in Lisp. Xerox used InterLisp. Symbolics, LMI and TI used Lisp Machine Lisp (descendant of MacLisp). With the appearance of Common Lisp, Common Lisp was supported on the Lisp Machines and some system software was ported to Common Lisp or later written in Common Lisp. [underline added]
  20. macrumors 68040

    Feb 2, 2008
    What's your point?

  21. macrumors 604

    Aug 9, 2009
    It was underlined: All operating system software was written in Lisp. Even if it's compiled to machine code, it's still written in Lisp. Or are you arguing that compilers are forbidden?

    Did someone at some point have to write assembler? Maybe, maybe not. It's possible there were existing tools that translated Lisp to other forms, such as assembler, and they used that for bootstrapping. So it's quite possible that no person ever wrote any machine code or even assembler.

    Further fanning the flames, see this regarding Java byte code:

    I fully realize this is not a complete JVM in hardware, and still requires software support for some byte-codes.
  22. macrumors 68040

    Feb 2, 2008
    Ok, but so what? C is a high level language explicitly created to write operating systems. I'm not arguing that you have to use assembly to write an operating system, but at least for x86 it's required for the boot loader. But that being said it can often be found in other parts as well, if for no other reason to optimize.

    There's quite a lot of resources available on this if you are interested, I feel this is moving on to the absurd.
  23. macrumors 6502a

    Feb 18, 2008
    Darwin is C & C++. what-languages-are-windows-mac-os-x-and-linux-written-in
  24. macrumors 6502

    Apr 24, 2008
    Re: OS in Java? Ha-ha-ha-ha-ha - that's rich!

  25. macrumors 68040

    Feb 2, 2008
    The kernel is written in assembly though as well as part of the vm, and it uses GRUB as a boot loader.

Share This Page