0

I was making a Linux OS from scratch. I came accross this article, which creates a bare minimum Linux OS which just prints hello world once linux kernel boot.

So my question is , in which space [kernel or user space] such a helloworld program is running ? My conscience says its kernel space, then how do I make it run in user space ?

For people who do not want to read that lengthy article here is what it does in short.

  • I formatted my USB flash drive [/dev/sdb on my linux system] with ext4 file format.
  • Mounted it to /mnt/mylinux folder.
  • Created folders /boot and /bin in it.
  • Placed a copy of Linux Kernel [I compiled from github source code.] bzImage in /boot folder
  • Installed grub2 in /boot folder of /dev/sdb, and created grub.cfg file to specify my init program.

My init program.

#include <stdio.h>
#include <unistd.h>

int main(void){
    while(1){
        printf("Hello World\n");
        sleep(1);
    }
}

My init program is statically compiled and placed in /bin folder.

So when I boot this drive , it starts printing : Hello World

enter image description here

  • 5
    It's running in user space. Your "Hello World" program is the init process (PID 1) for your system. If you really wanted to run it in kernel space, you would have to write your program as kernel module (or modify kernel source). – sebasth Dec 1 '17 at 7:38
  • related unix.stackexchange.com/questions/79659/… -- btw printf will resort to write at some point, which is a system call – n.caillou Dec 1 '17 at 7:41
  • 2
    "I was making a Linux OS from scratch" and "I am complete noob to Linux system" seem a bit contradictory to me. – Michael Homer Dec 1 '17 at 7:42
  • @MichaelHomer I cannot agree more, but it is as it is :) – user3769778 Dec 1 '17 at 9:24
  • @n.caillou thats what I am looking for. – user3769778 Dec 1 '17 at 9:26
0

In your case, it is a user space: you use standard libraries, so you use user space API to get things done.

If your program were executed by kernel, you have just internal kernel API (so kprintf), and a lot of restriction on what you can do. Additionally, you need to put into kernel or as kernel module. Kernel doesn't execute external code as kernel space.

Note: there are hybrid cases, but always initiated by kernel, for long lasting process, not to block kernel. In that case the code is handled by scheduler, and visible in the list of processes (usually low PID numbers, but not 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.