出现>符号并可以输入文档
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;
}
程序分析:
本程序主函数接受两个参数,为源文件目录和目标文件目录。
首先程序尝试打开源文件,若无法打开表明源文件不存在。
之后尝试打开目标文件,若成功打开目标文件表明目标文件已存在,询问用户是否覆盖。
若目标文件打开失败表明目标文件不存在,创建目标文件并将源文件的内容复制到目标文件中。