超详细西电软院操作系统实验报告DOC.docx
《超详细西电软院操作系统实验报告DOC.docx》由会员分享,可在线阅读,更多相关《超详细西电软院操作系统实验报告DOC.docx(31页珍藏版)》请在冰豆网上搜索。
超详细西电软院操作系统实验报告DOC
操作系统课程设计
实验报告册
班级:
1313012
学号:
...
姓名:
lan
教师:
杜军朝
实验1Linux(虚拟机)安装及Linux常用命令使用
实验2makefile的编写及Linux内核编译
实验3Linux的进程和线程
实验4同步与互斥
实验5文件系统
重要提示:
1.实验正文建议使用小四号或五号宋体。
2.若附加图形,则请直接嵌入到实验手册相应位置。
3.各实验的源程序,请按实验分目录存放,如第一个实验的源程序存放在目录lab1下,第二个实验的源程序存放在目录lab2下等等,依次类推。
4.可互相讨论,但严禁抄袭网络或同学的实验结果。
实验编号
1
题目
Linux(虚拟机)安装及Linux常用命令使用
实验目的
1、掌握至少一种Linux系统的安装
2、掌握虚拟机平台(VmwareWorkstation)的使用和配置
3、掌握至少一种配置Vmware中Linux系统与宿主机Windows系统的文件交换的方法
4、掌握常用的Linux命令
5、了解Linux的帮助系统
实验内容
1、Linux(虚拟机)安装及Linux常用命令使用
报告正文
1、目录操作命令
2、文件操作命令
3、文本文件查看命令
4、用户系统命令
5、网络相关命令
6、U盘的使用方法
7、用户管理命令
8、用户组账号管理
9、文件权限设置
10、设置文件属主和属组
实验编号
2
题目
makefile的编写及Linux内核编译
实验目的
1、掌握Linux中makefile文件的编写
2、理解Linux内核的Makefile
3、掌握至少一个版本Linux内核的编译步骤
4、了解Linux内核的配置过程
实验内容
1、准备工作:
相关软件的获取与安装(源代码,软件包)
2、编译工作:
配置,编译
3、修改启动配置文件:
修改grub2启动项
4、能够正确的编译源代码
5、能够用编译出的内核启动系统
报告内容要求
(1)实现方法和思路
(2)测试及结果
报告正文
内核编译过程
1、拷贝源代码到/usr/src目录下
将文件添加共享文件夹中,然后在terminal输入以下命令:
cd/mnt/hgfs
cp-rpackages/usr/src
cplinux-2.6.32.60/usr/src
2、在usr/src目录下解压源码:
cd/usr/src
tarxvflinux-2.6.32.60.tar.bz2
3、拷贝packages到“~”目录下:
cd/usr/src
cp-rpackages~
4、安装软件包:
dpkg–i*.deb
5、转到内核源代码所在的目录“/usr/src/linux-2.6.32.60”:
cd/usr/src/linux-2.6.32.60
6、输入makemenuconfig,进入generalsetup选项,进入localversion菜单,添加版本标示:
lan,保存并退出。
7、输入make语句,等待2小时……
8、makemodules_install
9、makeinstall
10、update-initramfs-c-k2.6.32.60lan
11、修改grub启动项:
cd/boot/grub
12、重新启动ubuntu
13、查看内核版本号:
实验编号
3
题目
Linux的进程和线程
实验目的
1、理解进程/线程的概念
2、掌握创建和终止进程/线程的方法
3、掌握与进程/线程控制相关的系统函数
实验内容
1、创建和终止进程/线程
2、使用进程/线程控制相关的系统函数
报告内容要求
(1)实现方法和思路
(2)测试及结果
报告正文
示例1
源代码:
#include
#include
#include
intmain(){
pid_tmyPid;
pid_tmyParentPid;
gid_tmyGid;
uid_tmyUid;
myPid=getpid();
myParentPid=getppid();
myGid=getgid();
myUid=getuid();
printf("myprocessidis%d\n",myPid);
printf("myparentisprocessidis%d\n",myParentPid);
printf("mygroupidis%d\n",myGid);
printf("myuseridis%d\n",myUid);
return0;
}
运行结果:
示例2
源代码:
#include
#include
#include
intmain(){
pid_tret;
intstatus,i;
introle=-1;
ret=fork();
if(ret>0){
printf("Parent:
Thistheparentprocess(pid%d)\n",getpid());
for(i=0;i<6;i++){
printf("Parent:
Atcount%d\n",i);
sleep(3);}
ret=wait(&status);//防止僵尸进程的产生
role=0;}
else
if(ret==0){
printf("Child:
Thisthechildprocess(pid%d)\n",getpid());
for(i=0;i<6;i++){
printf("Chile:
Atcount%d\n",i);
sleep
(1);
}
role=1;
}
else{
printf("Parent:
Errortryingtofork()(%d)\n",errno);
}
printf("%s:
Exiting...\n",((role==0)?
"Parent":
"Child"));
return0;
}
运行结果:
示例3
源代码:
#include
#include
#include
#include
voidcatch_ctlc(intsig_num){
printf("CaughtControl-C\n");
fflush(stdout);//清除标准输出的缓存区
}
intmain(){
signal(SIGINT,catch_ctlc);
printf("Goahead,makemyday.\n");
pause();
return0;
}
运行结果:
示例4
源代码:
#include
#include
#include
#include
#include
#include
voidusr1_handler(intsig_num){
printf("Parent(%d)gottheSIGUSR1\n",getpid());
}
intmain(){
pid_tret;
intstatus;
introle=-1;
ret=fork();
if(ret>0){
printf("Parent:
Thisistheparentprocess(pid%d)\n",getpid());
signal(SIGUSR1,usr1_handler);
role=0;
pause();
printf("Parent:
Awaitingchildexit\n");
ret=wait(&status);
}
else{
if(ret==0){
printf("Child:
Thisisthechildprocess(pid%d)\n",getpid());
role=1;
sleep
(1);
printf("Child:
SendingSIGUSR1topid%d\n",getppid());
kill(getppid(),SIGUSR1);
sleep
(2);
}else{
printf("Parent:
Errortryingtofork()(%d)\n",errno);
}
printf("%s:
Exiting…\n",((role==0)?
"Parent":
"Child"));
}
return0;
}
运行结果:
示例5
源代码:
#include
#include
#include
#include
#include
void*myThread(void*arg){
printf("Threadran\n");
pthread_exit(arg);
}
intmain()
{
intret;
pthread_tmythread;
ret=pthread_create(&mythread,NULL,myThread,NULL);
if(ret!
=0){
printf("Cannotcreatepthread(%s)\n",strerror(errno));
exit(-1);
}
return0;
}
运行结果:
程序无输出内容
改写程序如下:
#include
#include
#include
#include
#include
void*myThread(void*arg){
printf("Threadran\n");
pthread_exit(arg);
}
intmain()
{
intret;
pthread_tmythread;
ret=pthread_create(&mythread,NULL,myThread,NULL);
if(ret!
=0){
printf("Cannotcreatepthread(%s)\n",strerror(errno));
exit(-1);
}
if(ret==0){
printf("Cancreatepthread!
\n");
}
return0;
}
运行结果:
示例6
源代码:
#include
#include
void*myThread(void*arg){
printf("Thread%dstarted\n",(int)arg);
pthread_exit(arg);
}
#defineMAX_THREADS5
intmain(){
intret,i,status;
pthread_tthreadIds[MAX_THREADS];
for(i=0;iret=pthread_create(&threadIds[i],NULL,myThread,(void*)i);
if(ret!
=0){
printf("Errorcreatingthread%d\n",(void*)i);
}
}
for(i=0;iret=pthread_join(threadIds[i],(void**)&status);
if(ret!
=0){
printf("Errorjoiningthread%d\n",(void*)i);
}
else{
printf("Status=%d\n",status);
}
}
return0;
}
运行结果:
示例7
源代码:
#include
#include
#include
pthread_mutex_tcntr_mutex=PTHREAD_MUTEX_INITIALIZER;
longprotVariable=0L;
void*myThread(void*arg){
inti,ret;
for(i=0;i<10000;i++){
ret=pthread_mutex_lock(&cntr_mutex);
assert(ret==0);
protVariable++;
ret=pthread_mutex_unlock(&cntr_mutex);
assert(ret==0);
}
pthread_exit(NULL);
}
#defineMAX_THREADS10
intmain(){
intret,i;
pthread_tthreadIds[MAX_THREADS];
for(i=0;iret=pthread_create(&threadIds[i],NULL,myThread,NULL);
if(ret!
=0){
printf("Errorcreatingthread%d\n",(int)threadIds[i]);
}
}
for(i=0;iret=pthread_join(threadIds[i],NULL);
if(ret!
=0){
printf("Errorjoiningthread%d\n",(int)threadIds[i]);
}
}
printf("Theprotectedvariablevalueis%ld\n",protVariable);
ret=pthread_mutex_destroy(&cntr_mutex);
if(ret!
=0){
printf("Couldnotdestroythemutex\n");
}
return0;
}
运行结果:
实验编号
4
题目
同步与互斥
实验目的
1.学习理解并发中的互斥原理
2.掌握peterson算法的原理及实现方法
3.实现生产者消费者模型
实验内容
1.实现Peterson算法;理解互斥的概念
2.利用Peterson算法的原理;实现“生产者消费者”程序,理解同步互斥的概念
报告内容要求
(1)实现方法和思路
(2)测试及结果
报告正文
一、Peterson算法
本实验利用软件方法实现同步互斥,也就是通过peterson算法,不通过中断等物理机制实现同步互斥。
Peterson算法特点:
使用两个全局变量(flag[]和turn),flag[]表示哪个进程正在占用临界区及临界区的状态,turn表示能进入临界区的进程序号。
此算法涉及两个原则:
忙则等待,空闲让进。
源代码:
#include
#include
#include
#include
#definefalse0
#definetrue1
intflag[2];
intturn=0;
ints=0;
void*P0(void*arg);
void*P1(void*arg);
intmain()
{
pthread_tpt0,pt1;
flag[0]=false;
flag[1]=false;
pthread_create(&pt0,NULL,P0,NULL);
pthread_create(&pt1,NULL,P1,NULL);
pthread_join(pt0,NULL);
pthread_join(pt1,NULL);
return0;
}
void*P0(void*arg)
{
while(true){
flag[0]=true;
turn=1;
while(flag[1]&&turn==1)continue;
printf("Iamprocess0andIgots:
%d\n",s);
s++;
flag[0]=false;
sleep
(1);
}
}
void*P1(void*arg)
{
while(true){
flag[1]=true;
turn=0;
while(flag[0]&&turn==0)continue;
printf("Iamprocess1andIgots:
%d\n",s);
s++;
flag[1]=false;
sleep
(1);
}
}
运行结果:
2、用Peterson算法实现“生产者消费者”程序
源代码:
#include
#include
#defineMAX10//需要生产的数量
pthread_mutex_tthe_mutex;
pthread_cond_tcondc,condp;
intbuffer=0;//生产者、消费者使用的缓冲区
void*producer(void*ptr)
{
inti;
for(i=1;i<=MAX;i++)
{
pthread_mutex_lock(&the_mutex);//互斥使用缓冲区
while(buffer!
=0)pthread_cond_wait(&condp,&the_mutex);
printf("procucerproduceitem%d\n",i);
buffer=i;//将数据插入缓冲区
pthread_cond_signal(&condc);//唤醒消费者
pthread_mutex_unlock(&the_mutex);//释放缓冲区
}
pthread_exit(0);
}
void*consumer(void*ptr)
{
inti;
for(i=1;i<=MAX;i++)
{
pthread_mutex_lock(&the_mutex);//互斥使用缓冲区
while(buffer==0)pthread_cond_wait(&condc,&the_mutex);
printf("consumerconsumeitem%d\n",i);
buffer=0;//从缓冲区中取出数据
pthread_cond_signal(&condp);//唤醒生产者
pthread_mutex_unlock(&the_mutex);//释放缓冲区
}
pthread_exit(0);
}
intmain(intargc,char*argv[])
{
pthread_tpro,con;
pthread_mutex_init(&the_mutex,0);
pthread_cond_init(&condc,0);
pthread_cond_init(&condp,0);
pthread_create(&con,0,consumer,0);
pthread_create(&pro,0,producer,0);
pthread_join(pro,0);
pthread_join(con,0);
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
pthread_mutex_destroy(&the_mutex);
return0;
}
运行结果:
实验编号
5
题目
文件系统
实验目的
1.掌握锁的结构和操作
2.掌握如何对文件进行加锁,解锁
3.学会如何协调不同进程对同一文件的互斥操作
实验内容
1.文件锁的测试
以下选做:
2.获取文件的状态信息
3.创建一个带有空洞的文件
4.掌握lseek函数的用法
报告内容要求
(1)实现方法和思路
(2)测试及结果
报告正文
1、文件锁的测试
#include
#include
#include
#include
#include
#include
intmain(){
intfd;
pid_tpid;
structflockrdlock,wrlock;
rdlock.l_type=F_RDLCK;
rdlock.l_start=0;//读锁
rdlock.l_whence=SEEK_SET;
rdlock.l_len=0;
wrlock.l_type=F_WRLCK;
wrlock.l_start=0;//写锁
wrlock.l_whence=SEEK_SET;
wrlock.l_len=0;
if((fd=open("myfile",O_RDWR))<0){
printf("cannotopenthefile\n");
exit(EXIT_FAILURE);
}
if(write(fd,"abcdefgh",8)