《2023年山东大学操作系统实验报告进程同步实验.docx》由会员分享,可在线阅读,更多相关《2023年山东大学操作系统实验报告进程同步实验.docx(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、计算机科学与技术学院实验报告实验题目:实验四、进彳是同步实验学号:日期 20230409班级:计基地12姓名:实验目的:加深对并发协作进程同步与互斥概念的理解,观测和体验并发进程同步与互斥 操作的效果,分析与研究经典进程同步与互斥问题的实际解决方案。了解Linux 系统中IPC进程同步工具的用法,练习并发协作进程的同步与互斥操作的编程 与调试技术。实验内容:抽烟者问题。假设一个系统中有三个抽烟者进程,每个抽烟者不断地卷烟并抽烟。 抽烟者卷起并抽掉一颗烟需要有三种材料:烟草、纸和胶水。一个抽烟者有烟草, 一个有纸,另一个有胶水。系统中尚有两个供应者进程,它们无限地供应所有三 种材料,但每次仅轮流
2、提供三种材料中的两种。得到缺失的两种材料的抽烟者在 卷起并抽掉一颗烟后会发信号告知供应者,让它继续提供此外的两种材料。这一 过程反复进行。 请用以上介绍的IPC同步机制编程,实现该问题规定的功能。硬件环境:解决器:In t elCoreN i32350MCPU 2. 3 OGH z X 4 图形:Intel Sandy b ridge Mob i le x 8 6/MM X/S S E 2 内存:4 G操作系统:3 2位磁盘:20. 1 GB软件环境: ubuntul3 . 04实验环节:(1)新建定义了 P rod u c e r和consu m e r共用的I PC函数原型和变量的i pc
3、.h文献。(2)新建ipc.c文献,编写prod u cer和consum e r共用的I PC的具体相应函数。(3 )新建Prod ucer文献,一方面定义p rodu c er的一些行为,运用系统调用,建立共享内存 区域,设定其长度并获取共享内存的首地址。然后设定生产者互斥与同步的信号灯,并为他们 设立相应的初值。当有生产者进程在运营而其他生产者请求时,相应的信号灯就会阻止他,当 共享内存区域已满时,信号等也会提醒生产者不能再往共享内存中放入内容。(4 )新建Consum e r文献,定义consume r的一些行为,运用系统调用来创建共享内存 区域,并设定他的长度并获取共享内存的首地址。
4、然后设定消费者互斥与同步的信号灯,并 为他们设立相应的初值。当有消费进程在运营而其他消费者请求时,相应的信号灯就会阻止 它,当共享内存区域已空时,信号等也会提醒生产者不能再从共享内存中取出相应的内容。. Ukun(0)Ukun-Lenovo-ldeaPad-Y471A: /lab4likunlikun- Lenovo-IdeaPad-Y471A:$ cd Iab4likunlikun-Lenovo-IdeaPad-Y471A:/lab4$ ./consumer 2 3319 The consumerThe consumer gets 3319 The consumer The consumer
5、 gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 The consumer The consumer gets 3319 T
6、he consumer The consumer getshas tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue and paper has tobacco, glue a
7、nd paper has tobacco, glue and paper分析:多进程的系统中避免不了进程间的互相关系。进程互斥是进程之间发生的一 种间接性作用,一般是程序不希望的。通常的情况是两个或两个以上的进程需要 同时访问某个共享变量。我们一般将发生可以问共享变量的程序段称为临界区。 两个进程不能同时进入临界区,否则就会导致数据的不一致,产生与时间有关的 错误。解决互斥问题应当满足互斥和公平两个原则,即任意时刻只能允许一个进 程处在同一共享变量的临界区,并且不能让任一进程无限期地等待。进程同步是进程之间直接的互相作用,是合作进程间故意识的行为,典型的 例子是公共汽车上司机与售票员的合作。只
8、有当售票员关门之后司机才干启动车 辆,只有司机停车之后售票员才干开车门。司机和售票员的行动需要一定的协调。 同样地,两个进程之间有时也有这样的依赖关系,因此我们也要有一定的同步机 制保证它们的执行顺序。信号量机制就是其中的一种。信号灯机制即运用P v操作来对信号量进行解决。PV操作由P操作原语和V 操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下:P (S):将信号量S的值减1,即S=S-1;假如S3。,则该进程继续执行;否则该进程置为等待状态, 排入等待队列。V(S):将信号量S的值加1 4US=S+1;假如S0,则该进程继续执行;否则释放队列中第一个 等待信号量的进程。
9、P V操作的意义:我们用信号量及P V操作来实现进程的同步和互斥。信号量的数据结构为一个值和一个指针,指针指向等待该信号量的下一个进 程。信号量的值与相应资源的使用情况有关。当它的值大于0时,表达当前可用 资源的数量;当它的值小于0时,其绝对值表达等待使用该资源的进程个数。信 号量的值仅能由PV操作来改变。一般来说,信号量S3。时,S表达可用资源的数 量。执行一次P操作意味着请求分派一个单位资源,因此S的值减1;当S0时, 表达已经没有可用资源,请求者必须等待别的进程释放该类资源,它才干运营下 去。而执行一个V操作意味着释放一个单位资源,因此S的值加1 ;若S 0,表 达有某些进程正在等待该资
10、源,因此要唤醒一个等待状态的进程,使之运营下去。使用多于4个的生产者和消费者,以各种不同的启动顺序、不同的执行速率 检测以上示例程序和独立实验程序也能满足同步的规定。由于使用信号量满足进 程互斥的规定,任意时刻进入临界区的进程只有一个。而进程是通过信号量唤醒 阻塞进程,仍然可以实现进程同步。调试过程中碰到的重要问题及解决过程:(1)在修改程序时,最开始使用了 read。、sched u a I。、I ock(),但是编 译出现错误。解决方法:使用sleep。函数。(2)exit()缺少头文献。解决方法:加头文献# inclu d eos lee p。等I in u x系统调用缺少头文献。解决方
11、法:加头文献#includ e 体会和收获:通过本次实验,初步了解操作系统的进程同步的过程。我对生产者-消费者问 题的解决办法有了更全面的结识,同时对调试代码更加纯熟。本次实验最大的体 会就是,做东西要细心,在写代码的过程中,稍不留意就给后期调试工作带来很多 问题。运营的消费者应行与相应的生产者相应起来,只有这样运营结果才会对的。结论分析与体会:实现方式:Con s u mer :# i n c I u d e i pc. hi nt ma i n ( i n t argc, char *argv 口)。i n t rat e = 3 ;i nt c onsumer id=atoi (arg
12、v 1);buff_h = 101;buff_ n umber = 1;c g et_h = 103;eg e t_ n u m b er = 1 ;s hm_f Ig = IPC_CRE AT | 0644;b u ff_p t r = (char *) set_ s hm (buff_h, b u ff_numbe r , shm_ fig);o cget_ptr = (int *) s e t_s h m(cget_h, cget_ n u mber, shm_fIg);prod _ h = 20 1 ;。pmtx_h = 202;cons_h = 301 ;。c mtx_h = 302
13、;。sem_f Ig = IPC_CREAT |0 644 ;o sem_va I = b uf f_ n umber ;p rod_sem = set_sem (p r o d_h, s e m_ v a I, sem_fIg);sem_va I = 0 ;o cons_s e m = set_s e m (cons_h, s em_v a I, sem_fIg);。sem_v a I = 1 ;cmtx_ s e m = s et_sem ( c mtx_h, sem_va I, s em_fig);。i f (consum e r i d= O)。*cget_ptr=O ; w h i
14、I e (1) 。 i f ( b uff_p trO-A,=cons ume r id) 3 d own ( c ons_sem);o。 dow n ( c mt x _sem );。s leep (rate);。o if (buff_ptrO= A)(o o p r i n tf ( %d The co n sumer has glue. n The co n s umer g ets tobac c o and paper n, getp i d ();0 ft )o 。i f (bu f f_ptrO =B) 。 pr i n tf (%d The c o nsumer has p a
15、per. n The c o n su mer gets toba c co and g I uen, ge t p id ();0 )i f (buff_p tr 0 =C) 。o。o pr i n t f (%d The consumer has tobacco. n T h e co n sum e r gets glue and p a p e r n , get p id ();0 o 。 *c g e t _ p tr =(*cget_ p t r+ 1 );i f (* c g et_p t r%2=0)o b u f f_ p tr 0 = D;。 buf f _pt r 0
16、= E;。up (cmt x _sem);o 3 o up (prod_sem);00)b return EXIT_SUC CES S ;P r odu c er :#in c I u d e i p c . hi n t ma in(int a rgc, ch a r *argv) 。i nt r a t e= 3 ;。i n t p r oducer id=at o i ( a rg v 1 );。bu f f_h=1 01;b uff_ n umber=1;。pp u t_ h = 1 02;p p ut_numb e r = 1 ;s h m_f I g = l PC_CREAT| 0
17、644 ;f bu f f _p t r = (char ) set_s h m ( b uf f _ h , bu f f_ n um b e r , s h mpput_ptr = (i n t *)set_shm (p p ut_h, p put_ n umber, shm_f I g);prod_h = 201 ;pmt x _h = 202; cons_h = 30 1 ;cmtx_h = 302;o sem_f I g = I PC_C REAT |0644;s e m_va I = buf f _number ;p r od_sem = s e t _sem(p r o d_h,
18、 sem_ v a I , s em_f Ig);sem_va I = 0;c ons se m = s e t sem (cons h , s e m va I, s e m f I g);sem_vaI = 1;p m t x _ s em = set_se m (pmtx_h, s e m_va I , s e m_ f Ig);o i f (prod u c er i d=0) o buff_p t r 0= D;*p p u t_ptr=0;。)wh i I e (1) 。i f (buf f_ptr 0 - D pr o d u c er i d) o 。down ( p ro d
19、 _s e m); o。down (pm t x _ s e m); *p p ut_ptr =(*p put-ptr+ 1 ) %3;。 if (*p put_ptr = =0) 。b uff_ptr 0 = A;o 6 o p r i n t f (%d T he p roduc e r g i v es tobacco an d p a per n, getp i d ();00)。if ( *pp u t -ptr= 1 ) 。o。 bu f f _ptr0 = B;3 o pr i n tf ( %d Th e producer g i v e s t obacc o and g I
20、 uen , g e t p i d ();6 O。i f (*p p ut_ptr=2) o 。buff_ p tr 0 = C ;。 o print f ( % d Th e producer g i ve s glue and p a p er n ,get p id ();00o s Iee p (rate);。 up (pmtx_ s e m);。u p (c ons_sem);)。r eturn EX IT_SUCCESS;1 I pc. h:# i n c I ud e i pc. hi nt get_ipc_id(cha r *proc_f i I e , h_t h) 。FI
21、LE *pf;i nt m, n;o char line BUFSZ, col umBUFSZ;i f ( p f = fopen ( p ro c _f i I e , r ) ) = N ULL)(。perro r ( P r oc f i Ie not open); exit (EXIT_FA I LURE);0 )f g ets(l ine, BUFSZ, pf );。whi Ie(! f e o f (pf)(o m = n =0;f g e ts ( I ine, BUF SZ, pf);。whi le( I inem =)0 o m+;whi I e (I ine m !=)。o
22、。co I um n + = line m+;。c o I um n = 0;。i f (a t o i (col u m) != h)o cont i nue;。n= 0 ;wh i I e (I i nem=)o m+;while (line m !=) o co I u mn+ = I i ne m+;c o lum n = 0; m = a t o i (co I um);。fcI os e (pf);。retu r n m;。)。fcl o se (p f ); return 1 ;i n t dow n ( i nt s em_id) (o str u ct s e mbuf b
23、uf;o buf.se m_op = -1 ;。buf. s em_number = 0;buf. sem_f I g = SEM_U N D 0 ;。i f ( ( s emo p ( s em_ id, & b u f , 1) 0) pe r r or ( d own error );o o ex i t(EXIT_FAILURE);。)r eturn E X IT_SUCC E SS;1i n t up ( i nt sem_id)( st r u c t s e mbu f buf; buf. sem_ o p = 1; buf. sem_n u mber = 0; buf. sem
24、_f I g = SEM_UND0;o i f (semop (s e m i d, &bu f , 1 ) ) 0) 。per r o r (up e r ror );。ex it (EX I T_FA I LURE);)。retur n EX I T SUC C ESS;1i n t s et_sem ( h _t sem_h, i n t sem_va I, i nt sem_f I g) i nt sem_id;Sem_ u ns sem_arg;。i f (sem_i d = get_i pc_id (/pro c / sysv i pc/s em , sem_h ) 0 ) (。
25、i f ( s em_ i d = semge t ( s em_h, 1 , s em_f I g ) 0) o perror (sema p hore create e rror );o o exit (EXIT_FAILURE);00)。 sem _a r g . v a I - sem_ v a I ;。i f ( s emct I ( s e m_id, 0, SETVAL, s em_arg) 0) 3 perror(s e maph o re set err or);ex it (E X IT_FAILURE);。1)r eturn sem_i d; 一ch a r * se t
26、 _ s h m (h_ t s h m _h, i n t s hm_n u m be r , i n t s h m_fIg) o i n t m, s hm_id;char *s hm_buf;o i f (shm_i d = get_i pc_ i d(/p r oc/sy s vipc/shm, shm_h) 0 )( 一 一if ( (shm_id = s h m g et (shm_ h , s hm_n u m b er, s h m_f I g) ) 0) (。perror ( s hareMemo r y set error);3 exit (E X IT_FAILURE)
27、;00)。o if ( s hm_buf = (char *) shma t (s h m_id, 0,0) (ch a r *)0) (o p erro r ( g e t shareMem o ry e rror );exit(EXIT_FAI L URE);。)。for (m=0; ms h m_numb e r; m+)。 shm_b u fm = 0;0 )i f (shm_buf = (cha r shmat (shm_id, 0, 0) (c har *)0) 。perr o r (get s har e Memor y e rro r );exit (E X IT_FA I L
28、URE);0 )。return s h m_ b u f ;1i n t s et_msq(h_t msq_h, i n t msq_f I g) i n t msq_id; i f (m s q_id = ge t _i pc_i d ( / p r o c/sysvi p c/msg , msq_h) ) 0 ) (。o i f (msq_id = msgget(msq_h, msq_ f Ig) 0 ) 。p e rror (me ssageQu e ue s et e rror);。ex i t(EXIT_FAILURE);)。re t ur n m s q _i d ;实验结果:.
29、Ukun(0)Ukun-Lenovo-ldeaPad-Y471A: /lab4likunlikun- Lenovo-IdeaPad-Y471A:$ cd Iab4likunltkun-Lenovo-IdeaPad-Y471A:/lab4$ ./producer 03320 The producergivestobacco and glue3320 The producergivesglue and paper3320 The producergivestobacco and glue3320 The producergivestobacco and paper3320 The producer
30、givesglue and paper3320 The producergivestobacco and glue3320 The producergivestobacco and paper3320 The producergivesglue and paper3320 The producergivestobacco and glue3320 The producergivestobacco and paper3320 The producergivesglue and paper3320 The producergivestobacco and glue3320 The producer
31、givestobacco and paper3320 The producergivesglue and paper3320 The producergivestobacco and glue3320 The producergivestobacco and paper6 管 Ukun(0)Ukun-Lenovo-ldeaPad-Y471A: /lab4likunglikun-Lenovo-IdeaPad-Y471A:*/lab4$ ./producer 13321Theproducergivestobacco and paper3321Theproducergivesglue and pap
32、er3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper3321Theproducergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper3321Theproducergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper3321Theprod
33、ucergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper3321Theproducergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper3321Theproducergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco
34、 and paper3321Theproducergivesglue and paper3321Theproducergivestobacco and glue3321Theproducergivestobacco and paper1一 Ukun(fl)Ukun-Lenovo-ldeaPad-Y471A: /lab4Itkunltkun-Lenovo-IdeaPad-Y471A:,lab4$ ./consumer bash: ./consumerQ:没有那个文件或目录likunglikun-Lenovo-IdeaPad-Y471A:/Lab4$ 3096 The consumer has g
35、lue./consumer 0The consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacc
36、o and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumertobacco and has glue.paperThe consumer gets 3096 The consumert
37、obacco and has glue.paper。 Ukun(0)Ukun-Lenovo-ldeaPad-Y471A: /lab4likunlikun- Lenovo-IdeaPad-Y471A:$ cd lab4likunlikun-Lenovo-IdeaPad-Y471A:/lab4$ ./consumer 13318 The consumer The consumer getshas paper, tobacco andglue3318 The consumer The consumer getshas paper, tobacco andglue3318 The consumer T
38、he consumer getshas paper, tobacco andglue3318 The consumer The consumer getshas paper, tobacco andglue3318 The consumer The consumer getshas paper, tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue3318 The consumer The consumer getshas paper. tobacco andglue