操作系统(六)实验

任务简介

有5个涉及到多线程的问题,选其2进行解决。
选择用 C 或 C++ 来实现。但是,低级操作系统调用必须用普通的C语言。

学习成果

  • 多线程的debug
  • 同步,线程调度,抢占非抢占式调度算法

注意事项

  • 不允许使用占用cpu的等待操作。应该使用condition variable,当满足某种操作的时候再去睡眠或者苏醒和其他线程通信。在操作添加元素到数组,加锁等操作时,对每一步都输出当前的操作信息。程序干了什么
  • 尽量线程间分享尽量少的内容。避免出现死锁或者竞争条件
  • 随机数的随机种子,保证是固定的
  • 生成的可执行文件叫做simulation,总共有2个问题。每个问题都叫simulation?
  • 一个文件夹里,都是文件,没有子文件。打包这个文件夹????

使用makefile

构建c++或者c工程。

compile

可以使用c++11

Graceful Exit

对于5个问题,10s后都必须退出。
在main函数启动了所有线程之后,自己写个sleep(),计算10s,到10s修改全局变量。表明要能够退出了。

测试内存泄露Valgrind

valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./simulation

允许使用的api

• pthread_create
• pthread_join
• pthread_detach
• pthread_mutex_init
• pthread_mutex_destroy
• pthread_mutex_trylock
• pthread_mutex_lock
• pthread_cond_wait
• pthread_cond_signal
• pthread_cond_init
• you may also need the pthread_mutexattr_* functions

5个问题

Select A or B, and one of C,D,E to implement for your submission.

A The Producer-Consumer Problem

1 崩溃crashes

当读写同一块内存,不是原子操作,会造成崩溃。所以要加锁。

2 竞争条件race condition

当两个或多个线程可以访问共享数据并试图同时更改共享数据时,就会出现争用情况。因为线程调度算法可以随时在线程之间交换,所以您不知道线程尝试访问共享数据的顺序。

使用锁解决

3 死锁

互相等待,造成死锁
A lock occurs when multiple processes try to access the same resource at the same time.

One process loses out and must wait for the other to finish.

A deadlock occurs when the waiting process is still holding on to another resource that the first needs before it can finish.

So, an example:

Resource A and resource B are used by process X and process Y

X starts to use A.
X and Y try to start using B
Y ‘wins’ and gets B first
now Y needs to use A
A is locked by X, which is waiting for Y

4 条件变量

条件变量是一种用于在两个或多个线程之间发出信号的事件。一个或多个线程可以等待它获得信号再执行下面的操作,而另一个线程可以发出此信号。

5 题目要求

介绍
10个大小的数组,5个线程产生数组,5个线程使用数组。(输出,注意,消费了之后,缓冲区里的内容就没有了)
运行10s钟退出,没有crashes, race conditions or deadlocks.

生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bounded-buffer problem),是一个多线程同步问题的经典案例。该问题描述了共享固定大小缓冲区的两个线程——即所谓的“生产者”和“消费者”——在实际运行时会发生的问题。生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。与此同时,消费者也在缓冲区消耗这些数据。该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。

B The Readers and Writers Problem

介绍

读者—写者”问题—记录型信号量
一个数据文件或记录可被多个进程共享。

只要求读文件的进程称为“Reader进程”,其它进程则称为“Writer进程”。
允许多个进程同时读一个共享对象,但不允许一个Writer进程和其他Reader进程或Writer进程同时访问共享对象

C The Dining Philosophers’ Problem

https://www.tutorialspoint.com/dining-philosophers-problem-dpp
https://www.geeksforgeeks.org/dining-philosopher-problem-using-semaphores/

D The Sleeping Barbers Problem

E The Cigarette Smoker’s Problem

报告和github

You are to use Github as your project space. Make it a private project, and include your tutorial tutor as a collaborator.
In additional to the program solution, you are required to write a DOCX/PDF report describing the following.

  1. Identify yourself using student ID, Name, and Github project name/url
  2. Describe any issues and limitations of your implementation.
  3. The program solution implements two algorithms.
    a. One from A,B and one from C,D,E
  4. For each algorithm
    a. Describe in detail how each algorithm works, and how its design avoids things like deadlock,
    starvation, etc.
    i. Ensure that all variables have meaning, and describe these meaningful variables here.
    b. Describe in great detail, 2 real-world industrial / business scenarios, where each algorithm applies.
    i. Describe how your variables should translate to such an environment. For example, what
    would the Barber become?

评分标准

  • makefile,生成一个文件还是多个??要有版本控制
  • -Wall编译无错误和警告
  • 合法的代码规范,不使用魔法值。怎么去检查呢? clion太单纯了
  • 同步原语(4个标记)使用pthread库中的正确对象和方法来实现线程、锁定和休眠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值