Skip to content
Advertisement

Multithreaded word count in C

I know I said I would try to figure it out on my own and I really did, and then I looked elsewhere first before posting here again but then I just ended up with this mess:

JavaScript

How best should I fix this? Specifically these errors and warnings:

JavaScript

I think the part for counting words from the buffer is okay, but I’m not sure how the function to used by the threads should be set up and how arguments should be passed to the threads. It wants very specific variable types but I need to use other types and like I said it kind of just turned into a mess

Advertisement

Answer

lseek moves only the read pointer.
Once the reading pointer is placed in one place, you can read characters with read.


I think in your exercise, each thread must have its own reading pointer.

if the file is 800 bytes and divided into 8:

the thread 1 reads the bytes from 0 to 99,
lseek (0, SEEK_SET), read (file, buffer, 100), count words in buffer…

thread 2: lseek (100, SEEK_SET), read (file, buffer, 100), count words in buffer…

thread 3: lseek (200, SEEK_SET), read (file, buffer, 100) count words in buffer…

etc …

You do not have to create temporary files

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement