操作系统上机报告汇总.docx

上传人:b****5 文档编号:11983335 上传时间:2023-04-16 格式:DOCX 页数:15 大小:19.84KB
下载 相关 举报
操作系统上机报告汇总.docx_第1页
第1页 / 共15页
操作系统上机报告汇总.docx_第2页
第2页 / 共15页
操作系统上机报告汇总.docx_第3页
第3页 / 共15页
操作系统上机报告汇总.docx_第4页
第4页 / 共15页
操作系统上机报告汇总.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

操作系统上机报告汇总.docx

《操作系统上机报告汇总.docx》由会员分享,可在线阅读,更多相关《操作系统上机报告汇总.docx(15页珍藏版)》请在冰豆网上搜索。

操作系统上机报告汇总.docx

操作系统上机报告汇总

西安电子科技大学计算机学院

 

《操作系统实验》

实验报告

 

姓名:

学号:

班级:

 

一、题目

实验1:

LINUX/UNIXShell部分

(一)系统基本命令

1.登陆系统,输入whoami和pwd,确定自己的登录名和当前目录;

登录名admixx,当前目录/root

2.显示自己的注册目录?

命令在哪里?

a.键入echo$HOME,确认自己的主目录;主目录为/root

b.键入echo$PATH,记下自己看到的目录表;/usr/lib/qt-3.3/bin:

/usr/kerberos/sbin:

/usr/kerberos/bin:

/usr/local/sbin:

/usr/local/bin:

/sbin:

/bin:

/usr/sbin:

/usr/bin:

/usr/X11R6/bin:

/root/bin

c.键入whichabcd,看看得到的错误信息;

/usr/bin/which:

noabcdin(/usr/lib/qt-3.3/bin:

/usr/kerberos/sbin:

/usr/kerberos/bin:

/usr/local/sbin:

/usr/local/bin:

/sbin:

/bin:

/usr/sbin:

/usr/bin:

/usr/X11R6/bin:

/root/bin

再键入whichls和whichvi,对比刚刚得到的结果的目录是否在a.、b.

两题看到的目录表中;

aliasls=’ls—color=tty’

/bin/ls

/bin/vi

3.ls和cd的使用:

a.键入ls,ls-l,ls-a,ls-al四条命令,观察输出,说明四种不同使用方式的区别。

ls:

列出目录内容ls–l:

以长格式详细显示文件信息ls–a:

列出所有文件,包括以.文件ls–al:

以长格式显示当前目录下的所有文件

b.利用cd转到/bin,/sbin,/etc,/dev,再利用ls列出各个目录的内容,如果"迷路",可以利用pwd确定位置,或键入cd,cd~观察效果.

cd,cd~的区别:

cd:

回到上层目录cd~:

可进入用户的HOME目录

(二)基本操作

1.用cp将/usr/share目录下的exercise子目录连同目录下的文件拷贝到自己的主目录下,然后进入自己的exercise目录.

2.输入/输出重定向和cat,more等显示命令的配合使用:

a.输入cat命令列出目录下longtext文件中的内容;

b.输入cat命令列出目录下longtext文件中的内容,是否发现一屏显示不完?

c.使用more命令列出longtext的内容;

d.输入cathello.txt>hello2.txt,再输入cathello.txt>>hello2.txt,再检查hello2.txt的内容有何变化;说明>与>>的区别。

>是把hello.txt的内容覆盖在hello2.txt内>>是把hello.txt的内容续写在hello2.txt内

e.对比两条命令:

morelongtext和catlongtext|more,两者有何不同点?

morelongtext显示出已经显示出来的内容的百分比catlongtext|more不显示

f.执行命令echo

啥都没发生但如果输入echo>hello.txt文档就会被清空

g.输入命令cat<

出现>符号并可以输入文档

h.设计一条命令,使该命令可以从标准输出中读入;

cat<hello3.txtcathello3.txt就可以看到

(参考答案:

cat<hello3.txt)

3.特殊字符:

输入cat[alsoatext].txt,看看能否打开目录下的[alsoatext].txt文

件,若不能,该怎么办?

输入cat“[alsoatext].txt”

(参考答案:

cat\[also\a\text\].txt)

4.文件链接:

a.用ln命令为目录下的longtext文件建立一个硬链接,链接名为longtext2,然后把longtext复制一个新文件longtext3,用cat命令将hello.txt的内容追加到longtext的末尾,再用diff命令比较longtext,longtext2和longtext3,看看有什么结果,特别是比较一下longtext和longtext2是否相同;

longtext和longtext2一直是相同的在longtext后续写hello.txt之后,longtext和longtext2均与longtext3不同了

b.用ln命令给longtext3建立一个符号链接longtext4,用cat命令看看longtext4;然后删去longtext3,再用cat命令看看longtext4,是否有什么不同?

没有不同

c.删去longtext2,看看能否用cat命令看到longtext?

d.试着执行ln-s./abcde./nulllink,看看是否能建立文件链接.不能

5.查找命令find:

用find命令查找当前目录下所有以del开头或以del结尾的文件,并将其删除,要求删除前征求用户许可.

find./-namedel*

rm–idel*

find./(-namedel/*-o-name\*del\)-execrm-i{}\;

find./(-namedel/*-o-name\*del\)-exec-okrm{}\;

6.文件的属性:

a.用ls-l列出exercise目录下所有的文件和目录,观察其权限位;

b.将hello2.txt的读权限去掉,看看还能否用cat打开该文件;不能

c.将program目录的读权限去掉,看看是否能用ls命令看到其中内容?

不能

(三)Shell程序设计

编写一个Shell过程完成如下功能:

1.合并两个$1、$2文件为$3,并显示。

2.如果缺少$3,那么先报告缺少$3,将合并后的内容输出到CHENG.TXT,显示。

3.如果缺少$2、$3那么先报告缺少$2、$3,只显示$1的内容。

iftest-f"$3"

thencat$1$2>$3

cat$3

elseiftest-f"$2"

thenecho"$3isnotafileornotinthisdirectory"

cat$1$2>CHENG.TXT

catCHENG.TXT

elseecho"Neither$2or$3isafileorinthisdirectory"

cat$1

fi

fi

 

实验2:

实验题目

进程的建立

实验目的

创建进程及子进程

在父子进程间实现进程通信

实验软硬件环境

Linux、Windows98、Windows2000

实验内容

创建进程并显示标识等进程控制块的属性信息;

显示父子进程的通信信息和相应的应答信息。

(进程间通信机制任选)

实验步骤

创建进程;

显示进程状态信息;

实现父子进程通信;

考核指标

显示创建的进程及控制块参数;

显示进程间关系参数

源程序:

#include

#include

#include

#include

#include

#defineBUFFERSIZE32

intmain(){

pid_tpid;

intstatus;

intfd[2];

charbuf[BUFFERSIZE]="Thisisapipe\n";

if(pipe(fd)<0){

printf("pipeerror\n");

exit

(1);

}

pid=fork();

if(pid<0){

printf("forkerrot\n");

exit

(1);

}

if(pid==0){

printf("childprocess:

\n");//childprocess

printf("pid=%d\n",getpid());

printf("ppid=%d\n",getppid());

printf("gid=%d\n",getgid());

printf("write:

%s\n",buf);

close(fd[0]);//closewrite

write(fd[1],buf,sizeof(buf));//write

exit

(1);

}

else{//parenrprocess

if(pid!

=wait(&status)){

printf("waiterror\n");

}

printf("parentprocess:

\n");

printf("pid=%d\n",getpid());

printf("ppid=%d\n",getppid());

printf("gid=%d\n",getgid());

 

实验3:

实验题目

进程间的同步

实验目的

理解进程同步和互斥模型及其应用

实验软硬件环境

Linux、Windows98、Windows2000

实验内容

利用通信API实现进程之间的同步:

建立司机和售票员进程;

并实现他们间的同步运行。

实验步骤

创建进程;

实现同步操作或函数;

实现公共汽车司机和售票员开关车门及行车运行过程的同步模型;

显示同步运行的结果。

考核指标

显示司机和售票员进程的同步运行轨迹。

司机进程代码:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

charSEM_NAME1[]="s1";

charSEM_NAME2[]="s2";

intmain()

{

inti;

sem_t*s1;

sem_t*s2;

s1=sem_open(SEM_NAME1,O_CREAT,0644,0);

if(s1==SEM_FAILED)

{

perror("unabletocreatesemaphore");

sem_unlink(s1);

exit(-1);

}

s2=sem_open(SEM_NAME2,O_CREAT,0644,0);

if(s2==SEM_FAILED)

{

perror("unabletocreatesemaphore");

sem_unlink(s2);

exit(-1);

}

for(i=0;i<=2;i++){

printf("[driver]reachstation,stopcar\n");

sem_post(s2);

printf("[driver]waitingclosingdoor\n");

sem_wait(s1);

printf("[driver]leavingstation\n");

printf("carisrunning\n");

}

sleep

(2);

sem_close(s1);

sem_close(s2);

sem_unlink(SEM_NAME1);

sem_unlink(SEM_NAME2);

_exit(0);

}

售票员进程代码:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

charSEM_NAME1[]="s1";

charSEM_NAME2[]="s2";

intmain()

{

inti;

sem_t*s1;

sem_t*s2;

//create&initializesemaphore

s1=sem_open(SEM_NAME1,0,0644,0);

if(s1==SEM_FAILED)

{

perror("unabletocreatesemaphore");

sem_close(s1);

exit(-1);

}

s2=sem_open(SEM_NAME2,0,0644,0);

if(s2==SEM_FAILED)

{

perror("unabletocreatesemaphore");

sem_close(s2);

exit(-1);

}

for(i=0;i<=2;i++){

sem_wait(s2);

printf("[conductor]opendoor,passenger,closedoor\n");

sem_post(s1);

printf("[conductor]waitingreachingstation\n");

}

sem_close(s1);

sem_close(s2);

_exit(0);

}

运行结果:

 

结果分析:

利用PV操作实现司机进程和售票员进程之间的同步。

设司机进程的信号量为S1,售票员进程信号量为S2。

当车到站时,停车,进行操作V(S2),P(S1)。

此时司机进程被挂起,售票员进程开始进行。

售票员进程先进行P(S2)操作,若之前未进行V(S2)则被挂起,否则进程继续执行,打开车门。

待人上完后关车门,进行V(S1)操作,唤醒被挂起的司机进程。

汽车再次离站开出。

实验4:

实验题目

Linux文件系统实验

实验目的

 

通过熟悉使用相关命令、使用系统调用来读写文件熟悉Linux文件系统,并阅读Linux文件系统设计与实现的相关资料,加深理解。

实验软硬件环境

Linux

实验内容

文件命令

文件操作的系统调用

Linux文件系统理解

实验步骤

 

熟悉文件系统常用命令(ls,mv,rm,ln,cp,compress,uncompress,gzip,gunzip,rar,rpm,tar,df,mount,umount,chmod,chown,……);

建立相关用户和组,并熟悉文件系统的权限管理;

利用文件操作相关的系统调用实现一个文件拷贝命令(参数为源文件、目标文件,提示源文件不存在,提示目标文件存在是否覆盖);

阅读Linux文件系统设计与实现的相关资料;

考核指标

演示实验结果。

#include

#include

#include

#include

#include

#include

#defineBUFF_SIZE1024

intmain(intargc,char*argv[]){

intsrc_file,dest_file;

intreal_read_len;

unsignedcharbuff[BUFF_SIZE];

charcover;

//argcisnotcorrect

if(argc!

=3){

printf("Errorusecopy!

\n");

printf("Example:

\n");

printf("copysrc_filedest_file\n");

exit

(1);

}

//judgewheathersrc_fileexists

if((access(argv[1],F_OK)!

=0))

{

printf("sourcefiledosen'texist.\n");

exit(-1);

}

//judgetheexistenceofthedest_file

if((access(argv[2],F_OK)==0))

{

printf("dest_filealreadyexists.coverit?

\n");

printf("yes[y]ornot[n]?

\n");

scanf("%c",&cover);

if(cover=='n')

exit

(1);

else

printf("coverit!

\n");

}

//Opensrc_filereadonly

src_file=open(argv[1],O_RDONLY);

//Ifthedest_fileisnotexsit,thencreatenewone

dest_file=open(argv[2],O_WRONLY|O_CREAT,666);

//Openerror

if(src_file<0||dest_file<0){

printf("Openfileerror\n");

printf("Can'tcopy!

\n");

printf("Pleasecheckcmd:

copysrc_filedest_file\n");

exit

(1);

}

//Copysrc_filetodest_file

while((real_read_len=read(src_file,buff,sizeof(buff)))>0){

write(dest_file,buff,real_read_len);

}

//closefd

close(dest_file);

close(src_file);

return0;

}

程序分析:

本程序主函数接受两个参数,为源文件目录和目标文件目录。

首先程序尝试打开源文件,若无法打开表明源文件不存在。

之后尝试打开目标文件,若成功打开目标文件表明目标文件已存在,询问用户是否覆盖。

若目标文件打开失败表明目标文件不存在,创建目标文件并将源文件的内容复制到目标文件中。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1