您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 华科操作系统实验报告
11课程实验报告课程名称:操作系统原理专业班级:cs1209学号:姓名:指导教师:报告日期:2015年1月5日计算机科学与技术学院12目录1实验一Linux线程及信号灯..........................................................................11.1实验目的与内容....................................................................................11.1.1实验目的..........................................................................................11.1.2实验内容.........................................................................................11.2实验过程................................................................................................11.2.1预备知识.........................................................................................11.2.2实验测试与结果.............................................................................21.3实验总结................................................................................................31.4源代码....................................................................................................42实验二Linux进程及同步..............................................................................72.1实验目的与内容....................................................................................72.1.1实验目的.........................................................................................72.1.2实验内容.........................................................................................72.2实验过程................................................................................................72.2.1预备知识.........................................................................................72.2.2实验测试与结果.............................................................................82.3实验总结................................................................................................92.4源代码....................................................................................................93实验三Linux文件目录操作.........................................................................133.1实验目的与内容...................................................................................133.1.1实验目的........................................................................................133.1.2实验内容........................................................................................133.2实验过程...............................................................................................133.2.1预备知识........................................................................................133.2.2实验测试与结果............................................................................143.3实验总结...............................................................................................153.4源代码...................................................................................................151第1页实验一Linux线程及信号灯1.1实验目的与内容1.1.1实验目的1、熟悉Linux线程接口。2、熟悉Linux信号灯编程。3、了解Linux进程同步与通信的主要机制,并通过信号灯操作实现进程间的同步与互斥。1.1.2实验内容1、编程模拟实现飞机售票:创建多个售票线程;使用公用全局变量保存已售票数量;创建互斥信号灯;对售票线程临界区实施P、V操作;2、编程模拟实现双线程单缓冲区的合作创建一个计算线程与一个输出线程;创建同步信号灯;缓冲区用全局公用数组变量;对计算线程与输出线程实施P、V操作;1.2实验过程1.2.1预备知识Linux下的信号灯及其P、V操作:表1.1P、V操作定义voidp(intsemid,intsemno){structsembufsop;sop.sem_num=semno;sop.sem_op=-1;sop.sem_flg=0;semop(semid,&sop,1);}voidv(intsemid,intsemno){structsembufsop;sop.sem_num=semno;sop.sem_op=1;sop.sem_flg=0;semop(semid,&sop,1);}1第2页线程:表1.2线程相关函数线程创建pthread_create(pthread_t*thread,pthread_attr_t*attr,void*(*start_routine)(void*),void*arg);线程挂起pthread_join(pthread_tth,void**thread_retrun);共享内存:使用共享内存是运行在同一计算机上的进程进行进程间通信的最快的方法,shmget与shmat系统调用。进程控制:fork与execv系统调用;编译、编辑、调试表1.3编译、编辑、调试编译cc–otest-gtest.c–lpthreadcc–osub1sub1.c编辑vi调试gdb1.2.2实验测试与结果测试结果:模拟售票说明(图1.1.1和1.1.2):共有300张票,有三个线程分别代表三个售票窗口1第3页图1.1.1图1.1.2说明(图1.1.3和1.1.4):每个窗口每次只卖出一张,直到卖完图1.1.3图1.1.4测试结果:双线程单缓冲区的合作图1.21.3实验总结本次实验刚做的时候用三个线程做依次访问卖几张来卖出,后来老师让我改成每次买一张直到卖完,做出来了,但是我加的循环体加的位置不对,1第4页运行一两次可能没问题,但运行几万次可能会出现问题,此bug老师给我指出的,程序还有待优化。1.4源代码1:#includestdio.h#includepthread.h#includelinux/sem.h#includesys/types.h#includestdlib.hintticket=300;intmutex;voidp(intsemid,intsemno){structsembufsop;sop.sem_num=semno;sop.sem_op=-1;sop.sem_flg=0;semop(semid,&sop,1);}voidv(intsemid,intsemno){structsembufsop;sop.sem_num=semno;sop.sem_op=1;sop.sem_flg=0;semop(semid,&sop,1);}voidthread1(void){inti;p(mutex,0);printf(窗口1:本航班共有%d个座位,请问您需要买几张?\n,ticket);scanf(%d,&i);if(iticket)printf(窗口1:剩余票数不够);elseticket=ticket-i;v(mutex,0);}voidthread2(void){1第5页inti;p(mutex,0);printf(窗口2:本航班共有%d个座位,请问您需要买几张?\n,ticket);scanf(%d,&i);if(iticket)printf(窗口2:剩余票数不够);elseticket=ticket-i;v(mutex,0);}voidthread3(void){inti;p(mutex,0);printf(窗口3:本航班共有%d个座位,请问您需要买几张?\n,ticket);scanf(%d,&i);if(iticket)printf(窗口3:剩余票数不够);elseticket=ticket-i;v(mutex,0);}intmain(){pthread_tid1,id2,id3;intret1,ret2,ret3;unionsemunarg;mutex=semget(IPC_PRIVATE,IPC_CREAT);arg.val=1;semctl(mutex,0,IPC_RMID,0);ret1=pthread_create(&id1,NULL,(void*)thread1,NULL);ret2=pthread_create(&id2,NULL,(void*)thread2,NULL);ret3=pthread_create(&id3,NULL,(void*)thread3,NULL);if(ret1!=0){printf(Creat1stpthreaderror!\n);exit(1);}if(ret2!=0){printf(Creat2stpthreaderror!\n
本文标题:华科操作系统实验报告
链接地址:https://www.777doc.com/doc-7220104 .html