版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机科学与技术学院实验报告实验题目:实验四、进程同步实验学号:日期:20120409班级:计基地12姓名:实验目的:加深对并发协作进程同步与互斥概念的理解,观察和体验并发进程同步与互斥 操作的效果,分析与研究经典进程同步与互斥问题的实际解决方案。了解Lin ux系统中IPC进程同步工具的用法,练习并发协作进程的同步与互斥操作的编程 与调试技术。实验内容:抽烟者问题。假设一个系统中有三个抽烟者进程,每个抽烟者不断地卷烟并抽烟。 抽烟者卷起并抽掉一颗烟需要有三种材料: 烟草、纸和胶水。一个抽烟者有烟草, 一个有纸,另一个有胶水。系统中还有两个供应者进程,它们无限地供应所有三 种材料,但每次仅轮流
2、提供三种材料中的两种。得到缺失的两种材料的抽烟者在 卷起并抽掉一颗烟后会发信号通知供应者,让它继续提供另外的两种材料。这一 过程重复进行。请用以上介绍的IPC同步机制编程,实现该问题要求的功能。硬件环境:处理器:In tel? Core? i3-2350M CPU 2.30GHzx 4图形:In tel? Sa ndybridge Mobile x86/MMX/SSE2内存:4G操作系统:32位磁盘:20.1 GB软件环境:ubu ntu13.04实验步骤:(1) 新建定义了 producer和consumer共用的IPC函数原型和变量的ipc.h文件。(2) 新建ipc.c文件,编写prod
3、ucer 和consumer 共用的IPC的具体相应函数。(3) 新建Producer文件,首先定义producer的一些行为,利用系统调用,建立共享内存区域,设定其长度并获取共享内存的首地址。然后设定生产者互斥与同步的信号灯,并为他 们设置相应的初值。当有生产者进程在运行而其他生产者请求时,相应的信号灯就会阻止他,当共享内存区域已满时,信号等也会提示生产者不能再往共享内存中放入内容。(4) 新建Consumer文件,定义consumer的一些行为,利用系统调用来创建共享内存 区域,并设定他的长度并获取共享内存的首地址。然后设定消费者互斥与同步的信号灯,并 为他们设置相应的初值。当有消费进程在
4、运行而其他消费者请求时,相应的信号灯就会阻止它,当共享内存区域已空时,信号等也会提示生产者不能再从共享内存中取出相应的内容。运行的消费者应该与相应的生产者对应起来,只有这样运行结果才会正确。结论分析与体会:实现方式:Consumer :#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 = 103;cget_ nu mber = 1;shm flg = IPC CREAT
5、| 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,sem_flg);sem_val = 0;con s_sem = set_sem(
6、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_ptrO='A')printf("%dThe consumer has glue.nThe consumer getstobacco and paper n&
7、quot;,getpid();if(buff_ptr0='B')prin tf("%d The con sumer has paper.' nThe con sumer getstobacco and gluen",getpid();if(buff_ptrO='C')printf("%dThe consumerhas tobacco.'nTheconsumergets glue and paper' n",getpid();*cget ptr 二(*cget pt叶1);if(*cget_ptr%2
8、=0) buff_ptrO='D' else buff_ptrO='E' up(cmtx_sem); up(prod_sem);return EXIT_SUCCESS;Producer :#i nclude "ipc.h"int 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_CREAT|0644;buff_ptr = (char
9、*)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_sem(co ns_h,sem_val,sem_flg);se
10、m_val = 1;pmtx_sem = set_sem(pmtx_h,sem_val,sem_flg);if(producerid=0)buff_ptrO='D'*pput_ptr=0;while(1)if(buff_ptr0-'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("%dThe producer gives tobacco andpapern&q
11、uot;,getpid();if(*pput_ptr=1)buff_ptr0 = 'B'prin tf("%dThe producer gives tobacco andgluen",getpid();if(*pput_ptr=2)buff_ptr0 = C;prin tf("%d The producer gives glue and paper n",getpid();sleep(rate);up(pmtx_sem);up(c on s_sem);return EXIT_SUCCESS;Ipc.h :#i nclude "i
12、pc.h"int get_ipc_id(char *proc_file,h_t h) FILE *pf;int m,n;char lin 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 em='')m+;whil
13、e(linem !='') colu mn+ = lin em+;colum n = '0' if(atoi(colum) != h) con ti nue;n=0;while(l in em='')m+;while(linem !='') colu mn+ = lin em+;colum n = '0'm = atoi(colum);fclose(pf);return m; fclose(pf); return -1;int dow n(i nt sem_id) struct sembuf buf;buf.sem
14、_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; int up(i nt sem_id) struct sembuf buf; buf.sem_op = 1; buf.sem_ nu mber = 0; buf.sem_flg = SEM_UNDO; if(semop(semd,&buf,1) <0) per
15、ror("up error "); exit(EXIT_FAILURE);return 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) < 0 ) if(sem_id = semget(sem_h,1,sem_flg) < 0) perror("semaphore create error"); ex
16、it(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/shm",shm_h) < 0
17、 ) 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) < (char *)0) perror("get shareMemory error"); exit(EXIT_FAILURE);for(m=0; m<shm_ nu mber; m+) shm_bufm = 0;if(shm_buf = (char *)sh
18、mat(shmd,0,0) < (char *)0) perror("get shareMemory error");exit(EXIT_FAILURE);retur n shm_buf;int set_msq(h_t msq_h,i nt msq_flg) int msqd;if(msq_id = get_ipc_id("/proc/sysvipc/msg",msq_h) < 0 ) if(msq_id = msgget(msq_h,msq_flg) < 0) perror("messageQueue set error&q
19、uot;); exit(EXIT_FAILURE);return msqd;实验结果:0鈔 likunlikunMjenovo-IdeaPad-Y471A: /lab4ILkurillkivi- Lenovo - IdeaPdd - Y471A: -$ cd lab4likunyltkun * Lenovo*ideapad-Y471A:*/Iato4 ,/producer 0 332&33203320232053ZO3201329332033203329332013201320332033201320The The T heThe The lheTheTheThe TheThe The
20、 The The T heTheproducer producer producer producer prpducer producer producer producer producer producer producer producer producer producer produce prodjeerIves gtvea gtves gives give 5 give*; gtves gives glvz gives gtves gitve« gtves gives gives gtvestobacco and glu总 glue dnd pdper tabacco a
21、nd glue tobacco and paper ylue and paper tabacco and glue tobacco and pep©r qIlic and pper tobacco and glue tobacco and popcr glue and pdper tobacco and glu tobacco and papr glue and paper tabaccD and glue tobacco and paperlikunlikunMjenowddeaPad-Y471A:likimtllkun-Lenovo-IdePsd-Y471A:-/lBb4S */
22、producer 1J3Z1 lhe3321 The1321 The3321 The331 The33?1 The3321 The3321 The3321 The3321 The3321 The3121 The33Z1 The a丄 TheJ321 The3321 The3321 Thei321 The33?1 Thp3321 The3321 The3321 Th?producer producer producei producer producer producer producer producer producer producer producer producer producer
23、 producer producer producer producer producer producer producer producer produceigives tobacco and pap«r gtves gLp亡 and paper ytvei LobdccQ and glue gives tobacco and paper gtves glue and paper gtves tabacca and glue tobacco and paper gives glue and paper gives tobacco and glue gives tobacco an
24、d paper gtvt?s glue 由nd pper gives tobacco and glw gives tobacco and ppr glvei glue 日nd paper gtves tabacto 白nd glue gtves tabacca and paper gtves glue and paper gives tabacco end qlue givf3 tobacri rind pper gives glue and psper gives tobacco end glue glvps toba< co and ppprl ik u n 1 f kunMje n
25、 o vo- Ide a Pad -Y 471 At /lab4ItkurifLikin -Lenovo-IdeaPdd-Y471A:-/Lab4$ */consunerd bah: ./consu-nere:没有那个文件或口录 likurilikun Lenovo IdaPad-V471A:-/lab4$ r/cohsuner 9 5056 The cunsunerThe consumer gets工0北 The consumerThe consurer gets玉旳5 lhe rnnunerThe consumer gets3030 The consunerThe consulr gers
26、J096 The consurwrThe consumer gets1096 The cansurwrThe consumer gets3036 The consumerThe consumer gets3036 The consumerThe consumer qet5"4096 The ennsumerThe consumer getsJ036 The consunerThe consuwr gets3096 The consurwrhas glu巳* tobacco and has glum” tobacco nd glum* tobacco end has glue toba
27、cco and hos gluc« tobacco «nd h白左glue, tab白tto and has glue, tobacco and has gLue, tobacco and gl_u楼. tobacco and hd5 glue, tobacco 8nd ha各 glw.paperpaperpaperpaperpaperpaperpaperpaperpaperpaper0 likunilikun-Lenovo-IdeaPad-Y471A; /lab4ItkimfL Ikun - Leno vc -1 a Pad - Y471A: - 5 cd lab4Itk
28、unyltkun-Lenovo*LdeaPad-Y47iA:"/lab4$ ,/consumer 13IS The consuiner The consumer geti 3318 rhe ccnsuner The consumer gets 3318 The cqnsuner The con';iiTpr gets J318 The consuner The consumer sets 3318 Thp consuner The consurncr gets 3318 The consoner Tha conurrr gets 対iE The consular The co
29、nsurer gets 33is rhe consumer The consumer gets 3318 The cqnsuner The ronsLjF.pr gets 3318 The consumer The consumer getsThe consuwi The consurer getsh甘右 paper, t口bacco dnd has paper. tobacco and ha paper * tDhacco 2nd has pape* tobacco «r>d has priper, tobacco and has paper. tobacco and has
30、 paper. tobacco and hmm paper. tobacco and has paper + mbacco 2nd has paper, tobacco «r>d has paper. tobacco "dilueglueueglueglueglumgluegLuegl lieglueglue0Q UkunlikunMjenovoddeaPad-Y471A: /lab4ltkim<llkun-Lencvo-IdcAPjd-Y471A:-S cd lab4Itkunyltkun - Lenovo* idea Pad-Y4 71A:*/Iab4 ,/
31、con«upner 1315 The cohsup>er has tobacco.The consuner gets glut dnd paper3319 rhe consuner has Tobacco.glue and paper h日乞 tobacco.glue and paper has tobacco- glue and paper has tobacco.glue and ppor has tobacco.glue and paper has tobaccoglue and paper hmm tobacco.glue and paper has tobacco.g
32、lue and paper haw tobacco glue and paper has tobacco.glue /nd paperThe consumer gets 3319 The cansuner The conLjnter gets J319 The consuner The consumer sets 3319 Thp consurner The consumer gets J319 The consunerThe conurrr gets 1519 The consularThe consumer gets3319 The consumerhe consjrr gets 3319
33、 The consunerThe conuiner gets 3319 The consumerThe consumer gets 3.319 Tht? consumeiThe consumer get呂分析:多进程的系统中避免不了进程间的相互关系。进程互斥是进程之间发生的一 种间接性作用,一般是程序不希望的。通常的情况是两个或两个以上的进程需要 同时访问某个共享变量。我们一般将发生能够问共享变量的程序段称为临界区。 两个进程不能同时进入临界区,否则就会导致数据的不一致,产生与时间有关的 错误。解决互斥问题应该满足互斥和公平两个原则,即任意时刻只能允许一个进 程处于同一共享变量的临界区,而且不能让任一进程无限期地等待。进程同步是进程之间直接的相互作用,是合作进
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 城乡给排水工程建设事故预防技术服务报告模板
- 《电气控制及PLC》详细笔记
- 保健按摩师(高级)技能理论考试题库(含答案)
- 文书模板-个人所得税退税的租房合同
- 中考物理专项复习:浮力(原卷版)
- 2024年梯度飞片项目投资申请报告代可行性研究报告
- 2024年低温多效海水淡化装置项目资金申请报告代可行性研究报告
- 强化安全责任意识创建和谐平安校园
- 技能评定与评价技术规范
- Python程序设计实践- 习题及答案 ch09 实验5 选择结构程序设计
- 《现代教育原理》课件
- 卷烟物流安全规范培训课件
- 基础知识疏散逃生
- 煤炭行业碳排放与减排技术
- 无铅手工焊锡培训考试A试题(答案)
- 剪映的基础操作(剪映短视频创作案例教程)
- 监理投标书中服务承诺和优惠条件
- 医院环境的清洁与消毒护理课件
- 一校一策规划实施方案
- 《对外开放的中国》课件
- 超声科质量控制报告及整改
评论
0/150
提交评论