[comp.unix.wizards] Is it possible to get hold of a process's Process control Block given

fox@marlow.uucp (Paul Fox) (08/02/88)

In article <8807140043.ab12588@SEM.BRL.ARPA> bardhan@edu.lsu (Sanjoy Bardhan) writes:
>I am checking out some Unix internals. Is it possible to get hold of a process's
>Process control Block given the pid of the process. Essentially I would 
>like to get the code which takes care of the "PCB save" on a preemption 
>or swap.
>

I believe that you will need to walk down the 'struct proc proc[]' array
looking for a pid match.....

That answers your question (albeit badly)...can anybody answer
mine... I am trying to write/port the BSD 'top' utility
posted to usenet sometime ago, to Unix V.3. 

I've successfully managed to port it to SunOS 4 after a lot of changes,
but am having problems with the u-area.

Given that we start from the struct proc of a process, how
do we get to the u-area, if (a) the process/u-area is in memory,
and (b) the process/u-area is swapped out.

The system I am doing this on is uPort V.3/386.

Many thanks,

Paul Fox.

jfh@rpp386.UUCP (John F. Haugh II) (08/05/88)

In article <429@alice.marlow.uucp> fox@marlow.UUCP (Paul Fox) writes:
>That answers your question (albeit badly)...can anybody answer
>mine... I am trying to write/port the BSD 'top' utility
>posted to usenet sometime ago, to Unix V.3. 

who knows ...

the relevant area of the process table entry is p_addr.  on some
systems this is a union, with one part for the core address, and
another for the swap address.  select the appropriate one by looking
at p_stat.

if you post your source (or mail me a copy) i should be able to get
it ported to sco xenix in 10 or 15 minutes ;-)  below is my solution
to finding a user page.  it is cut from a soon to be posted crash
command.  ignore most of the cruft.  the includes should give you an
idea of where to look for hints as to how to get this to work.
----- crash/user.c -----
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/page.h>
#include <sys/seg.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/dir.h>
#include <sys/user.h>
#include <sys/var.h>
#include <sys/lock.h>
#include "crash.h"

findu (proc, slot, user)
struct	proc	*proc;
int	slot;
struct	user	*user;
{
	struct	proc	*procs = (struct proc *) namelist[NM_PROC].xl_value;
	long	swapaddr;
	int	i;

	if ((proc->p_flag & (SSWAP|SSPART)) || ! (proc->p_flag & SLOAD)) {
		swapaddr = proc->p_addr[0].te_frameno * NBPC;
		l_lseek (swapfd, swapaddr, 0);
		r_read (swapfd, user, sizeof *user);
	} else {
		l_lseek (memfd, proc->p_addr[0].te_frameno * NBPC, 0);
		r_read (memfd, user, sizeof *user);
	}
	if (user->u_procp - procs == slot)
		return (1);
	else
		return (0);
}

/* rest deleted */
-- 
John F. Haugh II                 +--------- Cute Chocolate Quote ---------
HASA, "S" Division               | "USENET should not be confused with
UUCP:   killer!rpp386!jfh        |  something that matters, like CHOCOLATE"
DOMAIN: jfh@rpp386.uucp          |         -- apologizes to Dennis O'Connor