- inp = p;
- sleeper = 0;
- if (nbig > MAXNBIG)
- nbig = MAXNBIG;
- if (nbig < 1)
- nbig = 1;
- biggot = 0;
- bplist.bp_link = 0;
- for (rp = allproc; rp != NULL; rp = rp->p_nxt) {
- if (!swappable(rp))
- continue;
- if (rp == inp)
- continue;
- if (rp->p_textp && rp->p_textp->x_flag&XLOCK)
- continue;
- if (rp->p_slptime > maxslp &&
- (rp->p_stat==SSLEEP&&rp->p_pri>PZERO||rp->p_stat==SSTOP)) {
- if (sleeper < rp->p_slptime) {
- p = rp;
- sleeper = rp->p_slptime;
- }
- } else if (!sleeper && (rp->p_stat==SRUN||rp->p_stat==SSLEEP)) {
- rppri = rp->p_rssize;
- if (rp->p_textp)
- rppri += rp->p_textp->x_rssize/rp->p_textp->x_ccount;
- if (biggot < nbig)
- nbp = &bigp[biggot++];
- else {
- nbp = bplist.bp_link;
- if (nbp->bp_pri > rppri)
- continue;
- bplist.bp_link = nbp->bp_link;
- }
- for (bp = &bplist; bp->bp_link; bp = bp->bp_link)
- if (rppri < bp->bp_link->bp_pri)
- break;
- nbp->bp_link = bp->bp_link;
- bp->bp_link = nbp;
- nbp->bp_pri = rppri;
- nbp->bp_proc = rp;
- }
- }
- if (!sleeper) {
- p = NULL;
- inpri = -1000;
- for (bp = bplist.bp_link; bp; bp = bp->bp_link) {
- rp = bp->bp_proc;
- rppri = rp->p_time+rp->p_nice-NZERO;
- if (rppri >= inpri) {
- p = rp;
- inpri = rppri;
- }
- }
- }
- /*
- * If we found a long-time sleeper, or we are desperate and
- * found anyone to swap out, or if someone deserves to come
- * in and we didn't find a sleeper, but found someone who
- * has been in core for a reasonable length of time, then
- * we kick the poor luser out.
- */
- if (sleeper || desperate && p || deservin && inpri > maxslp) {
- (void) splhigh();
- p->p_flag &= ~SLOAD;
- if (p->p_stat == SRUN)
- remrq(p);
- (void) spl0();
- if (desperate) {
- /*
- * Want to give this space to the rest of
- * the processes in core so give them a chance
- * by increasing the deficit.
- */
- gives = p->p_rssize;
- if (p->p_textp)
- gives += p->p_textp->x_rssize / p->p_textp->x_ccount;
- gives = imin(gives, lotsfree);
- deficit += gives;
- } else
- gives = 0; /* someone else taketh away */
- if (swapout(p, p->p_dsize, p->p_ssize) == 0)
- deficit -= imin(gives, deficit);
- else
- goto loop;