can someone explain to me, why do I need the fork()?
I know it creates a child process, but... what for? what does it do, and how does it help me with anything?
Thanks.
can someone explain to me, why do I need the fork()?
I know it creates a child process, but... what for? what does it do, and how does it help me with anything?
Thanks.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
Ever needed to split your application in two? Threads are one way, forking is another. Both have different effects.
Try writing a command line shell, that is normally implemented using fork/exec. There are many posts on here about that subject.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
so.. if I understand it right, it just copies all the variables in the parent process into the child's... that's it??? I can use a function for that, no?
Thanks.
Last edited by Devil Panther; 09-19-2003 at 02:19 PM.
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
It creates a second running process that shares and inherits from the parent.
There's an example here
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
but it doesn't really do much.
You could for example, create a menu program that ask the user to pick an option, then the program forks() and performs the selected task, whilst the menu program resumes, giving control back to the user.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
This means that the parent process goes on and does everything... while the child is running and doing something else?
So, this means that I can create more than one child process?
I kill the child process with exit(), but if I can create more than one, how do I kill process 1 instead of 2?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
>how do I kill process 1 instead of 2?
You mean kill the parent without killing the child or creating a zombie? Funny you should ask that, I answered that exact question in a previous thread about creating daemons. Daemons and zombies and forks, oh my!
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
Please take note that I am not a technical writer, nor do I care to become one.
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
Don't assume that I'm ever entirely serious or entirely joking.
I did read the this post, after tI asked
but still... my question was: can I create more than one child?
except that, are the other things I asked correct?
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.Bloodware.net - Developing free software for the community.
>can I create more than one child?
Sure. And you can do it several ways too. You can have each child have a child:
You can have a single parent with several children:Code:for (i = 1; i < n; i++) { if ((child = fork()) != 0) break; }
You can go completely nuts and go completely nuts by having every child loop forever and create children :Code:for (i = 1; i < n; i++) { if ((child = fork()) <= 0) break; }
>are the other things I asked correct?Code:for ( ; ; ) fork();
Pretty much, yea.
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
Please take note that I am not a technical writer, nor do I care to become one.
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
Don't assume that I'm ever entirely serious or entirely joking.