Print

Print


Follow-up Comment #5, bug #97269 (project xrootd):

Wait. I have found the small test program that I have written when I was
originally writing this forking functionality for CMS, and it works exactly
as expected and documented:


]==> cat fork_somewhat_better.cc         

#include <iostream>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *locker_thread( void * )
{
  std::cout << "Locker - locking the mutex" << std::endl;
  int st = pthread_mutex_lock( &mutex );
  std::cout << "Locker - locked the mutex: " << st << std::endl;
  sleep( 5 );
  std::cout << "Locker - unlocking the mutex" << std::endl;
  st = pthread_mutex_unlock( &mutex );
  std::cout << "Locker - unlocked the mutex: " << st << std::endl;
  sleep( 5 );
}

void prepare()
{
  std::cout << "Prepare [" << getpid() << "]: atfork - locking the mutex" <<
std::endl;
  int st = pthread_mutex_lock( &mutex );
  std::cout << "Prepare [" << getpid() << "]: atfork - locked the mutex: " <<
st << std::endl;
}

void child()
{
  std::cout << "Child [" << getpid() << "]: atfork - unlocking the mutex" <<
std::endl;
  int st = pthread_mutex_unlock( &mutex );
  std::cout << "Child [" << getpid() << "]: atfork - unlocked the mutex: " <<
st << std::endl;
}

void parent()
{
  std::cout << "Parent [" << getpid() << "]: atfork - unlocking the mutex" <<
std::endl;
  int st = pthread_mutex_unlock( &mutex );
  std::cout << "Parent [" << getpid() << "]: atfork - unlocked the mutex: "
<< st << std::endl;
}

int main( int argc, char **argv )
{
  std::cout << "Parent start." << std::endl;
  pthread_atfork( prepare, parent, child ); // <-- atfork
  pthread_t thread;
  pthread_create( &thread, 0, locker_thread, 0 );
  sleep( 1 );

  // child
  if( fork() == 0 )
  {
    std::cout << "Child forked" << std::endl;
    std::cout << "Child: trying to lock the mutex" << std::endl;
    int st = pthread_mutex_lock( &mutex );
    std::cout << "Child: mutex locked: " << st << std::endl;
  }
  // parent
  else
  {
    std::cout << "Parent: trying to lock the mutex" << std::endl;
    int st = pthread_mutex_lock( &mutex );
    std::cout << "Parent: mutex locked: " << st << std::endl;
    std::cout << "Parent: waiting for the thread" << std::endl;
    pthread_join( thread, 0 );
  }

  return 0;
};



]==> ./a.out                             
Parent start.
Locker - locking the mutex
Locker - locked the mutex: 0
Prepare [2138]: atfork - locking the mutex
Locker - unlocking the mutex
Prepare [2138]: atfork - locked the mutex: 0
Child [2151]: atfork - unlocking the mutex
Child [2151]: atfork - unlocked the mutex: 0
Child forked
Child: trying to lock the mutex
Child: mutex locked: 0
Parent [2138]: atfork - unlocking the mutex
Parent [2138]: atfork - unlocked the mutex: 0
Parent: trying to lock the mutex
Parent: mutex locked: 0
Parent: waiting for the thread
Locker - unlocked the mutex: 0


The issue must be elsewhere...

    _______________________________________________________

Reply to this item at:

  <http://savannah.cern.ch/bugs/?97269>

_______________________________________________
  Message sent via/by LCG Savannah
  http://savannah.cern.ch/

########################################################################
Use REPLY-ALL to reply to list

To unsubscribe from the XROOTD-DEV list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=XROOTD-DEV&A=1