Shell Script for multithreading a process

后端 未结 4 1643
失恋的感觉
失恋的感觉 2020-12-22 04:07

I am a Bioinformatician and recently stuck in a problem which requires some scripting to speed up my process. We have a software called PHASE and Command that i type in my c

4条回答
  •  無奈伤痛
    2020-12-22 05:13

    Bash does not support multi-threading, however it does support multi-processing. If you change your command to be:

    for i in {1..1000}; do
        ./PHASE test$i.inp test$i.out &
    done
    

    This will run each one a process and your computer will automatically schedule them based on how many core you have. 1000 Processes will have a lot of overhead compared to threads, but while not ideal it should still be fine.

    Edit: Here is a more advanced method if you want to prioritize getting progressive answers:

    #!/bin/bash
    # Number of cores and range end
    n=4
    e=1000
    
    # This function will do the processing
    process() {
        for ((i=$1; i <= $3; i += $2)); do
            ./PHASE test${i}.inp test${i}.out
            echo "Done $i"
        done
    }
    
    # For each core create a process and record the pid
    for ((i=1; i <= n; i++)); do
        process $i $n $e &
    done
    
    # Wait for each process to complete
    wait
    

提交回复
热议问题