473,411 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

C# Fork Help

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.Re place(".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

Apr 9 '07 #1
5 12194
On Sun, 08 Apr 2007 20:12:00 -0700, JoeW <te********@gmail.comwrote:
[...]
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?
Sort of. That is, it's been a very long time since I did any Unix
programming, but it's my understanding that Unix's idea of a process and a
thread is much more lightweight than those concepts in Windows. As such,
you can probably get quite a bit further growing the number of processes,
and possibly get far enough that the number of processes stabilizes.
Still, eventually even on a Unix computer it's possible that you'd run out
of resources and at a minimum, not be able to start any more processes (at
least not until the earlier ones have had a chance to finish running and
exit).

For what it's worth, I think your program would be more "bomb-like" :) if
each process created two more processes, rather than just one. ;)

I don't believe that Windows should actually crash when you reach the
maximum number of processes, but I can easily believe that it does. It's
been my experience that Windows itself (at least the NT-based line, which
includes XP and Vista) is generally pretty stable, but vulnerable to
poorly-written third-party components. In particular, drivers can very
easily send the OS into la-la land and cause it to crash. IMHO, an
interesting experiment would be to boot Windows into safe mode and run
your program and see what happens there. (Of course, it's always possible
that it is in fact Windows itself that's vulnerable to this condition...I
wouldn't rule anything out without testing it myself).
Or what would be a better solution to this?
Define "better solution". What are you trying to accomplish? Wantonly
creating thousands of new processes without limit isn't much of a
"solution" to anything, except overloading and possibly crashing your
computer. So I'd say that your current code does that pretty well. :)

If you have a different objective, then you should explain what that
objective is, and then perhaps someone can comment on whether there's a
"better solution" for that objective or not.

Pete
Apr 9 '07 #2
"JoeW" <te********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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.Re place(".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


Actually your system won't get at 1000 processes running, the system reserves a process
control structure but doesn't actually load and initializes that process before
Process.Start returns. So, the PID's you see don't reflect the running process,
The reason for the chaotic behavior is the result of a lack of resources, Windows cannot
create that many processes especially not that many "CLR" processes.
Each CLR process start with 3 threads , each having has a default stack of 1MB (committed),
add to that ~5MB of non shared memory for the program (CLR version dependant), so you easily
end up with a minimum of ~8MB Virtual memory per process.
Now, say that you have 2GB of RAM with a total of 4GB of VM available, that means that the
system starts thrashing even before 250 processes get created, and becomes highly
unresponsive.
When the system reaches it's max. VM size occupation, "hell breaks loose". The OS will try
to keep control, but it can no longer create processes, worse, it can no longer "initialize"
the already created processes, instead it starts recovery actions, displaying dialogs with
all kind of failure messages , possibly initiating DrWatson(s) (yet another process), which
will now require more resource like window handles and non-pageable pool memory for the USER
objects for the dialogs, but as I said your system has become highly unresponsive, you can
hardly act upon the dialog requests, and even if you can click a button and as such free a
resource, the system will automatically reassign the resource with another one, finally you
end with a system that has so many user requests pending that the system itself has
exhausted it's non pageable pool and the USER objects, you get the "false" impression that
the system crashed, but it has not (at least not on XP and higher).
If you can manage to respond to all pending request, it should be possible bring the system
back to a state where you can kill the running processes and finally get back to a normal
state.
Note that above is true for 64 bit system as well, at some time you will hit a wall, or the
RAM limit, the VM limit, the non-pageable pool and USER object resource limit and possibly
another one. Even with 8GB RAM and 12GB VM space, I wasn't able to create 1000 processes (on
W2K3 64bit) using the sample you posted, although, I was able to recover.

Willy.

Apr 9 '07 #3
On Apr 9, 1:06 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 08 Apr 2007 20:12:00 -0700, JoeW <teh.sn1...@gmail.comwrote:
[...]
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?

Sort of. That is, it's been a very long time since I did any Unix
programming, but it's my understanding that Unix's idea of a process and a
thread is much more lightweight than those concepts in Windows. As such,
you can probably get quite a bit further growing the number of processes,
and possibly get far enough that the number of processes stabilizes.
Still, eventually even on a Unix computer it's possible that you'd run out
of resources and at a minimum, not be able to start any more processes (at
least not until the earlier ones have had a chance to finish running and
exit).

For what it's worth, I think your program would be more "bomb-like" :) if
each process created two more processes, rather than just one. ;)

I don't believe that Windows should actually crash when you reach the
maximum number of processes, but I can easily believe that it does. It's
been my experience that Windows itself (at least the NT-based line, which
includes XP and Vista) is generally pretty stable, but vulnerable to
poorly-written third-party components. In particular, drivers can very
easily send the OS into la-la land and cause it to crash. IMHO, an
interesting experiment would be to boot Windows into safe mode and run
your program and see what happens there. (Of course, it's always possible
that it is in fact Windows itself that's vulnerable to this condition...I
wouldn't rule anything out without testing it myself).
Or what would be a better solution to this?

Define "better solution". What are you trying to accomplish? Wantonly
creating thousands of new processes without limit isn't much of a
"solution" to anything, except overloading and possibly crashing your
computer. So I'd say that your current code does that pretty well. :)

If you have a different objective, then you should explain what that
objective is, and then perhaps someone can comment on whether there's a
"better solution" for that objective or not.

Pete
Well by "better solution" I mean more efficient, as of now it only
creates a single new process each time. To me this is a very simple
recursive solution but I want something like O(2^n), you know 1 makes
2, 2 makes 4, etc. Again I know you guys probably have better things
to do than help me 'forkbomb" my system but its all for the sake of
learning and I thank you for the help.

Apr 9 '07 #4
On Mon, 09 Apr 2007 07:30:03 -0700, JoeW <te********@gmail.comwrote:
Well by "better solution" I mean more efficient, as of now it only
creates a single new process each time. To me this is a very simple
recursive solution but I want something like O(2^n), you know 1 makes
2, 2 makes 4, etc. Again I know you guys probably have better things
to do than help me 'forkbomb" my system but its all for the sake of
learning and I thank you for the help.
Well, if you find that 20 seconds is too long a time to wait for your
computer to become unusable, then as I mentioned you could simply start
two processes in your code instead of one. Though, even with that change
I think that you will find that the program has no more practical value
than the way it is now. :)

Note that the "O" notation can't be used to describe what you want. Even
adding a second call to start a second process in your code, the code
itself is still basically constant order. This isn't important here, but
if and when you start using "O" notation to describe actual algorithms, it
will be important for you to understand how it's used and you've used it
incorrectly here.

Pete
Apr 9 '07 #5
JoeW wrote:
>[...]
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?
If I remember correctly fork starts a new process on Unix systems, which
has the same memory content as it's parent process and the application
continues processing by returning from the fork function call in both
processes. In contrast to Windows, where a new started process will
start at it's main entry function and which hasn't "copied" or better
said mapped all the memory of it's parent process.

To really simulate fork() you would have to dig somewhat deeper (memory
handling etc.). In managed applications it's somewhat easier to share
memory between assembly instances, though it's still not the same as
fork accomplishes.

Or what would be a better solution to this?
In Windows you IMHO should use threads. They will share the same memory,
so are somewhat comparable to fork, but they will continue to share
memory IIRC in contrast to fork.
And to simulate fork returning 2 threads you would also have to dig very
deep into Windows internals, meaning you would have to start a new
"child thread" with the same stack as it's parent thread.

To make a long story short: IMHO fork cannot be simulated *easily* in
Windows. Use threading in both operating systems.
Thanks in advance
Hope it helps.
Joe
Andre
Apr 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: C Gillespie | last post by:
Dear All, I have a function def printHello(): fp = open('file','w') fp.write('hello') fp.close() I would like to call that function using spawn or fork. My questions are:
2
by: CwK | last post by:
How to use fork() system function to fork multi child process at the same time ? For example: Run a program to fork 5 child process at the same time and the parent must wait until all child...
4
by: marconi | last post by:
hallo there, Can anyone help me to look at my fork problems? I can't seem to work up the piping..the reason i need that for is because i need to make a counter global to client and...
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
16
by: mishra | last post by:
Hi, i thied the following code.. # include<stdio.h> int main() { int a; printf("Hello..."); a=fork(); printf("hi\n"); return(0);
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
4
by: AK | last post by:
Hi everyone, Please check out this query that I have in : http://groups.google.com/group/progav/browse_thread/thread/29057ec587dd46b3 (its a bit long, thats why I am not typing it again;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.