欢迎来到天天文库
浏览记录
ID:28175045
大小:141.12 KB
页数:12页
时间:2018-12-07
《操作系统实验报告进程管理》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、数学与计算机科学系实验报告课程:计算机操作系统地点:软件实验室二时间:2013年05月17日学生姓名班级***学***仪器编号组别隴一实验项目试验五、进程同步与共享内存管理指导教师实验目的1、理解Linux关于共享内存的概念,掌握Linux支持进程间内存共享的系统调用。2、巩固掌握进程同步概念。实验要求使用共享内存来完成进程间通信。要求建立一个利用共享内存机制的,关于经典同步问题readers/writers的解决方案。writer从用户处获得输入,然后将其写入共享内存,reader从共享内存获取信息,然后再在屏幕上打印出来。实验环
2、境实验内容及实验结果请写出具体的实验步骤,并给出相应的实验结果,附上编写的程序及其运行结果截图!!实验步骤:1)关于共享内存的系统调用程序首先调用shmget()函数建立一块共享内存,大小为1024个字节,该函数返回创建的共享内存的标识符。然后fork—个子进程,子进程调用shmat()函数将该共享内存连接(attach)到自己的虚存空间,即可通过普通的内存写操作(例如strcpy等),在该共享内存写入一个字符串,然后调用shmdt()函数断开(detach)与该共享闪存的连接。在这段时间里,父进程sleep,等待子进程完成上述操作
3、,然后调用shmctl()函数得到关于这块共享内存的相关信息,并打印岀来。然后调用shmat()函数将这块共亨内存连接(attach)到自己的虚存空间,即可通过普通的内存读操作(例如primf等),将该共享内存中的字符串读出来。最后,调用shmctl()函数,销毁该共亨内存。xinl.c/********xinlc~"个®单的彳吏用共享内存的例子***************/sxx@sxx-VirtualBox:-$catxinl•c^include^include#include4、s/ipc.h>#include#tnclude#include#tnclude^include#tnclude#ifdefined(一GNU_LIBRARY__)&&!defined(_SEM_SEMUN_UNDEFINED)#elseunionsemun{intval;structsentd_ds*buf;unsignedshorttnt*array;structseminfo*_buf;一#endif#de5、fineSHMDATASIZE1000#deftneBUFFERSIZE(SHMDATASIZE•sizeof(Int))#deftneSN_READ0#deftneSN_WRITE1intSemid=0;voidreader(void);voidwriter(tntshmtd);voiddeleteCvoid);voidsigdelete(intsignum);voidlocksem(intsemid,intsemnum);voidunlocksen(intsemid,intsemnum);voidwaitzero(lntsemi6、d,tntsemnum);voidwrite(intshmtd,intsemid,char*buffer);intmysenget(key_tkeyflntnsens^xntsenflg);intnysemctl(intsemid,intsennun^lntend,unionsemunarg);intmysemop(intsemid^structsembuf*sops,unsignednsops);intmyshmget(key_tkey,intsize,tntshnflg);void*nyshnat(intshmid,constv7、oid*shnaddr>intshmflg);intmyshmctl(intshmidftntend,structshnid_ds*buf);intnain(intargc^char*argv[]){if(argc<2){reader();}else{writer(atoi(argv[1]));}return0;}voidreader(void){unionsemunsunion;intsenid^shnid;void*shndata;void*myshnat(tntshntd,constvoid*shmaddr,intshmflg8、)
4、s/ipc.h>#include#tnclude#include#tnclude^include#tnclude#ifdefined(一GNU_LIBRARY__)&&!defined(_SEM_SEMUN_UNDEFINED)#elseunionsemun{intval;structsentd_ds*buf;unsignedshorttnt*array;structseminfo*_buf;一#endif#de
5、fineSHMDATASIZE1000#deftneBUFFERSIZE(SHMDATASIZE•sizeof(Int))#deftneSN_READ0#deftneSN_WRITE1intSemid=0;voidreader(void);voidwriter(tntshmtd);voiddeleteCvoid);voidsigdelete(intsignum);voidlocksem(intsemid,intsemnum);voidunlocksen(intsemid,intsemnum);voidwaitzero(lntsemi
6、d,tntsemnum);voidwrite(intshmtd,intsemid,char*buffer);intmysenget(key_tkeyflntnsens^xntsenflg);intnysemctl(intsemid,intsennun^lntend,unionsemunarg);intmysemop(intsemid^structsembuf*sops,unsignednsops);intmyshmget(key_tkey,intsize,tntshnflg);void*nyshnat(intshmid,constv
7、oid*shnaddr>intshmflg);intmyshmctl(intshmidftntend,structshnid_ds*buf);intnain(intargc^char*argv[]){if(argc<2){reader();}else{writer(atoi(argv[1]));}return0;}voidreader(void){unionsemunsunion;intsenid^shnid;void*shndata;void*myshnat(tntshntd,constvoid*shmaddr,intshmflg
8、)
此文档下载收益归作者所有