Kernel module
- C course
- Expert
- Lesson 44/47
- 6 min
Write a kernel module and syscall layer.
Kernel module
In the second lesson you add the kernel module. The kernel_init function initializes the kernel and prints a message. The syscall function doubles a system call number — a simple model of a syscall dispatcher.
In real kernels, syscall transitions from user space to kernel space; here a simplified version suffices. The kernel_init call is done at the start of main; this way the kernel is ready. The skeleton from the previous lesson is in the starting code; you add the new functions.
The syscall dispatcher pattern
A syscall dispatcher routes to different functions based on a number. In this project we use a simplified version: int syscall(int num) { return num * 2; }. A real dispatcher looks up a table and calls the relevant function.
The dispatcher pattern is implemented with a switch or a function pointer table. Here num * 2 simulates the dispatcher processing a call. kernel_init is called from main and signals the kernel is ready. These two functions form the kernel layer of the mini system.
Practice Add the kernel module
Below the existing declarations, write void kernel_init(void) that prints printf("Çekirdek başladı\n"). Write int syscall(int num) that returns num * 2;. In main call kernel_init().
Starter code
#include <stdio.h>
#include <string.h>
void kernel_init(void);
int parse_command(const char *cmd);
int security_check(const char *buf, int len);
int main(void) {
printf("Proje başladı\n");
return 0;
}
// kernel_init fonksiyonunu yaz: "Çekirdek başladı" yazdır
// syscall fonksiyonunu yaz: num * 2 döndür
// main'de kernel_init'i çağırSample solution
#include <stdio.h>
#include <string.h>
void kernel_init(void);
int parse_command(const char *cmd);
int security_check(const char *buf, int len);
void kernel_init(void) {
printf("Çekirdek başladı\n");
}
int syscall(int num) {
return num * 2;
}
int main(void) {
printf("Proje başladı\n");
kernel_init();
return 0;
}Expected output
Proje başladı Çekirdek başladı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the syscall return value
int syscall(int num) {
return num * ____;
}
int syscall(int num) {
return num * 2;
}
Explanation: return num * 2; doubles the syscall number.
2 What does kernel_init do?
- Reads a file
- Sets up a network connection
- Frees memory
- Initializes the kernel and prints a message Correct answer
Explanation: kernel_init initializes the kernel and prints 'Çekirdek başladı'.
3 Match kernel function
| Term | Meaning |
|---|---|
kernel_init |
Print kernel |
syscall |
Return num * 2 |
main |
Control flow |
printf |
Print message |