









还剩21页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
江林大学计算机科学与通信工程学院操作系统课程设计报告题目linux系统下实现PV操作班级软件工程1401姓名_____________学号匚一指导老师2016年12月27日1/21print-mother putan orange{0}into plate..formattemp printC current plate=:plate lock.relcase#
4.vmutex mutex.setO#mutex设置为True,其它线程可以使用#
5.vorange orange.release time.sleeprandom.random defsoncount:global empty,mutex,lock,orange fori inrangecount:#
1.porange orange.acquire#orange-1#
2.pmutex mutex.clear#mutex设置为False其它线程将等待#
3.get orangeif lock.acquire:for fruitin plate:if fruit.startsvvithOrange:temp=fruit plate.remove fruitbreak print-son takean orange{0}from plate..formattemp printCcurrent plate=plate lock.rclcase#
4.vmutex mutex.setO#mutex设置为True,其它线程可以使用#
5.vempty empty.release#将empty+1time.sleeprandom.random defdaughtercount:global empty,mutex,lock,apple fori inrangecount:#
1.papple apple.acquire#apple-1#
2.pmutex mutex.clear#mutex设置为False其它线程将等待#
3.get apple10/21if lock.acquire:for fruitin plate:if fruit.startswithApple1:temp=fruit plate.reinovefruit breakprint-daughter takean apple{0}from plate.formaUtemp printcurrent plate=plate lock.release#
4.vmutex mutex.set#mutex设置为True,其它线程可以使用#
5.vemply empty.release#将empty+1timc.slccprandom.random if_name___==_main_#初始化苹果和橘子的个数apples_basket=[Apple-A,Apple-B,Apple-C,Apple-D]#模拟苹果篮子,一共有4个苹果要放入水果盘oranges_baskct=[Orange-A\Orange-B*,Orange-C]#模拟橘子篮子,一共有3个橘子要放入水果盘#创建4个线程father=threading.Threadname=,Father,,target=father,args=apples_basket,#father线程mother=threading.Threadname=Mother,target=mother,args=orangcs_baskct,#mother线程son=threading.Threadname=Son\target=son,args=lenoranges_basket,#son线程daughter=threading.Threadname=Daughter,,target=daughter,args=lenapples_basket,#daughter线程#启动线程daughter.start son.start father.start11/21mother.start4结果—一色voidpvoid-pc:*/MyCode/BasicTest-Python/os_cjesign votdQvotd-pc:~/MyCode/BastcTest-Python/os_destgn$Is nainprocess.py nain-thread.py vold0votd-pc:*/MyCode/8astcTest-Python/os_destgn$python3main-thread.py-father putan appleApple-D into plate.current plate=[Apple-D]main22812-another putan orangeOrange-C into plate.current plate=[,Apple-D*,orange-c]an-daughter takeappleApple-D fron plate current plate=[Orange-C]-son takean orangeOrange-C fronplate.current plate=[]orangeOrangc-B into plate.onother putan=[Orange-B]appleApple-Ccurrent plate intoplate.=[Orange-B*,Apple--father putan C]current plate-son takean orangeorange-B from plate.current plate=[Apple-C]-,daughter takean appleApplecfronplate current plate-[]-nother putan orangeOrange-A intoplate.current plate=[Orange-A]appteApple-B intofather putan plate.current plate-[Orange-A,Apple-B]an-daughter takeappleApple-B fronplate current plate=[,Orange-A*]-son takean orangcOrangeA fronplate,current plate=[]-father putan appleApple-A intoplate.current plate=[Apple-A]-daughter takean appleApple-A fronplatecurrent plate=[]votdQvotd-pc:~/MyCode/BastcTestPython/os_desl9n$1Figure4线程实现结果
2.进程实现1原理multiprocessing.py模块提供/对线程的操作创建线程multiprocessing.Process target,args,target是要运行的函数,args是函数所需的参数创建信号量multiprocessing.Semaphore value,value是信号量的初始值acquire信号量T,当为0时,阻塞当前线程releaseO信号量+1,大于0,唤醒等待此信号量的一个线程创建互斥锁,multiprocessing.Lock,acquireO加锁,releaseO解锁12/212变量定义list类型的apples_basket存放所有的苹果定义list类型的orangcs_basket存放所有的橘子定义list类型的plate当做水果盘,可以放入苹果和橘子定义方法father,用于将苹果放入水果盘,也就是把apples_basket的一个苹果,放入plate列表中并打印相关信息定义方法malher,用于将苹果放入水果盘,也就是把oranges_baskel的一个橘子,放入plate列表中并打印相关信息定义方法son,用于从水果盘取出一个橘子,也就是把plate的一个橘子,拿出来,并打印相关信息定义方法daughter,用于从水果盘取出一个苹果,也就是把plate的一个苹果,拿出来,并打印相关信息3代码import randomimport multiprocessingimport timeempty=multiprocessing.Semaphore5#盘子的容量apple=multiprocessing.SemaphoreO#同步苹果的信号量orange=multiprocessing.Scm叩horcO#同步橘子的信号量mutex=multiprocessing.Event#表示四个进程互斥的访问盘子manager=muhiprocessing.Manager plate=manager.list#模拟水果盘lock=multiprocessing.Lock#plate互斥锁mutex.set#设置为True deffatherbasket:global empty,mutex,lock,apple whilelenbasket!=0:#当苹果篮子中没有苹果,则终止#l.pempty empty.acquire#将容量empty减去1#
2.pmutex mutex.clear#mutex设置为False其它线程将等待13/21#
3.put appleif lock.acquire:temp=basket.popO plate.appendtemp#从苹果篮子里拿出一个苹果放入水果盘print-fathcr putan applc{0}intoplate..formattcmp printcurrent plate=plate lock.release#
4.vmutex mutex.set#mutex设置为True,其它线程可以使用#
5.vapple apple.release time.sleeprandom.random defmotherbasket:global empty,mutex,lock,orange whilelcnbaskct!=0:#当橘子篮子中没有橘子,则终止#
1.pempty empty.acquire#将容量empty减去1#
2.pmutex mutex.clear#mutex设置为False其它进程将等待#
3.putorange if lock.acquire:temp=basket.popO plate.appendtemp print-mother putan orange{0}intop1ate..for matte mp printcurrentplate=plate lock.rclcase#
4.vmutex mutex.set#mutex设置为True,其它进程可以使用#
5.vorange orange.release time.sleeprandom.random defsoncount:global empty,mutex,lock,orange fori inrangecount:#
1.porange orange.acquire#orange-114/21#
2.pmutex mutex.clear#mutex设置为False其它进程将等待#
3.get orangeif lock.acquire:for fruitin plate:if fruit.startswithOrange:temp=fruit plate.removefruit breakprint-son takean orange{0}from plate..formattemp printcurrentplate=plate lock.release#
4.vmutex mutex.set#mutex设置为True,其它进程可以使用#
5.vempty empty.release#将emply+I timc.slccprandom.randomO defdaughtercount:global empty,mutex,lock,apple fori inrangecount:#
1.papple apple.acquire#apple-1#
2.pmutex mutex.clear#mutex设置为False其它进程将等待#
3.get appleif lock.acqiiire:for fruitin plate:if fruit.startswithC Apple1:temp二fruit plate.removefruit breakprint-daughtcr takean applc{0}fromplate.formaUtemp printcurrentplate=plate lock.release#
4.vmutex mutex.set#mutex设置为True,其它进程可以使用#
5.vempty empty.release#将empty+115/21time.sleeprandom.random if_name___==_main_#初始化苹果和橘子的个数apples_baskct=lApple-A,Apple-B*,Apple-C,nApple-D n]#模拟苹果篮子,一共有4个苹果要放入水果盘oranges_basket=[Orange-A\Orange-B,Orange-Cl#模拟橘子篮子,一共有3个橘子要放入水果盘#创建4个进程father=multiprocessing.Processname=Father\target=father,args=applcs_baskct,#father进程mother=multiprocessing.Processname=Mother,target=mother,args=oranges_basket,#mother进程son=multiprocessing.Processname=,Son,target=son,args=lenoranges_basket,#son进程daughter=multiprocessing.Processname=,Daughter,target=daughter,args=Ienapples_basket,#daughter进程#启动进程daughtcr.start son.start father.slart mother.startO daughter.join son.join fathcr.join mother.joinO printHEnd!”4结果16/21Figure5进程实现结果
六、总结因为之前就在慕课网上看过linux使用的教程,学过centos字符界面的使用在课程设计前就装好了winlO+Ubuntu
16.04LTS双系统所以在熟悉linux使用上并没有花时间初期使用linux最大的麻烦就是安装软件安装方式有源码安装和二进制包安装使用熟练后装软件的速度还是比windows上慢不过各有优缺点因为软件开发在linux上比较方便,就逐渐从windows平台转移到linux上了Ubuntu这个发行版本的图形界面做的还是比较美观的使用linux的过程中让我对多用户操作系统更加了解,虽然windows系统也是多用户操作系统,但是平常使用的时候都是超级管理员身份,所以感触不深17/21Linux对文件权限管理十分严格,只有相应的用户身份才能获得使用这个文件的特定权限使用python的原因是python的语法简洁、优雅没有c和c++的指针,实现比较方便Java的代码太过繁琐,不适合快速实现想法Python的中文文档相对较少,有些模块是对c的封装,看不见源码所以在学习threading,py和multiprocessing,py的时候百度的东西十分琐碎,影响了开发速度本次实验后,对线程和进程也有了更深更具体的体会进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源具体区别如下地址空间和其它资源进程间相互独立,同一进程的各线程间共享某进程内的线程在其它进程不可见通信进程间通信IPC,线程间可以直接读写进程数据段(如全局变量)来进行通信一一需要进程同步和互斥手段的辅助,以保证数据的一致性调度和切换线程上下文切换比进程上下文切换要快得多Python对线程的支持不如java等语言好因为Python多线程下,每个线程的执行方式
1.取GIL
2.执行代码直到sleep或者是python虚拟机将其挂起
3.释放GIL可见,某个线程想要执行,必须先拿到GIL,我们可以把GIL看作是“通行证”,并且在一个python进程中,GIL只有一个拿不到通行证的线程,就不允许进入CPU执行每次释放GIL锁,线程进行锁竞争、切换线程,会消耗资源并且由于GIL锁存在,python里一个进程永远只能同时执行一个线程(拿到G1L的线程才能执行),这就是为什么在多核CPU±,python的多线程效率并不高
1.
1.
1.
2.
2.
2.
2.
3.
3.
3.
4.
4.
4.
4.
4.
172.
214.
4.
5.
5.
6.
6.
1.项目建设对环境的影响错误!未定义书签
6.
6.
7.
2.职业安全卫生健康对策与措施.....错误!未定义书签
7.
7.
8.
9.
9.
2.项目实施进度安排...错误!未定义书签
9.
10.
10.
10.
10.
20.
2110.5招标方式错误!第11章投资估算与资金筹措错误!未定义书签
11.
11.
12.
12.
12.3财务评价错误!
13.
14.
14.2建议错误!21/21
一、实验题目
1.Linux系统简单使用1认识Linux2ubuntu安装3终端的简单使用4python
3.
5.2源码安装
2.多线程和多进程同步方法解决水果分配问题水果分配的问题桌上有一只盘子,每次只能放入5只水果爸爸专放苹果,妈妈专放橘子,一个儿子专等吃盘子中的橘子,一个女儿专等吃盘子中的苹果.用P,V操作实现爸爸、妈妈、儿子、女儿进程的同步控制补充设有两个篮子,分别有若干个苹果或橘子,爸爸和妈妈将每次从水果篮子中拿出一个水果放入水果盘中,儿子女儿则挑选各自喜欢的水果1分析问题,写出伪代码2线程实现3进程实现
二、实验目的和要求
1.认识和学会使用linux系统Linux是一种可以在PC机上执行的类似UNIX的操作系统,是一个完全免费的操作系统1991年,芬兰学生Linus Torvalds开发了这个操作系统的核心部分,因为是Linus改良的minix系统,故称之为Linux.
2.理解线程和进程的互斥和同步原理3/21同步是操作系统级别的概念,是在多道程序的环境下,存在着不同的制约关系,为了协调这种互相制约的关系,实现资源共享和进程协作,从而避免进程之间的冲突,引入了进程同步进程互斥是间接制约关系当一个进程进入临界区使用临界资源时,另一个进程必须等待只有当使用临界资源的进程退出临界区后,这个进程才会解除阻塞状态
3.使用信号量和互斥量解决问题通过设置一个表示资源个数的信号量S,通过对信号量S的P和V操作来实现进程的的互斥通过设置一个表示资源个数的信号量S,通过对信号量S的P和V操作来实现进程的的互斥P和V操作分别来自荷兰语Passercn和Vrijgcvcn,分别表示占有和释放P V操作是操作系统的原语,意味着具有原子性P操作首先减少信号量,表示有一个进程将占用或等待资源,然后检测S是否小于0,如果小于0则阻塞,如果大于0则占有资源进行执行V操作是和P操作相反的操作,首先增加信号量,表示占用或等待资源的进程减少了1个然后检测S是否小于0,如果小于0则唤醒等待使用S资源的其它进程
三、环境配置
1.安装ubuntu1下载系统镜像,ubuntu-
16.
04.l-desktop-amd
64.iso2制作启动U盘,使用Ultraiso软件将系统镜像写入U盘3开机进入BIOS界面,从U盘启动4对磁盘分区,等待安装结束5设置root密码,sudo passwdroot命令后输入两遍密码
2.熟悉ubuntu的使用4/21常用命令
(1)SU命令,切换用户
(2)pwd命令,打印当前工作目录的绝对路径
(3)1s命令,打印当前目录下的文件
(4)cd命令,切换工作目录
(5)mv命令,移动文件或目录
(6)rm命令,删除文件或目录
(7)shutdown命令,关闭计算机
(8)reboot命令,重启电脑
(9)tar命令,解压命令
3.安装vim编辑器在终端下运行apt-get install-y vim即可安装结束后,输入vim命令,显示如下图就说明安装成功:VIM-Vi IMprovedversion
7.
4.160by BramHoolcnndr etAl•Modified bybugzillaG-idhat.cu,4is op«n freelydistributable Helppoor children1n UgandaI typehelp1ccfEnter forInformation type:qEnter toexit typehelp Enteror F1for on-line helptypehelp version/£nr•r forversion nnfoe,e-i AHFigure1安装vim后输入vim显示的结果
4.源码安装python
3.
5.2因为linux内置的python的版本为
2.
7.5,所以为了程序设计的方便,使用
3.
5.2版本的终端下输入python即可进入python交互模式5/21安装前:void0Void-pc:python Python
2.
7.12default,Nov192016,06:48:10[GCC
5.
4.020160609]on Xinux2Type help,copyright,credits orlicense formore information.»exit vold@votd-pc:-$|j Figure2内置python运行结果安装步骤1进入下载目录,tar-zxvf Python-
3.
5.
2.tar.gz解压到当前目录下2进入解压目录,cd Python-
3.
5.23验证系统配置,./configure4编译和安装,makemake install5建立软链接,在/usr/bin目录下生成python3的软链接文件安装结果votd@votd-pc:-$python3Python
3.
5.2default,Dec242016,11:36:59[GCC
5.
4.020160609]on linuxType help,copyright,credits orlicense formore information.»exit votd@volci-pc::~$,Figure3python3安装结果设计思路
1.题目分析father、mather、son、daughter是四个线程或进程盘子plate是它们共享的变量,对盘子的操作要互斥Father和daughter要对apple同步Mother和son要对orange同步
2.伪代码fatherwhileTrue:pcmpty
6.21p mutex put apple vmutex vapplemotherwhileTrue:pempty pmutexputorange vmutexvorange sonwhilcTruc:porange pmutex get orangevmutex vemptydaughter:whiieTrue:papple pmutexgetapplevmutexvempty7/21
五、代码实现
1.线程实现1原理threading,py模块提供了对线程的操作创建线程threading.Thread target,args,target是要运行的函数,args是函数所需的参数创建信号量threading.Semaphore value,value是信号量的初始值acquire0信号量T,当为0时,阻塞当前线程release信号量+1,大于0,唤醒等待此信号量的一个线程创建互斥锁,threading.LockO,acquire0加锁,release解锁2变量定义list类型的apples_basket存放所有的苹果定义list类型的oranges_basket存放所有■的橘子定义list类型的plate当做水果盘,可以放入苹果和橘子定义方法father,用于将苹果放入水果盘,也就是把apples_basket的一个苹果,放入plate列表中并打印相关信息定义方法mather,用于将苹果放入水果盘,也就是把oranges_basket的一个橘子,放入plate列表中并打印相关信息定义方法son,用于从水果盘取出一个橘子,也就是把plate的一个橘子,拿出来,并打印相关信息定义方法daughter,用于从水果盘取出一个苹果,也就是把plate的一个苹果,拿出来,并打印相关信息3代码import randomimport threadingimport timeempty=threading.Scmaphorc5#盘子的容量apple=thrcading.ScmaphorcO#同步苹果的信号量orange=threading.SemaphoreO#同步橘子的信号量mutex=threading.Event#表示四个进程互斥的访问盘子
8.21plate=Iist#模拟水果盘lock=threading.Lock#plate互斥锁mutex.set#设置为True deffatherbasket:global empty,mutex,lock,apple whilelenbasket!=0:#当苹果篮子中没有苹果,则终止#
1.pcmpty empty.acquire#将容量empty减去1#
2.pmutex niutex.clear#mutex设置为False其它线程将等待#
3.put appleiflock.acquire:temp=basket.popO platc.appcndtcmp#从苹果篮子里拿出一个苹果放入水果盘print-father putan apple{0}intoplate..fonnattemp printcurrentplate=plate lock.release#
4.vmutex mutex.set#mulex设置为True,其它线程可以使用#
5.vapple apple.release time.slecprandom.randomO defmother basket:global empty,mulex,lock,orange whilelenbasket!=0:#当橘子篮子中没有橘子,则终止#
1.pempty empty.acquireO#将容量empty减去1#
2.pmutcx mutcx.clcar#mutex设置为False其它线程将等待#
3.putorange iflock.acquire:temp=basket.popO plate.appendtemp
9.21。


