home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / w / proc_compare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  3.9 KB  |  122 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)proc_compare.c    5.4 (Berkeley) 2/7/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Returns 1 if p2 is more active than p1
  40.  *
  41.  * The algorithm for picking the "more active" process is thus:
  42.  *
  43.  *    1) Runnable processes are favored over anything
  44.  *       else.  The runner with the highest cpu
  45.  *       utilization is picked (p_cpu).  Ties are
  46.  *       broken by picking the highest pid.
  47.  *    2) Next, the sleeper with the shortest sleep
  48.  *       time is favored.  With ties, we pick out
  49.  *       just short-term sleepers (p_pri <= PZERO).
  50.  *       Further ties are broken by picking the highest
  51.  *       pid.
  52.  *
  53.  *    NOTE - if you change this, be sure to consider making
  54.  *       the change in the kernel too (^T in kern/tty.c).
  55.  *
  56.  *    TODO - consider whether pctcpu should be used
  57.  *
  58.  */
  59.  
  60. #include <sys/param.h>
  61. #include <sys/time.h>
  62. #include <sys/proc.h>
  63.  
  64. #define isrun(p)    (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
  65.  
  66. #define    TESTAB(a, b)    ((a)<<1 | (b))
  67. #define    ONLYA    2
  68. #define    ONLYB    1
  69. #define    BOTH    3
  70.  
  71. proc_compare(p1, p2)
  72.     register struct proc *p1, *p2;
  73. {
  74.  
  75.     if (p1 == NULL)
  76.         return (1);
  77.     /*
  78.      * see if at least one of them is runnable
  79.      */
  80.     switch (TESTAB(isrun(p1), isrun(p2))) {
  81.     case ONLYA:
  82.         return (0);
  83.     case ONLYB:
  84.         return (1);
  85.     case BOTH:
  86.         /*
  87.          * tie - favor one with highest recent cpu utilization
  88.          */
  89.         if (p2->p_cpu > p1->p_cpu)
  90.             return (1);
  91.         if (p1->p_cpu > p2->p_cpu)
  92.             return (0);
  93.         return (p2->p_pid > p1->p_pid);    /* tie - return highest pid */
  94.     }
  95.     /*
  96.      * weed out zombies
  97.      */
  98.     switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
  99.     case ONLYA:
  100.         return (1);
  101.     case ONLYB:
  102.         return (0);
  103.     case BOTH:
  104.         return (p2->p_pid > p1->p_pid);    /* tie - return highest pid */
  105.     }
  106.     /* 
  107.      * pick the one with the smallest sleep time
  108.      */
  109.     if (p2->p_slptime > p1->p_slptime)
  110.         return (0);
  111.     if (p1->p_slptime > p2->p_slptime)
  112.         return (1);
  113.     /*
  114.      * favor one sleeping in a non-interruptible sleep
  115.      */
  116.      if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0)
  117.          return (1);
  118.      if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
  119.          return (0);
  120.     return(p2->p_pid > p1->p_pid);        /* tie - return highest pid */
  121. }
  122.