home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum16.lzh / SOFTWARE / C / TOP_MODIF / top.c < prev   
C/C++ Source or Header  |  1991-03-22  |  4KB  |  140 lines

  1. /* top   ein Programm, dass die Processe in Folge fallender CPU-Auslastung anzeigt */
  2. /* Uwe Simon     24.2.1988 */
  3. /* 22.3.91 wtg:
  4.     problem durch .../ti geloest - siehe auskommentierte Zeilen
  5.     (durch die division von "h" durch 100 wurde INT h = 0)
  6.     die Umrechnung in % erfolgt nun durch *100 im Print-Statement
  7. */
  8. #include <stdio.h>
  9. #include <procid.h>
  10. #include <termcap.h>
  11. #define BUFFERSIZE 256
  12. #define TCAPSLEN 400
  13. #define maxprocs 20
  14.  
  15. char tcapbuf[TCAPSLEN];
  16. char *BC;
  17. char *UP;
  18. char PC_=0;
  19. char gt[60],*cm,*cd,*gpt;
  20. short ospeed;
  21.  
  22. procid *buffer[BUFFERSIZE],*p,procdesc;
  23. unsigned int uticks[BUFFERSIZE],sticks[BUFFERSIZE],otime[BUFFERSIZE];
  24. int anz,tick,row,ende=0,all=0;
  25.  
  26. catchsignal()
  27. {
  28.     ende=1;
  29. }
  30.  
  31.  
  32. usage()
  33. {
  34.     fprintf(stderr,"Syntax: top [<opts>] [<num>]\n");
  35.     fprintf(stderr,"Function: display the processtable every num (def.: 10) seconds \n");
  36.     fprintf(stderr,"Options:\n");
  37.     fprintf(stderr,"\t-a\tdisplay all process\n");
  38.     exit(0);
  39. }
  40.  
  41.  
  42. tputc(c)
  43. char c;
  44. {
  45.     write(1,&c,1);
  46. }
  47.  
  48.  
  49. printproc(p,ti)
  50. int ti;
  51. procid *p;
  52. {
  53.     int gticks,gs,gu;
  54.     tputs(tgoto(cm,0,row),1,tputc);        
  55.     gticks=p->_sticks+p->_uticks;
  56.     gs=p->_sticks-sticks[p->_id];
  57.     sticks[p->_id]=p->_sticks;
  58.     gu=p->_uticks-uticks[p->_id];
  59.     uticks[p->_id]=p->_uticks;
  60.     if((gs<0)||(gu<0)) return(0);  /* don't display demaged Data */
  61.     if(((gs+gu)==0)&&!all) return(0); 
  62.     row++;
  63.     printf("%3d %3d %3d.%-3d %4d",p->_id,p->_pid,p->_group,p->_user,p->_prior);
  64. /*#1    printf("%5d.%02d %3d%% %5d.%02d %4d%% %6d.%02d ",p->_sticks/tick,p->_sticks%tick,gs/ti,p->_uticks/tick,p->_uticks%tick,gu/ti,gticks/tick,gticks%tick); */
  65.     printf("%5d.%02d %3d%% %5d.%02d %4d%% %6d.%02d ",p->_sticks/tick,p->_sticks%tick,gs*100/ti,p->_uticks/tick,p->_uticks%tick,gu*100/ti,gticks/tick,gticks%tick);
  66.      printf("%-18s",(long)p->_pmodul+(long)p->_pmodul->_mh._mname); /*-20*/
  67.      fflush(stdout);
  68. }
  69.  
  70.  
  71. main(argc,argv)
  72. int argc;
  73. char *argv[];
  74. {
  75.     int time,date,day,sl=10,i;
  76.     if(argc>1) {
  77.         if(*argv[1]=='-') {
  78.             if(toupper(argv[1][1])=='A') {
  79.                 argv++;
  80.                 argc--;
  81.                 all=1;
  82.             }
  83.             else usage();
  84.         }
  85.         if(argc>1) sl=atoi(argv[1]);
  86.     }
  87.     intercept(catchsignal);
  88.     if(tgetent(tcapbuf,getenv("TERM"))<=0) {
  89.         printf("unknown terminal type %s\n",getenv("TERM"));
  90.         exit(1);
  91.     }
  92.     gpt=gt;
  93.     if(((cm=tgetstr("cm",&gpt))==0)||((cd=tgetstr("cd",&gpt))==0)) {
  94.         printf("top needs 'cm' and 'cd' terminal capabilities\n");
  95.         exit(1);
  96.     }
  97.     tputs(tgoto(cm,0,0),1,tputc);        
  98.     tputs(cd,1,tputc);
  99.     _sysdate(3,&time,&date,&day,&tick);
  100.     tick=tick>>16;
  101.     
  102.     anz=_get_process_table(buffer,BUFFERSIZE<<2);
  103.     i=2;
  104.     while(buffer[i]) {  /* initialisiere Zaehler fuer die ticks */
  105.         uticks[buffer[i]->_id]=buffer[i]->_uticks;
  106.         sticks[buffer[i]->_id]=buffer[i]->_sticks;
  107.         otime[buffer[i]->_id]=time;
  108.         i++;
  109.     }
  110.     sleep(2);   /* 1 ! hier warten weil sonst devide by zero error */
  111.     while(1) {
  112.         int htick;
  113.         tputs(tgoto(cm,0,0),1,tputc);        
  114.         anz=_get_process_table(buffer,BUFFERSIZE<<2);
  115.         printf(" Id PId Grp.Usr Prio SysTime %%Sys UserTime %%User Totaltime Module");
  116.         fflush(stdout);
  117.         i=2; row=2;
  118.         while(i<anz) {
  119.             if(ende) {
  120.                 printf("\n\n");
  121.                 exit(0);
  122.             }
  123.             if((p=buffer[i])!=NULL) {
  124.                 int h;
  125.                 if(_get_process_desc(p->_id,sizeof(procid),&procdesc)!= -1) { /* nur wenn Prozess noch lebt */
  126.                     _sysdate(3,&time,&date,&day,&htick);
  127. /* ##1 */ /*                h=(time-otime[p->_id])*tick/100; */ /* h ticks since last call */
  128.                     h=(time-otime[p->_id])*tick; /* h ticks since last call */
  129.                     otime[p->_id]=time;
  130.                     printproc(p,h);
  131.                 }
  132.             }
  133.             i++;
  134.         }
  135.         tputs(cd,1,tputc);
  136.         sleep(sl);    /* sleep sl seconds to next run */
  137.     }
  138. }
  139.  
  140.