Chinaunix首页 | 论坛 | 博客
  • 博客访问: 637210
  • 博文数量: 51
  • 博客积分: 773
  • 博客等级: 军士长
  • 技术积分: 2392
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-07 21:32
文章分类
文章存档

2018年(1)

2013年(16)

2012年(34)

分类: LINUX

2012-05-09 22:35:10

将原来的例子进行了修改,可以上下翻页显示进程信息,并杀死选中的进程
  1. #include <curses.h>
  2. #include <string.h>

  3. #define MAXROW 1000 //最大行数
  4. #define MAXCOL 500 //最大列数

  5. WINDOW *scrn; //窗口

  6. char cmdoutlines[MAXROW][MAXCOL]; //存储ps -ax输出的进程信息,每行为一个进程

  7. int ncmdlines = 0; //总进程信息数
  8. int winrow = 0; //当前光标位置
  9. int curcmdline = 0; //当前页面输出进程的起始索引

  10. /*当前行高亮*/
  11. void highlight(int line)
  12. {
  13.     attron(A_BOLD); //输出粗体字
  14.     
  15.     mvaddstr(winrow, 0, cmdoutlines[line]);
  16.     move(winrow, 0);
  17.     
  18.     attroff(A_BOLD);
  19.     
  20.     refresh();
  21. }

  22. /*获取进程信息*/
  23. void runpsax()
  24. {
  25.     FILE *p;
  26.     char ln[MAXCOL];
  27.     int row;
  28.     char *tmp;
  29.     
  30.     p = popen("ps ax", "r");
  31.     
  32.     for (row = 0; row < MAXROW; row++)
  33.     {
  34.         tmp = fgets(ln, MAXCOL, p);
  35.         if ( tmp == NULL)
  36.             break;
  37.         strncpy(cmdoutlines[row], ln, COLS);
  38.         cmdoutlines[row][MAXCOL-1] = 0;
  39.     }
  40.     
  41.     ncmdlines = row; //总进程信息数
  42.     
  43.     pclose(p);
  44. }

  45. /*显示进程信息,startline是数组中的索引*/
  46. void showlines(int startline)
  47. {
  48.     clear();
  49.     
  50.     if (startline < 0)
  51.     {
  52.         startline = 0;
  53.     }
  54.     else if(startline > ncmdlines)
  55.     {
  56.         startline = ncmdlines;
  57.     }
  58.     
  59.     if (ncmdlines - startline < LINES)    
  60.     {
  61.         if (ncmdlines > LINES)
  62.             startline = ncmdlines - LINES;
  63.         else
  64.             startline = 0;
  65.     }
  66.     
  67.     curcmdline = startline; //开始输出信息的索引
  68.     
  69.     //在当前页面输出信息
  70.     for (winrow=0; winrow < LINES && startline < ncmdlines; winrow++, startline++)
  71.     {
  72.         mvaddstr(winrow, 0, cmdoutlines[startline]);
  73.     }
  74.     
  75.     refresh();

  76.     --winrow;
  77.     --startline;

  78.     move(winrow, 0);
  79. }

  80. //光标上移
  81. void updown(int inc)
  82. {
  83.     int tmp = winrow + inc;
  84.     
  85.     if (tmp < 0)    
  86.     {
  87.         showlines(curcmdline+tmp);
  88.         winrow = 0;
  89.         highlight(curcmdline);
  90.         move(0, 0);    
  91.     }
  92.     else if ( (tmp >= 0) && (tmp < LINES))
  93.     {
  94.         mvaddstr(winrow, 0, cmdoutlines[curcmdline+winrow]);
  95.         winrow = tmp;
  96.         highlight(curcmdline+tmp);
  97.     }
  98.     else
  99.     {
  100.         showlines(curcmdline+inc);
  101.         highlight(curcmdline+winrow);    
  102.     }
  103. }

  104. void rerun()
  105. {
  106.     runpsax();
  107.     showlines(0);    
  108. }

  109. void prockill() //杀死进程
  110. {
  111.     char *pid;

  112.     pid =strtok(cmdoutlines[curcmdline+winrow], " ");
  113.     kill(atoi(pid), 9);
  114.     
  115.     rerun();
  116. }

  117. int main()
  118. {
  119.     char c;
  120.     bool quit = false;

  121.     scrn = initscr();

  122.     noecho();
  123.     cbreak();
  124.     
  125.     runpsax();
  126.     showlines(0);
  127.     
  128.     while(!quit)
  129.     {
  130.         c = getch();
  131.     
  132.         switch (c)
  133.         {
  134.             case 'u':
  135.                 updown(-1);
  136.                 break;
  137.             case 'd':
  138.                 updown(1);
  139.                 break;
  140.             case 'r':
  141.                 rerun();
  142.                 break;
  143.             case 'k':
  144.                 prockill();
  145.                 break;
  146.             case 'q':
  147.                 quit = true;
  148.                 break;
  149.             default:
  150.                 break;
  151.         }
  152.     }
  153.     
  154.     endwin();
  155. }

阅读(1965) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

星期五啦2012-05-10 21:23:56

~~很好用,多谢了