操作系统上机报告材料96043Word文件下载.docx
《操作系统上机报告材料96043Word文件下载.docx》由会员分享,可在线阅读,更多相关《操作系统上机报告材料96043Word文件下载.docx(12页珍藏版)》请在冰点文库上搜索。
/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<
hello.txt,观察发生了什么?
啥都没发生但如果输入echo>
hello.txt文档就会被清空
g.输入命令cat<
<
end,看看屏幕上的反应:
出现>
符号并可以输入文档
h.设计一条命令,使该命令可以从标准输出中读入;
cat<
end>
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<
stdio.h>
stdlib.h>
unistd.h>
sys/wait.h>
fcntl.h>
#defineBUFFERSIZE32
intmain(){
pid_tpid;
intstatus;
intfd[2];
charbuf[BUFFERSIZE]="
Thisisapipe\n"
;
if(pipe(fd)<
0){
printf("
pipeerror\n"
);
exit
(1);
}
pid=fork();
if(pid<
forkerrot\n"
exit
(1);
if(pid==0){
printf("
childprocess:
\n"
//childprocess
pid=%d\n"
getpid());
ppid=%d\n"
getppid());
gid=%d\n"
getgid());
write:
%s\n"
buf);
close(fd[0]);
//closewrite
write(fd[1],buf,sizeof(buf));
//write
else{//parenrprocess
if(pid!
=wait(&
status)){
waiterror\n"
parentprocess:
实验3:
进程间的同步
理解进程同步和互斥模型及其应用
利用通信API实现进程之间的同步:
建立司机和售票员进程;
并实现他们间的同步运行。
实现同步操作或函数;
实现公共汽车司机和售票员开关车门及行车运行过程的同步模型;
显示同步运行的结果。
显示司机和售票员进程的同步运行轨迹。
司机进程代码:
#include<
sys/types.h>
sys/ipc.h>
sys/shm.h>
semaphore.h>
sys/stat.h>
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)
sem_unlink(s2);
for(i=0;
i<
=2;
i++){
[driver]reachstation,stopcar\n"
sem_post(s2);
[driver]waitingclosingdoor\n"
sem_wait(s1);
[driver]leavingstation\n"
carisrunning\n"
sleep
(2);
sem_close(s1);
sem_close(s2);
sem_unlink(SEM_NAME1);
sem_unlink(SEM_NAME2);
_exit(0);
}
售票员进程代码:
//create&
initializesemaphore
s1=sem_open(SEM_NAME1,0,0644,0);
s2=sem_open(SEM_NAME2,0,0644,0);
sem_wait(s2);
[conductor]opendoor,passenger,closedoor\n"
sem_post(s1);
[conductor]waitingreachingstation\n"
运行结果:
结果分析:
利用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文件系统设计与实现的相关资料;
演示实验结果。
#defineBUFF_SIZE1024
intmain(intargc,char*argv[]){
intsrc_file,dest_file;
intreal_read_len;
unsignedcharbuff[BUFF_SIZE];
charcover;
//argcisnotcorrect
if(argc!
=3){
Errorusecopy!
Example:
copysrc_filedest_file\n"
//judgewheathersrc_fileexists
if((access(argv[1],F_OK)!
=0))
sourcefiledosen'
texist.\n"
//judgetheexistenceofthedest_file
if((access(argv[2],F_OK)==0))
dest_filealreadyexists.coverit?
yes[y]ornot[n]?
scanf("
%c"
&
cover);
if(cover=='
n'
)
else
coverit!
//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){
Openfileerror\n"
Can'
tcopy!
Pleasecheckcmd:
copysrc_filedest_file\n"
}
//Copysrc_filetodest_file
while((real_read_len=read(src_file,buff,sizeof(buff)))>
write(dest_file,buff,real_read_len);
//closefd
close(dest_file);
close(src_file);
return0;
程序分析:
本程序主函数接受两个参数,为源文件目录和目标文件目录。
首先程序尝试打开源文件,若无法打开表明源文件不存在。
之后尝试打开目标文件,若成功打开目标文件表明目标文件已存在,询问用户是否覆盖。
若目标文件打开失败表明目标文件不存在,创建目标文件并将源文件的容复制到目标文件中。