473,732 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetCurr entProcess().Id ;
int val = 0;

try
{
Console.WriteLi ne("Pid {0}", pid);

string fileName =
Process.GetCurr entProcess().Ma inModule.FileNa me.Replace(".vs host",
"");

ProcessStartInf o info = new ProcessStartInf o(fileName);

info.UseShellEx ecute = false;

Process processChild = Process.Start(i nfo);

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 12230
On Sun, 08 Apr 2007 20:12:00 -0700, JoeW <te********@gma il.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********@gma il.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.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.GetCurr entProcess().Id ;
int val = 0;

try
{
Console.WriteLi ne("Pid {0}", pid);

string fileName =
Process.GetCurr entProcess().Ma inModule.FileNa me.Replace(".vs host",
"");

ProcessStartInf o info = new ProcessStartInf o(fileName);

info.UseShellEx ecute = false;

Process processChild = Process.Start(i nfo);

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...@nn owslpianmk.com>
wrote:
On Sun, 08 Apr 2007 20:12:00 -0700, JoeW <teh.sn1...@gma il.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********@gma il.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
8817
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
4079
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 exit. The child do some thing like to read different file at the same time.
4
2890
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 server.(once fork start, counter will reset). This allow me to communicate with my counter.....pls help and thanks a lot
6
2395
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
5802
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
5978
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(); value = value + 2; number = number + 2;
1
8258
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 file1, equal to the command cat <file1> I need to use execvp() and fork() system calls to create a new process that will type/cat any text file. In my code below, the execvp() call in the function at the bottom gets me the following: ...
3
5686
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 process I launch with execv the pppd to connect an embedded system to internet. The problem is that pppd fails. I have made two test program just to discard bugs (my applications is really big) and the result is that when I call execv from a...
4
1694
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; please do check that link) and please do respond... I know there are quite a number of members of progav in here... This is one query I need resolved asap... So, please
0
8944
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.