Windows get number of spawns of a single process?

Programming, for all ages and all languages.
Post Reply
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Windows get number of spawns of a single process?

Post 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. :P)

Thanks,
Troy
Last edited by Troy Martin on Sat Nov 15, 2008 8:19 pm, edited 1 time in total.
Image
Image
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 wish I could add more tex
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: C function to get pid from filename?

Post 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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: C function to get pid from filename?

Post 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.
Image
Image
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 wish I could add more tex
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: C function to get pid from filename?

Post 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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Windows get number of spawns of a single process?

Post by Troy Martin »

I think I'm going to stick to Windows and use taskkill and friends to do this.

Thanks though, pcmattman!
Image
Image
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 wish I could add more tex
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Windows get number of spawns of a single process?

Post 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
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Windows get number of spawns of a single process?

Post by Troy Martin »

You keep changing your username, Sandeep, it's hard to tell who you are! :P

Thanks for the code, I'll see if there's something similar in the Win32 API.
Image
Image
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 wish I could add more tex
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Windows get number of spawns of a single process?

Post 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():

Code: Select all

while(1)
{
    fork();
}
That'll fill up the RAM and swap with many copies of the same address space.
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: Windows get number of spawns of a single process?

Post by eddyb »

Oh, i remembered... i've made a "paint" bomb(you can change it's name :P ):
bomb.bat:

Code: Select all

@echo off
paint
bomb
have fun with it :D
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Windows get number of spawns of a single process?

Post 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.
Image
Image
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 wish I could add more tex
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: Windows get number of spawns of a single process?

Post by eddyb »

so, there is bomb, there isn't! :D
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Windows get number of spawns of a single process?

Post 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)
madeofstaples
Member
Member
Posts: 204
Joined: Thu Apr 12, 2007 8:15 am
Location: Michigan

Re: Windows get number of spawns of a single process?

Post 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?
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.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Windows get number of spawns of a single process?

Post by Troy Martin »

Yeah, that's what I'm thinking! Thanks!
Image
Image
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 wish I could add more tex
Post Reply