#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
//#include <stdlib.h>
#include <malloc.h>
#include <string.h>
//#define DEBUG
// 搜索/proc/*/task/* -name stat > /var/local/1.txt
// find /proc/*/tassk/* -name stat | awk -F/ '{if ( $3 != $5) print}'
/*
重写auto.sh 第一次搜索出线程ID
第二步反复调用/proc/$ID/task/* -name stat 得到....
*/
#ifdef DEBUG
#define PRINT(x) printf x
#else
#define PRINT(x)
#endif
char first = 1;
char num = 0;
typedef struct
{ unsigned pid;// 1
char name[19];// 2
char state;// 3
unsigned ppid;// 4
unsigned pgid;// 5
unsigned sid;// 6
unsigned utime;
unsigned ctime;
unsigned rt_priority;//实时优先级别
int priority;//分时优先级别
int nice;
unsigned rss;
char policy;
}Stat;//18 线程最多
Stat thread;
int FileReadLine(void *pfile, char* buffer)
{
FILE *ptmpfile = (FILE*)pfile;
int readcount = 0;
char tmpch = fgetc(ptmpfile);
while (!feof(ptmpfile))
{
buffer[readcount] = tmpch;
if (tmpch == 0xa)
{
buffer[readcount] = 0;
return (readcount-1);
}
readcount++;
tmpch = fgetc(ptmpfile);
}
return readcount;
}
long atoi(char *arg)//正数适用,负数出错,解决
{
long ret = 0;
char len = strlen(arg);
char i = 0;
char positive = 0;
while (i <len)
{
if (i == 0)
{
if (arg[i] == '-')
{
positive = 1;
i++;
continue;
}
}
ret = ret*10 + (arg[i]- 48);
i++;
}
return (positive?(-ret):ret);
}
void ProToName(Stat * p)
{
static unsigned char order = 1;
switch (order)
{
case 1:
//strcpy(p->name, "daxian.exe");
break;
case 2:
strcpy(p->name, "FP Task");
break;
case 3:
strcpy(p->name, "EventRead Task");
break;
case 4:
strcpy(p->name, "Aud_callback Task");
break;
case 5:
strcpy(p->name, "Blast_vp Task");
break;
case 6:
strcpy(p->name, "SwitchChanel Task");
break;
case 7:
strcpy(p->name, "HwManagement Task");
break;
case 8:
strcpy(p->name, "HwAvCtrl Task");
break;
case 9:
strcpy(p->name, "HwPeriod Task");
break;
case 10:
strcpy(p->name, "Sec_Monitor Task");
break;
case 11:
strcpy(p->name, "Ipanel Task");
break;
case 12:
strcpy(p->name, "Rtsp_recv Task");
break;
case 13:
strcpy(p->name, "BufInjection Task");
break;
case 14:
strcpy(p->name, "NetReceive Task");
break;
default:
strcpy(p->name, "Unkown Name");
break;
}
order++;
if (order == (num+1))
order =1;
}
int GetContent(char *pLine, Stat *des)//一行数据读取
{
char index= 0;
char *p_content = NULL;
while( index < 41)
{
if (index == 0) //pid 进程(包括轻量级进程,即线程)号
{
p_content = strtok(pLine, " ");
if (p_content == NULL)
{
break;
}
des->pid = atoi(p_content);
}
else if (index == 1) //应用程序或命令的名字
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
if (first)
strcpy(des->name, p_content);
}
else if (index == 2)//任务的状态
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->state = *p_content;
}
else if (index == 3)//父进程ID
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->ppid = atoi(p_content);
}
else if (index == 4)//线程组号
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->pgid = atoi(p_content);
}
else if (index == 5) //c该任务所在的会话组ID
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->sid= atoi(p_content);
}
else if (index == 6) //该任务的tty终端的设备号
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 7)//终端的进程组号
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 8) //进程标志位
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 9)//该任务不需要从硬盘拷数据而发生的缺页(次缺页)的次数
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 10)//累计的该任务的所有的waited-for进程曾经发生的次缺页的次数目
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 11)//该任务需要从硬盘拷数据而发生的缺页(主缺页)的次数
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 12)//累计的该任务的所有的waited-for进程曾经发生的主缺页的次数目
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 13)// 该任务在用户态运行的时间,单位为jiffies
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->utime = atoi(p_content);
}
else if (index == 14)//该任务在核心态运行的时间,单位为jiffies
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->ctime = atoi(p_content);
}
else if (index == 15)//累计的该任务的所有的waited-for进程曾经在用户态运行的时间,单位为jiffies
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
// des->utime += atoi(p_content);
}
else if (index == 16)//累计的该任务的所有的waited-for进程曾经在核心态运行的时间,单位为jiffies
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
// des->ctime += atoi(p_content);
}
else if (index == 17)//任务的动态优先级
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->priority = atoi(p_content);
}
else if (index == 18)//任务的静态优先级
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->nice = atoi(p_content);
}
else if (index == 19)//该任务所在的线程组里线程的个数
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
if (first)
num = atoi(p_content);
}
else if (index == 20)//由于计时间隔导致的下一个 SIGALRM 发送进程的时延,以 jiffy 为单位
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 21)// 该任务启动的时间,单位为jiffies
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 22)// 该任务的虚拟地址空间大小
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 23)//该任务当前驻留物理地址空间的大小
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->rss = atoi(p_content);
}
else if (index == 24)//该任务能驻留物理地址空间的最大值
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 25)//该任务在虚拟地址空间的代码段的起始地址
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 26)// 该任务在虚拟地址空间的代码段的结束地址
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 27)//该任务在虚拟地址空间的栈的结束地址
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 28)//esp(32 位堆栈指针) 的当前值, 与在进程的内核堆栈页得到的一致
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 29)//指向将要执行的指令的指针, EIP(32 位指令指针)的当前值
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 30)//待处理信号的位图,记录发送给进程的普通信号
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 31)// 阻塞信号的位图
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 32)// 忽略的信号的位图
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 33)//被俘获的信号的位图
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 34)//如果该进程是睡眠状态,该值给出调度的调用点
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 35)//被swapped的页数,当前没用
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 36)//所有子进程被swapped的页数的和,当前没用
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 37)//该进程结束时,向父进程所发送的信号
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 38)//运行在哪个CPU上
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
}
else if (index == 39)//实时进程的相对优先级别
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->rt_priority = atoi(p_content);
}
else if (index == 40)//进程的调度策略,0=非实时进程,1=FIFO实时进程;2=RR实时进程
{
p_content = strtok(NULL, " ");
if (p_content == NULL)
{
break;
}
des->policy = *p_content;
}
index++;
}
ProToName(des);
}
void print(Stat *p)
{
printf("%6u %18s %1c %5u %10u %4d %5d %5d %c %5u\n", p->pid, p->name, p->state, p->ppid, p->utime+p->ctime, p->nice, p->priority, p->rt_priority ,p->policy,p->rss);
}
int ReadFile(const char *path, char *buf)
{
int fd = open(path, O_RDONLY);
if (fd == -1)
{
//printf("open %s failure...\n", path);
return -1;
}
int re = read(fd, buf ,1024);
close(fd);
return re;
}
int GetCfgInfo(void *pfile, void* p_cfg_info, int process_id)
{
Stat *p_stat= (Stat *)p_cfg_info;
FILE *ptmpfile = (FILE*)pfile;
char i=0 ;
char cfg_context_line[256] = {0};
char buf[1024]={0};
ReadFile("/proc/loadavg", buf);
printf("CPU loadavg:%s", buf);
for(; ; )
{
memset(cfg_context_line, 0, 256);
memset(buf, 0, 1024);
if (feof(ptmpfile))
break;
if (!FileReadLine(ptmpfile, cfg_context_line))
{
break;
}
ReadFile(cfg_context_line,buf);
GetContent(buf, &thread);
if (first)
{
printf("Process (PID:%d) has %d threads now...\n", process_id, num);
printf("%6s %16s %s %5s %10s %4s %5s %5s %2s %5s\n", "PID", "Name", "S", "PPID", "Time(Jf)", "Nice", "D_P", "R_P", "PL", "MM");
first = 0;
}
print(&thread);
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
return 0;
}
int process_id = atoi(argv[1]);
FILE *fd =fopen( "1.txt", "r");
if (fd == NULL)
{
PRINT(("OPEN FILE FAILURE...\n"));
return -1;
}
GetCfgInfo((void *)fd, &thread, process_id);
fclose(fd);
return 1;
//打开stat文件
#if 0
char buf[1024];
char *path= "/proc/2/stat";
int fd = open(path, O_RDONLY);
if (fd == -1)
{
printf("open %s failure...\n", path);
return -1;
}
int re = read(fd, buf ,1024);
close(fd);
printf("%s %d\n", buf, re);
return 0;
int len=lseek(fd, 0, SEEK_END );
printf("%d\n", len);
return 0;
#endif
}