Chapter 2: Process Management and Scheduling
- daemonizeblocks the receipt of signals.
- initis used as the parent process of the daemon.
The more modern possibility to create a kernel thread is the auxiliary functionkthread_create.
kernel/kthread.c
struct task_struct *kthread_create(int (*threadfn)(void *data),
void *data,
const char namefmt[],
...)
The function creates a new kernel thread with its name given bynamefmt. Initially, the thread will be
stopped. To start it,wake_up_processneeds to be used. After this, the thread function given inthreadfn
will be called withdataas argument.
As an alternative, the macrokthread_run(which uses the same arguments askthread_create) will call
kthread_createto create the new thread, but will wake it up immediately. A kernel thread can also be
bound to a particular CPU by usingkthread_create_cpuinstead ofkthread_create.
Kernel threads appear in the system process list but are enclosed in square brackets in the output ofps
to differentiate them from normal processes.
wolfgang@meitner>ps fax
PID TTY STAT TIME COMMAND
2? S< 0:00 [kthreadd]
3? S< 0:00 _ [migration/0]
4? S< 0:00 _ [ksoftirqd/0]
5? S< 0:00 _ [migration/1]
6? S< 0:00 _ [ksoftirqd/1]
...
52? S< 0:00 _ [kblockd/3]
55? S< 0:00 _ [kacpid]
56? S< 0:00 _ [kacpi_notify]
...
If a kernel thread is bound to a particular CPU, the CPU’s number is noted after the slash.
2.4.3 Starting New Programs
New programs are started by replacing an existing program with new code. Linux provides theexecve
system call for this purpose.^17
Implementationofexecve
The entry point of the system call is the architecture-dependentsys_execvefunction. This function
quickly delegates its work to the system-independentdo_execveroutine.
kernel/exec.c
int do_execve(char * filename,
char __user *__user *argv,
(^17) There are otherexecvariants with different names in the C standard library, but ultimately all are based onexecve.Asinthe
above sections,execis often used to refer to any of these variants.