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

📄 m_ncr3000.c

📁 unix系统下top命令的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
      if (pp->pr_state != 0 &&	  (show_system || ((pp->pr_flag & SSYS) == 0)))	{	  total_procs++;	  process_states[pp->pr_state]++;	  if ((!pp->pr_zomb) &&	      (show_idle || (pp->pr_state == SRUN) || (pp->pr_state == SONPROC)) &&	      (!show_uid || pp->pr_uid == (uid_t) sel->uid))	    {	      *prefp++ = pp;	      active_procs++;	    }	}    }  /* if requested, sort the "interesting" processes */  if (compare != NULL)      qsort ((char *) pref, active_procs, sizeof (struct prpsinfo *), compare);  /* remember active and total counts */  si->p_total = total_procs;  si->p_active = active_procs;  /* pass back a handle */  handle.next_proc = pref;  handle.remaining = active_procs;  return ((caddr_t) & handle);}char fmt[MAX_COLS];			/* static area where result is built */char *format_next_process (		      caddr_t handle,		      char *(*get_userid) ()){  register struct prpsinfo *pp;  struct handle *hp;  register long cputime;  register double pctcpu;  /* find and remember the next proc structure */  hp = (struct handle *) handle;  pp = *(hp->next_proc++);  hp->remaining--;  /* get the cpu usage and calculate the cpu percentages */  cputime = pp->pr_time.tv_sec;  pctcpu = percent_cpu (pp);  /* format this entry */  (void) sprintf (fmt,		  Proc_format,		  pp->pr_pid,		  (*get_userid) (pp->pr_uid),		  pp->pr_pri - PZERO,		  pp->pr_nice - NZERO,		  format_k(pagetok (pp->pr_size)),		  format_k(pagetok (pp->pr_rssize)),		  state_abbrev[pp->pr_state],		  format_time(cputime),		  (pp->pr_cpu & 0377),		  100.0 * pctcpu,		  pp->pr_fname);  /* 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 (register struct nlist *nlst){  register int i;  struct stat stat_buf;  /* 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 != NULL)    {      if (nlst->n_type == 0)	{	  if (strcmp("sysinfo", nlst->n_name) == 0)	    {		/* check to see if /stats file system exists. If so, 	*/		/* ignore error. 					*/		if ( !((stat("/stats/sysinfo", &stat_buf) == 0) && 		  (stat_buf.st_mode & S_IFREG)) )		  {		    (void) fprintf (stderr, "kernel: no symbol named `%s'\n", nlst->n_name);		    i = 1;		  } else {		    use_stats = 1;		  }	    } else {	      /* this one wasn't found */	      (void) 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). * */intgetkval (	  unsigned long offset,	  int *ptr,	  int size,	  char *refstr){  if (lseek (kmem, (long) offset, 0) == -1)    {      if (*refstr == '!')	refstr++;      (void) fprintf (stderr, "%s: lseek to %s: %s\n",		      myname, refstr, sys_errlist[errno]);      quit (22);    }  if (read (kmem, (char *) ptr, size) == -1)    if (*refstr == '!')      /* we lost the race with the kernel, process isn't in memory */      return (0);    else      {	(void) fprintf (stderr, "%s: reading %s: %s\n",			myname, refstr, sys_errlist[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. */unsigned char sorted_state[] ={  0,				/* not used		*/  3,				/* sleep		*/  6,				/* run			*/  2,				/* zombie		*/  4,				/* stop			*/  5,				/* start		*/  7,				/* run on a processor   */  1				/* being swapped (WAIT)	*/};intproc_compare (	       struct prpsinfo **pp1,	       struct prpsinfo **pp2){    register struct prpsinfo *p1;    register struct prpsinfo *p2;    register long result;    /* remove one level of indirection */    p1 = *pp1;    p2 = *pp2;    /* compare percent cpu (pctcpu) */    if ((result = (long) (p2->pr_cpu - p1->pr_cpu)) == 0)      {	/* use cpticks to break the tie */	if ((result = p2->pr_time.tv_sec - p1->pr_time.tv_sec) == 0)	  {	    /* use process state to break the tie */	    if ((result = (long) (sorted_state[p2->pr_state] -				  sorted_state[p1->pr_state])) == 0)	      {		/* use priority to break the tie */		if ((result = p2->pr_oldpri - p1->pr_oldpri) == 0)		  {		    /* use resident set size (rssize) to break the tie */		    if ((result = p2->pr_rssize - p1->pr_rssize) == 0)		      {			/* use total memory to break the tie */			result = (p2->pr_size - p1->pr_size);		      }		  }	      }	  }      }    return (result);  }/*get process table*/voidgetptable (struct prpsinfo *baseptr){  struct prpsinfo *currproc;	/* pointer to current proc structure	*/  int numprocs = 0;  struct dirent *direntp;  for (rewinddir (procdir); direntp = readdir (procdir);)    {      int fd;      if ((fd = open (direntp->d_name, O_RDONLY)) < 0)	continue;      currproc = &baseptr[numprocs];      if (ioctl (fd, PIOCPSINFO, currproc) < 0)	{	  (void) close (fd);	  continue;	}      numprocs++;      (void) close (fd);    }  if (nproc != numprocs)    nproc = numprocs;}/* return the owner of the specified process, for use in commands.c as we're   running setuid root */uid_tproc_owner (pid_t pid){  register struct prpsinfo *p;  int i;  for (i = 0, p = pbase; i < nproc; i++, p++)    if (p->pr_pid == pid)      return (p->pr_uid);  return (-1);}intsetpriority (int dummy, int who, int niceval){  int scale;  int prio;  pcinfo_t pcinfo;  pcparms_t pcparms;  tsparms_t *tsparms;  strcpy (pcinfo.pc_clname, "TS");  if (priocntl (0, 0, PC_GETCID, (caddr_t) & pcinfo) == -1)    return (-1);  prio = niceval;  if (prio > PRIO_MAX)    prio = PRIO_MAX;  else if (prio < PRIO_MIN)    prio = PRIO_MIN;  tsparms = (tsparms_t *) pcparms.pc_clparms;  scale = ((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri;  tsparms->ts_uprilim = tsparms->ts_upri = -(scale * prio) / 20;  pcparms.pc_cid = pcinfo.pc_cid;  if (priocntl (P_PID, who, PC_SETPARMS, (caddr_t) & pcparms) == -1)    return (-1);  return (0);}/**************************************************************** * read_sysinfos() -						* *	Read all of the CPU specific sysinfo sturctures in from	* *	the /stats file system.					* ****************************************************************/read_sysinfos(num_cpus, buf)	int num_cpus;	struct sysinfo	*buf;{	static int	fd1=0;	/* file descriptor for /stats/sysinfo */	int		read_sz;	/* Open /stats/sysinfo one time only and leave it open */	if (fd1==0) { 		if ((fd1 = open("/stats/sysinfo", O_RDONLY)) == -1)			(void) fprintf (stderr, "%s: Open of /stats/sysinfo failed\n", myname);	}	/* reset the read pointer to the beginning of the file */	if (lseek(fd1, 0L, SEEK_SET) == -1)		(void) fprintf (stderr, "%s: lseek to beginning of /stats/sysinfo failed\n", myname);	read_sz = num_cpus * sizeof(buf[0]);	if (read(fd1, buf, read_sz) != read_sz)		(void) fprintf (stderr, "%s: Read of /stats/sysinfo failed\n", myname);}/**************************************************************** * sysinfo_data() -						* *	Add up all of the CPU specific sysinfo sturctures to	* *	make the GLOBAL sysinfo.				* ****************************************************************/sysinfo_data(num_cpus, global_si, percpu_si)	int num_cpus;	struct sysinfo	*global_si;	struct sysinfo	*percpu_si;{	struct sysinfo	*percpu_p;	int		cpu, i, *global, *src;	/* null out the global statistics from last sample */	memset(global_si, 0, sizeof(struct sysinfo));	percpu_p = (struct sysinfo *)percpu_si;	for(cpu = 0; cpu < num_cpus; cpu++) {		global = (int *)global_si;		src = (int *)percpu_p;		/* assume sysinfo ends on an int boundary */		/* Currently, all of the struct sysinfo members are the same		 * size as an int. If that changes, we may not be able to		 * do this. But this should be safe.		 */		for(i=0; i<sizeof(struct sysinfo)/sizeof(int); i++) {			*global++ += *src++;		}		percpu_p++;	}}

⌨️ 快捷键说明

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