int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { const ACE_TCHAR *name = argc == 1 ? ACE_TEXT("hello") : argv[1]; int iterations = argc > 2 ? ACE_OS::atoi (argv[2]) : 100; ACE_Process_Semaphore pm (1, name); ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); ACE_UNUSED_ARG (sa); for (int i = 0; i < iterations && !done; i++) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquiring\n")); if (pm.acquire () == -1) ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "acquire failed")); else ACE_DEBUG ((LM_DEBUG, "(%P|%t) = acquired\n")); ACE_OS::sleep (3); if (pm.release () == -1) ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); else ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); if (pm.tryacquire () == -1) ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "tryacquire failed")); else ACE_DEBUG ((LM_DEBUG, "(%P|%t) = tryacquire\n")); if (pm.release () == -1) ACE_DEBUG ((LM_DEBUG, "(%P|%t) = %p\n", "release failed")); else ACE_DEBUG ((LM_DEBUG, "(%P|%t) = released\n")); } if (argc > 2) pm.remove (); return 0; }
int ACE_TMAIN (int, ACE_TCHAR *[]) { // Create locked ACE_Process_Semaphore s (0, ACE_TEXT ("AceTest")); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Worker is going to acquire semaphore\n"))); s.acquire (); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Worker has acquired semaphore\n"))); // Do some init stuff. Simulated by a sleep... ACE_OS::sleep(10); s.release(); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Worker released semaphore\n"))); // Do the work... ACE_OS::sleep(5); return 0; }
// Spawns the process, and waits for it to finish booting. // Then uses bind_to_naming_service, get_stream_endpoint, and get_vdev // to get the object references to the various objects created in the // child int TAO_AV_Endpoint_Process_Strategy::activate (void) { ACE_Process process; // Create a new process to contain this endpoint this->pid_ = process.spawn (*this->process_options_); // Process creation failed if (this->pid_ == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) ACE_Process:: spawn failed: %p\n", "spawn"), -1); // Create a unique semaphore name, using my hostname, and pid. ACE_TCHAR sem_str [BUFSIZ]; // create a unique semaphore name ACE_OS::sprintf (sem_str, ACE_TEXT("%s:%s:%ld"), "TAO_AV_Process_Semaphore", this->host_, static_cast<long int> (this->pid_)); ORBSVCS_DEBUG ((LM_DEBUG, "(%P|%t) semaphore is %s\n", sem_str)); // Create the semaphore ACE_Process_Semaphore semaphore (0, // 0 means that the // semaphore is locked initially sem_str); // wait until the child finishes booting while (1) { if (semaphore.acquire () == -1) { // See if my child process is still alive -- if not, return an error if (ACE_OS::kill (this->pid_, 0) == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) Process_Strategy: Process being waited on died unexpectedly.\n"), -1); // if we were not interrupted due to a EINTR, break if (errno != EINTR) break; } else break; } // The job of the semaphore is done, remove it. if (semaphore.remove () == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) semaphore remove failed: %p\n", "remove"), -1); try { // Get ourselves a Naming service this->bind_to_naming_service (); // Get the stream endpoint created by the child from the naming service this->get_stream_endpoint (); // Get the Vdev created by the child from the naming service this->get_vdev (); } catch (const CORBA::Exception& ex) { ex._tao_print_exception ( "TAO_AV_Endpoint_Process_Strategy::activate"); return -1; } return 0; }