代写SOFTENG 370 Operating Systems课设、代做C/C++ 留学生作业、代写c/c++代码、代写C/C++编程作业
SOFTENG 370
Operating Systems
Assignment 2 - Thread Pools and Dispatch Queues
Worth 8%
final date 21st of September, 2018, 9:30pm
Introduction
A problem with using large numbers of threads in a program is the cost in time and memory of
repeatedly creating and destroying threads. One way to minimise the cost of using threads is via one
or more thread pools. The idea is to create a number of threads your application may use and store
them in a pool allocating a thread to a subtask from this pool on demand and returning the thread to
the pool when the task is completed. This way the penalty for creating threads is only paid once and
threads stay inactive in the pool until they are associated with a new subtask or until the thread pool
is dismantled.
One way such pools can be controlled is based on dispatch queues. These are queues on to which
tasks are added. The tasks are then processed either concurrently or serially depending on the type
of queue and how the tasks are added to them. In this assignment you have to implement a small
subset of such a system. Dispatching is the process of selecting a task to run and starting it running
by allocating it a thread from a thread pool. Generally tasks can either be blocks (anonymous
functions) or normal functions. In this assignment tasks for dispatching will be C functions.
Basic Idea
Rather than having to create threads and deal with them in order to use the multicore environments
of modern computers it makes sense to have a library which does this for us in such a way that
efficient use is made of all cores and yet our code doesn’t have to change whether there is one
processor in a system or sixty-four.
Of course because you are creating this library you will have to create threads and deal with them
but it should make programming using this library easier.
Types of Queues
There are two types of queues to implement for this assignment, serial queues and concurrent
queues. A serial queue dispatches a task and waits for the task to complete before selecting and
dispatching the next task. Obviously serial queues don’t provide concurrency between the tasks in
the same queue as only one task runs at a time but they do provide concurrency between the tasks in
the queue and tasks running on other queues (and the main thread of the application).
Concurrent queues dispatch tasks in the order they are added to the queue but they also allow tasks
from the same queue to run concurrently. Each concurrent queue has at least 2 threads associated
with it (a small thread pool) and as soon as a thread becomes available because it has finished
executing its current task, a new task can be dispatched on it from the front of the queue. In this
assignment you can create as many concurrent queues as you like.
All tasks on both serial and concurrent queues are dispatched in a FIFO fashion.
Splitting a Program up into Distinct Tasks
Even though dispatch queues can solve some problems of dealing with concurrency on multiple
cores they still require the programmer to split the program into mostly independent tasks. All tasks
on a concurrent dispatch queue must be able to operate safely in parallel. If you need to synchronize
the activity of several tasks you would normally schedule those tasks on a serial dispatch queue.
Page 1 of 8
Two Types of Dispatching
Regardless of the type of queue it is also possible to add tasks to a dispatch queue either
synchronously or asynchronously. If a task is added to a dispatch queue synchronously the call to
add the task does not return until the task which was added to the queue has completed. If a task is
added to a dispatch queue asynchronously the call to add the task returns almost immediately and
the calling thread may run in parallel to the task itself. The normal way of adding a task to a queue
is the asynchronous method.
Waiting for tasks
If a task was scheduled asynchronously there may be a time when some thread (e.g. the main thread
of the program) needs to wait for all the tasks submitted to a dispatch queue to complete. An
obvious example here would be when the program waits until all tasks begun in the program have
completed before the program terminates. Waiting for a dispatch queue to complete should work
both for serial and concurrent tasks. For this assignment if further tasks are added to a dispatch
queue after a thread has waited for that queue to complete those new tasks are ignored. Something
to think about: is there a race condition here?
How Many Threads?
In such a system the number of active threads in the system could vary according to the number of
cores in the machine and the load on the cores. Using some of the same techniques as were used by
batch systems to keep processor usage levels high, more threads can be scheduled to carry out tasks
when the load level drops and fewer threads used if the cores are being used at close to 100% load.
The load level is not just a local function associated with one particular program, it is a global value
determined by all of the work being done on the computer.
In this assignment you do not have to concern yourself with the load level of the cores. In fact you
should simply allocate the same number of threads to each concurrent dispatch queue as there are
cores or processors in the system. The first task of this assignment is to write a short C program
which runs on Linux in the labs and prints out the number of cores on the machine the program is
running on.
This program should be called num_cores.c and it should be compiled, executed and produce
output as shown below:
gcc num_cores.c -o num_cores
./num_cores
This machine has 2 cores.
Because some architectures use hyper-threading the operating system may see 4 processors when
only 2 cores are present. Your program should report as the number of cores the same number as
shown by the Gnome System Monitor program.
Page 2 of 8
In the dispatchQueue.h file you will find some types defined. You must use these types in your
program. You may extend the types by adding extra fields or attributes. You may also add your own
types (in fact you will probably have to).
You must use locks or semaphores (or both) to control the concurrency in your solutions. man -k
mutex, man -k pthread, and man -k sem will give you more than you need to know.
In the dispatchQueue.c file you must implement the following functions. None of these
functions return error results because you should print an error message and then exit the program if
an error occurs.
dispatch_queue_t *dispatch_queue_create(queue_type_t queueType)
Creates a dispatch queue, probably setting up any associated threads and a linked list to be used by
the added tasks. The queueType is either CONCURRENT or SERIAL.
Returns: A pointer to the created dispatch queue.
Example:
dispatch_queue_t *queue;
queue = dispatch_queue_create(CONCURRENT);
void dispatch_queue_destroy(dispatch_queue_t *queue)
Destroys the dispatch queue queue. All allocated memory and resources such as semaphores are
released and returned.
Example:
dispatch_queue_t *queue;

dispatch_queue_destroy(queue);
task_t *task_create(void (* work)(void *), void *param, char* name)
Creates a task. work is the function to be called when the task is executed, param is a pointer to
either a structure which holds all of the parameters for the work function to execute with or a single
parameter which the work function uses. If it is a single parameter it must either be a pointer or
something which can be cast to or from a pointer. The name is a string of up to 63 characters. This
is useful for debugging purposes.
Returns: A pointer to the created task.
Example:
void do_something(void *param) {
long value = (long)param;
printf(“The task was passed the value %ld.\n”, value);
}
task_t *task;
task = task_create(do_something, (void *)42, “do_something”);
Page 3 of 8
void task_destroy(task_t *task)
Destroys the task. Call this function as soon as a task has completed. All memory allocated to the
task should be returned.
Example:
task_t *task;

task_destroy(task);
void dispatch_sync(dispatch_queue_t *queue, task_t *task)
Sends the task to the queue (which could be either CONCURRENT or SERIAL). This function does
not return to the calling thread until the task has been completed.
Example:
dispatch_queue_t *queue;
task_t *task;

dispatch_sync(queue, task);
void dispatch_async(dispatch_queue_t *queue, task_t *task)
Sends the task to the queue (which could be either CONCURRENT or SERIAL). This function
returns immediately, the task will be dispatched sometime in the future.
Example:
dispatch_queue_t *queue;
task_t *task;

dispatch_async(queue, task);
void dispatch_queue_wait(dispatch_queue_t *queue)
Waits (blocks) until all tasks on the queue have completed. If new tasks are added to the queue
after this is called they are ignored.
Example:
dispatch_queue_t *queue;

dispatch_queue_wait(queue);
Page 4 of 8
void dispatch_for(dispatch_queue_t *queue, long number, void (*work)
(long))
Executes the work function number of times (in parallel if the queue is CONCURRENT). Each
iteration of the work function is passed an integer from 0 to number-1. The dispatch_for
function does not return until all iterations of the work function have completed.
Example:
void do_loop(long value) {
printf(“The task was passed the value %ld.\n”, value);
}
dispatch_queue_t *queue;

dispatch_for(queue, 100, do_loop);
/* This is sort of equivalent to:
for (long i = 0; i < 100; i++)
do_loop(i);
Except the do_loop calls can be done in parallel.
*/
This is how you get your marks
There are test files and a make file you can use to test your code.
Zip all C source files together into a file called A2.zip. Do not include the test files but do include
your num_cores.c, dispatchQueue.c and dispatchQueue.h files along with any other
files you may have added (most people won’t have any more).
Put the answers to the questions into a plain text file or pdf called either A2Answers.txt or
A2Answers.pdf.
Submit the zip file and the answers to the questions using the Canvas submission system
before 9:30pm on Friday the 21st of September.
The work you submit must be your own. Refer to the University’s academic honesty and plagiarism
information https://www.auckland.ac.nz/en/students/forms-policies-and-guidelines/student-policiesand-guidelines/academic-integrity-copyright.html.

1. num_cores
prints the correct number of cores for the machine it is running on.
[1 mark]
2. test1
2.1
works correctly
[2 marks]
Produces:
Page 5 of 8
test1 running
Safely dispatched
2.2
dispatch_queue_destroy returns the allocated resources
[1 mark]
2.3
task_destroy is called by the implementation and releases memory
[1 mark]
3. test2
works correctly – only get the mark if test1 worked correctly
[1 mark]
Produces:
Safely dispatched
4. test3
works correctly
[2 marks]
Produces:
Safely dispatched
test3 running
5. test4
5.1
works correctly
[2 marks]
Produces something like:
Safely dispatched
task "A"
task "B"
task "C"
task "D"
task "E"
task "F"
task "G"
task "H"
task "I"
task "J"
The counter is 4939859698
The order of the tasks may vary. The counter value will also vary but it should almost never
be 10,000,000,000.
5.2
Utilizes all cores
[2 marks]
Open system monitor before you run it and check that all CPUs go to 100%.
Page 6 of 8
6. test5
6.1
works correctly
[2 marks]
Produces:
Safely dispatched
task "A"
task "B"
task "C"
task "D"
task "E"
task "F"
task "G"
task "H"
task "I"
task "J"
The counter is 10000000000
The tasks must be in this order and the counter must always equal 10,000,000,000.
6.2
Mostly utilizes only one core.
[1 mark]
Check by observing system monitor.
7.
Implementation marks (applied if at least 2 of the previous tests passed)
7.1
Good identifiers [1 mark]
7.2
Consistently good indentation [1 mark]
7.3
Useful comments [1 mark]
7.4
No busy waits [1 mark]
7.5
No use of sleep to make synchronization work. [1 mark]
8.
The marker will use an unspecified test - using a combination of the implemented functions.
[5 marks]
9.
Question:
Explain how your implementation dispatches tasks from the dispatch queues. You must
describe how dispatching is done for both serial and concurrent dispatch queues. Give code
snippets from your solution as part of the explanation.
[5 marks]
2 marks for clarity (can the marker easily understand what you are saying).
2 marks for making sense.
Page 7 of 8
1 mark for including code snippets in the explanation.
10.
The marker will use a test program similar to testFor but with more intensive
computation.
works correctly
Output should be something like (order may be different):
The number is 999999999
The number is 1000000000
The number is 999999998
The number is 999999997
The number is 999999996
The number is 999999995
The number is 999999994
The number is 999999993
The number is 999999992
The number is 999999991
[2 marks]
All cores should be used when running.
11.
Question:
Using the time command, time running test4 and test5. Give the results and discuss them. Is
there anything unusual? Explain what you observe.
[3 marks]
1 mark for the results.
1 mark for comments.
1 mark for explanations.
http://www.daixie0.com/contents/13/1706.html

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codinghelp

转载于:https://www.cnblogs.com/pythonpythoncode/p/9682738.html

代写SOFTENG 370 Operating Systems课设、代做C/C++ 留学生作业、代写c/c++代码、代写C/C++编程作业...相关推荐

  1. 计算机组成课设怎么做,计算机组成原理课设1

    计算机组成原理课设1 (12页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.90 积分 哈工大 年 秋 季学期班号姓名计算机组成原理 试 题题号一二三 ...

  2. 临近期末,图书管理系统课设项目安排上(附源码)

    图书管理系统 项目介绍 项目截图 源码分享 项目介绍 本系统是一个基于java的图书管理系统,用Swing显示信息. 开发环境为IDEA,使用mysql数据库.用 户分为 用户和管理员. 项目截图 源 ...

  3. 基于ZigBee+ESP32+MQTT+EMQX+TomCat+Servlet接口+MySQL+安卓app的物联网课设

    文章目录 一.写在前面 二.课设简介 三.不眠夜开始了 1.基于zigbee网络数据采集的底层实现 2.基于ESP32和mqtt协议的数据上传功能 3.使用EMQX作为MQTT服务器软件 4.使用Ja ...

  4. 企业职工工资在线管理信息系统【生产实习课设报告】

    若本文对你有帮助的话,记得点赞.关注我哟! 大四上学期的课设之一"生产实习",一共上三周,我随便乱写的,害怕不及格的人可以做个参考.最后班里大多数人都和我一样得了90分.然而这是我 ...

  5. JAVA+MySQL 数据库课设的问题及解答的整理 以【学生管理系统】为例

    JAVA+MySQL 数据库课设的问题及解答的整理 以[学生管理系统]为例.帅气学长哦! 编写这篇博文初衷 MySQL的一些问题 Eclipse导入项目的一些问题 数据库的建立和连接 最后一步 编写这 ...

  6. 【课设生成器】我做了一个代码生成器

    文章目录 一.什么是代码生成器 0.视频介绍 1.做生成器的初衷 2.生成器的功能 4.资源内容 二.代码生成器的使用 1.文件目录介绍 2.启动系统 3.使用教程 3.1界面 3.2 生成 3.3 ...

  7. C语言课设:影片管理系统

    在接触并学习C语言之后,往往需要做一个简单的课设系统完成对C语言的学习.当然,这可能是我们接触代码以来写过的最长的代码,不排除对新手有一定难度.由于之前笔者划水了一段时间,导致一些知识遗忘了,所以只得 ...

  8. python课程设计题目-python课设题目

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! 最近咨询课设问题的同学越来越多了,大部分都是用pandas做数据统计的问题,我就找 ...

  9. java课程设计进程管理_Java课设总结(个人版)

    使用物理引擎JBox2D完成游戏关卡的各个物理状态模拟 根据物理引擎设计Bird,Pig,Brick等游戏中出现的可运动刚体类 建立JBox2d的工具类以实现 ###本人对这次课设的看法与吐槽 1. ...

最新文章

  1. 今日 Paper | 高效骨干搜索;学习扩充;最小人脸检测器;​DEPARA等
  2. Bootstrap3 表格-带边框的表格
  3. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f
  4. [PHP] 项目实践中的自动加载实现
  5. 业精于勤荒于嬉---Go的GORM查询
  6. intl扩展 mac 安装php_mac下php安装intl扩展代码分享
  7. 55寸鸿蒙安卓,深网|荣耀智慧屏发布:搭载鸿蒙系统 配55英寸屏3799元起
  8. python词频统计西游记实验报告_Python文本统计功能之西游记用字统计操作示例
  9. 免费的图标网站汇总,天气图标主题汇总
  10. virtualbox安装增强功能时【未能加载虚拟光盘】
  11. 2020年美赛C题(数据分析题)O奖论文笔记 (1)
  12. 怎么下载小程序图片 ?
  13. 三、大数据时代下的SQL Server第三方负载均衡方案----Moebius测试
  14. 世界星载SAR发展5——SIR-C(1994,美国)
  15. 开源、强大的Linux服务器集群管理工具,比宝塔好用!
  16. 机器学习实践:非监督学习-8
  17. 年底找工作,怎么解释离职的原因?
  18. win10修改用户名导致问题及解决
  19. navicat导出数据库数据
  20. JAVA设计模式什么鬼(适配器)——作者:凸凹里歐

热门文章

  1. 用户及权限管理功能常规测试
  2. 一条 SQL 语句在 MySQL 中如何被执行的?
  3. 谁说只有VGG才能做风格迁移,ResNet也可以
  4. 虚拟化涉及的关键技术都有哪些,分别实现了什么功能?
  5. VC6在64位Win7下调试无法退出的问题(缺少TLLOC.DLL和DM.dll)
  6. 约瑟夫环java链表_java使用链表实现约瑟夫环
  7. java中字节码_Java字节码浅析(三)
  8. maven使用小技巧 optional
  9. phpthink 隐藏index.php,nginx 配置--支持phpthink框架 path_info
  10. Windoes 远程桌面 对windows 传送大文件(镜像等)