pcntl_fork example

public function testBlockingLocks()
    {
        // Amount of microseconds we should wait without slowing things down too much         $clockDelay = 50000;

        $key = new Key(uniqid(__METHOD__, true));
        $parentPID = posix_getpid();

        // Block SIGHUP signal         pcntl_sigprocmask(\SIG_BLOCK, [\SIGHUP]);

        if ($childPID = pcntl_fork()) {
            // Wait the start of the child             pcntl_sigwaitinfo([\SIGHUP]$info);

            $store = $this->getStore();
            try {
                // This call should failed given the lock should already by acquired by the child                 $store->save($key);
                $this->fail('The store saves a locked key.');
            } catch (LockConflictedException $e) {
            } finally {
                // send the ready signal to the child

    $directories = [];
    for ($i = 1; $i <= 10; $i++) {
      $directories[] = 'public://a/b/c/d/e/f/g/h/' . $i;
    }

    $file_system = $this->container->get('file_system');

    $time_to_start = microtime(TRUE) + 0.1;
    // This loop creates a new fork to create each directory.     foreach ($directories as $directory) {
      $pid = pcntl_fork();
      if ($pid == -1) {
        $this->fail("Error forking");
      }
      elseif ($pid == 0) {
        // Sleep so that all the forks start preparing the directory at the same         // time.         usleep((int) (($time_to_start - microtime(TRUE)) * 1000000));
        $file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
        exit();
      }
    }

    
$functions[] = 'set';
      $functions[] = 'getAll';
    }

    $default_connection = Database::getConnectionInfo();
    Database::removeConnection('default');

    $time_to_start = microtime(TRUE) + 0.1;

    // This loop creates a new fork to set or get key values keys.     foreach ($functions as $i => $function) {
      $pid = pcntl_fork();
      if ($pid == -1) {
        $this->fail("Error forking");
      }
      elseif ($pid == 0) {
        Database::addConnectionInfo('default' . $i, 'default', $default_connection['default']);
        Database::setActiveConnection('default' . $i);
        // Create a new factory using the new connection to avoid problems with         // forks closing the database connections.         $factory = new KeyValueDatabaseFactory($this->container->get('serialization.phpserialize'), Database::getConnection());
        $store = $factory->get('test');
        // Sleep so that all the forks start at the same time.
Home | Imprint | This part of the site doesn't use cookies.