打印pid,写着玩。
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h> #define PROC "/proc/" typedef int Myfunc(int pid) ;
static Myfunc myfunc; #define P() printf("%s:%d\n",__FUNCTION__,__LINE__)
static int dopath(Myfunc *func); int main(int argc,char *argv[])
{
return dopath(myfunc);
}
static int
dopath(Myfunc *func)
{
//struct stat statbuf;
struct dirent *dirp;
DIR *dp;
int ret;
char *ptr; //if()
if((dp = opendir(PROC)) == NULL)
{
perror("open");
return -;
} while((dirp = readdir(dp)) != NULL)
{
if(strcmp(dirp->d_name,".") == ||strcmp(dirp->d_name,"..")==)
{
continue;
}
int pid = atoi(dirp->d_name);
//printf("pid =%d\n",pid);
if(pid == )
{
continue;
}
int ret = myfunc(pid);
if(ret != )
{
printf("parse error!\n");
return -;
}
}
if(closedir(dp) < )
{
printf("cant't close directory /proc");
return -;
}
return ;
}
static int
myfunc(int pid)
{
char path[]={};
const char *name="Name:";
const char *state = "State:";
char pname[] = {}; sprintf(path,"/proc/%d/status",pid);
if(access(path,F_OK)!=)
{
printf("%s not exist.\n",path);
return -;
} int fd = open(path,O_RDONLY);
if(fd<)
{
perror("open");
return -;
} char buf[]={};
int length = read(fd,buf,sizeof(buf));
if(length<)
{
perror("read");
close(fd);
return -;
}
char *p0 =strstr(buf,name);
char *p1 =strstr(buf,state);
memcpy(pname,p0+strlen(name)+,p1-p0-strlen(name)-);
printf("pname:%s\n",pname); close(fd); return ;
}
写几行代码来打印pid对应的进程名。
java版本:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader; /**
* @author jevan (jevan.cnblogs.com)
* @version 1.0 at 2013-11-27
*
*/
public class PrintPID { /**
* @param args
*/
public static void main(String[] args) {
printPid();
} private static void printPid()
{ File proc = new File("/proc");
for(File f:proc.listFiles())
{
if(f.getName().equals(".")||f.getName().equals(".."))
continue; if(f.isDirectory())
{
int pid = toInt(f.getName());
if(pid == 0)
continue; File fname = new File("/proc/"+f.getName()+"/status"); if(!fname.exists())
continue; String line0 =read0Line("/proc/"+f.getName()+"/status"); String[] tmp = line0.split(":",2);
if(tmp==null||tmp.length!=2)
{
continue;
}
p("==>"+tmp[1]); }
}
} public static void p(String msg)
{
System.out.println(msg);
} public static String read0Line(String filePath)
{
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(fis == null)
{
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
try {
String line0 = reader.readLine();
//p(line0);
return line0;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
fis.close();
} catch (Exception e2) {
}
}
return null;
} public static int toInt(String str)
{
int ret = 0;
try{
ret = Integer.valueOf(str);
}catch(Exception e)
{
ret = 0;
}
return ret;
} }
执行:
[jevan@media pname]$ time java PrintPID real 0m0.150s
user 0m0.098s
sys 0m0.056s
[jevan@media pname]$ time ./dir real 0m0.019s
user 0m0.000s
sys 0m0.005s
[jevan@media pname]$
打印pid,写着玩。的更多相关文章
- python写机器人玩僵尸骰子
		
python写机器人玩僵尸骰子由Al Sweigart用python发布注意:我正在为我的僵尸骰子模拟器寻找反馈,以及这一套指令.如果你觉得有什么地方可以改进,请发邮件到al@inventwithpy ...
 - c# 写着玩的,两个Task并发,一个写队列一个读队列的异常情况
		
class Program { class TestEnqueue { static Queue<string> str = new Queue<string>(); publ ...
 - Springboot+WebSocket+Kafka(写着玩的)
		
闹着玩的来源:前台发送消息,后台接受处理发给kafka,kafka消费者接到消息传给前台显示.联想到websocket. 最终效果如图: 页面解释: 不填写内容的话,表单值默认为Topic.Greet ...
 - php l练习(写着玩)
		
需求:假设今天24号,今天发表的评论显示几小时前发布,23号发布的显示 昨天几点发布的,再往前的就直接显示年月日. date_default_timezone_set('PRC'); // $crea ...
 - 自己写的书《深入理解Android虚拟机内存管理》,不出版只是写着玩
		
百度网盘地址:https://pan.baidu.com/s/1jI4xZgE 我给起的书名叫做<深入理解Android虚拟机内存管理>.本书分为两个部分,前半部分主要是我对Linux0. ...
 - 自己写着玩的一个天气APP
		
打开的界面: 向上滑动,进入主界面: 省份界面: 城市界面: 加载天气界面: 显示天气界面: 侧滑,显示地区,然后根据天气来显示一首诗句(晴,多云,雪,雨什么的): 第一次启动App的时候才会加载数据 ...
 - [uiautomator篇] [4] 运行成功的日志打印---最后写一个脚本来实现
		
Testing started at 18:23 ... 05/10 18:23:01: Launching ChangeTextBehaviorTestNo apk changes detected ...
 - 写着玩的bash脚本
		
#!/bin/bash function strealingManu { pwdfolder=`pwd` for var in "$@" do man 1 $var > $p ...
 - 《剑指offer》面试题12:打印1到最大的n位数
		
面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...
 
随机推荐
- hibernate系列笔记(1)---Hibernate增删改查
			
Hibernate增删改查 1.首先我们要知道什么是Hibernate Hibernate是一个轻量级的ORMapping对象.主要用来实现Java和数据库表之间的映射,除此之外还提供数据查询和数据获 ...
 - SerfJ REST
			
SerfJ提供了一个MVC架构,但它不包含任何模型,其主要特点是使用控制器collerlor,通过这些collerlor管理REST请求.可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无 ...
 - Apache2.2以上版本与Tomcat整合配置及负载均衡实现
			
apache2.2以上版本,无需使用jk_mod来集成tomcat,直接使用ajp,很方便. 修改apache配置文件httpd.conf 启用mod_proxy_ajp #LoadModule pr ...
 - JS判断手机当前的系统类型
			
<script language="javascript"> window.onload = function () { var n = navigator.userA ...
 - Azure Messaging-ServiceBus Messaging消息队列技术系列4-复杂对象消息是否需要支持序列化和消息持久化
			
在上一篇中,我们介绍了消息的顺序收发保证: Azure Messaging-ServiceBus Messaging消息队列技术系列3-消息顺序保证 在本文中我们主要介绍下复杂对象消息是否需要支持序列 ...
 - java基础:学生管理系统
			
package com.lovo.manager; import java.util.Scanner; /** * 学生管理 * * @author 向往的生活 * */public class St ...
 - JAVA基础:求一个数组的中心元素
 - 关于在链路聚合下做smart,monitor link的转发测试
			
关于在链路聚合下做smart,monitor link的转发测试 这次又要开始瞎折腾了.没事找事了.毕竟我还是很无聊的: 这个实验主要要涉及到以下几个方面: 1.gvrp的应用: 2.Smart Li ...
 - Permanent Generation Removal Overview(译文)
			
英文原稿:http://vdisk.weibo.com/s/vxGdGZEZTEjk 中文整理稿:http://it.deepinmind.com/gc/2014/05/14/metaspace-in ...
 - JavaSE之认识java
			
本来很早之前就应该总结自己在JavaSE中系统学到的知识了,马上就要出去工作了,想想自己还是非常菜的菜鸟,自己就夜不能寐呀.现在从zero基础开始带大家一起回顾学习的基础知识. 现在已经是凌晨了,但是 ...