⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m_hpux10.c

📁 unix系统下top命令的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	    && (state == PS_SLEEP || state == PS_STOP || state == PS_ZOMBIE)	    && (state != PS_SLEEP && pst[i].pst_pctcpu < CPU_IDLE_THRESH/100.0))	    pst[i].pst_stat = -1;			if (sel->uid > 0 && sel->uid != pst[i].pst_uid)	    pst[i].pst_stat = -1;	if (sel->command != NULL &&	    strncmp (sel->command, pst[i].pst_ucomm, strlen (pst[i].pst_ucomm)) != 0)	    pst[i].pst_stat = -1;	if (pst[i].pst_stat >= 0)	    active += 1;    }    si->procstates = process_states;    si->p_total = total;    si->p_active = active;    if (compare != NULL) 	qsort ((char *)pst, i, sizeof(*pst), compare);    /* handle is simply an index into the process structures */    handle = 0;    return (caddr_t) &handle;}/* * Find the terminal name associated with a particular * major/minor number pair */static char *term_name (term)struct psdev *term;{    dev_t dev;    int i;    if (term->psd_major == -1 && term->psd_minor == -1)	return "?";    dev = makedev (term->psd_major, term->psd_minor);    for (i = 0; i < nttys && ttynames[i].name[0] != '\0'; i++)    {	if (dev == ttynames[i].dev)	    return ttynames[i].name;    }    return "<unk>";}char *format_next_process(handle, get_userid)caddr_t handle;char *(*get_userid)();{    static char fmt[MAX_COLS];	/* static area where result is built */    char run [sizeof ("runNN")];    int idx;    struct pst_status *proc;    char *state;    int size;    register long cputime;    register double pct;    int where;    struct handle *hp;    struct timeval time;    struct timezone timezone;    /* sanity check */    if (handle == NULL)	return "";    idx = *((int *) handle);    while (idx < nproc && pst[idx].pst_stat < 0)	idx += 1;    if (idx >= nproc || pst[idx].pst_stat < 0)	return "";    proc = &pst[idx];    *((int *) handle) = idx+1;    /* set ucomm for system processes, although we shouldn't need to */    if (proc->pst_ucomm[0] == '\0')    {	if (proc->pst_pid == 0)	    strcpy (proc->pst_ucomm, "Swapper");	else if (proc->pst_pid == 2)	    strcpy (proc->pst_ucomm, "Pager");    }    size = proc->pst_tsize + proc->pst_dsize + proc->pst_ssize;    if (ncpu > 1 && proc->pst_stat == PS_RUN)    {	sprintf (run, "run%02d", proc->pst_procnum);	state = run;    }    else if (proc->pst_stat == PS_SLEEP)    {	switch (proc->pst_pri+PTIMESHARE) {	case PSWP:	state = "SWP"; break; /* also PMEM */	case PRIRWLOCK:	state = "RWLOCK"; break;	case PRIBETA:	state = "BETA"; break;	case PRIALPHA:	state = "ALPHA"; break;	case PRISYNC:	state = "SYNC"; break;	case PINOD:	state = "INOD"; break;	case PRIBIO:	state = "BIO"; break;	case PLLIO:	state = "LLIO"; break; /* also PRIUBA  */	case PZERO:	state = "ZERO"; break;	case PPIPE:	state = "pipe"; break;	case PVFS:	state = "vfs"; break;	case PWAIT:	state = "wait"; break;	case PLOCK:	state = "lock"; break;	case PSLEP:	state = "slep"; break;	case PUSER:	state = "user"; break;	default:	    if (proc->pst_pri < PZERO-PTIMESHARE)		state = "SLEEP";	    else		state = "sleep";	}    }    else	state = state_abbrev [proc->pst_stat];    /* format this entry */    sprintf(fmt,	    Proc_format,	    term_name (&proc->pst_term),	    proc->pst_pid,	    (*get_userid)(proc->pst_uid),	    proc->pst_pri,	    proc->pst_nice - NZERO,	    format_k(size),	    format_k(proc->pst_rssize),	    state,	    format_time(proc->pst_utime + proc->pst_stime),	    100.0 * proc->pst_pctcpu,	    printable(proc->pst_ucomm));    /* return the result */    return(fmt);}/* *  getkval(offset, ptr, size, refstr) - get a value out of the kernel. *	"offset" is the byte offset into the kernel for the desired value, *  	"ptr" points to a buffer into which the value is retrieved, *  	"size" is the size of the buffer (and the object to retrieve), *  	"refstr" is a reference string used when printing error meessages, *	    if "refstr" starts with a '!', then a failure on read will not *  	    be fatal (this may seem like a silly way to do things, but I *  	    really didn't want the overhead of another argument). *  	 */getkval(offset, ptr, size, refstr)unsigned long offset;int *ptr;int size;char *refstr;{    if (lseek(kmem, (long)offset, SEEK_SET) == -1) {        if (*refstr == '!')            refstr++;        (void) fprintf(stderr, "%s: lseek to %s: %s\n", KMEM, 		       refstr, strerror(errno));        quit(23);    }    if (read(kmem, (char *) ptr, size) == -1) {        if (*refstr == '!')             return(0);        else {            (void) fprintf(stderr, "%s: reading %s: %s\n", KMEM, 			   refstr, strerror(errno));            quit(23);        }    }    return(1);}    /* comparison routine for qsort *//* *  proc_compare - comparison function for "qsort" *	Compares the resource consumption of two processes using five *  	distinct keys.  The keys (in descending order of importance) are: *  	percent cpu, cpu ticks, state, resident set size, total virtual *  	memory usage.  The process states are ordered as follows (from least *  	to most important):  WAIT, zombie, sleep, stop, start, run.  The *  	array declaration below maps a process state index into a number *  	that reflects this ordering. */static unsigned char sorted_state[] ={    0,	/* not used		*/    3,	/* sleep		*/    6,	/* run			*/    4,	/* stop			*/    2,	/* zombie		*/    5,	/* start		*/    1,  /* other                */}; proc_compare(p1, p2)struct pst_status *p1;struct pst_status *p2;{    int result;    float lresult;    /* compare percent cpu (pctcpu) */    if ((lresult = p2->pst_pctcpu - p1->pst_pctcpu) == 0)    {	/* use cpticks to break the tie */	if ((result = p2->pst_cpticks - p1->pst_cpticks) == 0)	{	    /* use process state to break the tie */	    if ((result = sorted_state[p2->pst_stat] -			  sorted_state[p1->pst_stat])  == 0)	    {		/* use priority to break the tie */		if ((result = p2->pst_pri - p1->pst_pri) == 0)		{		    /* use resident set size (rssize) to break the tie */		    if ((result = P_RSSIZE(p2) - P_RSSIZE(p1)) == 0)		    {			/* use total memory to break the tie */			result = PROCSIZE(p2) - PROCSIZE(p1);		    }		}	    }	}    }    else    {	result = lresult < 0 ? -1 : 1;    }    return(result);}void (*signal(sig, func))()    int sig;    void (*func)();{    struct sigaction act;    struct sigaction oact;    memset (&act, 0, sizeof (act));    act.sa_handler = func;    if (sigaction (sig, &act, &oact) < 0)	return BADSIG;    return oact.sa_handler;}/* * proc_owner(pid) - returns the uid that owns process "pid", or -1 if *		the process does not exist. *		It is EXTREMLY IMPORTANT that this function work correctly. *		If top runs setuid root (as in SVR4), then this function *		is the only thing that stands in the way of a serious *		security problem.  It validates requests for the "kill" *		and "renice" commands. */int proc_owner(pid)int pid;{    int i;    for (i = 0;  i < nproc; i++)    {	if (pst[i].pst_pid == pid)	    return pst[i].pst_uid;    }    return -1;}static get_tty_names (dir, m)char *dir;int *m;{    char name [MAXPATHLEN+1];    struct dirent **namelist;    int i, n;    if ((n = scandir (dir, &namelist, NULL, NULL)) < 0)	return;    if (ttynames == NULL)    {	nttys = n;	ttynames = malloc (n*sizeof (*ttynames));    }    else    {	nttys += n;	ttynames = realloc (ttynames, nttys*sizeof (*ttynames));    }    for (i = 0; i < n; i++)    {	struct stat statbuf;	char *str = namelist[i]->d_name;	if (*str == '.')	    continue;	sprintf (name, "%s/%s", dir, str);	if (stat (name, &statbuf) < 0)	    continue;		if (!isalpha (*str))	    str = name + sizeof ("/dev");	if (S_ISCHR (statbuf.st_mode))	{	    ttynames [*m].dev = statbuf.st_rdev;	    strncpy (ttynames[*m].name, str, 8);	    ttynames[*m].name[9] = '\0';	    *m += 1;	}	else if (S_ISDIR (statbuf.st_mode))	    get_tty_names (name, m);    }    if (*m < nttys)	ttynames[*m].name[0] = '\0';    free (namelist);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -