Hi All,

I am new to kernel Programming when i started browsing the code i saw that there are so many main function.

Please give detailed explanation

Dani AI

Generated

Many occurrences of main in a Linux source checkout are normal and not a sign of a broken design. The repository contains many independent programs, tests and helpers (tools, samples, user‑space utilities, build helpers), and each C program compiled as its own executable defines an int main(...). The kernel proper, however, does not use the standard user‑space main as its single entry point.

Boot and initialization inside the kernel are architecture‑specific: early assembly does CPU/stack setup and then hands control to a C entry (commonly start_kernel in init/main.c). Subsystems and drivers register their own initializers via initcall macros (module_init, subsys_initcall, device initcalls, etc.), and modules supply init/exit routines that run when the module is loaded or unloaded. Those init functions behave like per‑component “mains” but are invoked by the kernel’s init machinery rather than by a single global main.

A minimal example of a module entry/exit looks like:

static int __init mymod_init(void)
{
    pr_info("mymod loaded\n");
    return 0;
}
static void __exit mymod_exit(void)
{
    pr_info("mymod unloaded\n");
}
module_init(mymod_init);
module_exit(mymod_exit);

Searching the tree for int main( will therefore return many hits because of separate build targets. Kernel threads and worker functions are additional “entry points” (created via kthread_create or workqueues) and can look like small per‑thread mains. Duplicate main only matters when multiple definitions are linked into the same binary; in the kernel source they live in separate build targets, so no conflict occurs. As pointed out, the kernel has many functions; as hinted, reading the kernel layout and boot sequence (see init/main.c and the arch entry code) clarifies which functions are the real boot/initialization entry points.

Recommended Answers

All 2 Replies

What are those so may main functions?

Kernel is the heart of the computer that is why it has so many functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.