Annotation of 43BSDReno/bin/ps/proc_compare.c, revision 1.1

1.1     ! root        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 are permitted provided
        !             6:  * that: (1) source distributions retain this entire copyright notice and
        !             7:  * comment, and (2) distributions including binaries display the following
        !             8:  * acknowledgement:  ``This product includes software developed by the
        !             9:  * University of California, Berkeley and its contributors'' in the
        !            10:  * documentation or other materials provided with the distribution and in
        !            11:  * all advertising materials mentioning features or use of this software.
        !            12:  * Neither the name of the University nor the names of its contributors may
        !            13:  * be used to endorse or promote products derived from this software without
        !            14:  * specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: static char sccsid[] = "@(#)proc_compare.c     5.3 (Berkeley) 6/26/90";
        !            22: #endif /* not lint */
        !            23: 
        !            24: /*
        !            25:  * Returns 1 if p2 is more active than p1
        !            26:  *
        !            27:  * The algorithm for picking the "more active" process is thus:
        !            28:  *
        !            29:  *     1) Runnable processes are favored over anything
        !            30:  *        else.  The runner with the highest cpu
        !            31:  *        utilization is picked (p_cpu).  Ties are
        !            32:  *        broken by picking the highest pid.
        !            33:  *     2) Next, the sleeper with the shortest sleep
        !            34:  *        time is favored.  With ties, we pick out
        !            35:  *        just short-term sleepers (p_pri <= PZERO).
        !            36:  *        Further ties are broken by picking the highest
        !            37:  *        pid.
        !            38:  *
        !            39:  *     NOTE - if you change this, be sure to consider making
        !            40:  *        the change in the kernel too (^T in kern/tty.c).
        !            41:  *
        !            42:  *     TODO - consider whether pctcpu should be used
        !            43:  *
        !            44:  */
        !            45: 
        !            46: #include <sys/param.h>
        !            47: #include <sys/time.h>
        !            48: #include <sys/proc.h>
        !            49: 
        !            50: #define isrun(p)       (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
        !            51: 
        !            52: #define        TESTAB(a, b)    ((a)<<1 | (b))
        !            53: #define        ONLYA   0x10
        !            54: #define        ONLYB   0x01
        !            55: #define        BOTH    0x11
        !            56: 
        !            57: proc_compare(p1, p2)
        !            58:        register struct proc *p1, *p2;
        !            59: {
        !            60: 
        !            61:        if (p1 == NULL)
        !            62:                return (1);
        !            63:        /*
        !            64:         * see if at least one of them is runnable
        !            65:         */
        !            66:        switch (TESTAB(isrun(p1), isrun(p2))) {
        !            67:        case ONLYA:
        !            68:                return (1);
        !            69:        case ONLYB:
        !            70:                return (0);
        !            71:        case BOTH:
        !            72:                /*
        !            73:                 * tie - favor one with highest recent cpu utilization
        !            74:                 */
        !            75:                if (p2->p_cpu > p1->p_cpu)
        !            76:                        return (1);
        !            77:                if (p1->p_cpu > p2->p_cpu)
        !            78:                        return (0);
        !            79:                return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
        !            80:        }
        !            81:        /*
        !            82:         * weed out zombies
        !            83:         */
        !            84:        switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
        !            85:        case ONLYA:
        !            86:                return (1);
        !            87:        case ONLYB:
        !            88:                return (0);
        !            89:        case BOTH:
        !            90:                return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
        !            91:        }
        !            92:        /* 
        !            93:         * pick the one with the smallest sleep time
        !            94:         */
        !            95:        if (p2->p_slptime > p1->p_slptime)
        !            96:                return (0);
        !            97:        if (p1->p_slptime > p2->p_slptime)
        !            98:                return (1);
        !            99:        /*
        !           100:         * favor one sleeping in a non-interruptible sleep
        !           101:         */
        !           102:         if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0)
        !           103:                 return (1);
        !           104:         if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
        !           105:                 return (0);
        !           106:        return(p2->p_pid > p1->p_pid);          /* tie - return highest pid */
        !           107: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.