fork and file descriptor

From: Jacky Cheung (jackylb_at_yahoo.com)
Date: 03/08/05


Date: Tue, 08 Mar 2005 11:39:54 +0800

Hi all,

My program has a main loop and a thread.

The main loop is used to fork a child and the thread is used write data
to dsp.

However I find that there is a cases when the thread opened the dsp, and
at that time the main loop fork a child out. This made the child has the
fd of dsp. So I am thinking to use mutex to make sure dsp is opened in
the child.

Here is my way:

Thread:

mutex lock
        open dsp
        write data to dsp
        close dsp
mutex unlock

main loop:

mutex lock
        if((child_pid=fork())<0){
                DEBUG(0, "fork error \n");
        }else if(child_pid==0){ //child
                //do sth here
        }else{ //parent
mutex unlock
        }

Is it the right location to place the mutex unlock under the parent
process in the main loop? Also does the child need to hanlde the mutex lock?

Thx a lot.

Jacky



Relevant Pages

  • Re: Ruby lacks atfork : The evil that lives in fork...
    ... so that there's no way to ensure mutex to share ... Give the resource to the child. ... fork() in a multi-threaded program. ... multi-threaded I/O libraries, which are almost sure to be invoked ...
    (comp.lang.ruby)
  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: Why lock-free implementations are faster than traditional using synchronization? Are they?
    ... Each time a thread is held at a mutex it looses its current time slice. ... while (!cmpxchg(&ptr, oldPtr, newValue)); ... Most platforms offer trivial operations like x+value, x-value, x&value etc. directly without a loop. ... This is where the second advantage of e compare exchange loop comes into play. ...
    (comp.programming.threads)
  • Re: EPERM from pthread_mutex_unlock after fork using pthread_atfork()
    ... So, if the fork() creates a copy of the parent, the child should get a copy of the mutex as well. ... it should be locked in the child process and the parent process once the forkprocessing is complete. ...
    (comp.programming.threads)
  • (another) datarelation datagrid question
    ... in a manner described best by "How to Display Child Table Rows as a Column ... that i would like bound to the final grid. ... inside that loop i loop thru the child table and add the data from the ... look exactly as i want and as a bonus it filters parent records without ...
    (microsoft.public.dotnet.framework.adonet)