




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、山东大学操作系统实验报告4进 程同步实验计算机科学与技术学院实验报告实验题目:实验四、进程同步实验学号:日期:20120409班级:计基地12姓名:实验目的:加深对并发协作进程同步与互斥概念的理解,观察和体验并发进程同步与互斥 操作的效果,分析与研究经典进程同步与互斥问题的实际解决方案。了解Lin UX系统中IPC进程同步工具的用法,练习并发协作进程的同步与互斥操作的编程 与调试技术。实验内容:抽烟者问题。假设一个系统中有三个抽烟者进程,每个抽烟者不断地卷烟并抽烟。 抽烟者卷起并抽掉一颗烟需要有三种材料: 烟草、纸和胶水。一个抽烟者有烟草, 一个有纸,另一个有胶水。系统中还有两个供应者进程,它
2、们无限地供应所有三 种材料,但每次仅轮流提供三种材料中的两种。得到缺失的两种材料的抽烟者在 卷起并抽掉一颗烟后会发信号通知供应者,让它继续提供另外的两种材料。这一 过程重复进行。请用以上介绍的IPC同步机制编程,实现该问题要求的功能。硬件环境:处理器:Intel? COre ?i3-2350M CPU 2.30GHZ × 4图形:In tel?San dybridgeMobilex86MMXSSE2内存:4G操作系统:32位磁盘:20.1 GB软件环境:UbU ntu13.04实验步骤:(1) 新建定义了 PrOdUCer 和 conSUmer共用的IPC函数原型和变量的ipc.h文
3、件。(2) 新建 ipc.c 文件,编写 PrOdUCer 和 COnSUmer 共用的IPC的具体相应函数。(3) 新建PrOdUCer文件,首先定义 PrOdUCer 的一些行为,利用系统调用,建立共享内存区域, 设定其长度并获取共享内存的首地址。 然后设定 生产者互斥与同步的信号灯,并为他们设置相应 的初值。当有生产者进程在运行而其他生产者请 求时,相应的信号灯就会阻止他,当共享内存区 域已满时,信号等也会提示生产者不能再往共享 内存中放入内容。(4) 新建 ConSUmer 文件,定义 conSUmer的一些行为,利用系统调用来创建共享内存区域,并 设定他的长度并获取共享内存的首地址。
4、然后设定消费者互斥与同步的信号灯,并为他们设置相 应的初值。当有消费进程在运行而其他消费者请 求时,相应的信号灯就会阻止它,当共享内存区 域已空时,信号等也会提示生产者不能再从共享 内存中取出相应的内容。运行的消费者应该与相应的生产者对应起来, 只有这样运行结果才会正确。结论分析与体会: 实现方式:COn sume:#i nclude "ipc.h"int main (i nt argc,char *argv) int rate = 3;int con SUmerid=atoi(argv1);buff_h = 101;buff_ nu mber = 1;Cget_h = 1
5、03;Cget_ nu mber = 1;Shm_flg = IPC_CREAT | 0644;buff_Ptr = (Char *)set_shm(buff_h,buff_ nu mber,shm_flg);Cget_Ptr = (int *)set_Shm(Cget_h,cget_ nu mber,shm_flg); prod_h = 201;PmtX_h = 202;cons_h = 301;CmtX_h = 302;Sem_flg = IPC_CREAT | 0644;Sem_val = buff_ nu mber;prod_Sem = Set_Sem(PrOd_h,sem_val,s
6、em_flg);Sem_val = 0;con s_Sem = Set_Sem(CO ns_h,sem_val,sem_flg);Sem_val = 1;CmtX_Sem = Set_Sem(CmtX_h,sem_val,sem_flg);if(con SUmerid=0)*cget_ptr=0;while(1)if(buff_ptr0-'A'=c on SUmerid)dow n(co ns_sem);dow n(cmtx_sem); sleep(rate);if(buff_ptr0='A')Printf("%dThe conSUmer has gl
7、ue.nTheconSUmer getstobacco and paper n",getpid(); if(buff_ptr0='B')Printf("%dThe conSUmer has paper.nTheconSUmer getstobacco and gluen",getpid(); if(buff_ptr0='C')Prin tf("%d The con SUmer has tobacco.nThe con SUmer getsglue and paper n",getpid(); *cget_ptr
8、= (*cget_pt r+1); if(*cget_ptr%2=0) buff_ptrO='D'elsebuff_ptrO='E'UP(CmtX_sem);UP(PrOd_sem);retur n EXIT_SUCCESS;PrOdUCer :#i nclude "ipc.h"nt main (i nt argc,char *argv)int rate=3;int PrOdUCerid=atoi(argv1); buff_h=101;buff_ nu mber=1;PPUt_h=102;PPUt_ nu mber=1;shm_flg=IPC
9、_CREAT|0644;buff_Ptr = (Char *)set_shm(buff_h,buff_ nu mber,shm_flg);PPUt_Ptr = (int *)set_Shm(PPUt_h,pput_ nu mber,shm_flg); prod_h = 201;PmtX_h = 202;cons_h = 301;CmtX_h = 302;Sem_flg = IPC_CREAT|0644;Sem_val = buff_ nu mber;prod_Sem = Set_Sem(PrOd_h,sem_val,sem_flg);Sem_val = 0;con s_Sem = Set_Se
10、m(CO ns_h,sem_val,sem_flg);Sem_val = 1;PmtX_Sem = Set_Sem(PmtX_h,sem_val,sem_flg); if(producerid=0)buff_ptr0='D'*pput ptr=0;while(1)if(buff_ptrO-D=PrOduCerid) dow n( prod_sem); dow n(pmtx_sem);*pput_Ptr = (*pput_Pt r+1)%3; if(*pput_Ptr=0) buff_ptr0 = 'A'Prin tf("%dPaPern",g
11、etpid();if(*PPut_Ptr=1) buff_ptr0 = 'B' Prin tf("%dThePrOdUCergivestobaccoandThePrOdUCergivestobaccoandgluen",getpid();if(*pput_ptr=2)buff_ptr0 = C;Printf("%d The PrOdUCer gives glue and papern",getpid(); sleep(rate);UP(PmtX_sem);UP(C on s_sem); _retur n EXIT_SUCCESS;pc.h
12、 :#i nclude "ipc.h"nt get_ipc_id(Char *proc_file,h_t h) FlLE *pf;int m,n;Char Iin eBUFSZ,columBUFSZ; if(pf = fope n(proc_file,"r") = NULL) Perror('卩roc file not ope n"); exit(EXIT_FAILURE);fgets(line, BUFSZ,pf); while(!feof(pf) m = n = 0;fgets(line, BUFSZ,pf); while(l in
13、 em='')m+;While(Ii nem !='') COIu mn+ = Iin em+;colum n = '0' if(atoi(colum) != h) con ti nue;n=0; while(l in em='') m+;while(linem !='') colu mn+ = Iin em+;colum n = '0' m = atoi(colum); fclose(pf); return m; fclose(pf); return -1;nt dow n(i nt Sem_id
14、) StrUCt SembUf buf; buf.sem_op = -1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(semd,&buf,1) <0) PerrOr("dow n error "); exit(EXIT_FAILURE); return EXIT_SUCCESS;nt up(i nt Sem_id) StrUCt SembUf buf; buf.sem_op = 1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(
15、semd,&buf,1) <0) PerrOr("up error "); exit(EXIT_FAILURE); retur n EXIT_SUCCESS;int set_sem(h_t sem_h,i nt sem_val,i nt sem_flg) int Semd;Sem _uns Sem_arg;if(semd = get_ipc_id("/proc/SySViPC/sem",sem_h) V 0 ) if(sem_id = Semget(Sem_h,1,sem_flg) V 0) PerrOr("semaphore C
16、reate error"); exit(EXIT_FAILURE);Sem_arg.val = Sem_val;if(semctl(semd,O,SETVAL,sem_arg) <0) PerrOr("semaphore Set error"); exit(EXIT_FAILURE);return Semd; _Char * Set_shm(h_t Shm_h,i nt Shm_ nu mber,i nt Shm_flg) int m,shmd;Char * Shm_buf;if(shm_id = get_ipc_id("/proc/SySViPC
17、/shm",shm_h) V O ) if(shmd = Shmget(Shm_h,shm _nu mber,shm_flg) <0) PerrOr("ShareMemory Set error"); exit(EXIT_FAILURE);if(shm_buf = (Char *)Shmat(Shmd,0,0) V (Char *)0) PerrOr("get ShareMemOry error");exit(EXIT_FAILURE);for(m=0; m<shm_ nu mber; m+) Shm_bufm = 0; _if(s
18、hm_buf = (Char *)Shmat(Shm_id,0,0) V (Char *)0)PerrOr("get ShareMemOry error"); exit(EXIT_FAILURE);retur n Shm_buf;Iint Set_msq(h_t msq_h,i nt msq_flg) int msqd;if(msq_id = get_ipc_id("/proc/SySViPC/msg",msq_h) V 0 ) if(msq id = msgget(msq h,msq flg) V 0)PerrOr("messageQueue
19、 Set error"); exit(EXIT_FAILURE);return msqd;实验结果:稱宀""IllCUrku-LeOYO-IdCaPad-Y471 A: lab4 ILkUrIfLtkUn * LenDVO * I a P ad - Y471A:-S Cd lb4 ltkunyitkurLeovo-ideaad-Y47iAj*,Lab4 ,/producer 0132B7hePrDdUCergivestobaccoand glue33Z0Theproducergivesglue andPaPer3320TheProclUCergluestobacc
20、oandglue3329TheProdUCergivestbaccandPaPer3320Theproducergive¾Lkje andPaPerThBPrnElUCergtesrohsrcanrlglue3329ThePrOdUCergivestobaccoandPaPer3320TheProdUCergives9lue rdPdPerSJ2&ThPPrOdUCFrgivestobaccoandglue3320ThePrOdiJCCrgivestobaccodPOPCr3320ThePrQdUCergivesglue andPdPer3320Th色producertoba
21、ccoandglu332BTheproducergM古tobaccoandPaP 电 r*5320IhePrDdUCergivesglue andPaPer33<0ThePrOelUCergluestbaccDandglue132TheProClUCergivestobaccaandPaPerOsG IikUnUlCUn-LenOVOJdeaPad-Y471 A: kb4IlkUneUkUnLenovo-IdeaPad-Y471A:*/lab4S ./producer 1 3321 3321 3321 3321 3321 332133213322 3321 3321 3321 3321
22、3321 3321 3321 3321 3321 3321 3321 3321 3321 3321The The Ihe Ihe The The 7he The The Ihe IhC The The The The Ihe The The Ihe The The ThePrOdUCer PrOdUCer producer producer producer PrOdUCer PrOdUCer PrOdUCer PrOdUCer PrOdUCer PrOdUCCr PrOdUCer producer PrOdUCer producer producer producer PrOdUCer Pr
23、OdUCer PrOdUCer PrOdUCer PrOddCergives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives gives givestobacco and PaPer glue and PdPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPer glue and PaPer tob
24、acco and glue tobacco and PaPer glue and PaPGr tobcco and glue tobacco and PaPer glue and PdPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPer glue and PaPer tobacco and glue tobacco and PaPerOsG IikUnUlCUn-LenOVOJdeaPad-Y471 A: kb4IlkUneUkUnLenovo-IdeaPad-Y471A
25、:*/lab4S ./consumer5 bash: .cosuner:没有那个文件或口录ItkUnlikun-Lenovo-IdeaPad-Y471A:-/Iab4$ ./consumer 3096 The COnSUnerThe consumer gets3096 The COnSUnerThe consumer getsMfIqfi Tha roumprThe COnSUner gets3096 The COnSUnerThe consumer gets3096 ThC COnSUnCrThe consumer gets3096 Ihe COnSUfnerThe consumer get
26、s3096 The COnSUnerThe consumer gets3096 The COnSUnerThe consumer gets3096 7he COnSUnerThe COnSUner gets3096 The COnSUnerThe COnSUmer gets3096 The COnSUInerhas g*lue tobacco and has glue, tobacco and hx gl up. tobacco and has glue, tobacco and has glue, tobcco and has glue, tobacco and has glue, toba
27、cco and has glue tobacco and has glue, tobacco and has glue, tobacco and has glue.PaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPerPaPer CG IikUnukUn-LenOVO-IdeaPad-Y471 A: Ub4ItkUnIllkUn-Lenovo-IdeaPad-Y471A:*$ Cd Idb4ItkUn¢1IkUnLQnovoIdwaPadY47ia:/lab4$ /consumer 1 3318 The COnSUnerThe consumer g
28、ets3318 Ihe consumerThe consumer gets3318 The consumerThe COnSUmer gets3318 The consumerThe consumer gets3318 7he COnSUfnerThG consumer gets3318 The COnSUnerThe COnSUmer gets3318 The consumerThe COnSUmer gets3318 Ihe consutnerThe COnSUmer gets3318 The consumerThe COnSUmer gets3318 The COnSUmerThe co
29、nsumer gets3318 The consumerhas paper, tobacco and has PaPer tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has paper, tobacco and has PaPer tobacco and has paper, tobacco and has paper, tobacco and has PaPer.gluegluegluegluegluegluegluegluegl
30、ueglueThQ SnSUEgets toba"o and glueO * LlkurHkun-Lcnovo-ldcaPad-Y471 A: lab4 Hku0ltkun-LenaVO-IdedPdd-Y471A:$ Cd Idb4I-Lkunyltkur * Lenovo-ideaPad-Y471A:*/Iato45 couFler 2 1319 The CclfiSUl*r h白右 tobaccoThe consumer geti glut and PdPer3319 The CCnlSUner has tobacco.The COnSUr,r gets glu and PaP
31、er3319 The CQnSUrTer h日毎 tQbac»The consumer gets glue And pppr J319 The cosurcr has tobacco.The COnSrr sets glue ODd PdPer 3319 ThP Consurner has tobacco.The COnSUrrc 9cts glue d PHaPCr 3319 The COnSner has tobacco. Tha COrirer gets glue ad PaPer Ui9 The consumer has tobacco The CDhSJrr gets gl
32、ue and PaPer 3319 The CeInlSUner hmm tobaccoI The ConSJr-r gets glu and PaPer 3319 The csuner has tobaccot The COnSLHTPr gets gLup AnCI PaPPr J319 The consumer has tobacco. The consrr gets glue and PdPer 5319 The COnSUWl has tobacco. Thm COnurr get呂 glumpafm分析:多进程的系统中避免不了进程间的相互关系。进程互斥是进程之间发生的一种间接性作用,一般是程序不希望的。 通常的情况是两个或两个以上的进程需要 同时访问某个共享变量。我们一般将发生能够问共享变量的程序段称为临界区。 两个进程不能同时进入临界区,否则就会导致数据的不一致,产生与时间有关的错误。解决互斥问题应该满足互斥和公平两个原则,即任意时刻只能允许一个进 程处于同一共享变量的临界区,而且不能让任一进程无限期地等待。进程同步是进程之间直接的相互作用,是合作进程间有意识的行为,典型的 例子是公共汽车上
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 石大学前儿童保育学课件1-7眼睛耳神经
- 云安全与边缘计算的协同防护-洞察阐释
- 遗传学课程内容更新与跨学科融合的创新模式
- 激励社会力量参与老年助餐服务的可行路径
- 心理健康课思维导图
- 2025至2030年中国波导合分路器行业投资前景及策略咨询报告
- 2025至2030年中国水瓶座图案拼图行业投资前景及策略咨询报告
- 2025至2030年中国气密测试机行业投资前景及策略咨询报告
- 2025至2030年中国橙柑桔清洗打蜡机行业投资前景及策略咨询报告
- 2025至2030年中国木炭烘干机行业投资前景及策略咨询报告
- 2024 - 2025学年浙美版一年级下册美术期末考试试卷及答案
- 口腔合伙人合同协议书
- 2025年中国车载显示行业市场前景预测及投资价值评估分析报告
- DB32T3436-2018 智能信包箱运营管理服务规范
- 地下工程施工安全防范措施
- 商业银行领导力提升培训心得体会
- 校招中建八局面试题目及答案
- 高效规划优化工业园区的基础设施布局
- 新能源汽车基础知识培训课件
- 客户入厂安全培训
- 浙江省医疗服务价格项目目录
评论
0/150
提交评论