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

📄 m_sco.c

📁 unix系统下top命令的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
        struct system_info *si;        struct process_select *sel;        int (*compare)();{register int i;register int total_procs;register int active_procs;register struct proc **prefp;register struct proc *pp;/* set up flags of what we are going to select *//* these are copied out of sel for simplicity */int show_idle = sel->idle;int show_system = sel->system;int show_uid = sel->uid != -1;int show_command = sel->command != NULL;   /* read all the proc structures in one fell swoop */   (void) getkval(proca, (int *)pbase, bytes, "proc array");   /* get a pointer to the states summary array */   si->procstates = process_states;   /* count up process states and get pointers to interesting procs */   total_procs = active_procs = 0;   memset((char *)process_states, 0, sizeof(process_states));   prefp = pref;   for (pp = pbase, i = 0; i < v.v_proc; pp++, i++) {      /*       * Place pointers to each valid proc structure in pref[].       * Process slots that are actually in use have a non-zero       * status field. Processes with SSYS set are system processes --       * these are ignored unless show_system is set.       */      if (pp->p_stat && (show_system || ((pp->p_flag & SSYS) == 0))) {         total_procs++;         process_states[pp->p_stat]++;	 if ((pp->p_stat != SZOMB) &&	     (show_idle || (pp->p_stat == SRUN) || (pp->p_stat == SONPROC)) &&	     (!show_uid || pp->p_uid == (ushort)sel->uid)) {		*prefp++ = pp;		active_procs++;	 }      }   }   /* if requested, sort the "interesting" processes */   if (compare)      qsort((char *)pref, active_procs, sizeof(struct proc *), compare);   /* remember active and total counts */   si->p_total = total_procs;   si->p_active = pref_len = active_procs;   /* pass back a handle */   handle.next_proc = pref;   handle.remaining = active_procs;   return((caddr_t)&handle);}char fmt[128];		/* static area where result is built */char *format_next_process(handle, get_userid)      caddr_t handle;      char *(*get_userid)();{register struct proc *pp;register time_t cputime;register double pct;int where;struct user u;struct handle *hp;char command[29];   /* find and remember the next proc structure */   hp = (struct handle *)handle;   pp = *(hp->next_proc++);   hp->remaining--;   /* get the process's user struct and set cputime */   if ((where = sysi86(RDUBLK, pp->p_pid, &u, sizeof(struct user))) != -1)      where = (pp->p_flag & SLOAD) ? 0 : 1;   if (where == -1) {      strcpy(command, "<swapped>");      cputime = 0;   } else {      /* set u_comm for system processes */      if (u.u_comm[0] == '\0') {	 if (pp->p_pid == 0)	    strcpy(command, "Swapper");	 else if (pp->p_pid == 2)	    strcpy(command, "Pager");	 else if (pp->p_pid == 3)	    strcpy(command, "Sync'er");      } else if (where == 1) {	 /* print swapped processes as <pname> */	 register char *s1;	 u.u_psargs[28 - 3] = '\0';	 strcpy(command, "<");	 strcat(command, strtok(u.u_psargs, " "));	 strcat(command, ">");	 while (s1 = (char *)strtok(NULL, " "))	    strcat(command, s1);      } else {	 sprintf(command, "%s", u.u_psargs);      }      cputime = u.u_utime + u.u_stime;   }   /* calculate the base for cpu percentages */   pct = pctdouble(pp->p_cpu);   /* format this entry */   sprintf(fmt,	   Proc_format,	   pp->p_pid,	   (*get_userid)(pp->p_uid),	   pp->p_pri - PZERO,	   pp->p_nice - NZERO,	   format_k(PROCSIZE(&u)),	   0,	   state_abbrev[pp->p_stat],	   format_time(cputime / Hz),	   printable(command));   return(fmt);}/* * 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 founded. */int check_nlist(nlst)    register struct nlist *nlst;{register int i = 0;   while (nlst->n_name) {      if (nlst->n_type == 0) {	 fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);	 i++;      }      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;{   if (lseek(kmem, (long)offset, SEEK_SET) == -1) {      if (*refstr == '!')         refstr++;      fprintf(stderr, "%s: lseek to %s: %s\n", KMEM,	       refstr, errmsg(errno));      quit(23);   }   if (read(kmem, (char *)ptr, size) == -1) {      if (*refstr == '!')         return 0;      fprintf(stderr, "%s: reading %s: %s\n", KMEM,	       refstr, errmsg(errno));      quit(23);   }   return(1);}    /* comparison routine for qsort *//* NOTE: this is specific to the BSD proc structure, but it should   give you a good place to start. *//* *  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		*/    5,	/* sleep		*/    6,	/* run			*/    2,	/* zombie		*/    4,	/* stop			*/    1,	/* start		*/    7,	/* onpr    		*/    3,	/* swap            	*/}; proc_compare(pp1, pp2)struct proc **pp1;struct proc **pp2;{register struct proc *p1;register struct proc *p2;register int result;register ulong lresult;   /* remove one level of indirection */   p1 = *pp1;   p2 = *pp2;   /* 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 time to break the tie */	 if ((result = (p2->p_utime + p2->p_stime) -		       (p1->p_utime + p1->p_stime)) == 0)	 {	    /* use resident set size (rssize) to break the tie */	    if ((result = p2->p_size - p1->p_size) == 0)	    {	       result = 0;	    }	 }      }   }    return(result);}/* returns uid of owner of process pid */proc_owner(pid)int pid;{register int cnt;register struct proc **prefp;register struct proc  *pp;   prefp = pref;   cnt = pref_len;   while (--cnt >= 0) {      if ((pp = *prefp++)->p_pid == (short)pid)	 return ((int)pp->p_uid);   }   return(-1);}int setpriority(int dummy, int who, int nicewal){   errno = 1;   return -1;}/* sigblock is not POSIX conformant */sigset_t sigblock (sigset_t mask){sigset_t oset;   sigemptyset(&oset);   sigprocmask(SIG_BLOCK, &mask, &oset);   return oset;}/* sigsetmask is not POSIX conformant */sigsetmask(sigset_t mask){sigset_t oset;   sigemptyset(&oset);   sigprocmask(SIG_SETMASK, &mask, &oset);   return oset;}

⌨️ 快捷键说明

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