⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel_threads.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>  <title></title>  <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />  <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />  <link rel="stylesheet" media="print" type="text/css" href="./print.css" />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="clear"><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#kernel_threads" class="toc">Kernel Threads</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#using_kernel_threads" class="toc">Using Kernel Threads</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#starting_a_kernel_thread" class="toc">Starting a Kernel Thread</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="kernel_threads" id="kernel_threads">Kernel Threads</a></h2><div class="level2"><p>The kernel often needs to establish processes that sleep or wait for events to occur. It can use a lightweight process or thread for this task.</p><p>A kernel thread is very similar to a user process or thread in that it is a schedulable entity. This means that it has its own task control block and it can sleep while waiting for system events to be routed to it.</p><p> The only difference between a kernel thread and a user thread is the fact that the kernel thread has no user task memory mapping. Like other user processes it shares the kernel address space but the thread operates only in kernel space with kernel privilages.</p><p>This also means that when activating a kernel thread a lot of the overhead (cache , tlb etc) involved with switching a process&rsquo;s memory map is  avoided.</p><p> Many kernel subsystems are managed by kernel threads. All the tasks in the [ ] braces in the example shown are, in fact, kernel threads.</p><pre class="code c">&nbsp;ps ax  PID TTY      STAT   TIME COMMAND    <span class="nu0">1</span> ?        S      <span class="nu0">0</span>:<span class="nu0">03</span> init <span class="br0">&#91;</span><span class="nu0">5</span><span class="br0">&#93;</span>    <span class="nu0">2</span> ?        SN     <span class="nu0">0</span>:<span class="nu0">00</span> <span class="br0">&#91;</span>ksoftirqd/<span class="nu0">0</span><span class="br0">&#93;</span>    <span class="nu0">3</span> ?        S&lt;     <span class="nu0">0</span>:<span class="nu0">01</span> <span class="br0">&#91;</span>events/<span class="nu0">0</span><span class="br0">&#93;</span>    <span class="nu0">4</span> ?        S&lt;     <span class="nu0">0</span>:<span class="nu0">00</span> <span class="br0">&#91;</span>khelper<span class="br0">&#93;</span>    <span class="nu0">5</span> ?        S&lt;     <span class="nu0">0</span>:<span class="nu0">00</span> <span class="br0">&#91;</span>netlink/<span class="nu0">0</span><span class="br0">&#93;</span>    <span class="nu0">6</span> ?        S&lt;     <span class="nu0">0</span>:<span class="nu0">02</span> <span class="br0">&#91;</span>kblockd/<span class="nu0">0</span><span class="br0">&#93;</span>   <span class="nu0">32</span> ?        S      <span class="nu0">0</span>:<span class="nu0">00</span> <span class="br0">&#91;</span>kapmd<span class="br0">&#93;</span>   <span class="nu0">37</span> ?        S&lt;     <span class="nu0">0</span>:<span class="nu0">00</span> <span class="br0">&#91;</span>aio/<span class="nu0">0</span><span class="br0">&#93;</span>   <span class="nu0">36</span> ?        S      <span class="nu0">0</span>:<span class="nu0">08</span> <span class="br0">&#91;</span>kswapd0<span class="br0">&#93;</span>&nbsp;</pre></div><!-- SECTION [1-1345] --><h3><a name="using_kernel_threads" id="using_kernel_threads">Using Kernel Threads</a></h3><div class="level3"><p> This is quite simple, you specify the function to  be used as a thread an optional data item (used as an argument for the thread), and the thread creation flags. Normally a fixed set of these flags are used for a &ldquo;normal&rdquo; kernel thread.</p><p>A thread is normally designed as a continous loop that calls some wait or scheduling function to stop it consuming 100% of the CPU.</p><p>The example thread also uses a completion mechanism to telling the process that created it that the thread has been established and is ready for business.</p><p>The thread must manage its own signals and also remove any unneeded memory mapping information.</p><p>NOTE If the thread is part of a device driver and also a module it will, by default, inherit the user memory space of the user process that called <strong>insmod</strong>. This user space mapping should be removed.</p><pre class="code c"><span class="co1">// simple thread code</span><span class="co1">// not you need to add the loop task and the yield function</span>&nbsp;<span class="kw4">static</span> <span class="kw4">int</span> mythread<span class="br0">&#40;</span><span class="kw4">void</span> * data<span class="br0">&#41;</span><span class="br0">&#123;</span>&nbsp;       <span class="kw4">struct</span> completion *comp = <span class="br0">&#40;</span><span class="kw4">struct</span> completion *<span class="br0">&#41;</span>data;&nbsp;       <span class="co1">// set up the thread</span>        daemonize<span class="br0">&#40;</span><span class="st0">"mythread"</span><span class="br0">&#41;</span>;  <span class="co1">// remove any user space mem maps and</span>                                <span class="co1">// signal handlers</span>        allow_signal<span class="br0">&#40;</span>SIGKILL<span class="br0">&#41;</span>;  <span class="co1">// allow a kill signal</span>&nbsp;       <span class="co1">// notify ready for action</span>       complete<span class="br0">&#40;</span>comp<span class="br0">&#41;</span>;&nbsp;       <span class="kw1">do</span>  <span class="br0">&#123;</span>             <span class="co1">// loop task to process data</span>             wait_event_interruptible<span class="br0">&#40;</span>...<span class="br0">&#41;</span><span class="co1">// yield process</span>&nbsp;       <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span>!signal_pending<span class="br0">&#40;</span>current<span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp;       <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">&#125;</span>&nbsp;<span class="co1">// simple thread generation code</span>&nbsp;<span class="kw4">static</span> <span class="kw4">int</span> generate_thread <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span><span class="br0">&#123;</span>        pid_t pid;        DECLARE_COMPLETION<span class="br0">&#40;</span>ready<span class="br0">&#41;</span>;        pid = kernel_thread<span class="br0">&#40;</span>mythread,  <span class="co1">// thread function</span>                      &amp;ready,     <span class="co1">// data argument</span>                      CLONE_FS | CLONE_SIGHAND <span class="co1">// flags</span>                      <span class="br0">&#41;</span>;&nbsp;       wait_for_completion<span class="br0">&#40;</span>&amp;ready<span class="br0">&#41;</span>;</pre></div><!-- SECTION [1346-3198] --><h3><a name="starting_a_kernel_thread" id="starting_a_kernel_thread">Starting a Kernel Thread</a></h3><div class="level3"><p>One of the first things a booting kernel does is start a kernel thread to complete the boot process.</p><pre class="code c"><span class="kw4">static</span> <span class="kw4">int</span> init<span class="br0">&#40;</span><span class="kw4">void</span> * unused<span class="br0">&#41;</span>;&nbsp;<span class="kw4">static</span> <span class="kw4">void</span> rest_init<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span><span class="br0">&#123;</span>        kernel_thread<span class="br0">&#40;</span>init,                      <span class="kw2">NULL</span>, CLONE_FS | CLONE_SIGHAND<span class="br0">&#41;</span>;        numa_default_policy<span class="br0">&#40;</span><span class="br0">&#41;</span>;        unlock_kernel<span class="br0">&#40;</span><span class="br0">&#41;</span>;        preempt_enable_no_resched<span class="br0">&#40;</span><span class="br0">&#41;</span>;        cpu_idle<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></pre><p> This &ldquo;init&rdquo; thread completes the boot process and then passes control to a user task. Infact the &ldquo;init&rdquo; thread becomes the &ldquo;init&rdquo; user process.</p></div><!-- SECTION [3199-] --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -