比思論壇

標題: top命令是怎么实现的 [打印本頁]

作者: 1370672326    時間: 2016-5-13 20:50
標題: top命令是怎么实现的
好奇top命令是怎么实现的,看代码看不太懂。

自己尝试写了个小程序,去读/proc下的文件获取所有进程信息,挺占cpu和内存。

top刷新频率1s,刷新频率很高了,但占很少资源,它的进程信息难道不是从/proc下的进程相关文件里读取的,还是有什么优化技巧?

作者: 1370672326    時間: 2016-5-13 20:51
坐等大神来解答啊
作者: 1370672326    時間: 2016-5-14 10:24
求大神解答,不要沉了啊
作者: pddnet    時間: 2016-5-24 15:59
top命令应该有开发者的文档的吧,查查看
作者: 1370672326    時間: 2016-5-25 12:27
pddnet 發表於 2016-5-24 15:59
top命令应该有开发者的文档的吧,查查看

源码里没有找到,不知道还能去哪找
作者: pddnet    時間: 2016-5-25 18:35
1370672326 發表於 2016-5-25 12:27
源码里没有找到,不知道还能去哪找

/*
* Copyright (c) 2008, The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*  * Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*  * Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the  
*    distribution.
*  * Neither the name of Google, Inc. nor the names of its contributors
*    may be used to endorse or promote products derived from this
*    software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/  
  
#include <ctype.h>  
#include <dirent.h>  
#include <grp.h>  
#include <pwd.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <sys/types.h>  
#include <unistd.h>  
  
作者: skyseawing    時間: 2016-6-29 13:53
等大神解答
作者: 1370672326    時間: 2016-6-30 16:18
pddnet 發表於 2016-5-25 18:35
/*
* Copyright (c) 2008, The Android Open Source Project
* All rights reserved.

没看懂想表达什么
作者: 1370672326    時間: 2016-7-10 19:36
elitebook 發表於 2016-7-9 15:40
http://blog.csdn.net/earbao/article/details/43453111
通过读/proc目录的文件

这个实现性能和top命令差太多,进程越多,占用cpu飙升
作者: alan3    時間: 2016-7-17 12:29
top估计是直接读取内核进程链等参数的,你直接通过文件系统去读写,那一定慢了
作者: 1370672326    時間: 2016-7-18 13:03
alan3 發表於 2016-7-17 12:29
top估计是直接读取内核进程链等参数的,你直接通过文件系统去读写,那一定慢了 ...

但还是不清楚具体实现
作者: alan3    時間: 2016-7-19 01:10
1370672326 發表於 2016-7-18 13:03
但还是不清楚具体实现

具体可以看源代码
作者: hdq90    時間: 2017-1-8 00:48
学习了

作者: aizikeli    時間: 2017-5-23 00:05
我之前也是通过读/proc目录的文件,不过就是太占CPU了
作者: givemeyourpic    時間: 2017-5-25 10:45
google 一下很容易找到源代码啊  

http://www.unixtop.org/download/
作者: LoisNice    時間: 2017-6-25 14:39
与其这样问,不如多学习一下,学到看得懂为止。。。。
作者: 0011022    時間: 2017-10-12 10:55
可以去看看top的源码

作者: 33445561    時間: 2019-4-29 16:58
大神大神。
作者: 大白菜    時間: 2019-7-2 02:41
好奇top命令是怎么实现的,看代码看不太懂。
作者: hdq90    時間: 2019-9-1 23:30
用就完了。
作者: pk1127ai    時間: 2023-8-4 14:04
好资源加分!




歡迎光臨 比思論壇 (http://184.95.51.83/) Powered by Discuz! X2.5