|
| 1 | + |
| 2 | +<!DOCTYPE html> |
| 3 | +<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]--> |
| 4 | +<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]--> |
| 5 | +<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]--> |
| 6 | +<head> |
| 7 | + <meta charset="utf-8"> |
| 8 | + <title>Linux PID - Jiajun Yao</title> |
| 9 | + <meta name="author" content="jjyao"> |
| 10 | + |
| 11 | + |
| 12 | + <meta name="description" content="In the Linux world, pid means two things. It’s the id of a process from POSIX’s point of view and the id of a task from kernel’s …"> |
| 13 | + |
| 14 | + |
| 15 | + <!-- http://t.co/dKP3o1e --> |
| 16 | + <meta name="HandheldFriendly" content="True"> |
| 17 | + <meta name="MobileOptimized" content="320"> |
| 18 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 19 | + |
| 20 | + |
| 21 | + <link rel="canonical" href="http://blog.jjyao.me/blog/2018/12/16/linux-pid"> |
| 22 | + <link href="/favicon.png" rel="icon"> |
| 23 | + <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css"> |
| 24 | + <link href="/atom.xml" rel="alternate" title="Jiajun Yao" type="application/atom+xml"> |
| 25 | + <script src="/javascripts/modernizr-2.0.js"></script> |
| 26 | + <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> |
| 27 | + <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script> |
| 28 | + <script src="/javascripts/octopress.js" type="text/javascript"></script> |
| 29 | + <!--Fonts from Google"s Web font directory at http://google.com/webfonts --> |
| 30 | +<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"> |
| 31 | +<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"> |
| 32 | + |
| 33 | + |
| 34 | + <script type="text/javascript"> |
| 35 | + var _gaq = _gaq || []; |
| 36 | + _gaq.push(['_setAccount', 'UA-32781930-1']); |
| 37 | + _gaq.push(['_trackPageview']); |
| 38 | + |
| 39 | + (function() { |
| 40 | + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| 41 | + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| 42 | + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| 43 | + })(); |
| 44 | + </script> |
| 45 | + |
| 46 | + |
| 47 | +</head> |
| 48 | + |
| 49 | +<body > |
| 50 | + <header role="banner"><hgroup> |
| 51 | + <h1><a href="/">Jiajun Yao</a></h1> |
| 52 | + |
| 53 | + <h2>Stay hungry, Stay foolish.</h2> |
| 54 | + |
| 55 | +</hgroup> |
| 56 | + |
| 57 | +</header> |
| 58 | + <nav role="navigation"><ul class="subscription" data-subscription="rss"> |
| 59 | + <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li> |
| 60 | + |
| 61 | +</ul> |
| 62 | + |
| 63 | +<form action="https://www.google.com/search" method="get"> |
| 64 | + <fieldset role="search"> |
| 65 | + <input type="hidden" name="q" value="site:blog.jjyao.me" /> |
| 66 | + <input class="search" type="text" name="q" results="0" placeholder="Search"/> |
| 67 | + </fieldset> |
| 68 | +</form> |
| 69 | + |
| 70 | +<ul class="main-navigation"> |
| 71 | + <li><a href="/">Blog</a></li> |
| 72 | + <li><a href="/blog/archives">Archives</a></li> |
| 73 | + <!--<li><a href="/about">About</a></li>--> |
| 74 | + <li><a href="/quotes/quotes.html">Quotes</a></li> |
| 75 | +</ul> |
| 76 | + |
| 77 | +</nav> |
| 78 | + <div id="main"> |
| 79 | + <div id="content"> |
| 80 | + <div> |
| 81 | +<article class="hentry" role="article"> |
| 82 | + |
| 83 | + <header> |
| 84 | + |
| 85 | + <h1 class="entry-title">Linux PID</h1> |
| 86 | + |
| 87 | + |
| 88 | + <p class="meta"> |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +<time class='entry-date' datetime='2018-12-16T08:15:30-08:00'><span class='date'><span class='date-month'>Dec</span> <span class='date-day'>16</span><span class='date-suffix'>th</span>, <span class='date-year'>2018</span></span> <span class='time'>8:15 am</span></time> |
| 95 | + |
| 96 | + </p> |
| 97 | + |
| 98 | + </header> |
| 99 | + |
| 100 | + |
| 101 | +<div class="entry-content"><p>In the Linux world, pid means two things. It’s the id of a process from POSIX’s point of view and the id of a task from kernel’s point of view.</p> |
| 102 | + |
| 103 | +<!-- more --> |
| 104 | + |
| 105 | + |
| 106 | +<h2>PID</h2> |
| 107 | + |
| 108 | +<p>In POSIX, process is an instance of a running program and it contains one or more threads. The id of a process is called pid.</p> |
| 109 | + |
| 110 | +<p>In the Linux kernel, task is the basic execution unit and is thread in the POSIX definition. Task is represented by <code>struct task_struct</code> in the code:</p> |
| 111 | + |
| 112 | +<div class='bogus-wrapper'><notextile><figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span> |
| 113 | +<span class='line-number'>2</span> |
| 114 | +<span class='line-number'>3</span> |
| 115 | +<span class='line-number'>4</span> |
| 116 | +<span class='line-number'>5</span> |
| 117 | +<span class='line-number'>6</span> |
| 118 | +</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">struct</span> <span class="n">task_struct</span> <span class="p">{</span> |
| 119 | +</span><span class='line'> <span class="p">...</span> |
| 120 | +</span><span class='line'> <span class="kt">pid_t</span> <span class="n">pid</span><span class="p">;</span> |
| 121 | +</span><span class='line'> <span class="kt">pid_t</span> <span class="n">tgid</span><span class="p">;</span> |
| 122 | +</span><span class='line'> <span class="p">...</span> |
| 123 | +</span><span class='line'><span class="p">}</span> |
| 124 | +</span></code></pre></td></tr></table></div></figure></notextile></div> |
| 125 | + |
| 126 | + |
| 127 | +<p>Here, pid is the id of a task and tgid is the id of the thread group that contains the task. As we can see, <code>task_struct.pid</code> basically is POSIX thread id and <code>task_struct.tgid</code> is POSIX process id.</p> |
| 128 | + |
| 129 | +<h2>EXAMPLES</h2> |
| 130 | + |
| 131 | +<p>Two different meanings of pid can cause lots of confusions and it’s important to know whether we are talking about pid in the POSIX context or in the Linux kernel context.</p> |
| 132 | + |
| 133 | +<h3>getpid</h3> |
| 134 | + |
| 135 | +<p><code>getpid()</code> is a function defined by the POSIX standard and pid means the id of a process in this context. Linux implements it by returning <code>task_struct.tgid</code>:</p> |
| 136 | + |
| 137 | +<div class='bogus-wrapper'><notextile><figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span> |
| 138 | +<span class='line-number'>2</span> |
| 139 | +<span class='line-number'>3</span> |
| 140 | +</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="n">SYSCALL_DEFINE0</span><span class="p">(</span><span class="n">getpid</span><span class="p">)</span> <span class="p">{</span> |
| 141 | +</span><span class='line'> <span class="k">return</span> <span class="n">task_tgid_vnr</span><span class="p">(</span><span class="n">current</span><span class="p">);</span> |
| 142 | +</span><span class='line'><span class="p">}</span> |
| 143 | +</span></code></pre></td></tr></table></div></figure></notextile></div> |
| 144 | + |
| 145 | + |
| 146 | +<h3>/proc/[pid]</h3> |
| 147 | + |
| 148 | +<p>The proc file system is an interface to kernel data structures and pid means the id of a task/thread in this context. For example, <code>/proc/[pid]/status</code> shows status information about the task/thread and the implementation is in <code>fs/proc/array.c</code>.</p> |
| 149 | + |
| 150 | +<h2>References</h2> |
| 151 | + |
| 152 | +<p>[1] <a href="https://www.kernel.org/doc/ols/2002/ols2002-pages-330-337.pdf">https://www.kernel.org/doc/ols/2002/ols2002-pages-330-337.pdf</a> <br/> |
| 153 | +[2] <a href="https://stackoverflow.com/questions/9305992/if-threads-share-the-same-pid-how-can-they-be-identified">https://stackoverflow.com/questions/9305992/if-threads-share-the-same-pid-how-can-they-be-identified</a></p> |
| 154 | +</div> |
| 155 | + |
| 156 | + |
| 157 | + <footer> |
| 158 | + <p class="meta"> |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +<span class="byline author vcard">Posted by <span class="fn">jjyao</span></span> |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +<time class='entry-date' datetime='2018-12-16T08:15:30-08:00'><span class='date'><span class='date-month'>Dec</span> <span class='date-day'>16</span><span class='date-suffix'>th</span>, <span class='date-year'>2018</span></span> <span class='time'>8:15 am</span></time> |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + </p> |
| 174 | + |
| 175 | + <div class="sharing"> |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +</div> |
| 180 | + |
| 181 | + |
| 182 | + <p class="meta"> |
| 183 | + |
| 184 | + <a class="basic-alignment left" href="/blog/2018/11/07/one-liners/" title="Previous Post: One-Liners">« One-Liners</a> |
| 185 | + |
| 186 | + |
| 187 | + </p> |
| 188 | + </footer> |
| 189 | +</article> |
| 190 | + |
| 191 | + <section> |
| 192 | + <h1>Comments</h1> |
| 193 | + <div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> |
| 194 | +</div> |
| 195 | + </section> |
| 196 | + |
| 197 | +</div> |
| 198 | + |
| 199 | +<aside class="sidebar"> |
| 200 | + |
| 201 | + <section> |
| 202 | + <h1>About Me</h1> |
| 203 | + <p style="margin-bottom:3px">Welcome to here! I'm jjyao and am working on the distributed graph database team at LinkedIn. My current interests are database systems and distributed systems.</p> |
| 204 | + <p> |
| 205 | + <a href="https://www.linkedin.com/in/jjyao/">LinkedIn</a> |
| 206 | + <a href="https://github.com/jjyao/" style="margin-left:20px">GitHub</a> |
| 207 | + </p> |
| 208 | +</section> |
| 209 | + |
| 210 | + |
| 211 | +</aside> |
| 212 | + |
| 213 | + |
| 214 | + </div> |
| 215 | + </div> |
| 216 | + <footer role="contentinfo"><p> |
| 217 | + Copyright © 2018 - jjyao - |
| 218 | + <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span> |
| 219 | +</p> |
| 220 | + |
| 221 | +</footer> |
| 222 | + |
| 223 | + |
| 224 | +<script type="text/javascript"> |
| 225 | + var disqus_shortname = 'jjyao'; |
| 226 | + |
| 227 | + |
| 228 | + // var disqus_developer = 1; |
| 229 | + var disqus_identifier = 'http://blog.jjyao.me/blog/2018/12/16/linux-pid/'; |
| 230 | + var disqus_url = 'http://blog.jjyao.me/blog/2018/12/16/linux-pid/'; |
| 231 | + var disqus_script = 'embed.js'; |
| 232 | + |
| 233 | + (function () { |
| 234 | + var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; |
| 235 | + dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script; |
| 236 | + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); |
| 237 | + }()); |
| 238 | +</script> |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + <script type="text/javascript"> |
| 247 | + (function(){ |
| 248 | + var twitterWidgets = document.createElement('script'); |
| 249 | + twitterWidgets.type = 'text/javascript'; |
| 250 | + twitterWidgets.async = true; |
| 251 | + twitterWidgets.src = '//platform.twitter.com/widgets.js'; |
| 252 | + document.getElementsByTagName('head')[0].appendChild(twitterWidgets); |
| 253 | + })(); |
| 254 | + </script> |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | +</body> |
| 261 | +</html> |
0 commit comments