Chinaunix

标题: 如何将shell程序加到c语言程序里去,编译成功呢 [打印本页]

作者: gouxuan81    时间: 2003-05-06 16:07
标题: 如何将shell程序加到c语言程序里去,编译成功呢
linux系统下想读取top命令后产生的信息,如果不用shell编程,纯粹的c语言编程能否实现读取信息(不用一点shell编程)请给予提示;
从文件中读取相关信息这很容易,问题是top是一个命令,不是一个文件(不要告诉我,先top>;aaa形成aaa文件,这样的话就用到了shell编程);


看来不用shell不好编,如果用的话,如何将shell程序加到c语言程序里去呢
这是我毕业设计的关键一步,敬请帮助
在此先行谢过
作者: 蓝色键盘    时间: 2003-05-06 17:28
标题: 如何将shell程序加到c语言程序里去,编译成功呢
如何将shell程序加到c语言程序里去呢

使用system或者exec系列函数

从文件中读取相关信息这很容易,问题是top是一个命令,不是一个文件(不要告诉我,先top>;aaa形成aaa文件,这样的话就用到了shell编程);

到unix编程FAQ中,查找一下,里面有几个例子,能实现top的部分输出结果。
作者: shangxd    时间: 2003-05-07 14:02
标题: 如何将shell程序加到c语言程序里去,编译成功呢
好像可以直接从内核中读取数据,你可以看看top的源代码,看看有没有帮助。
作者: cs    时间: 2003-05-07 14:38
标题: 如何将shell程序加到c语言程序里去,编译成功呢
read /proc for the information of all running processes,  in fact,  top get the info there.
作者: 蓝色键盘    时间: 2003-05-07 15:06
标题: 如何将shell程序加到c语言程序里去,编译成功呢


  1. #define _KMEMUSER
  2.      #include <sys/proc.h>;
  3.      #include <kvm.h>;
  4.      #include <fcntl.h>;
  5.    
  6.      char regexpstr[256];
  7.      #define INIT            register char *sp=regexpstr;
  8.      #define GETC()          (*sp++)
  9.      #define PEEKC()         (*sp)
  10.      #define UNGETC(c)       (--sp)
  11.      #define RETURN(pointer) return(pointer);
  12.      #define ERROR(val)
  13.      #include <regexp.h>;
  14.    
  15.      pid_t
  16.      getpidbyname(char *name,pid_t skipit)
  17.      {
  18.          kvm_t *kd;
  19.          char **arg;
  20.          int error;
  21.          char *p_name=NULL;
  22.          char expbuf[256];
  23.          char **freeme;
  24.          int curpid;
  25.          struct user * cur_user;
  26.          struct user myuser;
  27.          struct proc * cur_proc;
  28.    
  29.    
  30.          if((kd=kvm_open(NULL,NULL,NULL,O_RDONLY,NULL))==NULL){
  31.              return(-1);
  32.          }
  33.          sprintf(regexpstr,"^.*/%s$",name);
  34.          compile(NULL,expbuf,expbuf+256,'\0');
  35.    
  36.          while(cur_proc=kvm_nextproc(kd)){
  37.              curpid = cur_proc->;p_pid;
  38.              if((cur_user=kvm_getu(kd,cur_proc))!=NULL){
  39.                  error=kvm_getcmd(kd,cur_proc,cur_user,&arg,NULL);
  40.                  if(error==-1){
  41.                      if(cur_user->;u_comm[0]!='\0'){
  42.                          p_name=cur_user->;u_comm;
  43.                      }
  44.                  }
  45.                  else{
  46.                      p_name=arg[0];
  47.                  }
  48.              }
  49.              if(p_name){
  50.                  if(!strcmp(p_name,name)){
  51.                      if(error!=-1){
  52.                          free(arg);
  53.                      }
  54.                      if(skipit!=-1 && ourretval==skipit){
  55.                          ourretval=-1;
  56.                      }
  57.                      else{
  58.                          close(fd);
  59.                          break;
  60.                      }
  61.                      break;
  62.                  }
  63.                  else{
  64.                      if(step(p_name,expbuf)){
  65.                          if(error!=-1){
  66.                              free(arg);
  67.                          }
  68.                          break;
  69.                      }
  70.                  }
  71.              }
  72.              if(error!=-1){
  73.                  free(arg);
  74.              }
  75.              p_name=NULL;
  76.          }
  77.          kvm_close(kd);
  78.          if(p_name!=NULL){
  79.              return(curpid);
  80.          }
  81.          return (-1);
  82.      }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2