0

I have some code that needs to benchmark multiple algorithms. But before they can be benchmarked they need to get prepared. I would like to do this preparation multi-threaded, but the benchmarking needs to be sequential. Normally I would make threads for the preparation, wait for them to finish with join and let the benchmarking be done in the main thread. However the preparation and benchmarking are done in a seperate process after a fork because sometimes the preparation and the benchmarking may take too long. (So there is also a timer process made by a fork which kills the other process after x seconds.) And the preparation and benchmarking have to be done in the same process otherwise the benchmarking does not work. So I was wondering if I make a thread for every algorithm if there is a way to let them run concurrently until a certain point, then let them all wait untill the others reach that point and then let them do the rest of the work sequentially.

Here is the code that would be executed in a thread:

void prepareAndBenchmark(algorithm) {
    //The timer thread that stops the worker after x seconds
    pid_t timeout_pid = fork();
    if (timeout_pid == 0) {
        sleep(x);
        _exit(0);
    }
    //The actual work
    pid_t worker_pid = fork();
    if (worker_pid == 0) {
        //Concurrently:
        prepare(algorithm)
        //Concurrently up until this point
        //At this point all the threads should run sequentially one after the other:
        double result = benchmark(algorithm)
        exit(0);
    } 
    int status;
    pid_t exited_pid = wait(&status);
    if (exited_pid == worker_pid) {
        kill(timeout_pid, SIGKILL);
        if(status == 0) {
            //I would use pipes to get the result of the benchmark.
        } else {
            //Something went wrong
        }
    } else {
        //It took too long.
        kill(worker_pid, SIGKILL); 
    }
    wait(NULL); 
}

I have also read that forking in threads migth give problems, would it be a problem in this code?

I think I could use mutex to have only one thread benchmarking at a time, but I don't want to have a thread benchmarking while others are still preparing.

Tkbdsl3cbB
  • 13
  • 3
  • Sounds like a `barrier_wait`. – Mark Setchell Aug 03 '20 at 18:19
  • 1
    Use a condition variable along with a counter that gets incremented by each thread when it reaches the checkpoint. Then when the count gets high enough, start the benchmarking serialized by a mutex. – TainToTain Aug 03 '20 at 19:05
  • But will that work inside of a fork()? – Tkbdsl3cbB Aug 03 '20 at 21:14
  • Came here to essentially say the same thing as @TainToTain . Given you can [use shared memory](https://stackoverflow.com/a/13274800/3735890) between forked processes, you should be able to more or less use the same method – DragonJawad Aug 04 '20 at 21:10
  • 1
    @Tkbdsl3cbB pthread supports [interprocess primitives](https://stackoverflow.com/a/20326345/6392253). But I thought you wanted to make each algorithm to benchmark run in a separate thread instead of a separate process. – TainToTain Aug 04 '20 at 23:25

0 Answers0