Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pchipchip

macrumors regular
Original poster
Sep 7, 2011
131
0
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?
 

throAU

macrumors G3
Feb 13, 2012
8,817
6,985
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.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
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.
 

Comeagain?

macrumors 68020
Feb 17, 2011
2,190
46
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?
 

throAU

macrumors G3
Feb 13, 2012
8,817
6,985
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.
 
Last edited:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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?

Linus Torvalds did. But then he didn't ask anyone where to start.
 

adrian.oconnor

macrumors 6502
Jan 16, 2008
326
3
Nottingham, England
Linus Torvalds did. But then he didn't ask anyone where to start.

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.
 

mrbash

macrumors 6502
Aug 10, 2008
251
1
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.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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.


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?
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
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?

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.

http://en.wikipedia.org/wiki/Darwin_(operating_system)

Here is an Apple support document describing how to obtain and build Darwin.

https://developer.apple.com/library/mac/#documentation/Darwin/Conceptual/KernelProgramming/build/build.html
 

dmz

macrumors regular
Jan 29, 2007
139
0
Canada
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!

:apple:dmz
 

Anonymous Freak

macrumors 603
Dec 12, 2002
5,561
1,252
Cascadia
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!

:apple:dmz

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.
 

truehybridx

macrumors member
Dec 6, 2010
86
0
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?

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
 

adildacoolset

macrumors 65816
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.
 

mrbash

macrumors 6502
Aug 10, 2008
251
1
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.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.

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.

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.

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.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
What's your point?

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:
http://en.wikipedia.org/wiki/Jazelle

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

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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?

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.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
The kernel is written in assembly though as well as part of the vm, and it uses GRUB as a boot loader.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.