Quote:
Originally posted by paul
Can I do other things until the fork ends?
|
fork() creates an entire new process (which is identical to the old process). the only difference between them is the value they get from the fork() function call.
now that you've forked, you want to exec() a new program. exec throws out the currently running process (in this case your child process) and replaces it with a new one (mpg123, in your case).
you seem to have gotten this far.
...but wait, there's more.
the forked processes are independant of each other. they can both run at the same time. however, there is a special relationship between a parent and child process in unix. when the child exits, it sends to the parent a signal (SIGCHLD) that the child has quit, and the child becomes a zombie process until reaped by the parent. you will need to implement a signal(2) handler to notify the parent of when the child exited and then use something like waitpid(2) to clean up the child.
...and more. when the parent process exits or dies, unix automatically kills all children processes. uncoupling them is somewhat of a mess, and i'm not going to cover it.
btw, using system() would not have worked, as it would have stalled your process until the program you are running exited. calling system() on a filetype (and not an executable, as suggested above), is a bad idea! you do not know which program will actually be called to handle that file type (or even if they are associated with anything on the current system).