Need advice/help with fork() 
Author Message
 Need advice/help with fork()

I'm new to multiprocess design and have been working on
a real-time computer music environment that has come of
age to use it.

Essentially, I'd like to do the following:

/* Function that parses input and puts things on */
/* a queue to be played by function play() */

function parse();

/* Pops off whatever there is on queue() and plays it */

function play();

I'd like function parse to do something like this:

function parse() {
        parse_input();
        for() /* start itself again in the background */

Quote:
}

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.

Any help/input would be appreciated.

DT
--
David Topper (me)
Research Assistant
Columbia University Center for Computer Music
Student of Computer Music, Science, Art & Life
http://www.*-*-*.com/ ~topper



Wed, 08 Sep 1999 03:00:00 GMT  
 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/



Wed, 08 Sep 1999 03:00:00 GMT  
 Need advice/help with fork()

Quote:

>I'm new to multiprocess design and have been working on
>a real-time computer music environment that has come of
>age to use it.

The best newsgroup to ask about fork() is comp.unix.programmer. Neither
C nor C++ define a fork() function so the 2 language newsgroups aren't
relevant to your question.

Quote:

>Essentially, I'd like to do the following:

>/* Function that parses input and puts things on */
>/* a queue to be played by function play() */

...

Quote:
>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.

It sounds like you need sone sort of inter-process communication. Again
comp.unix.programmer is a good place to ask about this for Unix platforms
(including Linux).

--
-----------------------------------------


-----------------------------------------



Wed, 08 Sep 1999 03:00:00 GMT  
 Need advice/help with fork()

Quote:

> 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.

The fork() system_call returns :

-1 -    in case of error.
 0 -    if the current context is the child process.
 child pid -    if the current context is the parent.

So if I understand you correctly, you wanna do something like this:

int parse()
{
        parse_args();
        pid = fork();
        if(pid == -1) {
                perror("fork");
                return(-1);
        }
        if(!pid ) { /* child process context */
                play();
                exit(0);
        }
        /* continue execution of parent process */

Quote:
}

However, queue manipulation should probably be done by the parent
process if you don't want a complete mess.


Thu, 16 Sep 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Need urgent help with this (fork, dup, pipes)

2. URGENT : Help needed with system()/fork() call

3. Unix: Need help with fork() or other methods of multithreading

4. need help with ?fork()?

5. Help needed with concurrent programing: ie Fork()

6. Need Help/Advice With This C Program...

7. Advice Please Really Need Help! :)

8. C enigmas - need help/advice

9. Need Help with array...Please Advice

10. NEED advice with writing help files!

11. fork without fork !!

12. Need fork functionality in CreateProcess

 

 
Powered by phpBB® Forum Software