首页 > 其他分享 >Thread Exercises C语言线程

Thread Exercises C语言线程

时间:2023-05-26 20:24:30浏览次数:70  
标签:files functions slow Thread will 线程 edu Exercises your


1/4

Assignment 4: Threads

Due 11 Jun by 23:59 Points 10 Available until 15 Jun at 23:59

Assignment 4 - Thread Exercises

Due date 11:59pm - Sunday Week 13.

This assignment is designed to test your understanding of threads, mutexes and signalling.

Download the following files:

slow_functions.h (https://myuni.adelaide.edu.au/courses/85264/files/12976373/download?wrap=1)

(https://myuni.adelaide.edu.au/courses/85264/files/12976373/download?download_frd=1)

slow_functions.c (https://myuni.adelaide.edu.au/courses/85264/files/12976372/download?wrap=1)

(https://myuni.adelaide.edu.au/courses/85264/files/12976372/download?download_frd=1)

part2.c (https://myuni.adelaide.edu.au/courses/85264/files/12976419/download?wrap=1)

(https://myuni.adelaide.edu.au/courses/85264/files/12976419/download?download_frd=1)

You will need to include them in your compilation. You will write your own part1.c and modify part2.c.

Part 1 - Threads (25%)

Within "slow_functions.c" there are two "slow functions".

The first, requires a second to 'generate data'. It then increments a counter guarded by a mutex. If

the counter reaches 10, it prints "Work Done".

The second prints a message "Start 2", then waits for 2 seconds before printing "End 2".

The goal is to get the "Work Done" string to land in between "Start 2" and "End 2".

Start 2

Work Done

End 2

You are to write a file "part1.c". You may do what you like in this code (part1.c) but you must call both

"slow functions" AND you may not modify "slow_functions.c". Additionally, you may not call

slow_functions1 unless slow_functions2 has been called - the code prevents it from working!

Herein lies the challenge. To get the correct order of print statements (above), you need to run

slow_function1 ten times. However, the first time you run slow_functions1 must be after you have run

slow_functions2 (because there is a mutex initialisation needed). Both Start 2 and End 2 are in

slow_function2. Hence, you need to somehow run slow_function1 ten times in between starting

slow_function2 and finishing slow_function2. Keep in mind that you only have 2 seconds and yet

slow_function1 requires 1 second to run. Sounds impossible right?

26/05/2023, 16:04 Assignment 4: Threads

2/4

Threads are your friend.

Part 2 - Signalling and Mutexes (50 %)

Having solved a fairly small threading task, now it is time to try to get threads to communicate well.

Like part 1, this section relies on functions found in "slow_functions.c". Again, you may not modify

this file. You will also be provided with part2.c. This file contains two functions (where you will do your

work) and a main (which you will leave unmodified - well, you are allowed to add stuff before and

after the // ### DO NOT MODIFY sections, but please don't make it a big "if (false)" or something

ridiculously like that.

The main function does the following:

It reads input from a file (i.e. ./PART2 < input_file). It then starts two threads: writer and reader. Your

job is to write the contents of these two functions.

writer

The goal of writer is simple. It must call bad_write (found in slow_functions.c) once for each line of

input.

reader

The goal of reader is simple. It must call bad_read (also found in slow_functions.c) once each time

something is written by bad_write.

Now here is the trick. bad_read and bad_write both access a single buffer and both have some

arbitrary delays in them. This means that simply having two threads trying to write to the buffer and

read from it simultaneously will never give you the right output. bad_read replaces anything it reads

from the buffer with garbage text so beware.

Fortunately whenever bad_write writes, it sets a flag and whenever bad_read reads it sets it back.

You can access this flag via get_written (it is hidden in slow_functions.c) so this can help you get

the timing right.

So mutexes will be your friend here. You will have to decide how best to use them. Moreover,

mutexes are likely to be insufficient. You will probably need some wait conditions (maybe two) to

make sure the two threads play well with each other to get the desired output.

Part 3 - Style (25 %)

Of the code you write, make sure it is well commented and formatted. You should know the drill by

now.

Submission

Makefile

I am going to do the following:

26/05/2023, 16:04 Assignment 4: Threads

3/4

make part1

make part2

./PART1

./PART2 < input_file

It is up to you to ensure your Makefile (which is required) will handle this sensibly. Do not forget that

threaded programs need -pthread as a flag and that you will need to include slow_functions.c in

your commit/compilation.

SVN

This assignment is basically like every other assignment you’ve ever done in CS... but just as a

reminder:

The handin key for this exercise is: assignment4. The following SVN commands will enable

you to make a repository for this assignment. Please note the following:

Perform these steps in the order written once only!

Replace aaaaaa, where it appears in the commands, with YOUR student id.

Some commands are long — they must be typed on one line.

Use the Unix “cd” command to change to the place where you want your exercise

directory to be stored, then type these commands:

svn mkdir --parents -m "spc assignment4 start" https://version-control.adelaide.edu.au/svn/a1XXXXXX/20XX/

s1/spc/assignment4 (https://version-control.adelaide.edu.au/svn/a1aaaaaa/2020/s1/spc/assignment3)

(creates this directory in your svn tree) (change the a1xxxxxx and 20YY)

svn co https://version-control.adelaide.edu.au/svn/a1XXXXXX/20YY/s1/spc/assignment4 .

(checks out a working copy in your directory) You can now begin work.

You can add a file to the repository by typing the commands:

svn add NAME-OF-FILE

svn commit -m "REASON-FOR-THE-COMMIT"

where “reason-for-the-commit” should be some brief text that explains why you changed the code

since the last commit. Note that you only need to add a file once — after that, SVN will “know” it is in

the repository. You are now ready to commence working on the exercise.

The files you handin must include:

1. Your C source files as specified above.

2. A Makefile that will compile your C sources as specified above.

Make sure you commit your files frequently, in case you have an accident. The University’s SVN

repository is very reliable, and is backed up regularly — your computer probably is not... Regular

26/05/2023, 16:04 Assignment 4: Threads

4/4

submission is also a good defence against plagiarism by others, since the submissions are dated.

We will test the behaviour of your scripts using an automated tester. The tester is thorough, and will

find places where your scripts do not work correctly. If it finds an error, it will offer a (vaguish) hint. To

encourage you to test your own work, the tester will not be fully available in the first few days before

the exercise deadline.

The websubmission system will award up to 6 marks automatically. We will manually check the code

for style and commenting. Note that we reserve the right to deduct marks if your code does anything

egregious or games the system to obtain marks.

 

标签:files,functions,slow,Thread,will,线程,edu,Exercises,your
From: https://www.cnblogs.com/simpleyfc/p/17435715.html

相关文章

  • 8、Hystrix 线程池隔离与接口限流
    线程池隔离技术的设计原则Hystrix采取了bulkhead舱壁隔离技术,来将外部依赖进行资源隔离,进而避免任何外部依赖的故障导致本服务崩溃线程池隔离,学术名称:bulkhead,舱壁隔离外部依赖的调用在单独的线程中执行,这样就能跟调用线程隔离开来,避免外部依赖调用timeout耗时过长,导致调用线程......
  • Java大文件分片上传/多线程上传方案
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux支持所有浏览器。支持文件批量上传支持文件夹上传,且要求在服务端保留层级结构。文件夹数量要求支持到10W......
  • Java大文件分片上传/多线程上传实例解析
    ​ javaweb上传文件上传文件的jsp中的部分上传文件同样可以使用form表单向后端发请求,也可以使用ajax向后端发请求    1. 通过form表单向后端发送请求         <formid="postForm" action="${pageContext.request.contextPath}/UploadServlet" method="pos......
  • Java笔记(九):线程池
    三大方法Executors.newSingleThreadExecutor();//单个线程Executors.newFixedThreadPool(5);//固定的线程池大小Executors.newCachedThreadPool();//可伸缩的以上底层都是由ThreadPoolExecutor实现阿里开发手册:线程池不允许使用Executors去创建,而是通过ThreadP......
  • C++写得统计线程利用率的小工具
    thread_usage.h#ifndef__THREAD_USAGE__#define__THREAD_USAGE__#include<fstream>#include<string>#include<map>#include<pthread.h>#include<sys/time.h>namespacethread{classCThreadUsage{pu......
  • Java大文件分片上传/多线程上传源代码
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • Java大文件分片上传/多线程上传源码
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • Java大文件分片上传/多线程上传代码
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。首先我们需要了解的是上传文件三要素:1.表单提交方式:post(get方式提交有大小限制,post没有)2.表单的enctype属性:必须设置为multipart/form-data.3.表单必须......
  • 深度解析线程的正确停止方法
    深度解析线程的正确停止方法一、解惑1.什么情况下,线程需要被停止?线程和任务被创建和启动之后,大部分情况下都是自然运行到结束的,自然停止,但有些情况会需要用到停止线程,如:用户主动取消服务被快速关闭运行出错或超时情况下等线程都需要被停止这些情况都需要主动来......
  • 【java】同步异步和多线程编程
    Java基本概念并发基于时间段内的,同时发生(处理多个任务的能力,时间段)存在同步和互斥的问题(任务之间的时序问题)同步:前一个处理的结果作为下一个处理的资源(互相之间有依赖)互斥:不能同时使用临界资源。解决时序问题的机制:锁,信号量,原子操作Java中的多线程机制并行(完全......