Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


sys_futex(tidptr, FUTEX_WAKE, 1, NULL, NULL, 0);
}
...
}
In addition,sys_futex, a fast userspace mutex, is used to wake processes waiting for this event,
namely, the end of the thread.

The above flags can be used from within userspace to check when threads are generated and destroyed
in the kernel.CLONE_CHILD_SETTIDandCLONE_PARENT_SETTIDare used to check when a thread is gen-
erated;CLONE_CHILD_CLEARTIDis used to pass information on the death of a thread from the kernel to
userspace. These checks can genuinely be performed in parallel on multiprocessor systems.

2.4.2 Kernel Threads


Kernel threadsare processes started directly by the kernel itself. They delegate a kernel function to a
separate process and execute it there in ‘‘parallel‘‘ to the other processes in the system (and, in fact, in
parallel to execution of the kernel itself).^15 Kernel threads are often referred to as(kernel) daemons.They
are used to perform, for example, the following tasks:

❑ To periodically synchronize modified memory pages with the block device from which the pages
originate (e.g., files mapped usingmmap).
❑ To write memory pages into the swap area if they are seldom used.
❑ To manage deferred actions.
❑ To implement transaction journals for filesystems.

Basically, there are two types of kernel thread:

❑ Type 1— The thread is started and waits until requested by the kernel to perform a specific
action.
❑ Type 2— Once started, the thread runs at periodic intervals, checks the utilization of a specific
resource, and takes action when utilization exceeds or falls below a set limit value. The kernel
uses this type of thread for continuous monitoring tasks.

Thekernel_threadfunction is invoked to start a kernel thread. Its definition is architecture-specific, but
it always uses the same prototype.

<asm-arch/processor.h>
int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)

The function passed with thefnpointer is executed in the generated thread, and the argument specified
inargis automatically passed to the function.^16 CLONEflags can be specified inflags.

The first task ofkernel_threadis to construct apt_regsinstance in which the registers are supplied
with suitable values, as would be the case with a regularforksystem call. Then the familiardo_fork
function is invoked.

p = do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL);

(^15) On multiprocessor systems, the processes genuinely execute in parallel; on single-processor systems, the scheduler simulates par-
allel execution.
(^16) Arguments allow the function to be used for different purposes by indicating what needs to be done.

Free download pdf