Page 1 of 1
Windows get number of spawns of a single process?
Posted: Sat Nov 15, 2008 7:37 pm
by Troy Martin
Is there a function to get a pid from a filename? I'm used to doing C under Windows and not POSIX-compliant systems.
OK, not looking for that anymore. I need something to get the number of spawns of the same process. On Windows.
(I'm writing a fork bomb killer for fun.
)
Thanks,
Troy
Re: C function to get pid from filename?
Posted: Sat Nov 15, 2008 7:41 pm
by pcmattman
Why would a filename have a PID?
Unless you're talking about a "<app>.pid" file, which is just a normal text file with the PID stored as the only content.
Re: C function to get pid from filename?
Posted: Sat Nov 15, 2008 7:57 pm
by Troy Martin
Sorry if I wasn't clear, I mean get the PID of a process that I know the filename of.
Or, if better, get the PID of any process that has >10 copies running at one time.
Re: C function to get pid from filename?
Posted: Sat Nov 15, 2008 8:01 pm
by pcmattman
Oh, right.
I'm not 100% sure how to go about that with a *nix system because I haven't spent much time programming on one (I'm a Windows programmer too, the closest I get to *nix programming is my OS user software packages).
I'm sure there's a command you could run that would show you what's running, and then you'd need to parse that.
Re: Windows get number of spawns of a single process?
Posted: Sat Nov 15, 2008 8:10 pm
by Troy Martin
I think I'm going to stick to Windows and use taskkill and friends to do this.
Thanks though, pcmattman!
Re: Windows get number of spawns of a single process?
Posted: Sun Nov 16, 2008 1:09 pm
by DeletedAccount
In C# following pice of code will do the trick l
Code: Select all
using System.Diagnostics ;
.....
....
public void some_func()
{
Process p = Process.GetProcessbyName("procname");
Console.WriteLine(p.Id.ToString());
}
The Win32 api based soln may take a bit more time, i do not have any reference handy right now
Regards
Sandeep
Re: Windows get number of spawns of a single process?
Posted: Sun Nov 16, 2008 1:20 pm
by Troy Martin
You keep changing your username, Sandeep, it's hard to tell who you are!
Thanks for the code, I'll see if there's something similar in the Win32 API.
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 12:07 am
by pcmattman
There's no real ability to "fork" in the Windows API (though there is a function that does a
similar action).
A fork bomb should just involve constantly calling fork():
That'll fill up the RAM and swap with many copies of the same address space.
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 12:46 am
by eddyb
Oh, i remembered... i've made a "paint" bomb(you can change it's name
):
bomb.bat:
have fun with it
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 9:53 am
by Troy Martin
Not sure if I mentioned this, but this fork bomb is meant to kill off other fork bombs. It needs to detect if there are any processes with >15 spawns and kills them all before they can crash the machine.
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 10:19 am
by eddyb
so, there is bomb, there isn't!
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 11:13 am
by ru2aqare
I don't know if any Windows API exists to query how many times a process has called the CreateProcess function, but you could inject a DLL into the target process and have it patch/redirect the CreateProcess function... Dll redirection won't work, since this function is located in kernel32.dll, and that's on the "known DLLs list" (list of DLLs for which LoadLibrary uses only the %windir%\System32 or %windir%\SysWOW64 directory)
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 4:28 pm
by madeofstaples
See
this example.
simply count the number of processes that list the executable in question as a module, remember their process IDs, if the count is too large (or, perhaps it'd be better to test if the count increases ridiculously quickly), terminate the list of process IDs associated with the executable.
Is that what you're looking for?
Re: Windows get number of spawns of a single process?
Posted: Mon Nov 17, 2008 7:08 pm
by Troy Martin
Yeah, that's what I'm thinking! Thanks!