联系QQ:928900200

CSCI 3120 Operating Systems

Summer 2014 Handout 3
Assignment 2
Date Due: June 5, 2014 by 9:00 pm electronically in the SVN directory
https://svn.cs.dal.ca/3120/<your id>/a2 where <your id> is your bluenose username.
Problem 1
(5 marks)
a) Describe the actions taken by a kernel to context-switch between heavyweight processes.
Indicate if the order of certain actions is important.
b) What, if anything, changes for a context switch between threads?
Problem 2
(3 marks) Page 153, problem 3.18
What are the bene ts and the disadvantages of each of the following? Consider both the system
level and the programmer level. (note, item b is intentionally missing)
a) synchronous and asynchronous communication
c) send by copy and send by reference
d) xed-sized and variable-sized messages
Problem 3
(3 marks) Page 193, problem 4.18
Consider a multicore system and a multi-threaded program written using the many-to-many
threading model. Let the number of user-level threads in the program be grater than the number
of processing cores in the system. Discuss the performance implications of the following scenarios.
a) The number of kernel threads allocated to the program is less than the number of processing
cores.
b) The number of kernel threads allocated to the program is equal to the number of processing
cores.
c) The number of kernel threads allocated to the program is greater than to the number of
processing cores but less than the number of user-level threads..
1
Problem 4
(4 marks)
Consider the following set of processes, with the length of the CPU burst given in milliseconds
process burst time arrival time priority
P1 9 0 3
P2 5 2 2
P3 3 3 5
P4 8 5 4
P5 2 6 1
Draw four Gantt charts that illustrate the execution of these processes using the following
scheduling algorithms: FCFS, SJF, nonpreemptive priority where a smaller priority number implies
a higher priority, and preemptive priority where a smaller priority number implies a lower priority.
Problem 5
(3 marks)
Here is the shortest remaining time rst schedule for the processes of problem 4:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
--- - ----- --- ------- ------------- -----------------
P P P P P P P
1 2 3 5 2 1 4
Calculate the average wait time, average turnaround time, and average normalized turnaround
time of this schedule.
Problem 6
(20 marks)
Objective
The objective of this question is to have you become familiar with programming with the pthread
library interface and to try a program design that uses communicating threads and uses polling to
synchronize.
Overview
You are going to design a program using a model-view-controller framework. Your program will
have one main task to accomplish. A \model" thread will handle that task. You will have separate
threads that provide windows into the model's calculations. These threads are the viewers. You
will have one additional thread, called the controller, that will accept input from the user and relay
user commands, as appropriate, to the model and view threads.
All communication will be through shared memory.
2
The task
You have a choice of tasks to accomplish in this framework. Choose one of the tasks. Both are
essentially the same at their core and create a time series of results.
Task 1: The game of life
In the game of life, you are given a two-dimensional grid. Some cells are set as \alive" and the
others are \dead" as part of the initial con guration. As we move from one time step to the next,
cells either become/stay alive or die. The rules work as follows:
a cell that is currently alive will die in the next con guration if it has fewer than 2 or more
than 3 neighbouring cells that are alive.
a cell that is currently alive with 2 or 3 alive neighbours stays alive.
a dead cell with exactly 3 alive neighbours becomes alive.
a dead cell with more than or fewer than 3 alive neighbours remains dead.
For this task, a neighbour of a cell is a cell that is immediately touching the current cell to the
north, east, west, south, northeast, southeast, southwest, or northwest.
The task is to run iterations of this game and to report on each iteration.
Represent a cell that is alive with the value 1 and a cell that is dead with the value 0.
For example, a 4x4 grid that starts with a vertical bar of live cells works as follows with the
legend \.1*" :
iteration 0 (start)
.*..
.*..
.*..
....
iteration 1
....
***.
....
....
iteration 2
.*..
.*..
.*..
....
Sample starting patterns for the grid can be found at http://en.wikipedia.org/wiki/Conway's Game of Life
3
Task 2: Finite element method calculation
A nite element method is an approach to calculate the value of some function over a complicated
surface. The surface is divided into small areas ( nite elements) and these nite elements are
connected together through some form of adjacency. You then iteratively calculate a value for each
nite element based on the values of its neighbouring elements in the previous iteration.
In this example, the nite elements will be the cells of a two-dimensional grid. All cell values
start with value 0 except for the cells on the left and right edges. You will be given xed values
for the left and right edge elements of the grid; these cell values do not change across all iterations.
The value of a cell in the grid at the next iteration is the average value of its neighbouring cells
(north, south, east, and west) in the current iteration (normally, the function is more complex than
just an average). A cell at the top or the bottom has 3 neighbours rather than 4, which is factored
in to the average calculation.
For example, consider this rst version of a 4x4 grid where the values may represent tempera-
tures and where we have a hot spot in one corner (100) and one edge that is cooled (10):
iteration 0 (start)
100 0 0 10
90 0 0 10
80 0 0 10
70 0 0 10
iteration 1
100 33.3 3.3 10
90 22.5 2.5 10
80 20.0 2.5 10
70 23.3 3.3 10
iteration 2
100 41.9 15.3 10
90 36.5 9.6 10
80 32.1 9.0 10
70 31.1 11.9 10
iteration 3
100 50.6 20.5 10
90 43.2 17.7 10
80 39.2 15.9 10
70 38.0 16.7 10
iteration 4
100 54.6 26.1 10
90 49.4 22.4 10
80 44.3 20.9 10
70 42.0 21.3 10
In a well-behaved system, we hope that these iterations ultimately converge to a stable set of
values across the grid that represent the value of the function as applied across the whole grid.
4
Threads
I describe the tasks of each thread. How the threads coordinate among themselves will be described
separately.
Controller
The controller thread will gather input from the user to modify the behaviour of the model and
the view threads. Commands to the controller are:
start view <type> < le> where \type" is either \full" or \summary" (explained later). This
command begins a new view thread for the speci ed elements, assigns a view number to it,
and prints that view number to the user. There will be at most 3 views.
view < X > legend <legend info> where X is the view number that we are modifying and
\legend info" is a string that alternates a character and a number (with a space between
each), like \e 10 f 20 g" and means that when printing the grid, any value < 10 is printed as
\e", any value 10 and < 20 is printed as f, and any value 20 is printed as g. This legend
can have more than just two boundary cases.
model < lename> to have the model switch its operation to use the content of < lename>
as a starting con guration for a new set of iterations on the grid.
end { to end the operation of the whole system cleanly.
Model
The model does the calculations of one of the two tasks previously described. Which one is up to
you to choose.
The model will have a \published" grid and a \work" grid. View threads will look at the content
of whatever is identi ed as the published grid to display.
When started, the model should be passed a le name that contains the initial con guration
for the problem. Put that con guration into the published grid. Then, it creates the next iteration
of the problem in the working grid. When done, it will make the information of the working grid
available as the published grid.
When the controller gives the model thread a new lename, the controller gets to nish its
current grid and publishes it before starting with the content of the new lename.
Along with the published grid, the model grid updates an interaction count number to let the
view threads know the iteration number of the published grid.
View
A view thread copies information about the published grid into a le. The lename is provided as
the view is created.
There are two types of views: a full view and a summary view. A full view will print the
iteration number of the grid and will then print the entire contents of the grid to the le. It will
use a legend to translate the values of the grid into single characters. The operation of the legend
has been done in the controller part of this write-up. Without a legend speci ed, the full view will
5
use the legend \. 1 *" | so anything less than 1 will be a period and any value 1 or more is a *.
If the legend is a single character (no cases) then print the values of the grid directly using 4 digits
with 1 decimal point of precision.
For example, a full view of the FEM \iteration 4" sample data in this description with legend
\. 25 : 50 * 75 #" would appear as:
iteration 4
#*:.
#:..
#:..
*:..
A summary view prints the iteration number of the published grid and then prints a value that
represents the grid. If you are programming the game of life then you print the number of cells
that are alive. If you are programming the nite element model then you print the average value
of all cells to 1 decimal point of precision.
When the view is asked to end, it closes its le.
Data structures
The model and all views must be able to see the published grid and the iteration count.
Messages
All messages will be passed through shared memory. So a message is passed from the controller to
the model by both threads having access to a common variable value. I recommend putting all the
values in one struct and passing a pointer to that struct to the threads as they are created.
The controller must be able to send the following information to the model:
a le name
the number of view threads that need to read the published grid
The model must be able to send the following information to the controller in response to a
message from the controller:
the iteration number when the new number of view threads send by the controller to the
model should begin printing the published grid
The controller must be able to send the following information to the view threads:
a new print legend
The view thread has no information to return to the controller.
How will the threads know that there is a message waiting? They will look to a shared variable
value. The controller will share a value with the model thread and with each view thread. We'll
generically call these variables \message ready" variables. All these variables begin with a value of
0. When the controller has data to send to the model thread, it will wait for the \message ready"
variable with the model thread to be 0, will copy the values to send to a place where the model
6
thread can nd them, and will then set the \message ready" variable value to 1. If the controller
needs a response, it can then wait for the variable value to return back to 0 and read the response
from memory.
The model and the view threads will periodically poll the shared \message ready" variable that
they share with the controller. If the value is 1, it will copy out the message information, set-up
any response information needed (model to controller) and then set the \message ready" variable
value back to 0.
Synchronization
Synchronization is about how the threads coordinate among themselves. For example, you don't
want a view thread to be printing the published grid if the model thread is changing it at the same
time. Here is how the synchronization will work:
A view thread will know which iteration grid it has last printed. The view thread will poll the
iteration number for a change. When the iteration number of the published grid increases then the
view thread will print the new grid and will again wait for the iteration number to increase.
The view threads and the model thread will need to share one variable, call it \printed" for
now. When a view thread nishes printing the current grid, it will increment the \printed" variable
by 1.
The model thread will create its working grid values. Once the working grid is ready to be
published, it will poll the \printed" variable. Once the \printed" variable's value matches the
number of view threads that the controller has told to the model thread, the model thread sets
\printed" to 0, updates the published grid, and nally increments the iteration number by 1. It
can then go on to calculate the next working grid while the view threads copy out the published
grid.
When a view thread is to be created, the controller will rst notify the model of the new number
of view threads, will get back which iteration the view thread can begin to look at the published
grid, and will then pass this grid value to the new view thread as part of its start-up information.
Model pseudocode
The model requires the most synchronization, so I will provide some pseudocode for it. There are
still details for you to ll in, especially where to connect with the shared data.
loop:
/* Poll for instructions from controller */
if "message ready" from controller = 1:
determine the type of message
make a copy of the appropriate variable values from the message
if being notified of new view threads
put next grid iteration number where the controller can find it
if being notified of a new file name
remember the new file name
set "message ready" from controller = 0
7
if I have a new file name
load the new file name as the working grid
else
calculate next iteration of the grid
/* Poll for all the viewers to be done with the earlier published grid */
while printed < number of view threads for the current grid iteration
do nothing
printed = 0
/* Publish the next grid */
make the calculated grid available as the published grid
increment current grid iteration number by 1
include a delay to slow down the model
Input le formats
The input le for the game of life has two integers on the rst line as the number of rows and then
the number of columns in the grid. Each subsequent row will contain a number of characters that
matches the number of columns and is either 0 for a dead cell or 1 for a live cell.
For example, the le for the game of life example given earlier is:
4 4
0100
0100
0100
0000
The input le for the nite element model has two integers on the rst line as the number
of rows and then the number of columns in the grid. The next line has a list of space-separated
integers that correspond to the top-to-bottom values of the left column in the grid (so a number of
integers matching the number of rows). The rows after that has a list of space-separated integers
that correspond to the top-to-bottom values of the right column in the grid.
For example, the le for the nite element model given earlier is:
4 4
100 90 80 70
10 10 10 10
Moving forward
DO NOT START WRITING THIS PROGRAM WITH THREADS! Implement the model and
views as functions and then have some unthreaded program call the model to calculate one iteration
of the grids followed by calling the view function and then calling a controller function to look for
user data. Only after you have the whole ow working should you begin to add the threading.
8
Plan out all your data structures and code before starting. It will save you headaches later on.
To start, assume that the grid size is no bigger than 100x100. Once your code is working you
can then worry about always allocating the appropriate grid size as given in the le.
Avoid global variables if you can. This assignment can be done with no global variables.
However, if it's easier to start with global variables then use them to get going.
Grading Scheme
documentation, coding style, modularity, error codes, memory management | 3
the model calculation works | 3
the viewer operations work, including the legend functions | 2
the controller gets appropriate commands | 2
the model, viewer, and controller interoperate properly as threads, including using several
viewers at once | 5
your solution that includes threads provides isolation of information between threads and
does not use global variables | 2
test cases | 3
9

C语言程序代写(Linux下线程)的更多相关文章

  1. 如何鉴别程序抄袭c语言程序代写

    如何鉴别程序抄袭:如何鉴别一份程序代码是抄袭另一份程序.输入:两个C语言源程序文件 输出:抄袭了多少?给出最相似的片段,判断是谁抄袭了谁? 提示:首先进行统一规范化排版,去掉无谓的空格.空行,然后比对 ...

  2. 实验教学管理系统 c语言程序代写源码下载

    问题描述:实验室基本业务活动包括:实验室.班级信息录入.查询与删除:实验室预定等.试设计一个实验教学管理系统,将上述业务活动借助计算机系统完成. 基本要求: 1.必须存储的信息 (1)实验室信息:编号 ...

  3. C语言程序代写

    MTRX1702 - C ProgrammingAssignment 1This assignment requires you to design and build a program to co ...

  4. C语言程序代写(qq:928900200)

    1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...

  5. C语言程序代写(QQ:928900200)

    1.学生成绩统计 要求描述: 用结构数组实现学生信息的统计功能. struct student { long no; /*学号*/ char name[10]; /*姓名*/ char sex; /* ...

  6. CS代码代写, 程序代写, java代写, python代写, c/c++代写,csdaixie,daixie,作业代写,代写

    互联网一线工程师程序代写 微信联系 当天完成特色: 互联网一线工程师 24-48小时完成.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor code,代码原创.参考课程sl ...

  7. 基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写

    基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写 专业程序代写服务(QQ:928900200) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...

  8. Linux 下线程的理解

    2017-04-03 最近深入研究了下Linux线程的问题,发现自己之前一直有些许误解,特记之…… 关于Linux下的线程,各种介绍Linux的书籍都没有深入去解释的,或许真的如书上所述,Linux本 ...

  9. linux下线程

    linux下线程 线程与进程的关系: 之前转载的微信文章,进程与线程的差别已经说得比較清楚了.能够查看之前转载的文章.linux进程与线程的差别. 创建一个线程: #include<pthrea ...

随机推荐

  1. Jar包转成Dll的方式(带嵌套的jar也能做) (转)

    研究很好几天,终于成功了.因为写了一个Java的项目,现在要求要改写成C#版本的.但是其中用到了svnkit,svnkit是java平台的.改写成C#的话,要使用SharpSVN,但是SharpSVN ...

  2. Setup SSH and SVN on Windows Server

    cygwin: install sshd, cygrunsrv http://lifehacker.com/205090/geek-to-live--set-up-a-personal-home-ss ...

  3. HDU 4921 Map

    题意: 给n个节点  他们形成了最多10条链  每条最多1000的长度  每一个节点有个val  你能够选择任何位置截断链  断点前的全部节点被你获得  通过题中计算公式得出你的val  问  通过随 ...

  4. ThinkPHP 的模型使用对数据库增删改查(五)

    原文:ThinkPHP 的模型使用对数据库增删改查(五) ThinkPHP 的模型使用 // 直接连接数据库,但是得先去配置文件中配置下才行 class IndexAction extends Act ...

  5. ubuntu install mysql server method

         recently try to install mysql in my computer so that  I can practise some sql statement on seve ...

  6. WCF(1)----服务创建

    本例中,我们通过一个关于Camera的服务为例子来说明WCF的开发流程,该服务比较简单,只是用来实现对Camera的添加,枚举,删除等操作. 详细步骤如下: 1:创建一个WCF Service Lib ...

  7. 结合使用AngularJS和Django

    原地址 好吧,我承认自己很懒,时间又不够用. 翻译的几个文章都是虎头蛇尾,但我保证这次肯定不太监. 关键的单词不翻译,实在觉得翻译成汉语很别扭,括号里是参考翻译. 有问题和建议尽管提出来,我会改进完善 ...

  8. H2O是开源基于大数据的机器学习库包

    H2O是开源基于大数据的机器学习库包 H2O能够让Hadoop做数学,H2O是基于大数据的 统计分析 机器学习和数学库包,让用户基于核心的数学积木搭建应用块代码,采取类似R语言 Excel或JSON等 ...

  9. hdu 1086(判断线段相交)

    传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...

  10. hdu 4291 A Short problem(矩阵+取模循环节)

    A Short problem                                                          Time Limit: 2000/1000 MS (J ...