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
Last edited by Troy Martin on Sat Nov 15, 2008 8:19 pm, edited 1 time in total.
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
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.
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
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.
I think I'm going to stick to Windows and use taskkill and friends to do this.
Thanks though, pcmattman!
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
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
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.
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
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.
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
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)
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?
Some people are offended by the verifiable truth; such people tend to remain blissfully unencumbered by fact.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.