kernal coding

i have to make a project on operating system so i decided to design a small kernal with few commands like help,cut,list etc.i know c & c++ basics very well.
so plzz tell me from where i will start....
thanks in advance
You have to make an operating system? Or a shell?
http://en.wikipedia.org/wiki/Operating_system
http://en.wikipedia.org/wiki/Shell_(computing)

If it is just a shell, here is a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

void show_help()
  {
  cout << "My Shell -- by Someone\n\n"

          "Available commands are:\n"
          "  help    This help\n"
          "  run     Run a system command\n" 
          "  quit    Terminate the shell\n";
  }

void show_unrecognized_command()
  {
  cout << "Unrecognized command.\n";
  }

int main()
  {
  show_help();

  while (true)
    {
    cout << "\nshell> " << flush;
    string users_input;
    getline( cin, users_input );

    string cmd, args;
    istringstream iss( users_input );
    iss >> cmd;
    iss >> ws;
    getline( iss, args );

    if (cmd == "quit") break;

    else if (cmd == "help") show_help();

    else if (cmd == "run") system( args.c_str() );

    else show_unrecognized_command();
    }

  cout << "Good bye!\n";
  return 0;
  }

Hope this helps.
That shell looks excellent, Duoas.

I'd make a non-system-using cmd, but I wouldn't be able to code my own shutdown, movefile or similar functions yet.

Making a textual bootloader is surprisingly easy, although writing 'the' repuires about 10 lines of code, and there's no stdin for input. In fact I have no idea how to get input like that...
hey duoas, before you gave me a link to this on a different one. but for a cross-platform one, with all commands supported you could use :
system(cmd.c_str()) instead of the if statements. on that other topic firedraco (i think that was his name) mentioned this. would probably be good for this guy too.
so the code would look something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

void show_help()
  {
  cout << "My Shell -- by Someone\n\n"

          "Available commands are:\n"
          "  help    This help\n"
          "  run     Run a system command\n" 
          "  quit    Terminate the shell\n";
  }

void show_unrecognized_command()
  {
  cout << "Unrecognized command.\n";
  }

int main()
  {
  show_help();

  while (true)
    {
    cout << "\n> " << flush;
    string users_input;
    getline( cin, users_input );

    string cmd, args;
    istringstream iss( users_input );
    iss >> cmd;
    iss >> ws;
    getline( iss, args );
    
    if (cmd == "end") break;
    else system(cmd.c_str());

    }

  cout << "Goodbye!\n";
  return 0;
  }


correct me if im wrong, but that should add every command and work cross platform, unless some of your stuff was OS-specific, dont believe it was though. now all we need is someone to entirely recode bash or MS-DOS in C++ with a gui using different style console commands.
i.e.
exe(file.exe)
to execute file.exe.
:P lol that'd be pretty pointless though. i'd probably use it though. just because it seems cool.


anyway, ive got a question about your run command. so basically it's taking that argument from after the space using get line, then its using system(filename.fileextension); right? pretty cool though. never woulda thought of it.
@elvenspike
using system is not recomended, there is an article here about this, an example on system("PAUSE"). another thing you wont be able to work around is parameter passing, eg system("file.exe /h") (as far as I know anyway)

@avebeginz
if you want to code an os, plan c/c++ wont do, since you'll have to implement some of stuff (input) in asm, but from what I hear there are tutorials and you could just copy that code from them.
if you want to program a kernel for an os then visit this website it will tell you what you need to know and how to get started! there are a couple of tutorials on the basics but its all programmed in either c or c++ language. i've maid a couple basic os and it's really interesting! the GUI or OSI (Graphical User Interface and Operating System Interface)

URL: http://osdev.org
Gregor,
to work around system() could you not do this:
1
2
const char* cmdToSend = ("myProgram.exe %c " myArgs.c_str());
system(cmdToSend)
chris, I have no idea what you're doing in the snippet you posted :) However, I've tried it again and it works fine. I'm rly a windows guy atm so I just use CreateProcess or whatever I need from win32 api.

EDIT: I'm also looking for programmers about my knowledge level, to write some code with. I think we can help each other if you're interested in writing something togather. Should be an interesting expirience.
Last edited on
You're probably above my knowledge level :P

I tend to be overly ambitious though, and think "Yeah, I can write a keyboard driver"...
... "Lol, what does kernel panic mean?"

On that example, though, I was demonstrating a possible way of sending an argument called myArgs of type std::string. Because the only argument of the system() function is a const char*, I was thinking maybe that would work...

The .c_str() member function converts an std::string into a const char*, and as system() accepts only C strings, I thought it might work...

I was just playing around earlier, I wrote a little shell. It doesn't really do anything though:

One day I hope to make a bare-bones operating system for gaming. It'd be very basic, 8-bit (256) colours, but able to run games by switching to a 32 or 24-bit mode. Then when the game returns it'd switch back to 256 colours. Sort of like games console OSs, but I'd write everything to do with memory and the video card in ASM (which I'm learning at the moment), so it'd have really fast execution time for memory accesses and such... Of course, I'd have to run it on x86 or the ASM wouldn't work...
Last edited on
There is no such thing as overly ambitious. I'd rly like you to add me on an IM, gregorcasar@msn.com|@google.com

For the game operating system, let me suggest sumething: Read linux kernel source, try to compile, try to install your version. I know it's nice to reinvent and you do learn alot, but it rarely has any polished results (I'm dying inside cos' I cant express myself well enough in englsh:S). However, if you try to build on top of an existing linux distro (or something similar), tweak it a bit, and optimise it for gaming - I bet i'd take you the same amount of knowledge and energy, just the result would be better:) ...I've been thinking about this some time ago, and am planing to read the kernel code when I'll have the time


As for the example - what I tought at first was that system doesnt do spaces. I was wrong. Because it does, you can actually just parse the string (sprintf or something) and pass it to the function, much like you wrote. And yes, you'd have to cast the string to cstring for that to work :P


Oh, and
I'd have to run it on x86 or the ASM wouldn't work...
x86 is here to stay :]

cheers
Last edited on
Lol, I might download the kernel source actually.

As for the x86, I believe x64 will eventually replace it. But what I meant was that games consoles such as the xbox 360 and PS3 use the IBM PowerPC and Cell architectures, respectively.

If we did make a custom Linux distribution, how would we run the games? We'd need to have some kind of converter to convert the .exe files (most games are for Windows) so that they could run on Windows. Failing that we'd have to only run games that were created for Linux (which are very few) or use WINE, which doesn't work very often.

The only other option is to fly to Redmond and steal the Windows source. It'd be in a big glass box with all those weird motion-sensing lasers that you can only see if you spray them with some chemical (which doesn't make a lick of sense, I guess the spray would be to refract the light through the liquid droplets; so that you could see them. But that doesn't solve the problem of infrared (or UV) not being visible to human beings anyway.)

I was just reading the GRUB bootloader source code actually, it's pretty interesting. The only problem is I'm unfamiliar with the AT&T syntax (until yesterday I didn't know it existed); so I'd have to transfer it into Intel so I could read it and then back to AT&T so that I could compile it with GAS :S I can't run any of my Intel-syntax-created files (which are .COM files) on Windows because I have Vista x64. So I have to switch to Ubuntu to compile, link and run them. >:(

Anyway; I seriously doubt I have the skill to do anything much with the kernel source that they haven't done already. I'll download and read some of it (maybe print it for a bedtime story. I could read the comments like "and then the programmer said "// This is a subroutine to...; we added this to fix the bug of... when... -- 01/01/09")

Oh and Gregor; what nationality are you?
Last edited on
@Chrisname: wow... You are so interested in kernel programming! But how much experience do you have? I think all OS programmers are starting in the wrog way. I'd better start by doing simpler things like:
a) writing a very simple FAT fs editing program or any other low level tool
b) get a 16bit compiler and start writing programs for DOS. DOS will give you more freedom. Do something with video memory, write your own 'clear screen' function and then a simple shell. And slowly take pc control from DOS (windows 3.1?)
c) and then... OMG! The only thing i'm missing is a bootloader!
Oh and a good link:
www.osdever.net
I'm interested in it, yes. In fact, operating systems have become something of an obsession of mine. Virtually every day I read something about them... there's just so/too much to learn :(

I was looking at the Linux kernel map earlier: http://www.makelinux.net/kernel_map
There's no way I could implement that. But then, Linus Torvalds was 21 and I was -3.5 when he started that. Maybe in 6 years I'll have... err... 6 years of programming experience, so maybe then.

And I have literally no experience with operating systems, except using them.

Null; making a bootloader isn't that hard. I could (probably) make a basic one that prints "Hello World" to the screen. It takes a lot of code to do though, and I couldn't actually write it to a bootsector.

But once I steal copy read the GRUB source; I'll know how to do it!!!

I think the bootloader should be the first thing you write... followed by a shell. But you also need to write a self-hosting C compiler so you can compile the GNU binutils for your kernel -- they wouldn't work. Unless you use the Linux kernel, but then it isn't your own OS as such.

By the way, is DOS free yet? Or should I use FreeDOS?
I'm interested in this too :-) I read a lot of tutorials about os development, i know know a little about this and that... but i'm not writing an os now - maybe later. I just try to do something around that... (simple FAT fs driver now)
I can't implement linux kernel too... But there's a lot of perfect code and you can learn from it!
By the way, is DOS free yet? Or should I use FreeDOS?

i don't know, Ms-dos and freeDOS are almost the same except the word 'free'
I'm slovenian (yeah, google that, you will see that ve'we been bitches for a long time...untill we asked croatians if they'd like to go independant from serbs along with us, they declined tought we're mental - serbs declared war on us, occupied us and THAN croatians decide to go indep, drawing the full military force of serbs to them. Thanks, Croatia! ... Also, YOU gave OUR land away to end the WW :: now you know more about history, brag about it to your professors, thank me later), why is that important?


About the games: I wrote a whole paragraph than deleted it; fact beeing: games would have to be rewriten (if they were to use the optimised OS fully for example)

@Null:
I concur. DOS is OP for learning asm :]
Topic archived. No new replies allowed.