Skip to content

linxin8/OS

Repository files navigation

OS

一个简单的操作系统内核,实现了简单的功能,如启动,线程/进程管理,内存管理,文件管理,输入输出,用户进程,系统调用等,总代码7k行左右。

Docker 一键运行

sudo docker run --net=host -e DISPLAY=127.0.0.1:10.0 -v $HOME/.Xauthority:/root/.Xauthority:rw  -v /tmp/.X11-unix:/tmp/.X11-unix linxin8/os /run_gui.sh

Demo

#include "disk/file_system.h"
#include "kernel/asm_interface.h"
#include "kernel/init.h"
#include "kernel/interrupt.h"
#include "kernel/keyboard.h"
#include "kernel/memory.h"
#include "kernel/timer.h"
#include "lib/debug.h"
#include "lib/macro.h"
#include "lib/new.h"
#include "lib/stdint.h"
#include "lib/stdio.h"
#include "lib/stdlib.h"
#include "process/process.h"
#include "thread/sync.h"
#include "thread/thread.h"

int main()
{
    init_all();
    printf("main pid %d\n", getpid());
    Process::execute((void*)user_main, "u1");
    Interrupt::enable();
    while (true)
    {
        Thread::yield();
    }
    return 0;
}

void user_main(void* arg)
{
    UNUSED(arg);
    printf("\nuser process pid %d\n\n", getpid());
    int32_t test = 0;
    while (true)
    {
        char    buffer[2];
        int32_t fd[2];
        if (pipe(fd) == -1)
        {
            printf("pip failed");
            while (true) {}
        }
        auto pid = fork();
        if (pid == 0)
        {
            test--;
            printf("i am child %d, test addr: %x, value %d\n\n", getpid(), &test, test);
            write(fd[1], "123456", 6);
            printf("write ok\n");
            while (true) {}
        }
        test++;
        printf("i am parent %d, child %d, test addr: %x, value %d\n\n", getpid(), pid, &test, test);
        while (true)
        {
            read(fd[0], buffer, 1);
            printf("%c", buffer[0]);
        }
        while (true) {}
    }
} 

GitHub

项目环境

bochs
g++-10
nasm
ubuntu

环境搭建

安装nasm

sudo apt install nasm

安装bochs

sudo apt install -y g++ libx11-dev libxrandr-dev  build-essential libgtk2.0-dev libreadline-dev libcanberra-gtk-module
wget https://sourceforge.net/projects/bochs/files/bochs/2.6.9/bochs-2.6.9.tar.gz/download
mv download bochs.tar
tar -xvf bochs.tar bochs-2.6.9/
cd bochs-2.6.9/
./configure --enable-debugger --with-x11 --enable-all-optimizations
make
sudo make install

安装g++-10

sudo apt install g++-10

运行

  1. 下载项目
  2. 进入项目目录
  3. 执行命令 make run

Make 参数

make 编译
make run 编译并运行
make rebuild 清理项目并重新编译

内核开发与启动流程

开发流程

启动流程

关于C++

C++语言特性的使用

关于一些技术细节

一些技术细节

参考资料

操作系统真相还原
bochs参考手册
osdev

About

一个简单的操作系统内核

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published