
Need advice/help with fork()
[>I'd like function parse to do something like this:
[>function parse() {
[> parse_input();
[> for() /* start itself again in the background */
[>}
[>so both it and the play() function can run asyncronously.
[>Yes, they both need to access the queue ... which needs
[>to keep the correct information.
------- Well, to have several processes access the same memory
you need to setup a shared memory area. I have some code to do
that, I can send it to you if you want. It basically involves the
use of shmget/shmat/shmdt/shmctl to setup/attach/detach/delete
shared memory.
Using fork is a bit tricky at first. fork () creates another process
that is totally identical (with some exceptions like pipes, etc) to
the process that created it. So how do you distinguish the two?
fork () will return the process id of the child to the parent, and
NULL to the child. After that the two processes can start doing
different things.
int main ()
{
long
temp;
temp = fork ();
if (temp == NULL) {
printf ("I am the child process [%d]\n", getpid ());
/* Do stuff */
} else {
printf ("I am the parent process of process [%d]\n", temp);
/* Do other stuff */
wait (); /* Wait for child to exit first */
}
return (0);
Quote:
}
--
Mariusz Zydyk http://www.ucalgary.ca/~mszydyk/