can we do fork() in c#

L

lucifer

hi
i have code that i have to implement in c#

Code:

/* Become deamon + unstopable and no zombies children
(= no wait()) */
if(fork() != 0)
return 0; /* parent returns OK to shell */
(void)signal(SIGCLD, SIG_IGN); /* ignore child death */
(void)signal(SIGHUP, SIG_IGN); /* ignore terminal hangups */
for(i=0;i<32;i++)
(void)close(i); /* close open files */
(void)setpgrp(); /* break away from process group
*/


if((pid = fork()) < 0) {
log(ERROR,"system call","fork",0);
}
else {
if(pid == 0) { /* child */
(void)close(listenfd);
web(socketfd,hit); /* never returns */
} else { /* parent */
(void)close(socketfd);
}
}
}


i know the code is creating child process mine question is can we do
fork in c# or is there any other method to do so eg threading etc
 
J

jmcgrew

I don't think you need to fork at all. The C code you posted only uses
fork() to become a daemon, that is, a background process. That's so you
can start the program at the Unix prompt and have it immediately return
to the prompt, even though the daemon keeps running.

In Windows, you can just start a program and let it run in the
background with no extra work needed. Even from the command prompt, if
you type "notepad", you immediately get another prompt.

Jesse
 
F

Frans Bouma [C# MVP]

lucifer said:
hi
i have code that i have to implement in c#

Code:

/* Become deamon + unstopable and no zombies children
(= no wait()) */
if(fork() != 0)
return 0; /* parent returns OK to shell */
(void)signal(SIGCLD, SIG_IGN); /* ignore child death */
(void)signal(SIGHUP, SIG_IGN); /* ignore terminal hangups */
for(i=0;i<32;i++)
(void)close(i); /* close open files */
(void)setpgrp(); /* break away from process group
*/


if((pid = fork()) < 0) {
log(ERROR,"system call","fork",0);
}
else {
if(pid == 0) { /* child */
(void)close(listenfd);
web(socketfd,hit); /* never returns */
} else { /* parent */
(void)close(socketfd);
}
}
}


i know the code is creating child process mine question is can we do
fork in c# or is there any other method to do so eg threading etc

You can mimic fork, which simply spawns another thread at the same
position, by using threads indeed. Simply start a thread which actually
runs your program and let the main thread return.

FB


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top