What is C - is it an operating system?

It makes sense that something like the operating system will be written in C. But how much of this and what is C? I mean, in C, if you need a bunch of memory, you would call malloc. But does the OS even have a bunch? As far as I know, malloc queries the operating system for memory and then adds it to a linked list or binary tree or something like that. How about a call stack? The OS is responsible for setting up all this stuff that is used by other applications, but how to do it? When you want to open or create a file in C, the corresponding functions request the operating system for that file. so ... What is C on the other side of this call? Or at the other end of the memory allocation?

Also, what part of the operating system will actually be written in C? All this? What about architecture dependent code? What about higher levels of abstraction - does it ever write in higher level languages ​​like C ++?

I mean, I'm just asking for this out of sheer curiosity. Now I download the latest Linux kernel, but it runs forever. I'm not sure if I can follow the code, or if I get into the insurmountable complexity of the network that I have never seen before.

+46
c kernel
Jul 08 '09 at 7:53
source share
9 answers

What is C?

Basically ANSI C, with a lot of time looking at the machine code that it generates.

But does the OS even have a bunch?

Malloc queries the operating system for a pointer to some memory that is allowed to be used. If a program running in the OS (user mode) tries to access memory that it does not belong to, it will give a segmentation error. OS is allowed to directly access all physical memory in the system, malloc is not required, no seg-failures at any address that exists.

How about a call stack?

The call stack actually often works at the hardware level with a list of links.

To access OS files, you need access to a disk driver that needs to know how to read the file system on the disk (there are many different types). Sometimes the OS has a built-in firmware, but I think most often the bootloader loads it, and it loads another (larger) one. The disk driver has access to the hardware IO of the physical disk and builds from it.

+33
Jul 08 '09 at 8:05
source share

Great questions, everyone. The answer is: virtually none of the standard C libraries are available in the C dialect used to record the operating system. In the Linux kernel, for example, the standard memory allocation functions malloc, nmalloc, free, etc. Replaced by special kernel memory allocation and interval functions kmalloc and kfree with special restrictions on their use. The operating system must provide its own "heap" - in the Linux kernel, the pages of physical memory that were allocated to use the kernel must be unrecoverable and often physically complex. See this Linux journal article on kmalloc and kfree . Similarly, the kernel of the operating system supports its own special call stack, the use of which requires special support from the memory from the GCC compiler.

Also, what part of the operating system will actually be written in C? All this?

As far as I know, operating systems are mostly written in C. Some architecture-specific functions are encoded in assembler, but usually very little to improve portability and maintainability: the Linux kernel has some assembler, but tries to minimize it as much as possible.

How about code architecture dependency? What about higher levels of abstraction - is it ever written in higher level languages ​​like C ++?

Typically, the kernel will be written in pure C, but sometimes higher-level structures and APIs are written in a higher-level language. For example, the Cocoa / API framework on MacOS is written in Objective-C, and the higher-level BeOS APIs are written in C ++. Most of the Microsoft.NET platform was written in C # with the "Common Language Runtime", written in a combination of C ++ and assembler. The QT widget set most commonly used on Linux is written in C ++. Of course, this introduces philosophical questions about what is considered an "operating system."

The Linux kernel is definitely worth a look at this, although I must say that it is huge and scary for anyone who reads from scratch.

+46
Jul 08 '09 at 8:27
source share

C is a very low level language and you can do many things directly. Any of the C library methods (for example, malloc, printf, crlscr, etc.) must first be implemented to call them from C (see, for example, the concepts of libc). I will give an example below.

Let's see how the C library methods are implemented under the hood. We will look at the clrscr example. When you apply such methods, you will access system devices directly. For example, for clrscr (screen cleaning), we know that video memory is at 0xB8000. Therefore, to write to or clear the screen, we begin by assigning a pointer to this location.

In video.c

void clrscr() { unsigned char *vidmem = (unsigned char *)0xB8000; const long size = 80*25; long loop; for (loop=0; loop<size; loop++) { *vidmem++ = 0; *vidmem++ = 0xF; } } 

Now write our mini-core. This will clear the screen when control is transferred to our "core" from the bootloader. In main.c

 void main() { clrscr(); for(;;); } 

To compile our "core", you can use gcc to compile it into a pure bin format.

 gcc -ffreestanding -c main.c -o main.o gcc -c video.c -o video.o ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin 

If you notice the ld options above, you will see that we specify the default boot location of your kernel as 0x1000. Now you need to create a bootloader. From the loader logic, you can transfer control to your kernel, for example

 jump 08h:01000h 

Usually you write down the bootloader logic in Asm. Even before that, you may need to see how Boot Boots - Click here .

Better to start with a thinner operating system to explore. See this video of your OS tutorial.

http://www.acm.uiuc.edu/sigops/roll_your_own/

+24
Jul 08 '09 at 8:07
source share

But how much of this and which C?

Some parts must be recorded in the assembly.

I mean, in C, if you need a bunch of memory, you would call malloc. But does the OS even have a bunch? As far as I know, malloc queries the operating system for memory, and then adds it to a linked list or binary tree or something like that.

Some OSs have a bunch. At the lowest level, they are slabs of memory that are crossed out by so-called pages. Then your C library is broken in its malloc variable-sized schema. You should learn about virtual memory, which is a common memory scheme in modern operating systems.

If you want to open or create a file in C, the corresponding functions request the operating system for this file. therefore ... What is C on the other side of this call?

You invoke build procedures that request hardware with instructions such as IN and OUT. With raw memory access, sometimes you have areas of memory designed to communicate with and from equipment. This is called DMA.

I'm not sure if I can keep track of the code, or if I fall into the insurmountable complexity of the network that I have never seen before.

Yes. First you should pick up a book about hardware and OS.

+5
Jul 08 '09 at 8:06
source share

I mean, in C, if you need a bunch of memory, you would call malloc. But does the OS even have a bunch? As far as I know, malloc queries the operating system for memory, and then adds it to a linked list or binary tree or something like that. How about a call stack?

Many of what you say in your question is actually executed by a runtime library in user space.

All that the OS requires is to load the program into memory and go to it, more precisely, after it is executed by the user space program. The heap and stack are simply areas of virtual memory in processes. The stack is just a register of pointers in the processor.

Allocating physical memory is what is done at the OS level. OS usually allocates pages of a fixed size, which are then displayed in the user spatial process.

+2
Jul 08 '09 at 8:06
source share

You should read Linux 3 device drivers . This explains the linux kernel internals very well.

+2
Jul 08 '09 at 8:35
source share

I would not read the Linux kernel, it is too difficult for beginners.

Osdev is a great place to start reading. I made a small book with information from Osdev about a school subject. It works on vmware, bochs and qemu, so it is easy to check. Here is the source code .

+1
Jul 08 '09 at 8:05
source share

Traditionally, C is mainly required for kernel and device drivers due to hardware interactions. However, languages ​​such as C ++ and Java can be used for the entire operating system.

For more information, I found that the design and implementation of Andrew Tannenbaum operating systems is especially useful for LOTS code samples.

0
Jul 08 '09 at 8:02
source share

The malloc and memory management functions are not keywords in C. These are functions of the standard OS libraries. I do not know the name of this standard (it is unlikely that it is a POSIX standard - I did not find a mention), but it exists - you use malloc in C applications on most platforms.

If you want to know how the Linux kernel works, I recommend this book http://oreilly.com/catalog/9780596005658/ . I think this is a good explanation with some C code inserted :).

0
Jul 08 '09 at 9:04
source share



All Articles