具有生产者/消费者模型的文件复制工具 [英] File Copy Tool w/ Producer/Consumer Model

查看:217
本文介绍了具有生产者/消费者模型的文件复制工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在看我的下一个学校任务,我很困惑。我想我会来专家一些方向。我对同步的知识严重缺乏,我没有这么热在mcopyfile分配它指的。可怕是可能是一个好字。如果我可以得到一些方向如何完成这个问题,这将是非常感激。不寻找有人做我的任务,只需要有人指出我的方向正确。婴儿步骤。


基于您在实验2中创建的多线程文件复制工具
(mcopyfile),现在请使用worker
pool(Producer-Consumer模型)实现,它使用固定的
线程数来处理加载(无论
目录中要复制多少个文件)。你的程序应该创建1个文件副本生产者
线程和多个文件副本消费者线程(这个数字从命令行参数取得
)。文件副本生产者线程将
生成一个(源和目标)文件描述符在缓冲区
结构的有界大小的列表。每次当生产者访问缓冲区时,它将写入
一个(源,目标)文件条目(每次访问)。并且所有文件副本
消费者线程将从此缓冲区读取,执行实际文件
复制任务,并删除相应的文件条目(每个消费者
每次将消耗一个条目)。生产者和消费者
线程将向标准输出写一个消息,给出文件名
和完成状态(例如,对于生产者:完成将
file1放入缓冲区,对于消费者: 完成将file1复制到...)。



解决方案

假设你知道如何生成线程,让我分解你的问题。有以下组件:


  1. 生产者。 根据源目录输入参数

  2. 任务,为消费者任务是用于消费者执行其复制任务的信息。即源文件描述符和目标文件描述符的元组。

  3. 队列。它是 的中心通信。 会将 消耗。

  4. 。您有一个实际工人池,它将任务作为输入,并执行复制操作。

问题,为消费者产生生产者和 n 线程的线程。这是线程执行的操作:




  • 生产者线程


    1. 对于源目录中的文件列表:


      1. 任务< - (源文件路径,目标文件路径)

      2. 取得 锁定

      3. $ b
      4. 释放队列上的锁定

      5. 取得 锁定


      6. b


    2. 消费者主题


      1. True:


        1. 如果队列大小== 0:


          1. 睡一段时间


        2. 其他:


          1. 取得队列锁定

          2. 取消列出任务

          3. 取消锁定队列

          4. 执行复制操作

          5. stdout 上获取锁定

          6. 写入 stdout b
          7. 释放 stdout上的锁定






我希望这有助于。


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 …").

解决方案

Assuming, you know how to spawn threads, let me break down the problem for you. There are following components:

  1. Producer. It generates Tasks for the Consumers based on the source directory input parameter.
  2. Task. A task is information for Consumer to execute its copy task. Namely a tuple of source file descriptor and destination file descriptor.
  3. Queue. It is the central piece of communication between Producer and Consumer. Producers writes Tasks to Queue and Consumer consumes it.
  4. 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

    1. For list of files in the source directory:

      1. Task <- (Source file path, destination file path)
      2. Acquire lock on Queue
      3. Write Task to queue
      4. Release lock on Queue
      5. Acquire lock on stdout
      6. Write to stdout
      7. Release lock on stdout

  • Consumer thread

    1. While True:

      1. If size of queue == 0:

        1. Sleep for some time

      2. Else:

        1. Acquire lock on Queue
        2. Dequeue a Task
        3. Release lock on Queue
        4. Execute copy operation
        5. Acquire lock on stdout
        6. Write to stdout
        7. Release lock on stdout

I hope this helps.

这篇关于具有生产者/消费者模型的文件复制工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆