山东大学操作系统试验报告4进程同步试验_第1页
山东大学操作系统试验报告4进程同步试验_第2页
山东大学操作系统试验报告4进程同步试验_第3页
山东大学操作系统试验报告4进程同步试验_第4页
山东大学操作系统试验报告4进程同步试验_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

1、山东大学操作系统实验报告4进程同步实验计算机科学与技术学院实验报告实验题目:实验四、进程同步实验学号:日期:20120409班级:计基地12姓名:实验目的:加深对并发协作进程同步与互斥概念的理解,观察和体验并发进程同步与互斥操作的效果,分析与研究经典进程同步与互斥问题的实际解决方案。了解Linux系统中IPC进程同步工具的用法,练习并发协作进程的同步与互斥操作的编程与调试技术。实验内容:抽烟者问题。假设一个系统中有三个抽烟者进程,每个抽烟者不断地卷烟并抽烟。抽烟者卷起并抽掉一颗烟需要有三种材料:烟茸、纸和胶水。一个抽烟者有烟茸,一个有纸,另一个有胶水。系统中还有两个供应者进程,它们无限地供应所

2、有三种材料,但每次仅轮流提供三种材料中的两种。得到缺失的两种材料的抽烟者在卷起并抽掉一颗烟后会发信号通知供应者,让它继续提供另外的两种材料。这一过程重复进行。请用以上介绍的IPC同步机制编程,实现该问题要求的功能。硬件环境:处理器:Intel?Core?i3-2350MCPU2.30GHzX4图形:Intel?SandybridgeMobilex86/MMX/SSE2内存:4G操作系统:32位磁盘:20.1GB软件环境:ubuntu13.04实验步骤:(1)新建定义了producer和consumer共用的IPC函数原型和变量的ipc.h文件。(2)新建ipc.c文件,编写producer和c

3、onsumer共用的IPC的具体相应函数。(3)新建Producer文件,首先定义producer的一些行为,利用系统调用,建立共享内存区域,设定其长度并获取共享内存的首地址。然后设定生产者互斥与同步的信号灯,并为他们设置相应的初值。当有生产者进程在运行而其他生产者请求时,相应的信号灯就会阻止他,当共享内存区域已满时,信号等也会提示生产者不能再往共享内存中放入内容。(4)新建Consumer文件,定义consumer的些行为,利用系统调用来创建共享内存区域,并设定他的长度并获取共享内存的首地址。然后设定消费者互斥与同步的信号灯,并为他们设置相应的初值。当有消费进程在运行而其他消费者请求时,相应

4、的信号灯就会阻止它,当共享内存区域已空时,信号等也会提示生产者不能再从共享内存中取出相应的内容。运行的消费者应该与相应的生产者对应起来,只有这样运行结果才会正确。结论分析与体会:实现方式:Consumer#includeipc.hintmain(intargc,char*argv)intrate=3;intconsumerid=atoi(argv1);buff_h=101;buff_number=1;cget_h=103;cget_number=1;shm_flg=IPC_CREAT|0644;buff_ptr=(char*)set_shm(buff_h,buff_number,shm_flg

5、);cget_ptr=(int*)set_shm(cget_h,cget_number,shm_flg);prod_h=201;pmtx_h=202;cons_h=301;cmtx_h=302;sem_flg=IPC_CREAT|0644;sem_val=buff_number;prod_sem=set_sem(prod_h,sem_val,sem_flg);sem_val=0;cons_sem=set_sem(cons_h,sem_val,sem_flg);sem_val=1;cmtx_sem=set_sem(cmtx_h,sem_val,sem_flg);if(consumerid=0)*

6、cget_ptr=0;while(1)if(buff_ptr0-A=consumerid)down(cons_sem);down(cmtx_sem);sleep(rate);if(buff_ptr0=A)printf(%dTheconsumerhasglue.nTheconsumergetstobaccoandpapern,getpid();if(buff_ptr0=B)printf(%dTheconsumerhaspaper.nTheconsumergetstobaccoandgluen,getpid();if(buff_ptr0=C)printf(%dTheconsumerhastobac

7、co.nTheconsumergetsglueandpapern,getpid();)*cget_ptr=(*cget_ptr+1);if(*cget_ptr%2=0)buff_ptr0=D;elsebuff_ptr0=E;up(cmtx_sem);up(prod_sem);)returnEXIT_SUCCESS;Producer:#includeipc.hntmain(intargc,char*argv)intrate=3;intproducerid=atoi(argv1);buff_h=101;buff_number=1;pput_h=102;pput_number=1;shm_flg=I

8、PC_CREAT|0644;buff_ptr=(char*)set_shm(buff_h,buff_number,shm_flg);pput_ptr=(int*)set_shm(pput_h,pput_number,shm_flg);prod_h=201;pmtx_h=202;cons_h=301;cmtx_h=302;sem_flg=IPC_CREAT|0644;sem_val=buff_number;prod_sem=set_sem(prod_h,sem_val,sem_flg);sem_val=0;cons_sem=set_sem(cons_h,sem_val,sem_flg);sem_

9、val=1;pmtx_sem=set_sem(pmtx_h,sem_val,sem_flg);if(producerid=0)buff_ptr0=D;*pputptr=0;)while(1)if(buff_ptr0-D=producerid)down(prod_sem);down(pmtx_sem);*pput_ptr=(*pput_ptr+1)%3;if(*pput_ptr=0)buff_ptr0=A;printf(%dTheproducergivestobaccoandpapern,getpid();)if(*pput_ptr=1)buff_ptr0=B;printf(%dTheprodu

10、cergivestobaccoandgluen,getpid();)if(*pput_ptr=2)buff_ptr0=C;printf(%dTheproducergivesglueandpapern,getpid();)sleep(rate);up(pmtx_sem);up(cons_sem);).)returnEXIT_SUCCESS;pc.h:#includeipc.hntget_ipc_id(char*proc_file,h_th)FILE*pf;intm,n;charlineBUFSZ,columBUFSZ;if(pf=fopen(proc_file,r)=NULL)perror(Pr

11、ocfilenotopen);exit(EXIT_FAILURE);)fgets(line,BUFSZ,pf);while(!feof(pf)m=n=0;fgets(line,BUFSZ,pf);while(linem=)m+;while(linem!=)column+=linem+;column=0;if(atoi(colum)!=h)continue;n=0;while(linem=)m+;while(linem!=)column+=linem+;column=0;m=atoi(colum);fclose(pf);returnm;fclose(pf);return-1;ntdown(int

12、sem_id)structsembufbuf;buf.sem_op=-1;buf.sem_number=0;buf.sem_flg=SEM_UNDO;if(semop(sem_id,&buf,1)0)perror(downerror);exit(EXIT_FAILURE);returnEXIT_SUCCESS;ntup(intsem_id)structsembufbuf;buf.sem_op=1;buf.sem_number=0;buf.sem_flg=SEM_UNDO;if(semop(sem_id,&buf,1)0)perror(uperror);exit(EXIT_FAILURE);re

13、turnEXIT_SUCCESS;intset_sem(h_tsem_h,intsem_val,intsem_flg)intsem_id;Sem_unssem_arg;if(sem_id=get_ipc_id(/proc/sysvipc/sem,sem_h)0)if(sem_id=semget(sem_h,1,sem_flg)0)perror(semaphorecreateerror);exit(EXIT_FAILURE);sem_arg.val=sem_val;if(semctl(sem_id,0,SETVAL,sem_arg)0)perror(semaphoreseterror);exit

14、(EXIT_FAILURE);returnsem_id;一char*set_shm(h_tshm_h,intshm_number,intshm_flg)intm,shm_id;char*shm_buf;if(shm_id=get_ipc_id(/proc/sysvipc/shm,shm_h)0)if(shm_id=shmget(shm_h,shm_number,shm_flg)0)perror(shareMemoryseterror);exit(EXIT_FAILURE);if(shm_buf=(char*)shmat(shm_id,0,0)(char*)0)perror(getshareMe

15、moryerror);exit(EXIT_FAILURE);for(m=0;mshm_number;m+)shm_bufm=0;.if(shm_buf=(char*)shmat(shm_id,0,0)(char*)0)perror(getshareMemoryerror);exit(EXIT_FAILURE);returnshm_buf;Iintset_msq(h_tmsq_h,intmsq_flg)intmsq_id;if(msq_id=get_ipc_id(/proc/sysvipc/msg,msq_h)0)if(msqid=msgget(msqh,msqflg)0)perror(mess

16、ageQueueseterror);exit(EXIT_FAILURE);)returnmsq_id;)实验结果:OIikunikun-Lenovu-1deaPad-Y471A:ILkuriCLtkun-Lemvo*IdaPad-Y471Acdlab4ltkun(dltkunLenovo-ideaPad-Y47iA:*/,Lab45,/producer0132BTheproducergivestobaccoandglu。332HTheproducergtveaglueandpdper3320Theproducergivestobaccoandglue1320Theproducergivesto

17、baccaandpaper3320Theproducergive5glueandpapergTheproducergluesrohsrrdanrlglue1329Theproducergive与tobacco己ndpaperJ320Theproducergivesgluearidpaper3J20Th?producergtveqtobacco同ndglueS32GTheproducergivestobaccoandpopcr3320Theproducergtvesglueandpaper3320Theproducergivesrotoaccoandg】u电1329Thproducergives

18、tobaccoaridpmp。广3320Theproducergtvesglueandpaper339Theproducergivestabaccaandglue1320TheproducergivestobaccaandpaperGlikun)Ukun-Lenovo-ldeaPad-Y471A:/ldb4llkunllkun-Lenovo-IdeaPad-Y471A:*/lab4S./producer13321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobaccoan

19、dglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobaccoandglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobaccoandglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobacc

20、oandglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobaccoandglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestobaccoandglue3321Theproducergivestobaccoandpaper3321Theproducergivesglueandpaper3321Theproducergivestob

21、accoandglue3321IheproducergivestobaccoandpaperGlikun)Ukun-Lenovo-ldeaPad-Y471A:/ldb4hasglue,tobaccoandhasglue,tobaccoandMuglup.tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue,tobaccoandhasglue.paperpaperpaperpaperpaperpape

22、rpaperpaperpaperpaperllkunllkun-Lenovo-IdedPad-Y471A;*/lab4S./consumer5bash:./consumer:没有那个文件或目录llkunltkun-Lenovo-IdeaPad-Y471A:-/lab4$./consumer03096TheconsunerTheconsumergets3096TheconsumerTheconsumergets7hprcnqumrTheconsumergets3096TheconsumerTheconsumergets3096TheconsumerTheconsumergets3096Theco

23、nsumerTheconsumergets3096TheconsumerTheconsumergets3096TheconsumerTheconsumergets3096TheconsumerTheconsumergets3096TheconsumerTheconsumergets3096Theconsumer,一Ukun0)Ukun-Lenovo-ldeaPad-Y471A:-/lab4likunllkun-Lenovo-IdedPad-Y471A:*$cdlab4likunltkun-Lenovo-ideapad-Y471A:-/Lab4$./consumer13318Theconsume

24、rhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoand

25、glue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Iheconsutnerhaspaper.Theconsumergetstobaccoandglue3318Theconsunerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.Theconsumergetstobaccoandglue3318Theconsumerhaspaper.TheconsumergetstobaccoandglueO*LlkunHkun-Lcnovo-ldaPad-Y471

26、A:/lab4ILkuueitkun-Lenovo-IdedPdd-Y471A:-$cdlab4Itkunyltkun*Lenovo-ideapad-Y471A:*/Iato45,/conurier21319Theccinsui*erhastobacco.Theconsumergetiglutandpaper3319rheccnsunerhastobacco.Theconsulrgetsglueandpaper3319ThecnsunerhastQb日工匚。.TheconsumergetsglueandppprJ319Theconsuncrhastobacco.Theconsurrsetsglueendpaper3319Jh?consurnerhastobacco.Theconsurcrgetsglueandppcr3319Theconsunerhastobacco.ThaEC*fiUMgetsglueandpaperTheconsumerhastobacco.Theconsjrrgetsglueandpaper3319rheconsumerhastobacco.Theconsrget

温馨提示

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

评论

0/150

提交评论