1*cf961205Swiz.\" $NetBSD: proc_compare.3,v 1.2 2011/10/21 12:58:53 wiz Exp $ 29e0e0569Schristos.\" 39e0e0569Schristos.\" Copyright (c) 2011 The NetBSD Foundation, Inc. 49e0e0569Schristos.\" All rights reserved. 59e0e0569Schristos.\" 69e0e0569Schristos.\" This code is derived from software contributed to The NetBSD Foundation 79e0e0569Schristos.\" by Christos Zoulas. 89e0e0569Schristos.\" 99e0e0569Schristos.\" Redistribution and use in source and binary forms, with or without 109e0e0569Schristos.\" modification, are permitted provided that the following conditions 119e0e0569Schristos.\" are met: 129e0e0569Schristos.\" 1. Redistributions of source code must retain the above copyright 139e0e0569Schristos.\" notice, this list of conditions and the following disclaimer. 149e0e0569Schristos.\" 2. Redistributions in binary form must reproduce the above copyright 159e0e0569Schristos.\" notice, this list of conditions and the following disclaimer in the 169e0e0569Schristos.\" documentation and/or other materials provided with the distribution. 179e0e0569Schristos.\" 3. All advertising materials mentioning features or use of this software 189e0e0569Schristos.\" must display the following acknowledgement: 199e0e0569Schristos.\" This product includes software developed by the NetBSD 209e0e0569Schristos.\" Foundation, Inc. and its contributors. 219e0e0569Schristos.\" 4. Neither the name of The NetBSD Foundation nor the names of its 229e0e0569Schristos.\" contributors may be used to endorse or promote products derived 239e0e0569Schristos.\" from this software without specific prior written permission. 249e0e0569Schristos.\" 259e0e0569Schristos.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 269e0e0569Schristos.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 279e0e0569Schristos.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 289e0e0569Schristos.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 299e0e0569Schristos.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 309e0e0569Schristos.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 319e0e0569Schristos.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 329e0e0569Schristos.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 339e0e0569Schristos.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 349e0e0569Schristos.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 359e0e0569Schristos.\" POSSIBILITY OF SUCH DAMAGE. 369e0e0569Schristos.\" 379e0e0569Schristos.\" 389e0e0569Schristos.Dd October 20, 2011 399e0e0569Schristos.Dt PROC_COMPARE 3 409e0e0569Schristos.Os 419e0e0569Schristos.Sh NAME 429e0e0569Schristos.Nm proc_compare 43*cf961205Swiz.Nd compare two processes' interactivity 449e0e0569Schristos.Sh LIBRARY 459e0e0569Schristos.Lb libutil 469e0e0569Schristos.Sh SYNOPSIS 479e0e0569Schristos.In sys/sysctl.h 489e0e0569Schristos.Ft int 499e0e0569Schristos.Fn "proc_compare" "const struct kinfo_proc2 *p1" "const struct kinfo_lwp *l1" \ 509e0e0569Schristos"const struct kinfo_proc2 *p2" "const struct kinfo_lwp *l2" 519e0e0569Schristos.Sh DESCRIPTION 529e0e0569SchristosThe 539e0e0569Schristos.Fn proc_compare 549e0e0569Schristosfunction compares two processes that are on the same terminal for their 559e0e0569Schristosinteractivity. 569e0e0569SchristosThis means that the process returned is the one that has a better chance 579e0e0569Schristosbeing the active foreground process on that tty. 589e0e0569SchristosThis algorithm is used in the kernel for 599e0e0569Schristos.Dv SIGINFO 609e0e0569Schristosreporting and in userland by 619e0e0569Schristos.Xr w 1 . 629e0e0569Schristos.Pp 639e0e0569SchristosThe algorithm used is as follows: 649e0e0569Schristos.Bl -bullet -compact -offset indent 659e0e0569Schristos.It 669e0e0569SchristosIf one of them is runnable, it is preferred. 679e0e0569Schristos.It 68*cf961205SwizIf both are runnable, the one with the largest CPU percent is preferred. 699e0e0569Schristos.It 70*cf961205SwizIn a CPU percent tie, the one started more recently wins. 719e0e0569Schristos.It 72*cf961205SwizIf none are runnable, and one of them is a zombie, the non-zombie is preferred 739e0e0569Schristos.It 749e0e0569SchristosIf both are zombies, the one started more recently wins. 759e0e0569Schristos.It 769e0e0569SchristosIf neither is a zombie, the one with the smaller sleep time wins. 779e0e0569Schristos.It 78*cf961205SwizIn a tie, and one is sleeping in non-interruptible sleep, prefer that one. 799e0e0569Schristos.It 809e0e0569SchristosIf both are in the same state, the one started more recently is preferred. 819e0e0569Schristos.El 829e0e0569SchristosIn all cases where the most recently started wins, if there was no winner, 83*cf961205Swizthe one with the largest PID wins. 849e0e0569Schristos.Sh RETURN VALUES 859e0e0569SchristosThe 869e0e0569Schristos.Fn proc_compare 879e0e0569Schristosfunction returns 889e0e0569Schristos.Dv 0 899e0e0569Schristosif 909e0e0569Schristos.Fa p1 919e0e0569Schristosis to be preferred 929e0e0569Schristosand 939e0e0569Schristos.Dv 1 949e0e0569Schristosif 959e0e0569Schristos.Fa p2 969e0e0569Schristosis to be preferred. 979e0e0569Schristos.Sh SEE ALSO 989e0e0569Schristos.Xr w 1 999e0e0569Schristos.Sh HISTORY 1009e0e0569SchristosThe 1019e0e0569Schristos.Fn proc_compare 102*cf961205Swizwas extracted from 103*cf961205Swiz.Pa src/sys/kern/tty.c 104*cf961205Swizand 105*cf961205Swiz.Pa src/usr.bin/w/proc_compare.c 1069e0e0569Schristosand merged in 107*cf961205Swiz.Nx 6.0 . 108