Which is the optimum number of threads in a 2 core system, supposing that it has to: 1) read a file from HDD 2) encrypt the file by xor-ing its bytes with a secret key 3) transfer the encrypted file on a network
I think that 2) can be done in parallel, 3) can be done in parallel but as I understand 1) cannot be done in parallel, so the system has to use only 1 core to read the file into RAM, then 2) and 3) can be done using 2 cores. Am I correct?
See file reading is not a CPU bound process its IO bound process. So its handled directly by the DMA controllers only. Anyway all those file operations should be handled in single threaded only.
You can parallize the xoring operation after read the contents from the file by separate thread. Even you can place place your file junks in one queue and the multi-threaded x-oring thread will read the content from queue and do its operation and send the result back to the network. If you are using async socket channel then it wont block you so each x-oring thread could place its contents in that channel.
Thats it.