How to parallelize string search in a file using fork? (GNU Linux / g ++)

I have a text file with several lines, and I am looking for a line in this file. I need to pass the following command line parameters to the program:
- the path to the file
- the line I'm looking for, - - the maximum number of processes that the program allows the "plug" to perform this task.

How should such a program be built?

+3
source share
4 answers

A few thoughts.

  • , , , ( , . , ...).
  • , - / .

, , (, - )...


? . . .

+3

, , , - . , , . (, ).

+2

, , ( ?), ( ) - :

  • (, fopen, fseek (, END), fclose)
  • : :
    startIndex = indexOfProcess * fileSize / numberOfProcesses
    endIndex = (indexOfProcess + 1) * fileSize / numberOfProcesses
    
    , ( ).
  • open the file in each process (in read mode), fseek to the start index, find the line as if you had one file of size (endIndex - startIndex), and upload the results to the screen (or if you have more specific requirements, tell us about them).
+1
source

Either this is homework, or it is useless. The bottleneck is disk bandwidth, not processor power. You will only slow down using simultaneous access.

+1
source

Source: https://habr.com/ru/post/1698174/


All Articles