jmd@loral.UUCP (John McDaid) (11/10/88)
Hello, I have a situation where given a process id, I need to find the child(ren) of that pid. The only way I can think of doing this, without execing some kind of shell script that runs ps -l through awk, is to get the necessary information out of the process table via /dev/kmem. I am running DEC 3.0 Ultirx on uVAX 2000. The reason I need to be able to find the child of a given pid is this: I am forking off an xterm process from my program and I need to be able to kill the xterm process at will. The problem is that xterm is set uid to root and it will not except my SIGTERM or SIGKILL. I CANNOT set uid to root the parent process. But, using the pid from xterm as the parent, I can kill the csh that xterm invokes (the child of xterm) and then xterm would itself exit. I am guessing that by using /dev/kmem, I could seek to the offset where the process table starts, and then read through the process table until I find my given pid as a parent pid. The only problem is that I do not know how to calculate the offset. I have looked at the source for ps on 4.3BSD but I seem to think that there has to be an easier way. Can anyone out there help me out with this problem or maybe even suggest a way for me to get rid of the xterm process. Please respond via e-mail. Thanks in advance to anyone that gives this some thought. John McDaid jmd@loral.UUCP
matt@oddjob.uchicago.edu (Matt Crawford) (11/11/88)
Wouldn't it be a whole lot easier to have the shell save its own pid in a known file, or maybe a file specified through an environment variable? Maybe put if ( $?SAVEPID ) then echo $$ > $SAVEPID endif in your .cshrc? ________________________________________________________ Matt Crawford matt@oddjob.uchicago.edu
bzs@encore.com (Barry Shein) (11/11/88)
From: jmd@loral.UUCP (John McDaid) >Hello, > >I have a situation where given a process id, I need to find the child(ren) >of that pid. The only way I can think of doing this, without execing >some kind of shell script that runs ps -l through awk, is to get the >necessary information out of the process table via /dev/kmem. Why are you so shy about groveling a ps listing? You don't have to use awk if you're worried about all the overhead/memory, you can use popen() and just get it yourself with a little string manipulation. Think of it this way, given that you'll mostly have to duplicate all of ps's code (it's probably doing it correctly) into yours, and that ps (or your code) will spend most of its time doing I/O on /dev/kmem anyhow the fork/exec of ps is a tiny cost (I could go along with fork/execing things like awk therafter is unacceptable to you, but I doubt you'll get what you want much faster than just forking ps.) If ps seems slow on your system it's probably not the fork overhead, you won't likely do better, there's no such thing as a free brunch. And note that /dev/kmem on many systems is becoming only readable to setgid programs (eg. ps) so if you don't use ps your program may stop working somewhere else, or someday and need a complete rewrite anyhow. And if your code is just a clone of ps you won't be able to give it away. Unless there's some very special reason you don't want to fork ps that you're not telling us I say do it, and get onto unsolved problems. I can't tell you how many times I've just popen'd a ps (or even an ls for that matter, particularly in the days when directory reading was less portable) and I've never been sorry. And I'm very impatient. Do it like the pros, cheat! -Barry Shein, ||Encore||
reggie@pdn.UUCP (George W. Leach) (11/11/88)
In article <4161@encore.UUCP> bzs@encore.com (Barry Shein) writes: >>I have a situation where given a process id, I need to find the child(ren) >>of that pid. >Why are you so shy about groveling a ps listing? You don't have to use >awk if you're worried about all the overhead/memory, you can use >popen() and just get it yourself with a little string manipulation. >Do it like the pros, cheat! Thats right! We all do this. In fact, take a look at "The UNIX Programming Environment", Brian Kernighan and Rob Pike, Prentice-Hall, on pages 190-192 at the zap program. Copy and modify it to to your needs. -- George W. Leach Paradyne Corporation ..!uunet!pdn!reggie Mail stop LF-207 Phone: (813) 530-2376 P.O. Box 2826 Largo, FL USA 34649-2826
levy@ttrdc.UUCP (Daniel R. Levy) (11/15/88)
In article <4920@pdn.UUCP>, reggie@pdn.UUCP (George W. Leach) writes: < In article <4161@encore.UUCP> bzs@encore.com (Barry Shein) writes: < >>I have a situation where given a process id, I need to find the child(ren) < >>of that pid. < >Why are you so shy about groveling a ps listing? You don't have to use < >awk if you're worried about all the overhead/memory, you can use < >popen() and just get it yourself with a little string manipulation. < >Do it like the pros, cheat! < Thats right! We all do this. In fact, take a look at "The UNIX < Programming Environment", Brian Kernighan and Rob Pike, Prentice-Hall, < on pages 190-192 at the zap program. Copy and modify it to to your needs. Well... It's not apparent what his application wants to do with the information. If this is fairly time critical stuff, e.g., catch and kill all the children of a given process ASAP, ps may not be fast enough. I don't know about BSD, but on System V, ps will rebuild the /etc/ps_data file every time it finds that /etc/passwd has been updated. This can take several CPU seconds. -- |------------Dan Levy------------| THE OPINIONS EXPRESSED HEREIN ARE MINE ONLY | Bell Labs Area 61 (R.I.P., TTY)| AND ARE NOT TO BE IMPUTED TO AT&T. | Skokie, Illinois | |-----Path: att!ttbcad!levy-----|