免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2091 | 回复: 4
打印 上一主题 下一主题

如何编程查看LINUX系统信息 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-16 19:41 |只看该作者 |倒序浏览
如何编程查看LINUX系统信息 比如CPU利用率等等

论坛徽章:
0
2 [报告]
发表于 2006-02-16 19:45 |只看该作者
贴一个我以前写的吧,或许对你有点用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>

#include <sys/types.h>

#define NAME_SIZE 20
#define BUF_SIZE 50

struct proc_list{
        pid_t pid;
        pid_t ppid;
        char name[NAME_SIZE];
        char status;
        struct proc_list *next;
};

struct proc_list *inport_list(char *);
void show_info(struct proc_list *);
int read_info(char *,struct proc_list *);
void free_list(struct proc_list *);
int is_num(char *);

int main()
{
        struct proc_list *head = inport_list("/proc");
        if(head==NULL){
                printf("inport list failed !\n");        
        }        
        show_info(head);
        free_list(head);
        return 0;
}

struct proc_list *inport_list(char *dir){
        DIR *dsptr;
        struct dirent *dptr;
        struct proc_list *head=NULL,*ptr=NULL;
        if((dsptr=opendir(dir))==NULL){
                printf("Error open the dir %s\n",dir);
                exit(-1);               
        }
        
        // read all the dir and file under directory "dir" ;
        while((dptr=readdir(dsptr))!=NULL){
                if(is_num(dptr->d_name)==0){ // determine if the d_name is a number ;
                        
                        // request some memeory , some = sizeof(struct proc_list) ;
                        struct proc_list *item = (struct proc_list *)malloc(sizeof(struct proc_list));
               
                        if(item==NULL){
                                printf("memory allocate error !\n");
                                // must do some free() work here !
                                free_list(head);
                                exit(-1);
                        }
               
                        memset(item,0,sizeof(struct proc_list));
                        
                        // link the item to proc_list list ;
                        if(head==NULL)
                                        head=ptr=item;
                        else{
                                ptr->next=item;
                                ptr=ptr->next;                        
                        }
               
                        item->pid = atoi(dptr->d_name);
                        
                        if(read_info(dptr->d_name,item)!=0){ // determine if read error occurred ;
                                // must do some free() work here ;
                                free_list(head);
                                exit(-1);                        
                        }                        
                }
        }
        closedir(dsptr);
        return head;
}

int read_info(char *d_name,struct proc_list *item){
        char dir[20];
        char name[NAME_SIZE];
        char buffer[BUF_SIZE];
        FILE *fptr;
        
        sprintf(dir,"%s%s","/proc/",d_name);
        chdir(dir);
        
        fptr=fopen("status","r");
        if(fptr==NULL){
                printf("Error reading %s%s\n",dir,"status");
                return -1;
        }
        
        while(fgets(buffer,BUF_SIZE-1,fptr))
    {
             if(strstr(buffer,"Name:")){
                sscanf(buffer,"%s%s",name,item->name);
                }else if(strstr(buffer,"PPid:")){
                sscanf(buffer,"%s%d",name,&(item->ppid));
        }else if(strstr(buffer,"State:")){
            sscanf(buffer,"%s %c",name,&(item->status));
        }
        }
        fclose(fptr);
        return 0;
}

void free_list(struct proc_list *head){
        struct proc_list *ptr;
        while(head!=NULL){
                ptr=head;
                head=head->next;
                free(ptr);
        }
}

void show_info(struct proc_list *head){
        struct proc_list *ptr;
        printf("pid\tppid\tstatus\tname\n");
        for(ptr=head;ptr!=NULL;ptr=ptr->next)
                printf("%d\t%d\t%c\t%s\n",ptr->pid,ptr->ppid,ptr->status,ptr->name);
}

int is_num(char *d_name){
        int i,size;
        size = strlen(d_name);
        if(size==0) return -1;
        
        // determine if the dir name is number ;
        for(i=0;i<size;i++)
                if(d_name[i]<'0' || d_name[i]>'9') return -1;
        return 0;
}

论坛徽章:
0
3 [报告]
发表于 2006-02-16 19:59 |只看该作者
以前写的程序现在看来有很多问题,sscanf()和sprintf()有可能导致溢出,虽然定义buffer大小的时候保证了不会溢出,但总觉得不爽。还有,通过链表的方式效率不高。LZ看着办吧。

论坛徽章:
0
4 [报告]
发表于 2006-02-16 20:25 |只看该作者
上面好象是进程的信息吧

论坛徽章:
0
5 [报告]
发表于 2006-02-16 20:28 |只看该作者
是啊,你只要改成读/proc下你所期望的文件就可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP