{"id":261,"date":"2009-03-18T01:02:25","date_gmt":"2009-03-17T17:02:25","guid":{"rendered":"http:\/\/www.darksun190.cn\/blog\/archives=261"},"modified":"2009-03-18T01:02:25","modified_gmt":"2009-03-17T17:02:25","slug":"insscaonuodhieia","status":"publish","type":"post","link":"http:\/\/darksun190.cn\/blog\/?p=261","title":{"rendered":"\u50cf\u6124\u9752\u4e00\u6837\u56de\u7b54\u95ee\u9898"},"content":{"rendered":"<p>\u4f5c\u4e3a\u6280\u672f\u7248\u9762\u7684\u7248\u4e3b\uff0c\u56de\u7b54\u95ee\u9898\u5e94\u8be5\u8ba9\u4eba\u611f\u89c9\u5982\u6c90\u6625\u98ce\uff0c\u6253\u4e0d\u8fd8\u624b\uff0c\u9a82\u4e0d\u8fd8\u53e3\uff0c\u53ef\u60dc\u6211\u7684\u5883\u754c\u4e00\u76f4\u4e0d\u591f\u3002<\/p>\n<p>\u53d1\u4fe1\u4eba: lcrystal (\u5c0f\u6c34\u6676), \u4fe1\u533a: Linux<br \/>\n\u6807&#160; \u9898: \u5982\u4f55\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b<br \/>\n\u53d1\u4fe1\u7ad9: \u5929\u5927\u6c42\u5b9eBBS (Mon Mar 16 12:41:51 2009), \u8f6c\u4fe1(bbs.tju.edu.cn)<\/p>\n<p>\u8981\u7528c\u5199\u4e2a\u51fd\u6570\u7ed9java\u7528\uff0c\u63a5\u53d7\u4e2a\u53c2\u6570\u4e3a\u7cfb\u7edf\u7684\u8fdb\u7a0b\u540d\uff0c\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b\uff0c\u770b\u8fd9\u4e2a\u8fdb\u7a0b<br \/>\n\u540d\u662f\u5426\u5df2\u7ecf\u8fd0\u884c\uff0c\u662f\u5c31\u8fd4\u56detrue\uff0c\u4e0d\u662f\u8fd4\u56defalse<\/p>\n<p>\u73b0\u5728\u7684\u95ee\u9898\u662f\uff0c\u5982\u4f55\u53d6\u5f97\u7cfb\u7edf\u7684\u6240\u6709\u8fdb\u7a0b\u540d\u6765\u6bd4\u8f83\uff0c\u7528ps\u7684\u8bdd\uff0c\u6211\u76ee\u524d\u53ea\u80fd\u60f3\u5230\u5b9a\u5411\u5230\u4e2a\u6587<br \/>\n\u4ef6\uff0c\u7136\u540e\u518d\u8bfb\u6587\u4ef6\uff0c\u611f\u89c9\u6709\u70b9\u4e0d\u6b63\u89c4\uff0c\u6709\u4ec0\u4e48\u51fd\u6570\u53ef\u4ee5\u53d6\u5f97\u7cfb\u7edf\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u8fd4\u56de\u4e2a<br \/>\nstruct\u4ec0\u4e48\u7684\uff0c\u6211\u77e5\u9053\u5728windows\u4e0b\u6709\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u4e0d\u8fc7linux\u5462\uff1f<br \/>\n&#8212;<br \/>\n\u53d1\u4fe1\u4eba: yudianzhiyu (yudianzhiyu), \u4fe1\u533a: Linux<br \/>\n\u6807&#160; \u9898: Re: \u5982\u4f55\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b<br \/>\n\u53d1\u4fe1\u7ad9: \u5929\u5927\u6c42\u5b9eBBS (Mon Mar 16 12:42:55 2009), \u8f6c\u4fe1(bbs.tju.edu.cn)<\/p>\n<p>\u627e\u672c\u5173\u4e8elinxu\u4e0b\u7f16\u7a0b\u7684\u4e66\u770b\u770b<br \/>\n\u3010 \u5728 lcrystal \u7684\u5927\u4f5c\u4e2d\u63d0\u5230: \u3011<br \/>\n: \u8981\u7528c\u5199\u4e2a\u51fd\u6570\u7ed9java\u7528\uff0c\u63a5\u53d7\u4e2a\u53c2\u6570\u4e3a\u7cfb\u7edf\u7684\u8fdb\u7a0b\u540d\uff0c\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b\uff0c\u770b\u8fd9\u4e2a\u8fdb<br \/>\n\u7a0b\u540d\u662f\u5426\u5df2\u7ecf\u8fd0\u884c\uff0c\u662f\u5c31\u8fd4\u56detrue\uff0c\u4e0d\u662f\u8fd4\u56defalse<br \/>\n: \u73b0\u5728\u7684\u95ee\u9898\u662f\uff0c\u5982\u4f55\u53d6\u5f97\u7cfb\u7edf\u7684\u6240\u6709\u8fdb\u7a0b\u540d\u6765\u6bd4\u8f83\uff0c\u7528ps\u7684\u8bdd\uff0c\u6211\u76ee\u524d\u53ea\u80fd\u60f3\u5230\u5b9a\u5411\u5230\u4e2a<br \/>\n\u6587\u4ef6\uff0c\u7136\u540e\u518d\u8bfb\u6587\u4ef6\uff0c\u611f\u89c9\u6709\u70b9\u4e0d\u6b63\u89c4\uff0c\u6709\u4ec0\u4e48\u51fd\u6570\u53ef\u4ee5\u53d6\u5f97\u7cfb\u7edf\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u8fd4\u56de\u4e2a<br \/>\nstruct\u4ec0\u4e48\u7684\uff0c\u6211\u77e5\u9053\u5728windows\u4e0b\u6709\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u4e0d\u8fc7linux\u5462\uff1f<\/p>\n<p>&#8212;<br \/>\n\u53d1\u4fe1\u4eba: darksun (\u5927\u4e2a | \u5355\u8eab\u4e3b\u4e49), \u4fe1\u533a: Linux<br \/>\n\u6807&#160; \u9898: Re: \u5982\u4f55\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b<br \/>\n\u53d1\u4fe1\u7ad9: \u5929\u5927\u6c42\u5b9eBBS (Mon Mar 16 23:17:41 2009), \u8f6c\u4fe1(bbs.tju.edu.cn)<\/p>\n<p>man\u4e00\u4e0bkill<br \/>\nkill\u4e00\u4e0binit\u5e94\u8be5\u5c31\u53ef\u4ee5<br \/>\nps\uff1a\u8fd9\u79cd\u95ee\u9898\u5b8c\u5168\u53ef\u4ee5\u7528google\u89e3\u51b3<br \/>\n\u3010 \u5728 lcrystal (\u5c0f\u6c34\u6676) \u7684\u5927\u4f5c\u4e2d\u63d0\u5230: \u3011<br \/>\n:<br \/>\n: \u8981\u7528c\u5199\u4e2a\u51fd\u6570\u7ed9java\u7528\uff0c\u63a5\u53d7\u4e2a\u53c2\u6570\u4e3a\u7cfb\u7edf\u7684\u8fdb\u7a0b\u540d\uff0c\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b\uff0c\u770b\u8fd9\u4e2a\u8fdb<br \/>\n\u7a0b\u540d\u662f\u5426\u5df2\u7ecf\u8fd0\u884c\uff0c\u662f\u5c31\u8fd4\u56detrue\uff0c\u4e0d\u662f\u8fd4\u56defalse<br \/>\n&#8212;<br \/>\n\u53d1\u4fe1\u4eba: lcrystal (\u5c0f\u6c34\u6676), \u4fe1\u533a: Linux<br \/>\n\u6807&#160; \u9898: Re: \u5982\u4f55\u53d6\u5f97\u5f53\u524d\u7cfb\u7edf\u7684\u8fdb\u7a0b<br \/>\n\u53d1\u4fe1\u7ad9: \u5929\u5927\u6c42\u5b9eBBS (Tue Mar 17 11:35:03 2009), \u672c\u7ad9(bbs.tju.edu.cn)<\/p>\n<p>\u697c\u4e0a\u7684\u771f\u6ca1\u52b2\uff0c\u4f60g\u4e00\u4e2a\u6211\u770b\u770b\uff0c\u4f60\u77e5\u9053windows\u4e0b\u7684\u5982\u4f55\u5199\u5417\uff1f\u4f60\u81ea\u5df1g\u4e00\u4e0b\u770b\u770b\uff0c\u7136\u540e\u518d<br \/>\n\u770b\u770b\u4f60\u8bf4\u7684\u90a3\u4e2a\u548c\u6211\u8981\u7684\u4e00\u6837\u5417\uff1f<br \/>\n&#8212;<br \/>\n\u4e8e\u662f\u4e4e\uff0c\u6211\u51b3\u5b9a\u5982\u540c\u6124\u9752\u4e00\u6837\u7684\u9a82\u56de\u53bb\uff0c\u5b54\u8001\u592b\u5b50\u8bf4\uff1a\u201c\u4ee5\u5fb7\u62a5\u6028\uff0c\u4f55\u4ee5\u62a5\u5fb7\u3002\u201d\u6240\u4ee5\u6211\u8981\u9a82\u56de\u53bb\uff0c\u8fd8\u8981\u9a82\u5f97\u6709\u6c34\u5e73\uff01<br \/>\n\u5bf9\u89e3\u51b3\u8be5\u95ee\u9898\u6709\u5174\u8da3\u7684\u8bf7\u53c2\u9605\u5f15\u7528\u7684\u4e24\u6bb5\u6750\u6599\uff0c\u5bf98g\u6709\u5174\u8da3\u7684\u8bf7\u76f4\u63a5\u53c2\u9605\u6700\u540e\u4e00\u90e8\u5206\uff0c\u770b\u7535\u89c6\u5267\u5b66\u82f1\u8bed\u6700\u5927\u7684\u6536\u83b7\u5c31\u662f\u9a82\u4eba\u3002<\/p>\n<p>I&#8217;m very upset when I finish my work today in 11.p.m. So I&#8217;m more upset when I see your words.<br \/>\nThis is good answer for you, cause I want to yell sb. &amp; you&#8217;re that guy, so I must give you sth. to start the yelling.<br \/>\nabout the kill function in linux environment.<br \/>\n&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____<br \/>\n10.9. kill and raise Functions<br \/>\nThe kill function sends a signal to a process or a group of processes. The raise function allows a process to send a signal to itself.<\/p>\n<p>raise was originally defined by ISO C. POSIX.1 includes it to align itself with the ISO C standard, but POSIX.1 extends the specification of raise to deal with threads (we discuss how threads interact with signals in Section 12.8). Since ISO C does not<br \/>\ndeal with multiple processes, it could not define a function, such as kill, that requires a process ID argument.<\/p>\n<p>#include &lt;signal.h&gt;<\/p>\n<p>int kill(pid_t pid, int signo);<\/p>\n<p>int raise(int signo);<\/p>\n<p>Both return: 0 if OK, 1 on error<\/p>\n<p>The call<\/p>\n<p>raise(signo);<\/p>\n<p>is equivalent to the call<\/p>\n<p>kill(getpid(), signo);<\/p>\n<p>There are four different conditions for the pid argument to kill.<\/p>\n<p>pid &gt; 0<br \/>\nThe signal is sent to the process whose process ID is pid.<\/p>\n<p>pid == 0<br \/>\nThe signal is sent to all processes whose process group ID equals the process group ID of the sender and for which the sender has permission to send the signal. Note that the term all processes excludes an implementation-defined set of system<br \/>\nprocesses. For most UNIX systems, this set of system processes includes the kernel processes and init (pid 1).<\/p>\n<p>pid &lt; 0<br \/>\nThe signal is sent to all processes whose process group ID equals the absolute value of pid and for which the sender has permission to send the signal. Again, the set of all processes excludes certain system processes, as described earlier.<\/p>\n<p>pid == 1<br \/>\nThe signal is sent to all processes on the system for which the sender has permission to send the signal. As before, the set of processes excludes certain system processes.<\/p>\n<p>As we&#8217;ve mentioned, a process needs permission to send a signal to another process. The superuser can send a signal to any process. For other users, the basic rule is that the real or effective user ID of the sender has to equal the real or effective<br \/>\nuser ID of the receiver. If the implementation supports _POSIX_SAVED_IDS (as POSIX.1 now requires), the saved set-user-ID of the receiver is checked instead of its effective user ID. There is also one special case for the permission testing: if the<br \/>\nsignal being sent is SIGCONT, a process can send it to any other process in the same session.<\/p>\n<p>POSIX.1 defines signal number 0 as the null signal. If the signo argument is 0, then the normal error checking is performed by kill, but no signal is sent. This is often used to determine if a specific process still exists. If we send the process the<br \/>\nnull signal and it doesn&#8217;t exist, kill returns 1 and errno is set to ESRCH. Be aware, however, that UNIX systems recycle process IDs after some amount of time, so the existence of a process with a given process ID does not mean that it&#8217;s the process<br \/>\nthat you think it is.<\/p>\n<p>Also understand that the test for process existence is not atomic. By the time that kill returns the answer to the caller, the process in question might have exited, so the answer is of limited value.<\/p>\n<p>If the call to kill causes the signal to be generated for the calling process and if the signal is not blocked, either signo or some other pending, unblocked signal is delivered to the process before kill returns. (Additional conditions occur with<br \/>\nthreads; see Section 12.8 for more information.)<br \/>\n&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____<\/p>\n<p>&#8212;&#8212; This is from the APUE, I don&#8217;t know if this is the correct things you are looking for, but I think it can help you a little.<br \/>\nAnd if you want to get all process by C language, you can look this<\/p>\n<p>&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____<\/p>\n<p>\/* vi: set sw=4 ts=4: *\/<br \/>\n\/*<br \/>\n* Mini ps implementation(s) for busybox<br \/>\n*<br \/>\n* Copyright (C) 1999,2000 by Lineo, inc. &#160;Written by Erik Andersen<br \/>\n* &lt;andersen@lineo.com&gt;, &lt;andersee@debian.org&gt;<br \/>\n*<br \/>\n*<br \/>\n* This contains _two_ implementations of ps for Linux. &#160;One uses the<br \/>\n* traditional \/proc virtual filesystem, and the other use the devps kernel<br \/>\n* driver (written by Erik Andersen to avoid using \/proc thereby saving 100k+).<br \/>\n*<br \/>\n*<br \/>\n*<br \/>\n* This program is free software; you can redistribute it and\/or modify it<br \/>\n* under the terms of the GNU General Public License as published by the Free<br \/>\n* Software Foundation; either version 2 of the License, or (at your option)<br \/>\n* any later version.<br \/>\n*<br \/>\n* This program is distributed in the hope that it will be useful, but WITHOUT<br \/>\n* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or<br \/>\n* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for<br \/>\n* more details.<br \/>\n*<br \/>\n* You should have received a copy of the GNU General Public License along with<br \/>\n* this program; if not, write to the Free Software Foundation, Inc., 59 Temple<br \/>\n* Place, Suite 330, Boston, MA 02111-1307 USA<br \/>\n*<br \/>\n*\/<\/p>\n<p>#include &#8220;internal.h&#8221;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;dirent.h&gt;<br \/>\n#include &lt;errno.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;ctype.h&gt;<br \/>\n#include &lt;sys\/ioctl.h&gt;<br \/>\n#define BB_DECLARE_EXTERN<br \/>\n#define bb_need_help<br \/>\n#include &#8220;messages.c&#8221;<\/p>\n<p>#define TERMINAL_WIDTH &#160;79 &#160; &#160; &#160;\/* not 80 in case terminal has linefold bug *\/<\/p>\n<p>#if ! defined BB_FEATURE_USE_DEVPS_PATCH<\/p>\n<p>\/* The following is the first ps implementation &#8212;<br \/>\n* the one using the \/proc virtual filesystem.<br \/>\n*\/<\/p>\n<p>#if ! defined BB_FEATURE_USE_PROCFS<br \/>\n#error Sorry, I depend on the \/proc filesystem right now.<br \/>\n#endif<\/p>\n<p>typedef struct proc_s {<br \/>\nchar<br \/>\ncmd[16]; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; \/* basename of executable file in call to exec(2) *\/<br \/>\nint<br \/>\nruid, rgid, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;\/* real only (sorry) *\/<br \/>\npid, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; \/* process id *\/<br \/>\nppid; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;\/* pid of parent process *\/<br \/>\nchar<br \/>\nstate; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; \/* single-char code for process state (S=sleeping) *\/<br \/>\n} proc_t;<\/p>\n<p>static int file2str(char *filename, char *ret, int cap)<br \/>\n{<br \/>\nint fd, num_read;<\/p>\n<p>if ((fd = open(filename, O_RDONLY, 0)) == -1)<br \/>\nreturn -1;<br \/>\nif ((num_read = read(fd, ret, cap &#8211; 1)) &lt;= 0)<br \/>\nreturn -1;<br \/>\nret[num_read] = 0;<br \/>\nclose(fd);<br \/>\nreturn num_read;<br \/>\n}<\/p>\n<p>static void parse_proc_status(char *S, proc_t * P)<br \/>\n{<br \/>\nchar *tmp;<\/p>\n<p>memset(P-&gt;cmd, 0, sizeof P-&gt;cmd);<br \/>\nsscanf(S, &#8220;Name:\\t%15c&#8221;, P-&gt;cmd);<br \/>\ntmp = strchr(P-&gt;cmd, &#8216;\\n&#8217;);<br \/>\nif (tmp)<br \/>\n*tmp = &#8216;\\0&#8217;;<br \/>\ntmp = strstr(S, &#8220;State&#8221;);<br \/>\nsscanf(tmp, &#8220;State:\\t%c&#8221;, &amp;P-&gt;state);<\/p>\n<p>tmp = strstr(S, &#8220;Pid:&#8221;);<br \/>\nif (tmp)<br \/>\nsscanf(tmp, &#8220;Pid:\\t%d\\n&#8221; &#8220;PPid:\\t%d\\n&#8221;, &amp;P-&gt;pid, &amp;P-&gt;ppid);<br \/>\nelse<br \/>\nfprintf(stderr, &#8220;Internal error!\\n&#8221;);<\/p>\n<p>\/* For busybox, ignoring effective, saved, etc *\/<br \/>\ntmp = strstr(S, &#8220;Uid:&#8221;);<br \/>\nif (tmp)<br \/>\nsscanf(tmp, &#8220;Uid:\\t%d&#8221;, &amp;P-&gt;ruid);<br \/>\nelse<br \/>\nfprintf(stderr, &#8220;Internal error!\\n&#8221;);<\/p>\n<p>tmp = strstr(S, &#8220;Gid:&#8221;);<br \/>\nif (tmp)<br \/>\nsscanf(tmp, &#8220;Gid:\\t%d&#8221;, &amp;P-&gt;rgid);<br \/>\nelse<br \/>\nfprintf(stderr, &#8220;Internal error!\\n&#8221;);<\/p>\n<p>}<\/p>\n<p>extern int ps_main(int argc, char **argv)<br \/>\n{<br \/>\nproc_t p;<br \/>\nDIR *dir;<br \/>\nFILE *file;<br \/>\nstruct dirent *entry;<br \/>\nchar path[32], sbuf[512];<br \/>\nchar uidName[10] = &#8220;&#8221;;<br \/>\nchar groupName[10] = &#8220;&#8221;;<br \/>\nint len, i, c;<br \/>\n#ifdef BB_FEATURE_AUTOWIDTH<br \/>\nstruct winsize win = { 0, 0 };<br \/>\nint terminal_width = TERMINAL_WIDTH;<br \/>\n#else<br \/>\n#define terminal_width &#160;TERMINAL_WIDTH<br \/>\n#endif<\/p>\n<p>if (argc &gt; 1 &amp;&amp; strcmp(argv[1], dash_dash_help) == 0) {<br \/>\nusage (&#8220;ps\\n&#8221;<br \/>\n#ifndef BB_FEATURE_TRIVIAL_HELP<br \/>\n&#8220;\\nReport process status\\n&#8221;<br \/>\n&#8220;\\nThis version of ps accepts no options.\\n&#8221;<br \/>\n#endif<br \/>\n);<br \/>\n}<\/p>\n<p>dir = opendir(&#8220;\/proc&#8221;);<br \/>\nif (!dir)<br \/>\nfatalError(&#8220;Can&#8217;t open \/proc\\n&#8221;);<\/p>\n<p>#ifdef BB_FEATURE_AUTOWIDTH<br \/>\nioctl(fileno(stdout), TIOCGWINSZ, &amp;win);<br \/>\nif (win.ws_col &gt; 0)<br \/>\nterminal_width = win.ws_col &#8211; 1;<br \/>\n#endif<\/p>\n<p>fprintf(stdout, &#8220;%5s &#160;%-8s %-3s %5s %s\\n&#8221;, &#8220;PID&#8221;, &#8220;Uid&#8221;, &#8220;Gid&#8221;,<br \/>\n&#8220;State&#8221;, &#8220;Command&#8221;);<br \/>\nwhile ((entry = readdir(dir)) != NULL) {<br \/>\nuidName[0] = &#8216;\\0&#8217;;<br \/>\ngroupName[0] = &#8216;\\0&#8217;;<\/p>\n<p>if (!isdigit(*entry-&gt;d_name))<br \/>\ncontinue;<br \/>\nsprintf(path, &#8220;\/proc\/%s\/status&#8221;, entry-&gt;d_name);<br \/>\nif ((file2str(path, sbuf, sizeof sbuf)) != -1) {<br \/>\nparse_proc_status(sbuf, &amp;p);<br \/>\n}<\/p>\n<p>\/* Make some adjustments as needed *\/<br \/>\nmy_getpwuid(uidName, p.ruid);<br \/>\nif (*uidName == &#8216;\\0&#8217;)<br \/>\nsprintf(uidName, &#8220;%d&#8221;, p.ruid);<br \/>\nmy_getgrgid(groupName, p.rgid);<br \/>\nif (*groupName == &#8216;\\0&#8217;)<br \/>\nsprintf(groupName, &#8220;%d&#8221;, p.rgid);<\/p>\n<p>sprintf(path, &#8220;\/proc\/%s\/cmdline&#8221;, entry-&gt;d_name);<br \/>\nfile = fopen(path, &#8220;r&#8221;);<br \/>\nif (file == NULL)<br \/>\ncontinue;<br \/>\ni = 0;<br \/>\nlen = fprintf(stdout, &#8220;%5d %-8s %-8s %c &#8220;, p.pid, uidName, groupName,<br \/>\np.state);<br \/>\nwhile (((c = getc(file)) != EOF) &amp;&amp; (i &lt; (terminal_width-len))) {<br \/>\ni++;<br \/>\nif (c == &#8216;\\0&#8217;)<br \/>\nc = &#8216; &#8216;;<br \/>\nputc(c, stdout);<br \/>\n}<br \/>\nif (i == 0)<br \/>\nfprintf(stdout, &#8220;[%s]&#8221;, p.cmd);<br \/>\nfprintf(stdout, &#8220;\\n&#8221;);<br \/>\n}<br \/>\nclosedir(dir);<br \/>\nreturn(TRUE);<br \/>\n}<\/p>\n<p>#else \/* BB_FEATURE_USE_DEVPS_PATCH *\/<\/p>\n<p>\/* The following is the second ps implementation &#8212;<br \/>\n* this one uses the nifty new devps kernel device.<br \/>\n*\/<\/p>\n<p>#include &lt;linux\/devps.h&gt; \/* For Erik&#8217;s nifty devps device driver *\/<\/p>\n<p>extern int ps_main(int argc, char **argv)<br \/>\n{<br \/>\nchar device[] = &#8220;\/dev\/ps&#8221;;<br \/>\nint i, j, len, fd;<br \/>\npid_t num_pids;<br \/>\npid_t* pid_array = NULL;<br \/>\nstruct pid_info info;<br \/>\nchar uidName[10] = &#8220;&#8221;;<br \/>\nchar groupName[10] = &#8220;&#8221;;<br \/>\n#ifdef BB_FEATURE_AUTOWIDTH<br \/>\nstruct winsize win = { 0, 0 };<br \/>\nint terminal_width = TERMINAL_WIDTH;<br \/>\n#else<br \/>\n#define terminal_width &#160;TERMINAL_WIDTH<br \/>\n#endif<\/p>\n<p>if (argc &gt; 1 &amp;&amp; **(argv + 1) == &#8216;-&#8216;)<br \/>\nusage(&#8220;ps-devps\\n\\nReport process status\\n\\nThis version of ps accepts no options.\\n\\n&#8221;);<\/p>\n<p>\/* open device *\/<br \/>\nfd = open(device, O_RDONLY);<br \/>\nif (fd &lt; 0)<br \/>\nfatalError( &#8220;open failed for `%s&#8217;: %s\\n&#8221;, device, strerror (errno));<\/p>\n<p>\/* Find out how many processes there are *\/<br \/>\nif (ioctl (fd, DEVPS_GET_NUM_PIDS, &amp;num_pids)&lt;0)<br \/>\nfatalError( &#8220;\\nDEVPS_GET_PID_LIST: %s\\n&#8221;, strerror (errno));<\/p>\n<p>\/* Allocate some memory &#8212; grab a few extras just in case<br \/>\n* some new processes start up while we wait. The kernel will<br \/>\n* just ignore any extras if we give it too many, and will trunc.<br \/>\n* the list if we give it too few. &#160;*\/<br \/>\npid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));<br \/>\npid_array[0] = num_pids+10;<\/p>\n<p>\/* Now grab the pid list *\/<br \/>\nif (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)&lt;0)<br \/>\nfatalError(&#8220;\\nDEVPS_GET_PID_LIST: %s\\n&#8221;, strerror (errno));<\/p>\n<p>#ifdef BB_FEATURE_AUTOWIDTH<br \/>\nioctl(fileno(stdout), TIOCGWINSZ, &amp;win);<br \/>\nif (win.ws_col &gt; 0)<br \/>\nterminal_width = win.ws_col &#8211; 1;<br \/>\n#endif<\/p>\n<p>\/* Print up a ps listing *\/<br \/>\nfprintf(stdout, &#8220;%5s &#160;%-8s %-3s %5s %s\\n&#8221;, &#8220;PID&#8221;, &#8220;Uid&#8221;, &#8220;Gid&#8221;,<br \/>\n&#8220;State&#8221;, &#8220;Command&#8221;);<\/p>\n<p>for (i=1; i&lt;pid_array[0] ; i++) {<br \/>\nuidName[0] = &#8216;\\0&#8217;;<br \/>\ngroupName[0] = &#8216;\\0&#8217;;<br \/>\ninfo.pid = pid_array[i];<\/p>\n<p>if (ioctl (fd, DEVPS_GET_PID_INFO, &amp;info)&lt;0)<br \/>\nfatalError(&#8220;\\nDEVPS_GET_PID_INFO: %s\\n&#8221;, strerror (errno));<\/p>\n<p>\/* Make some adjustments as needed *\/<br \/>\nmy_getpwuid(uidName, info.euid);<br \/>\nif (*uidName == &#8216;\\0&#8217;)<br \/>\nsprintf(uidName, &#8220;%ld&#8221;, info.euid);<br \/>\nmy_getgrgid(groupName, info.egid);<br \/>\nif (*groupName == &#8216;\\0&#8217;)<br \/>\nsprintf(groupName, &#8220;%ld&#8221;, info.egid);<\/p>\n<p>len = fprintf(stdout, &#8220;%5d %-8s %-8s %c &#8220;, info.pid, uidName, groupName, info.state);<\/p>\n<p>if (strlen(info.command_line) &gt; 1) {<br \/>\nfor( j=0; j&lt;(sizeof(info.command_line)-1) &amp;&amp; j &lt; (terminal_width-len); j++) {<br \/>\nif (*(info.command_line+j) == &#8216;\\0&#8217; &amp;&amp; *(info.command_line+j+1) != &#8216;\\0&#8217;) {<br \/>\n*(info.command_line+j) = &#8216; &#8216;;<br \/>\n}<br \/>\n}<br \/>\n*(info.command_line+j) = &#8216;\\0&#8242;;<br \/>\nfprintf(stdout, &#8220;%s\\n&#8221;, info.command_line);<br \/>\n} else {<br \/>\nfprintf(stdout, &#8220;[%s]\\n&#8221;, info.name);<br \/>\n}<br \/>\n}<\/p>\n<p>\/* Free memory *\/<br \/>\nfree( pid_array);<\/p>\n<p>\/* close device *\/<br \/>\nif (close (fd) != 0)<br \/>\nfatalError(&#8220;close failed for `%s&#8217;: %s\\n&#8221;, device, strerror (errno));<\/p>\n<p>exit (0);<br \/>\n}<\/p>\n<p>#endif \/* BB_FEATURE_USE_DEVPS_PATCH *\/<\/p>\n<p>&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____<br \/>\nit&#8217;s the code of ps command in linux environment.<br \/>\nOK, here is the end of the good answers.<\/p>\n<p>&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____&#8212;&#8212;______&#8212;&#8211;_____&#8212;&#8211;_____<br \/>\nLet&#8217;s start the bad answer.<br \/>\nDon&#8217;t say thanks to me, I cost lots of time to answer this stupid question not for some stupid words which said by stupid people.<br \/>\nI don&#8217;t care how the shit Windows API do this function.<br \/>\nI don&#8217;t care what function you really want.<br \/>\nAnd I&#8217;m angry when I cost time looking for answer by google.com but received the words &#8221; you&#8217;re boring so much.&#8221;<br \/>\nby the way, go to hell, &#8220;g.cn&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3a\u6280\u672f\u7248\u9762\u7684\u7248\u4e3b\uff0c\u56de\u7b54\u95ee\u9898\u5e94\u8be5\u8ba9\u4eba\u611f\u89c9\u5982\u6c90\u6625\u98ce\uff0c\u6253\u4e0d\u8fd8\u624b\uff0c\u9a82\u4e0d\u8fd8\u53e3\uff0c\u53ef\u60dc\u6211\u7684\u5883\u754c\u4e00\u76f4\u4e0d\u591f\u3002 \u53d1\u4fe1\u4eba: lcr &hellip; <a href=\"http:\/\/darksun190.cn\/blog\/?p=261\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u50cf\u6124\u9752\u4e00\u6837\u56de\u7b54\u95ee\u9898\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":0,"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/darksun190.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}