问题
so I was looking over my next school assignment, and I'm baffled. I figured I would come to the experts for some direction. My knowledge on synchronization is severely lacking, and I didn't do so hot on the "mcopyfile" assignment it refers to. Terrible would probably be a good word for it. If I could get some direction on how to accomplish this problem, it would be much appreciated. Not looking for someone to do my assignment, just need someone to point me in the right direction. baby steps.
Based on the multi-threaded file copy tool (mcopyfile) you have created in Lab 2, now please use a worker pool (Producer-Consumer model) implementation that uses a fixed number of threads to handle the load (regardless how many files in the directory to copy). Your program should create 1 file copy producer thread and multiple file copy consumer threads (this number is taken from the command-line argument). The file copy producer thread will generate a list of (source and destination) file descriptors in a buffer structure with bounded size. Each time when the producer accesses the buffer it will write one (source, destination) file entry (per visit). And all file copy consumer threads will read from this buffer, execute the actual file copy task, and remove the corresponding file entry (each consumer will consume one entry each time). Both producer and consumer threads will write a message to standard output giving the file name and the completion status (e.g., for producer: “Completing putting file1 in the buffer”, for consumer: “Completing copying file1 to …”).
回答1:
Assuming, you know how to spawn threads, let me break down the problem for you. There are following components:
- Producer. It generates Tasks for the Consumers based on the source directory input parameter.
- Task. A task is information for Consumer to execute its copy task. Namely a tuple of source file descriptor and destination file descriptor.
- Queue. It is the central piece of communication between Producer and Consumer. Producers writes Tasks to Queue and Consumer consumes it.
- Consumer. You have a pool of actual workers that take Task as input and executes copy operation.
Now as per the question, spawn a thread for producer and n threads for consumers. And this is what the threads do:
Producer thread
- For list of files in the source directory:
- Task <- (Source file path, destination file path)
- Acquire lock on Queue
- Write Task to queue
- Release lock on Queue
- Acquire lock on stdout
- Write to stdout
- Release lock on stdout
- For list of files in the source directory:
Consumer thread
- While True:
- If size of queue == 0:
- Sleep for some time
- Else:
- Acquire lock on Queue
- Dequeue a Task
- Release lock on Queue
- Execute copy operation
- Acquire lock on stdout
- Write to stdout
- Release lock on stdout
- If size of queue == 0:
- While True:
I hope this helps.
回答2:
Your assignment looks pretty straightforward to me once you know what API/library you'll use for the threading functionality.
First, you'll parse the command-line argument and create the specified number of threads, then from the main thread obtain the list of files in the folder and start putting them in an array (like a std::vector) that is shared among the threads and is synchronized with a mutex (or a critical section on Windows). Whenever one of the consumer threads acquires the mutex, it makes a copy of a file entry in the array, removes that entry from the array, releases the mutex so that another thread can start doing the same, and starts copying the file represented by the entry it removed from the array.
I would give you some code snippets, but you didn't say what API/library you're using for the threading functionality.
来源:https://stackoverflow.com/questions/13062275/file-copy-tool-w-producer-consumer-model