Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!think!snorkelwacker!mit-eddie!bu.edu!buengc!bph From: bph@buengc.BU.EDU (Blair P. Houghton) Newsgroups: comp.unix.questions Subject: Re: Killing the correct process Message-ID: <5382@buengc.BU.EDU> Date: 15 Feb 90 20:28:09 GMT References: <22332@adm.BRL.MIL> <5312@star.cs.vu.nl> <1212@root44.co.uk> Reply-To: bph@buengc.bu.edu (Blair P. Houghton) Followup-To: comp.unix.questions Organization: Boston Univ. Col. of Eng. Lines: 54 In article <1212@root44.co.uk> gwc@root.co.uk (Geoff Clare) writes: >In article <5312@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: >>In article <22332@adm.BRL.MIL> marwood@ncs.dnd.ca (Gordon Marwood) >>>[wants to timeout ftp.] >> >>How about using the following general purpose script? If that's out of the >>question, the script might still give you a hint how to solve your problem. > >[ extremely complicated script deleted ] > >There is a much simpler way than Maarten's. Edited for brevity... Called with "foo (time in seconds) (command and arguments)" >#! /bin/sh >pid=$$ >( > sleep "$time" > # use SIGTERM first to allow process to clean up > kill $pid >/dev/null 2>&1 At this point, and I'll admit it's a rare possibility, but not an impossibility, especially on multiprocessor machines, what happens if the $command (see below) has already exited and some other process (possibly on another processor) has begun with the same pid? Or won't that happen while this backgrounded process is tying up the process-group number? > sleep 2 > # if process hasn't died yet, use SIGKILL > kill -$SIGKILL $pid >/dev/null 2>&1 >) & > >exec "$command" I'm trying to implement exactly this control structure in C, now, and one of my least favorite problems is that of getting the status of a process I may not own without having to do system("ps -l##### > /tmp/foo"); /* ##### is the pid */ (or fork-and-exec, etc... Actually, the system() call wouldn't be much of a diseconomy, because it only has to execute the ps(1) once or twice after what may be several hours of sleep()'ing.) I mean, just how the heck does ps(1) do it? Everything I've seen implies it goes through /dev/mem one byte at a time. --Blair "Oh, lovely..."