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

📄 m_aux3.c

📁 unix系统下top命令的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	(void)getkval(AVAILRMEM_OFS, (char *)&availrmem, sizeof(availrmem),		      "availrmem");	(void)getkval(AVAILSMEM_OFS, (char *)&availsmem, sizeof(availsmem),		      "availsmem");        (void)getkval(SWAPTAB_OFS, (char *)&swaptab, sizeof(swaptab),		      "swaptab");	for (i = swaptot = swapfree = 0; i < MSFILES; ++i)	    if (swaptab[i].st_dev)	    {		swaptot += swaptab[i].st_npgs;		swapfree += swaptab[i].st_nfpgs;	    }	memory_stats[0] = pagetok(availrmem - freemem);	memory_stats[1] = pagetok(freemem);	memory_stats[2] = pagetok(maxmem - availrmem);	memory_stats[3] = pagetok(swaptot - swapfree);	memory_stats[4] = pagetok(swapfree);    }    update_proc_table();    /* search proc structures for newest process id */    {	struct top_proc *p;	time_t t = 0;	pid_t id = 0;	for (p = ptable; p != eptable; ++p)	{	    if (!p->p_stat)		continue;	    if (p->p_start > t || p->p_start == t && p->p_pid > id)	    {		t = p->p_start;		id = p->p_pid;	    }	}	if (id > last_pid || id < last_pid - 10000)	    last_pid = id;	info->last_pid = last_pid;    }    /* set arrays and strings */    info->cpustates = cpu_states;    info->memory = memory_stats;}caddr_tget_process_info(si, sel, compare)     struct system_info *si;     struct process_select *sel;     int (*compare)();{    int total_procs;    struct top_proc *p, **a;    /* these are copied out of sel for speed */    int show_idle, show_system, show_uid, show_command;    /* get a pointer to the states summary array */    si->procstates = process_states;    /* set up flags which define what we are going to select */    show_idle = sel->idle;    show_system = sel->system;    show_uid = sel->uid != -1;    show_command = sel->command != NULL;    /* count up process states and get pointers to interesting procs */    total_procs = 0;    memset(process_states, 0, sizeof(process_states));    for (p = ptable, a = pactive; p != eptable; ++p)    {	int stat = p->p_stat, flag = p->p_flag;	if (stat == 0 || (flag & SSYS) && !show_system)	    continue;	total_procs++;	process_states[stat]++;	if (stat != SZOMB &&	    (show_idle || stat == SRUN || stat == SIDL || stat == SONPROC ||	     ((stat == SSLEEP || stat == SSTOP) &&	      (flag & (SINTR | SSYS)) == 0)) &&	     (!show_uid || p->p_uid == (uid_t)sel->uid))	{	    /* add it to our active list */	    *a++ = p;	}    }    /* remember active and total counts */    si->p_total = total_procs;    si->p_active = a - pactive;    /* if requested, sort the "interesting" processes */    if (compare != NULL)	qsort(pactive, si->p_active, sizeof(struct top_proc *), compare);    /* set up to iterate though processes */    nextactive = pactive;    /* don't even pretend the return value isn't bogus */    return 0;}char *format_header(uname_field)    char *uname_field;{    int len = strlen(uname_field);    if (len > 8)	len = 8;    memcpy(strchr(fmt_header, 'X'), uname_field, len);    return fmt_header;}char *format_next_process(handle, get_userid)     caddr_t handle;     char *(*get_userid)();{    static char fmt[128];	/* static area where result is built */    struct top_proc *pp = *nextactive++;    sprintf(fmt,	    "%5d %5d %-8.8s %3d %4d %5s %-5s %6s %6.2f%% %6.2f%% %.14s",	    pp->p_pid,	    pp->p_pgrp,	    (*get_userid)(pp->p_uid),	    pp->p_pri,	    pp->p_nice,	    format_k(pp->p_size),	    state_abbrev[pp->p_stat],	    format_time((time_t)pp->p_time / v.v_hz),	    pp->p_wcpu * 100.0,	    pp->p_pcpu * 100.0,	    pp->p_name);    /* return the result */    return (fmt);}/* * check_nlist(nlst) - checks the nlist to see if any symbols were not *              found.  For every symbol that was not found, a one-line *              message is printed to stderr.  The routine returns the *              number of symbols NOT found. */intcheck_nlist(nlst)     register struct nlist *nlst;{    register int i;    /* check to see if we got ALL the symbols we requested */    /* this will write one line to stderr for every symbol not found */    i = 0;    while (nlst->n_name[0])    {	if (nlst->n_value == 0)	{	    /* this one wasn't found */	    fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);	    i = 1;	}	nlst++;    }    return (i);}/* *  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;{    extern int errno;    extern char *sys_errlist[];    if (lseek(kmem, offset, 0) < 0 || read(kmem, ptr, size) != size)    {	if (*refstr == '!')	{	    return (0);	}	else	{	    fprintf(stderr, "top: getkval for %s: %s\n",		    refstr, sys_errlist[errno]);	    quit(23);	    /*NOTREACHED */	}    }    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,	/* runable              */    1,	/* zombie               */    4,	/* stop                 */    5,	/* start                */    7,	/* running              */    2,	/* swapping             */};proc_compare(pp1, pp2)    struct top_proc **pp1, **pp2;{    struct top_proc *p1, *p2;    int result;    double dresult;    /* remove one level of indirection */    p1 = *pp1;    p2 = *pp2;    /* compare percent cpu */    dresult = p2->p_pcpu - p1->p_pcpu;    if (dresult != 0.0)	return dresult > 0.0 ? 1 : -1;    /* use process state to break the tie */    if ((result = (sorted_state[p2->p_stat] -		   sorted_state[p1->p_stat])) == 0)    {	/* use priority to break the tie */	if ((result = p2->p_pri - p1->p_pri) == 0)	{	    /* use total memory to break the tie */	    result = p2->p_size - p1->p_size;	}    }    return result;}/* * 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. */intproc_owner(pid)    int pid;{    struct top_proc *p;    for (p = ptable; p != eptable; ++p)	if (p->p_pid == pid)	    return p->p_uid;    return -1;}/*  * setpriority(int which, pid_t pid, int val) * This system does not have this system call -- fake it */intsetpriority(which, pid, val)    int which, pid, val;{#ifndef IMPLEMENT_SETPRIORITY    errno = ENOSYS;    return -1;#else    struct top_proc *p;    struct proc proc;    int uid;    /* sanity check arguments */    val += NZERO;    if (val < 0)	val = 0;    else if (val > 39)	val = 39;    p = lookup_proc(pid);    if (p->p_pid == -1)    {	errno = ESRCH;	return -1;    }    getkval((long)v.ve_proctab+p->p_slot*sizeof(proc),	    (char *)&proc, sizeof(proc), "proc array");    if (proc.p_stat == 0 || proc.p_pid != pid)    {	errno = ESRCH;	return -1;    }    /* make sure we don't allow nasty people to do nasty things */    uid = getuid();    if (uid != 0)    {	if (uid != proc.p_uid || val < proc.p_nice)	{	    errno = EACCES;	    return -1;	}    }    /* renice */    proc.p_nice = val;    if (lseek(kmem, (v.ve_proctab + p->p_slot*sizeof(proc) +		     offsetof(struct proc, p_nice), 0) < 0 ||	write(kmem, &rp->p_nice, sizeof(rp->p_nice)) != sizeof(rp->p_nice))    {	return -1;    }    return 0;#endif}

⌨️ 快捷键说明

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