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
Wrap
C/C++ Source or Header
|
1991-03-22
|
4KB
|
140 lines
/* top ein Programm, dass die Processe in Folge fallender CPU-Auslastung anzeigt */
/* Uwe Simon 24.2.1988 */
/* 22.3.91 wtg:
problem durch .../ti geloest - siehe auskommentierte Zeilen
(durch die division von "h" durch 100 wurde INT h = 0)
die Umrechnung in % erfolgt nun durch *100 im Print-Statement
*/
#include <stdio.h>
#include <procid.h>
#include <termcap.h>
#define BUFFERSIZE 256
#define TCAPSLEN 400
#define maxprocs 20
char tcapbuf[TCAPSLEN];
char *BC;
char *UP;
char PC_=0;
char gt[60],*cm,*cd,*gpt;
short ospeed;
procid *buffer[BUFFERSIZE],*p,procdesc;
unsigned int uticks[BUFFERSIZE],sticks[BUFFERSIZE],otime[BUFFERSIZE];
int anz,tick,row,ende=0,all=0;
catchsignal()
{
ende=1;
}
usage()
{
fprintf(stderr,"Syntax: top [<opts>] [<num>]\n");
fprintf(stderr,"Function: display the processtable every num (def.: 10) seconds \n");
fprintf(stderr,"Options:\n");
fprintf(stderr,"\t-a\tdisplay all process\n");
exit(0);
}
tputc(c)
char c;
{
write(1,&c,1);
}
printproc(p,ti)
int ti;
procid *p;
{
int gticks,gs,gu;
tputs(tgoto(cm,0,row),1,tputc);
gticks=p->_sticks+p->_uticks;
gs=p->_sticks-sticks[p->_id];
sticks[p->_id]=p->_sticks;
gu=p->_uticks-uticks[p->_id];
uticks[p->_id]=p->_uticks;
if((gs<0)||(gu<0)) return(0); /* don't display demaged Data */
if(((gs+gu)==0)&&!all) return(0);
row++;
printf("%3d %3d %3d.%-3d %4d",p->_id,p->_pid,p->_group,p->_user,p->_prior);
/*#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); */
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);
printf("%-18s",(long)p->_pmodul+(long)p->_pmodul->_mh._mname); /*-20*/
fflush(stdout);
}
main(argc,argv)
int argc;
char *argv[];
{
int time,date,day,sl=10,i;
if(argc>1) {
if(*argv[1]=='-') {
if(toupper(argv[1][1])=='A') {
argv++;
argc--;
all=1;
}
else usage();
}
if(argc>1) sl=atoi(argv[1]);
}
intercept(catchsignal);
if(tgetent(tcapbuf,getenv("TERM"))<=0) {
printf("unknown terminal type %s\n",getenv("TERM"));
exit(1);
}
gpt=gt;
if(((cm=tgetstr("cm",&gpt))==0)||((cd=tgetstr("cd",&gpt))==0)) {
printf("top needs 'cm' and 'cd' terminal capabilities\n");
exit(1);
}
tputs(tgoto(cm,0,0),1,tputc);
tputs(cd,1,tputc);
_sysdate(3,&time,&date,&day,&tick);
tick=tick>>16;
anz=_get_process_table(buffer,BUFFERSIZE<<2);
i=2;
while(buffer[i]) { /* initialisiere Zaehler fuer die ticks */
uticks[buffer[i]->_id]=buffer[i]->_uticks;
sticks[buffer[i]->_id]=buffer[i]->_sticks;
otime[buffer[i]->_id]=time;
i++;
}
sleep(2); /* 1 ! hier warten weil sonst devide by zero error */
while(1) {
int htick;
tputs(tgoto(cm,0,0),1,tputc);
anz=_get_process_table(buffer,BUFFERSIZE<<2);
printf(" Id PId Grp.Usr Prio SysTime %%Sys UserTime %%User Totaltime Module");
fflush(stdout);
i=2; row=2;
while(i<anz) {
if(ende) {
printf("\n\n");
exit(0);
}
if((p=buffer[i])!=NULL) {
int h;
if(_get_process_desc(p->_id,sizeof(procid),&procdesc)!= -1) { /* nur wenn Prozess noch lebt */
_sysdate(3,&time,&date,&day,&htick);
/* ##1 */ /* h=(time-otime[p->_id])*tick/100; */ /* h ticks since last call */
h=(time-otime[p->_id])*tick; /* h ticks since last call */
otime[p->_id]=time;
printproc(p,h);
}
}
i++;
}
tputs(cd,1,tputc);
sleep(sl); /* sleep sl seconds to next run */
}
}