《《操作系统原理》实验5--Linux 多线程编程.docx》由会员分享,可在线阅读,更多相关《《操作系统原理》实验5--Linux 多线程编程.docx(4页珍藏版)》请在课桌文档上搜索。
1、操作系统原理实验报告实验序号:5实验项目:Linux多线程编程学号姓名专业、班级实验地点指导教师实验时间一、实验目的及要求通过本实验的学习,掌握LinUX多线程编程的基本方法。二、实验设备(环境)及要求VMware虚拟机软件,Ubuntu系统三、实验内容与步骤1、根据教材实践Linux线程管理指令jonzubuntu:-/CProgran$psHPIDTTYSTATTIMECOMMAND2896?Sl6:06usrbtngnone-keyring-daemon-daenontze-login2896?Sle:eeusrbtngnone-keyring-daemon-daenontze-logi
2、n2896?Slzusrbtngnoe-keyring-daemon-daeontze-login2896?Sl6:66usrbtngnofne-keyring-daemon-daenontze-login2896?SIe:eeusrbtgnome-keyring-daemon-daemont,ze-login2896?Slrusrbtgnone-keyring-daemon-daemontze-login2896?Sl6:06usrbtngnor)e-keyring-daemon-daemontze-login2896?Sle:eeusrbtgnoekeyring-daemon-daemon
3、tze-login297?SSl6:06gnoe-session-sesston=ubuntu297?SSl6:06gnome-session-sesston:ubuntu297?Ssle:00gnome-session-sessto=ubutu297?SSlzgnoe-session-sesston三ubuntu2942?Ss6:06usrbtnssh-agentusrbtndbus-launch-exit-wtth-se2945?Se:eeusrbtdbus-launch-ettwtthsesslogone-session2946?Ss6:03/btn/dbus-daemon-fork-p
4、rtnt-pid5-print-address2961?Sl6:62usrltbgnone-settings-daerongnone-settings-daemon2961?Slo:eeusrltbgnone-settings-daenogone-settings-daemon2961?Slzusrltbgnone-settingsdaenongnone-settings-daemon2967?S6:06usrltbgvfsgvfsd2969?Sle:eeusrltbgvfsgvfs-fuse-daemonfhReJoz.gvfsjonzubuntu:/CProgram$ps-eLfUIDPI
5、DPPIDLWPCtroot11root2Q2Qroot323root424root525root727root828root929root121root11211root12212root13213root15215root16216root17217root18218root19219root222ILWPSZRSSPSRSTIMETTY1918176Sep28?11Sep28?1Sep28?1OSep28?1OSep28?1Sep28?1OSep28?1OSep28?1OSep28?11Sep28?11Sep28?11Sep28?11Sep28?1Sep28?1Sep28?11Sep28
6、?1O1Sep28?1Sep28?利用ps命令加上参数对线程进行管理。2、利用LinUX多线程编程实现以下功能:创建两个子进程;一个子线程(生产者线程)依次向缓冲区写入整数0,1,2,.,19;另一个子线程(消费者线程)暂停3s后,从缓冲区读数,每次读一个,并将读出的数字从缓冲区删除,然后将数字显示出来;父线程等待子线程2(消费者线程)的退出信息,待收集到该信息后,父线程就返回。jonz0ubuntu:-CProgram$ jonzubuntu:/CProgram$ jonz0ubut:-/CProgramSvimexan5.cgcc-oexam5exam5.c-Ipthread.exam5W
7、ritebufferfinish!Readbufferthreadreaddata:1234567891111213141516171819readover!wattingforreadthreadtofinish.readthreadtest0K!程序执行过程glotebuffer;red-buffer-thread(*arg)ttt,x;prtntf(for(t三;t(x-globe.buffert;prttf(tx);globe-buffer(t;)prtntf();wrttebuffer_thread()for(t;t;t*)(globebuffert三t;Rtn()tntres;pt
8、hred.tred-thread;wrtte-buffer.thread();print);,read-txffer-thread,): ););5 );sleop();res=pthreadcreate(Sreadthread,tf(res!)(prttf(extt();)sleep();prtntf(respthread_Jolc(r,dd_thread,if(res!)(rtntf(xlt();)PrInF);return;实验结果及源代码如上图四、分析与讨论多线程任务其实才是真正意义上的资源执行者,应该掌握其使用方法。五、教师评语成绩签名:日期:附:第二题实验源代码includeinc
9、lude#include#includeincludecharglobe-buffer100;void*read_buffer-lhread(void*arg)(inti,x;prinlf(Readbufferthreadreaddata:n);for(i=0;i20;i+)(x=globe-bufferi;printf(%d,x);globe_bufferi=0;清空1printf(nreadover!n);)voidwrite_buffer_lhread()(inti;for(i=0;i20;i+)(globe_bufferi=i;intmain()intres;pthread_lread
10、_thread;write_buffer_thread();printf(,nWritebufferfinish!n);sleep(3);res=pthread_create(&read_thread,NULL,read_buffer_thread,NULL);if(res!=O)(printf(,ReadThreadcreatError!);exit(O);1sleep(l);printf(waitingforreadthreadtofinish.Anp);res=pthreadjoin(read_thread,NULL);if(res!=O)(printf(,readthreadjoinfailed!nu);exit(O);)printf(,readthreadtestOK!n,);returnO;