J
JoeW
Now before I go into detail I just want to say that this is purely for
my own benefit and has no real world usage. I remember way back when
the tool for *nix systems called forkbomb was created. I recently
explained it to a friend of mine and at that time decided to see if I
could replicate it in C#. I have created an application that, to me at
least, mimics what fork would/should do on *nix/bsd systems. I know
that fork spawns a new process, basically identical to the parent
process. For C# this was rather easy, at least getting a new process
started. What I have is this.
while (rc >= 0)
{
rc = fork();
if (rc == 0) break;
}
static int fork()
{
int pid = Process.GetCurrentProcess().Id;
int val = 0;
try
{
Console.WriteLine("Pid {0}", pid);
string fileName =
Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost",
"");
ProcessStartInfo info = new ProcessStartInfo(fileName);
info.UseShellExecute = false;
Process processChild = Process.Start(info);
val = 1;
}
catch
{
val = 0;
}
return val;
}
Now when I run this, all hell breaks loose and my system will get to
around 1000 processes in roughly 20 seconds and then crash. Is this
essentially what fork() would do in the above mentioned environments?
Or what would be a better solution to this?
Thanks in advance
Joe
my own benefit and has no real world usage. I remember way back when
the tool for *nix systems called forkbomb was created. I recently
explained it to a friend of mine and at that time decided to see if I
could replicate it in C#. I have created an application that, to me at
least, mimics what fork would/should do on *nix/bsd systems. I know
that fork spawns a new process, basically identical to the parent
process. For C# this was rather easy, at least getting a new process
started. What I have is this.
while (rc >= 0)
{
rc = fork();
if (rc == 0) break;
}
static int fork()
{
int pid = Process.GetCurrentProcess().Id;
int val = 0;
try
{
Console.WriteLine("Pid {0}", pid);
string fileName =
Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost",
"");
ProcessStartInfo info = new ProcessStartInfo(fileName);
info.UseShellExecute = false;
Process processChild = Process.Start(info);
val = 1;
}
catch
{
val = 0;
}
return val;
}
Now when I run this, all hell breaks loose and my system will get to
around 1000 processes in roughly 20 seconds and then crash. Is this
essentially what fork() would do in the above mentioned environments?
Or what would be a better solution to this?
Thanks in advance
Joe