操作系统linux系统下实现pv操作_第1页
操作系统linux系统下实现pv操作_第2页
操作系统linux系统下实现pv操作_第3页
操作系统linux系统下实现pv操作_第4页
操作系统linux系统下实现pv操作_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGEPAGE21/21 计算机科学与通信工程学院操作系统课程设计报告题目:linux系统下实现PV操作班级: 软件工程1401姓名:学号: 3指导老师:20161227目录一、实验题目 3二、实验目的和要求 3三、环境配置 4四、设计思路 6五、代码实现 8六、总结 17一、实验题目LinuxLinuxubuntu终端的简单使用python3.5.2多线程和多进程同步方法解决水果分配问题:水果分配的问题:桌上有一只盘子,每次只能放入5只水果。爸爸专放苹果,妈妈专放橘子,一个儿子专等吃盘子中的橘子,一个女儿专等吃盘:设有两个篮子,分别有若干个苹果或橘子,爸爸和妈妈将每次从水果篮子中拿出一个水果放入水果盘中,儿子女儿则挑选各自喜欢的水果。分析问题,写出伪代码线程实现进程实现二、实验目的和要求认识和学会使用linuxLinuxPCUNIX的操作系统,是一个完全免费的操作系统。1991LinusTorvaldsLinusminixLinux.理解线程和进程的互斥和同步原理:同步是操作系统级别的概念,进程之间的冲突,引入了进程同步。进程互斥是间接制约关系。当一个进程进入临界区使用临界资源时,另一个进程必须等待。只有当使用临界资源的进程退出临界区后,这个进程才会解除阻塞状态。使用信号量和互斥量解决问题:S,SPV来实现进程的的互斥。S,SPV来实现进程的的互斥。PVPasseren放。PVPS0,00VP1S0,0S资源的其它进程。三、环境配置安装ubuntu下载系统镜像,ubuntu-16.04.1-desktop-amd64.isoUUltraisoUBIOSU对磁盘分区,等待安装结束。root,sudopasswdroot命令后输入两遍密码。熟悉ubuntu常用命令:supwdlscdmvrmshutdownreboottar安装vim在终端下运行apt-getinstall–yvim即可。安装结束后,输入vim命令,显示如下图就说明安装成功:Figure1安装vim后输入vim显示的结果源码安装python3.5.2linuxpython2.7.5,3.5.2pythonpython安装前:Figure2内置python运行结果安装步骤:进入下载目录,tar–zxvfPython-3.5.2.tar.gz目录下进入解压目录,cdPython-3.5.2验证系统配置,./configure编译和安装,make&&makeinstall建立软链接,在/usr/binpython3安装结果:四、设计思路

Figure3python3安装结果题目分析:father、mather、son、daughterplateFatherdaughterappleMothersonorange伪代码:father:while(True):p(empty)p(mutex)putapplev(mutex)v(apple)mother:while(True):p(empty)p(mutex)putorangev(mutex)v(orange)son:while(True):p(orange)p(mutex)getorangev(mutex)v(empty)daughter:while(True):p(apple)p(mutex)getapplev(mutex)v(empty)五、代码实现线程实现原理threading.py模块提供了对线程的操作。创建线程threading.Thread(target, args),target是要运行的数,args是函数所需的参数。创建信号量threading.Semaphore(value),valueacquire()信号量-1,0release()信号量+1,大于0,唤醒等待此信号量的一个线程。创建互斥锁,threading.Lock(),acquire()加锁,release()解锁。变量listapples_basket存放所有的苹果listoranges_basket定义list类型的plate当做水果盘,可以放入苹果和橘子定义方法father,用于将苹果放入水果盘,也就是把apples_basket的一个苹果,放入plate列表中。并打印相关信息。定义方法mather,用于将苹果放入水果盘,也就是把oranges_basket的一个橘子,放入plate列表中。并打印相关信息。sonplate橘子,拿出来,并打印相关信息。daughterplate一个苹果,拿出来,并打印相关信息。代码importrandomimportthreadingimporttimeempty=threading.Semaphore(5) #盘子的容量apple=threading.Semaphore(0) #同步苹果的信号量orange=threading.Semaphore(0) #同步橘子的信号量mutex=threading.Event() #表示四个进程互斥的访问盘子plate=list() #模拟水果盘lock=threading.Lock() #plate互斥锁mutex.set() #设置deffather(basket):globalempty,mutex,lock,applewhilelen(basket)!=0: #当苹果篮子中没有苹果,则终止#1.p(empty)empty.acquire() #empty#2.p(mutex)mutex.clear() #mutex设置False其它线程将等待#3.putappleiflock.acquire():temp=basket.pop()plate.append(temp) #从苹果篮子里拿出一个苹果放入水果盘

print('->fatherputanapple({0})intoplate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)mutex.set() #mutex设置,其它线程可以使用#5.v(apple)apple.release()time.sleep(random.random())defmother(basket):globalempty,mutex,lock,orangewhilelen(basket)!=0: #当橘子篮子中没有橘子,则终止#1.p(empty)empty.acquire() #empty#2.p(mutex)mutex.clear() #mutex设置False其它线程将等待#3.put(orange)iflock.acquire():temp=basket.pop()plate.append(temp)print('->motherputanorange({0})intoplate.'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它线程可以使用#5.v(orange)orange.release()time.sleep(random.random())defson(count):globalempty,mutex,lock,orangeforiinrange(count):#1.p(orange)orange.acquire() #orange#2.p(mutex)mutex.clear() #mutex设置False其它线程将等待#3.getorangeiflock.acquire():forfruitinplate:iffruit.startswith('Orange'):temp=fruitplate.remove(fruit)breakprint('->sontakeanorange({0})fromplate.'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它线程可以使用#5.v(empty)empty.release() #将empty+time.sleep(random.random())defdaughter(count):globalempty,mutex,lock,appleforiinrange(count):#1.p(apple)apple.acquire() #apple#2.p(mutex)mutex.clear() #mutex设置False其它线程将等待#3.getappleiflock.acquire():forfruitinplate:iffruit.startswith('Apple'):temp=fruitplate.remove(fruit)breakprint('->daughtertakeanapple({0})fromplate'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它线程可以使用#5.v(empty)empty.release() #将empty+time.sleep(random.random())if name ==' main ':#初始化苹果和橘子的个数apples_basket=['Apple-A','Apple-B','Apple-C',"Apple-D"] #模拟苹果篮子,一共有4个苹果要放入水果盘oranges_basket=['Orange-A','Orange-B','Orange-C'] #模拟子篮3个橘子要放入水果盘#创建4个线程father=threading.Thread(name='Father',target=father,args=(apples_basket,)) #father线程mother=threading.Thread(name='Mother',target=mother,args=(oranges_basket,)) #mother线程son=threading.Thread(name='Son',target=son,args=(len(oranges_basket),)) #son线程daughter=threading.Thread(name='Daughter',target=daughter,args=(len(apples_basket),)) #daughter线程#启动线程daughter.start()son.start()father.start()mother.start()结果Figure4线程实现结果进程实现原理multiprocessing.py模块提供了对线程的操作。创建线程multiprocessing.Process(target,args),target是要运行的函数,args是函数所需的参数。multiprocessing.Semaphore(value),valueacquire()-1,当为0release()信号量+10,唤醒等待此信号量的一个线程。创建互斥锁,multiprocessing.Lock(),acquire()解锁。变量listapples_basket存放所有的苹果listoranges_basket定义list类型的plate当做水果盘,可以放入苹果和橘子定义方法father,用于将苹果放入水果盘,也就是把apples_basket的一个苹果,放入plate列表中。并打印相关信息。定义方法mather,用于将苹果放入水果盘,也就是把oranges_basket的一个橘子,放入plate列表中。并打印相关信息。sonplate橘子,拿出来,并打印相关信息。daughterplate一个苹果,拿出来,并打印相关信息。代码importrandomimportmultiprocessingimporttimeempty=multiprocessing.Semaphore(5) #盘子的容量apple=multiprocessing.Semaphore(0) #同步苹果的信号量orange=multiprocessing.Semaphore(0) #同步橘子的信号量mutex=multiprocessing.Event() #表示四个进程互斥的访问盘manager=multiprocessing.Manager()plate=manager.list() #模拟水果盘lock=multiprocessing.Lock() #plate互斥锁mutex.set() #设置deffather(basket):globalempty,mutex,lock,applewhilelen(basket)!=0: #当苹果篮子中没有苹果,则终止#1.p(empty)empty.acquire() #empty#2.p(mutex)mutex.clear() #mutex设置False其它线程将等待入水果盘

#3.putappleiflock.acquire():temp=basket.pop()plate.append(temp) #从苹果篮子里拿出一个苹果放print('->fatherputanapple({0})intoplate.'.format(temp))print(' currentplate=',plate)lock.release()#4.v(mutex)mutex.set() #mutex设置,其它线程可以使用#5.v(apple)apple.release()time.sleep(random.random())defmother(basket):globalempty,mutex,lock,orangewhilelen(basket)!=0: #当橘子篮子中没有橘子,则终止#1.p(empty)empty.acquire() #empty#2.p(mutex)mutex.clear() #mutex设置False其它进程将等待#3.put(orange)iflock.acquire():temp=basket.pop()plate.append(temp)print('->motherputanorange({0})intoplate.'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它进程可以使用#5.v(orange)orange.release()time.sleep(random.random())defson(count):globalempty,mutex,lock,orangeforiinrange(count):#1.p(orange)orange.acquire() #orange#2.p(mutex)mutex.clear() #mutex设置False其它进程将等待#3.getorangeiflock.acquire():forfruitinplate:iffruit.startswith('Orange'):temp=fruitplate.remove(fruit)breakprint('->sontakeanorange({0})fromplate.'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它进程可以使用#5.v(empty)empty.release() #将empty+time.sleep(random.random())defdaughter(count):globalempty,mutex,lock,appleforiinrange(count):#1.p(apple)apple.acquire() #apple#2.p(mutex)mutex.clear() #mutex设置False其它进程将等待#3.getappleiflock.acquire():forfruitinplate:iffruit.startswith('Apple'):temp=fruitplate.remove(fruit)breakprint('->daughtertakeanapple({0})fromplate'.format(temp))print(' currentplate=',lock.release()#4.v(mutex)mutex.set() #mutex设置,其它进程可以使用#5.v(empty)empty.release() #将empty1time.sleep(random.random())if name ==' main ':#初始化苹果和橘子的个数apples_basket=['Apple-A','Apple-B','Apple-C',"Apple-D"] #模拟苹果篮子,一共有4个苹果要放入水果盘oranges_basket=['Orange-A','Orange-B','Orange-C'] #模拟子篮3个橘子要放入水果盘#创建4个进程father=multiprocessing.Process(name='Father',target=father,args=(apples_basket,)) #father进程mother=multiprocessing.Process(name='Mother',target=mother,args=(oranges_basket,)) #mother进程son=multiprocessing.Process(name='Son',target=son,args=(len(oranges_basket),)) #son进程daughter=multiprocessing.Process(name='Daughter',target=daughter,args=(len(apples_basket),)) #daughter进程#启动进程daughter.start()son.start()father.start()mother.start()daughter.join()son.join()father.join()mother.join()print("End!")结果六、总结

Figure5进程实现结果linuxcentos7win10Ubuntu16.04LTS使用上并没有花时间。linuxwindowslinuxwindowslinuxUbuntu这个发行版本的图形界面做的还是比较美观的。linuxwindowsLinux特定权限。pythonpythoncc+现比较方便。JavaPythoncthreading.pymultiprocessing.py响了开发速度。功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调CPU一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。具体区别如下:内的线程在其它进程不可见。通信:进程间通信IPC,线程间可以直接读写进程数据段(如全局变量)来进行通信——需要进程同步和互斥手段的辅助,以保证数据的一致性。调度和切换:线程上下文切换比进程上下文切换要快得多。PythonjavaPython执行方式:GILsleeppython3GIL可见,某个线程想要执行,必须先拿到GIL,我们可以把GILpython,GILCPUGILGILpythonGILCPUpythonpythonIOIOpythonIO在多核CPU下,用多进程效率高,在单核CPU下,使用多线程效率高。此次课程设计收获最大的还是学会了怎么用信号量控制线程和多个进之间的同步和互斥

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论